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.

3398 lines
114KB

  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. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include <limits.h>
  23. //#define DEBUG
  24. //#define MOV_EXPORT_ALL_METADATA
  25. #include "libavutil/attributes.h"
  26. #include "libavutil/channel_layout.h"
  27. #include "libavutil/intreadwrite.h"
  28. #include "libavutil/intfloat.h"
  29. #include "libavutil/mathematics.h"
  30. #include "libavutil/avstring.h"
  31. #include "libavutil/dict.h"
  32. #include "libavutil/opt.h"
  33. #include "libavutil/timecode.h"
  34. #include "libavcodec/ac3tab.h"
  35. #include "avformat.h"
  36. #include "internal.h"
  37. #include "avio_internal.h"
  38. #include "riff.h"
  39. #include "isom.h"
  40. #include "libavcodec/get_bits.h"
  41. #include "id3v1.h"
  42. #include "mov_chan.h"
  43. #if CONFIG_ZLIB
  44. #include <zlib.h>
  45. #endif
  46. /*
  47. * First version by Francois Revol revol@free.fr
  48. * Seek function by Gael Chardon gael.dev@4now.net
  49. */
  50. #include "qtpalette.h"
  51. #undef NDEBUG
  52. #include <assert.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_metadata_track_or_disc_number(MOVContext *c, AVIOContext *pb,
  61. unsigned len, const char *key)
  62. {
  63. char buf[16];
  64. short current, total = 0;
  65. avio_rb16(pb); // unknown
  66. current = avio_rb16(pb);
  67. if (len >= 6)
  68. total = avio_rb16(pb);
  69. if (!total)
  70. snprintf(buf, sizeof(buf), "%d", current);
  71. else
  72. snprintf(buf, sizeof(buf), "%d/%d", current, total);
  73. av_dict_set(&c->fc->metadata, key, buf, 0);
  74. return 0;
  75. }
  76. static int mov_metadata_int8_bypass_padding(MOVContext *c, AVIOContext *pb,
  77. unsigned len, const char *key)
  78. {
  79. char buf[16];
  80. /* bypass padding bytes */
  81. avio_r8(pb);
  82. avio_r8(pb);
  83. avio_r8(pb);
  84. snprintf(buf, sizeof(buf), "%d", avio_r8(pb));
  85. av_dict_set(&c->fc->metadata, key, buf, 0);
  86. return 0;
  87. }
  88. static int mov_metadata_int8_no_padding(MOVContext *c, AVIOContext *pb,
  89. unsigned len, const char *key)
  90. {
  91. char buf[16];
  92. snprintf(buf, sizeof(buf), "%d", avio_r8(pb));
  93. av_dict_set(&c->fc->metadata, key, buf, 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. char buf[20];
  101. avio_r8(pb); // unknown
  102. genre = avio_r8(pb);
  103. if (genre < 1 || genre > ID3v1_GENRE_MAX)
  104. return 0;
  105. snprintf(buf, sizeof(buf), "%s", ff_id3v1_genre_str[genre-1]);
  106. av_dict_set(&c->fc->metadata, key, buf, 0);
  107. return 0;
  108. }
  109. static int mov_read_custom_metadata(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  110. {
  111. char key[1024]={0}, data[1024]={0};
  112. int i;
  113. AVStream *st;
  114. MOVStreamContext *sc;
  115. if (c->fc->nb_streams < 1)
  116. return 0;
  117. st = c->fc->streams[c->fc->nb_streams-1];
  118. sc = st->priv_data;
  119. if (atom.size <= 8) return 0;
  120. for (i = 0; i < 3; i++) { // Parse up to three sub-atoms looking for name and data.
  121. int data_size = avio_rb32(pb);
  122. int tag = avio_rl32(pb);
  123. int str_size = 0, skip_size = 0;
  124. char *target = NULL;
  125. switch (tag) {
  126. case MKTAG('n','a','m','e'):
  127. avio_rb32(pb); // version/flags
  128. str_size = skip_size = data_size - 12;
  129. atom.size -= 12;
  130. target = key;
  131. break;
  132. case MKTAG('d','a','t','a'):
  133. avio_rb32(pb); // version/flags
  134. avio_rb32(pb); // reserved (zero)
  135. str_size = skip_size = data_size - 16;
  136. atom.size -= 16;
  137. target = data;
  138. break;
  139. default:
  140. skip_size = data_size - 8;
  141. str_size = 0;
  142. break;
  143. }
  144. if (target) {
  145. str_size = FFMIN3(sizeof(data)-1, str_size, atom.size);
  146. avio_read(pb, target, str_size);
  147. target[str_size] = 0;
  148. }
  149. atom.size -= skip_size;
  150. // If we didn't read the full data chunk for the sub-atom, skip to the end of it.
  151. if (skip_size > str_size) avio_skip(pb, skip_size - str_size);
  152. }
  153. if (*key && *data) {
  154. if (strcmp(key, "iTunSMPB") == 0) {
  155. int priming, remainder, samples;
  156. if(sscanf(data, "%*X %X %X %X", &priming, &remainder, &samples) == 3){
  157. if(priming>0 && priming<16384)
  158. sc->start_pad = priming;
  159. return 1;
  160. }
  161. }
  162. if (strcmp(key, "cdec") == 0) {
  163. // av_dict_set(&st->metadata, key, data, 0);
  164. return 1;
  165. }
  166. }
  167. return 0;
  168. }
  169. static const uint32_t mac_to_unicode[128] = {
  170. 0x00C4,0x00C5,0x00C7,0x00C9,0x00D1,0x00D6,0x00DC,0x00E1,
  171. 0x00E0,0x00E2,0x00E4,0x00E3,0x00E5,0x00E7,0x00E9,0x00E8,
  172. 0x00EA,0x00EB,0x00ED,0x00EC,0x00EE,0x00EF,0x00F1,0x00F3,
  173. 0x00F2,0x00F4,0x00F6,0x00F5,0x00FA,0x00F9,0x00FB,0x00FC,
  174. 0x2020,0x00B0,0x00A2,0x00A3,0x00A7,0x2022,0x00B6,0x00DF,
  175. 0x00AE,0x00A9,0x2122,0x00B4,0x00A8,0x2260,0x00C6,0x00D8,
  176. 0x221E,0x00B1,0x2264,0x2265,0x00A5,0x00B5,0x2202,0x2211,
  177. 0x220F,0x03C0,0x222B,0x00AA,0x00BA,0x03A9,0x00E6,0x00F8,
  178. 0x00BF,0x00A1,0x00AC,0x221A,0x0192,0x2248,0x2206,0x00AB,
  179. 0x00BB,0x2026,0x00A0,0x00C0,0x00C3,0x00D5,0x0152,0x0153,
  180. 0x2013,0x2014,0x201C,0x201D,0x2018,0x2019,0x00F7,0x25CA,
  181. 0x00FF,0x0178,0x2044,0x20AC,0x2039,0x203A,0xFB01,0xFB02,
  182. 0x2021,0x00B7,0x201A,0x201E,0x2030,0x00C2,0x00CA,0x00C1,
  183. 0x00CB,0x00C8,0x00CD,0x00CE,0x00CF,0x00CC,0x00D3,0x00D4,
  184. 0xF8FF,0x00D2,0x00DA,0x00DB,0x00D9,0x0131,0x02C6,0x02DC,
  185. 0x00AF,0x02D8,0x02D9,0x02DA,0x00B8,0x02DD,0x02DB,0x02C7,
  186. };
  187. static int mov_read_mac_string(MOVContext *c, AVIOContext *pb, int len,
  188. char *dst, int dstlen)
  189. {
  190. char *p = dst;
  191. char *end = dst+dstlen-1;
  192. int i;
  193. for (i = 0; i < len; i++) {
  194. uint8_t t, c = avio_r8(pb);
  195. if (c < 0x80 && p < end)
  196. *p++ = c;
  197. else if (p < end)
  198. PUT_UTF8(mac_to_unicode[c-0x80], t, if (p < end) *p++ = t;);
  199. }
  200. *p = 0;
  201. return p - dst;
  202. }
  203. static int mov_read_covr(MOVContext *c, AVIOContext *pb, int type, int len)
  204. {
  205. AVPacket pkt;
  206. AVStream *st;
  207. MOVStreamContext *sc;
  208. enum AVCodecID id;
  209. int ret;
  210. switch (type) {
  211. case 0xd: id = AV_CODEC_ID_MJPEG; break;
  212. case 0xe: id = AV_CODEC_ID_PNG; break;
  213. case 0x1b: id = AV_CODEC_ID_BMP; break;
  214. default:
  215. av_log(c->fc, AV_LOG_WARNING, "Unknown cover type: 0x%x.\n", type);
  216. avio_skip(pb, len);
  217. return 0;
  218. }
  219. st = avformat_new_stream(c->fc, NULL);
  220. if (!st)
  221. return AVERROR(ENOMEM);
  222. sc = av_mallocz(sizeof(*sc));
  223. if (!sc)
  224. return AVERROR(ENOMEM);
  225. st->priv_data = sc;
  226. ret = av_get_packet(pb, &pkt, len);
  227. if (ret < 0)
  228. return ret;
  229. st->disposition |= AV_DISPOSITION_ATTACHED_PIC;
  230. st->attached_pic = pkt;
  231. st->attached_pic.stream_index = st->index;
  232. st->attached_pic.flags |= AV_PKT_FLAG_KEY;
  233. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  234. st->codec->codec_id = id;
  235. return 0;
  236. }
  237. static int mov_read_udta_string(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  238. {
  239. #ifdef MOV_EXPORT_ALL_METADATA
  240. char tmp_key[5];
  241. #endif
  242. char str[1024], key2[16], language[4] = {0};
  243. const char *key = NULL;
  244. uint16_t langcode = 0;
  245. uint32_t data_type = 0, str_size;
  246. int (*parse)(MOVContext*, AVIOContext*, unsigned, const char*) = NULL;
  247. if (c->itunes_metadata && atom.type == MKTAG('-','-','-','-'))
  248. return mov_read_custom_metadata(c, pb, atom);
  249. switch (atom.type) {
  250. case MKTAG(0xa9,'n','a','m'): key = "title"; break;
  251. case MKTAG(0xa9,'a','u','t'):
  252. case MKTAG(0xa9,'A','R','T'): key = "artist"; break;
  253. case MKTAG( 'a','A','R','T'): key = "album_artist"; break;
  254. case MKTAG(0xa9,'w','r','t'): key = "composer"; break;
  255. case MKTAG( 'c','p','r','t'):
  256. case MKTAG(0xa9,'c','p','y'): key = "copyright"; break;
  257. case MKTAG(0xa9,'g','r','p'): key = "grouping"; break;
  258. case MKTAG(0xa9,'l','y','r'): key = "lyrics"; break;
  259. case MKTAG(0xa9,'c','m','t'):
  260. case MKTAG(0xa9,'i','n','f'): key = "comment"; break;
  261. case MKTAG(0xa9,'a','l','b'): key = "album"; break;
  262. case MKTAG(0xa9,'d','a','y'): key = "date"; break;
  263. case MKTAG(0xa9,'g','e','n'): key = "genre"; break;
  264. case MKTAG( 'g','n','r','e'): key = "genre";
  265. parse = mov_metadata_gnre; break;
  266. case MKTAG(0xa9,'t','o','o'):
  267. case MKTAG(0xa9,'s','w','r'): key = "encoder"; break;
  268. case MKTAG(0xa9,'e','n','c'): key = "encoder"; break;
  269. case MKTAG(0xa9,'m','a','k'): key = "make"; break;
  270. case MKTAG(0xa9,'m','o','d'): key = "model"; break;
  271. case MKTAG(0xa9,'x','y','z'): key = "location"; break;
  272. case MKTAG( 'd','e','s','c'): key = "description";break;
  273. case MKTAG( 'l','d','e','s'): key = "synopsis"; break;
  274. case MKTAG( 't','v','s','h'): key = "show"; break;
  275. case MKTAG( 't','v','e','n'): key = "episode_id";break;
  276. case MKTAG( 't','v','n','n'): key = "network"; break;
  277. case MKTAG( 't','r','k','n'): key = "track";
  278. parse = mov_metadata_track_or_disc_number; break;
  279. case MKTAG( 'd','i','s','k'): key = "disc";
  280. parse = mov_metadata_track_or_disc_number; break;
  281. case MKTAG( 't','v','e','s'): key = "episode_sort";
  282. parse = mov_metadata_int8_bypass_padding; break;
  283. case MKTAG( 't','v','s','n'): key = "season_number";
  284. parse = mov_metadata_int8_bypass_padding; break;
  285. case MKTAG( 's','t','i','k'): key = "media_type";
  286. parse = mov_metadata_int8_no_padding; break;
  287. case MKTAG( 'h','d','v','d'): key = "hd_video";
  288. parse = mov_metadata_int8_no_padding; break;
  289. case MKTAG( 'p','g','a','p'): key = "gapless_playback";
  290. parse = mov_metadata_int8_no_padding; break;
  291. }
  292. if (c->itunes_metadata && atom.size > 8) {
  293. int data_size = avio_rb32(pb);
  294. int tag = avio_rl32(pb);
  295. if (tag == MKTAG('d','a','t','a')) {
  296. data_type = avio_rb32(pb); // type
  297. avio_rb32(pb); // unknown
  298. str_size = data_size - 16;
  299. atom.size -= 16;
  300. if (atom.type == MKTAG('c', 'o', 'v', 'r')) {
  301. int ret = mov_read_covr(c, pb, data_type, str_size);
  302. if (ret < 0) {
  303. av_log(c->fc, AV_LOG_ERROR, "Error parsing cover art.\n");
  304. return ret;
  305. }
  306. }
  307. } else return 0;
  308. } else if (atom.size > 4 && key && !c->itunes_metadata) {
  309. str_size = avio_rb16(pb); // string length
  310. langcode = avio_rb16(pb);
  311. ff_mov_lang_to_iso639(langcode, language);
  312. atom.size -= 4;
  313. } else
  314. str_size = atom.size;
  315. #ifdef MOV_EXPORT_ALL_METADATA
  316. if (!key) {
  317. snprintf(tmp_key, 5, "%.4s", (char*)&atom.type);
  318. key = tmp_key;
  319. }
  320. #endif
  321. if (!key)
  322. return 0;
  323. if (atom.size < 0)
  324. return AVERROR_INVALIDDATA;
  325. str_size = FFMIN3(sizeof(str)-1, str_size, atom.size);
  326. if (parse)
  327. parse(c, pb, str_size, key);
  328. else {
  329. if (data_type == 3 || (data_type == 0 && (langcode < 0x400 || langcode == 0x7fff))) { // MAC Encoded
  330. mov_read_mac_string(c, pb, str_size, str, sizeof(str));
  331. } else {
  332. avio_read(pb, str, str_size);
  333. str[str_size] = 0;
  334. }
  335. av_dict_set(&c->fc->metadata, key, str, 0);
  336. if (*language && strcmp(language, "und")) {
  337. snprintf(key2, sizeof(key2), "%s-%s", key, language);
  338. av_dict_set(&c->fc->metadata, key2, str, 0);
  339. }
  340. }
  341. av_dlog(c->fc, "lang \"%3s\" ", language);
  342. av_dlog(c->fc, "tag \"%s\" value \"%s\" atom \"%.4s\" %d %"PRId64"\n",
  343. key, str, (char*)&atom.type, str_size, atom.size);
  344. return 0;
  345. }
  346. static int mov_read_chpl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  347. {
  348. int64_t start;
  349. int i, nb_chapters, str_len, version;
  350. char str[256+1];
  351. if ((atom.size -= 5) < 0)
  352. return 0;
  353. version = avio_r8(pb);
  354. avio_rb24(pb);
  355. if (version)
  356. avio_rb32(pb); // ???
  357. nb_chapters = avio_r8(pb);
  358. for (i = 0; i < nb_chapters; i++) {
  359. if (atom.size < 9)
  360. return 0;
  361. start = avio_rb64(pb);
  362. str_len = avio_r8(pb);
  363. if ((atom.size -= 9+str_len) < 0)
  364. return 0;
  365. avio_read(pb, str, str_len);
  366. str[str_len] = 0;
  367. avpriv_new_chapter(c->fc, i, (AVRational){1,10000000}, start, AV_NOPTS_VALUE, str);
  368. }
  369. return 0;
  370. }
  371. static int mov_read_dref(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  372. {
  373. AVStream *st;
  374. MOVStreamContext *sc;
  375. int entries, i, j;
  376. if (c->fc->nb_streams < 1)
  377. return 0;
  378. st = c->fc->streams[c->fc->nb_streams-1];
  379. sc = st->priv_data;
  380. avio_rb32(pb); // version + flags
  381. entries = avio_rb32(pb);
  382. if (entries >= UINT_MAX / sizeof(*sc->drefs))
  383. return AVERROR_INVALIDDATA;
  384. av_free(sc->drefs);
  385. sc->drefs_count = 0;
  386. sc->drefs = av_mallocz(entries * sizeof(*sc->drefs));
  387. if (!sc->drefs)
  388. return AVERROR(ENOMEM);
  389. sc->drefs_count = entries;
  390. for (i = 0; i < sc->drefs_count; i++) {
  391. MOVDref *dref = &sc->drefs[i];
  392. uint32_t size = avio_rb32(pb);
  393. int64_t next = avio_tell(pb) + size - 4;
  394. if (size < 12)
  395. return AVERROR_INVALIDDATA;
  396. dref->type = avio_rl32(pb);
  397. avio_rb32(pb); // version + flags
  398. av_dlog(c->fc, "type %.4s size %d\n", (char*)&dref->type, size);
  399. if (dref->type == MKTAG('a','l','i','s') && size > 150) {
  400. /* macintosh alias record */
  401. uint16_t volume_len, len;
  402. int16_t type;
  403. avio_skip(pb, 10);
  404. volume_len = avio_r8(pb);
  405. volume_len = FFMIN(volume_len, 27);
  406. avio_read(pb, dref->volume, 27);
  407. dref->volume[volume_len] = 0;
  408. av_log(c->fc, AV_LOG_DEBUG, "volume %s, len %d\n", dref->volume, volume_len);
  409. avio_skip(pb, 12);
  410. len = avio_r8(pb);
  411. len = FFMIN(len, 63);
  412. avio_read(pb, dref->filename, 63);
  413. dref->filename[len] = 0;
  414. av_log(c->fc, AV_LOG_DEBUG, "filename %s, len %d\n", dref->filename, len);
  415. avio_skip(pb, 16);
  416. /* read next level up_from_alias/down_to_target */
  417. dref->nlvl_from = avio_rb16(pb);
  418. dref->nlvl_to = avio_rb16(pb);
  419. av_log(c->fc, AV_LOG_DEBUG, "nlvl from %d, nlvl to %d\n",
  420. dref->nlvl_from, dref->nlvl_to);
  421. avio_skip(pb, 16);
  422. for (type = 0; type != -1 && avio_tell(pb) < next; ) {
  423. if(url_feof(pb))
  424. return AVERROR_EOF;
  425. type = avio_rb16(pb);
  426. len = avio_rb16(pb);
  427. av_log(c->fc, AV_LOG_DEBUG, "type %d, len %d\n", type, len);
  428. if (len&1)
  429. len += 1;
  430. if (type == 2) { // absolute path
  431. av_free(dref->path);
  432. dref->path = av_mallocz(len+1);
  433. if (!dref->path)
  434. return AVERROR(ENOMEM);
  435. avio_read(pb, dref->path, len);
  436. if (len > volume_len && !strncmp(dref->path, dref->volume, volume_len)) {
  437. len -= volume_len;
  438. memmove(dref->path, dref->path+volume_len, len);
  439. dref->path[len] = 0;
  440. }
  441. for (j = 0; j < len; j++)
  442. if (dref->path[j] == ':')
  443. dref->path[j] = '/';
  444. av_log(c->fc, AV_LOG_DEBUG, "path %s\n", dref->path);
  445. } else if (type == 0) { // directory name
  446. av_free(dref->dir);
  447. dref->dir = av_malloc(len+1);
  448. if (!dref->dir)
  449. return AVERROR(ENOMEM);
  450. avio_read(pb, dref->dir, len);
  451. dref->dir[len] = 0;
  452. for (j = 0; j < len; j++)
  453. if (dref->dir[j] == ':')
  454. dref->dir[j] = '/';
  455. av_log(c->fc, AV_LOG_DEBUG, "dir %s\n", dref->dir);
  456. } else
  457. avio_skip(pb, len);
  458. }
  459. }
  460. avio_seek(pb, next, SEEK_SET);
  461. }
  462. return 0;
  463. }
  464. static int mov_read_hdlr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  465. {
  466. AVStream *st;
  467. uint32_t type;
  468. uint32_t av_unused ctype;
  469. int title_size;
  470. char *title_str;
  471. if (c->fc->nb_streams < 1) // meta before first trak
  472. return 0;
  473. st = c->fc->streams[c->fc->nb_streams-1];
  474. avio_r8(pb); /* version */
  475. avio_rb24(pb); /* flags */
  476. /* component type */
  477. ctype = avio_rl32(pb);
  478. type = avio_rl32(pb); /* component subtype */
  479. av_dlog(c->fc, "ctype= %.4s (0x%08x)\n", (char*)&ctype, ctype);
  480. av_dlog(c->fc, "stype= %.4s\n", (char*)&type);
  481. if (type == MKTAG('v','i','d','e'))
  482. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  483. else if (type == MKTAG('s','o','u','n'))
  484. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  485. else if (type == MKTAG('m','1','a',' '))
  486. st->codec->codec_id = AV_CODEC_ID_MP2;
  487. else if ((type == MKTAG('s','u','b','p')) || (type == MKTAG('c','l','c','p')))
  488. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  489. avio_rb32(pb); /* component manufacture */
  490. avio_rb32(pb); /* component flags */
  491. avio_rb32(pb); /* component flags mask */
  492. title_size = atom.size - 24;
  493. if (title_size > 0) {
  494. title_str = av_malloc(title_size + 1); /* Add null terminator */
  495. if (!title_str)
  496. return AVERROR(ENOMEM);
  497. avio_read(pb, title_str, title_size);
  498. title_str[title_size] = 0;
  499. if (title_str[0])
  500. av_dict_set(&st->metadata, "handler_name", title_str +
  501. (!c->isom && title_str[0] == title_size - 1), 0);
  502. av_freep(&title_str);
  503. }
  504. return 0;
  505. }
  506. int ff_mov_read_esds(AVFormatContext *fc, AVIOContext *pb, MOVAtom atom)
  507. {
  508. AVStream *st;
  509. int tag;
  510. if (fc->nb_streams < 1)
  511. return 0;
  512. st = fc->streams[fc->nb_streams-1];
  513. avio_rb32(pb); /* version + flags */
  514. ff_mp4_read_descr(fc, pb, &tag);
  515. if (tag == MP4ESDescrTag) {
  516. ff_mp4_parse_es_descr(pb, NULL);
  517. } else
  518. avio_rb16(pb); /* ID */
  519. ff_mp4_read_descr(fc, pb, &tag);
  520. if (tag == MP4DecConfigDescrTag)
  521. ff_mp4_read_dec_config_descr(fc, st, pb);
  522. return 0;
  523. }
  524. static int mov_read_esds(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  525. {
  526. return ff_mov_read_esds(c->fc, pb, atom);
  527. }
  528. static int mov_read_dac3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  529. {
  530. AVStream *st;
  531. int ac3info, acmod, lfeon, bsmod;
  532. if (c->fc->nb_streams < 1)
  533. return 0;
  534. st = c->fc->streams[c->fc->nb_streams-1];
  535. ac3info = avio_rb24(pb);
  536. bsmod = (ac3info >> 14) & 0x7;
  537. acmod = (ac3info >> 11) & 0x7;
  538. lfeon = (ac3info >> 10) & 0x1;
  539. st->codec->channels = ((int[]){2,1,2,3,3,4,4,5})[acmod] + lfeon;
  540. st->codec->channel_layout = avpriv_ac3_channel_layout_tab[acmod];
  541. if (lfeon)
  542. st->codec->channel_layout |= AV_CH_LOW_FREQUENCY;
  543. st->codec->audio_service_type = bsmod;
  544. if (st->codec->channels > 1 && bsmod == 0x7)
  545. st->codec->audio_service_type = AV_AUDIO_SERVICE_TYPE_KARAOKE;
  546. return 0;
  547. }
  548. static int mov_read_dec3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  549. {
  550. AVStream *st;
  551. int eac3info, acmod, lfeon, bsmod;
  552. if (c->fc->nb_streams < 1)
  553. return 0;
  554. st = c->fc->streams[c->fc->nb_streams-1];
  555. /* No need to parse fields for additional independent substreams and its
  556. * associated dependent substreams since libavcodec's E-AC-3 decoder
  557. * does not support them yet. */
  558. avio_rb16(pb); /* data_rate and num_ind_sub */
  559. eac3info = avio_rb24(pb);
  560. bsmod = (eac3info >> 12) & 0x1f;
  561. acmod = (eac3info >> 9) & 0x7;
  562. lfeon = (eac3info >> 8) & 0x1;
  563. st->codec->channel_layout = avpriv_ac3_channel_layout_tab[acmod];
  564. if (lfeon)
  565. st->codec->channel_layout |= AV_CH_LOW_FREQUENCY;
  566. st->codec->channels = av_get_channel_layout_nb_channels(st->codec->channel_layout);
  567. st->codec->audio_service_type = bsmod;
  568. if (st->codec->channels > 1 && bsmod == 0x7)
  569. st->codec->audio_service_type = AV_AUDIO_SERVICE_TYPE_KARAOKE;
  570. return 0;
  571. }
  572. static int mov_read_chan(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  573. {
  574. AVStream *st;
  575. if (c->fc->nb_streams < 1)
  576. return 0;
  577. st = c->fc->streams[c->fc->nb_streams-1];
  578. if (atom.size < 16)
  579. return 0;
  580. ff_mov_read_chan(c->fc, pb, st, atom.size - 4);
  581. return 0;
  582. }
  583. static int mov_read_wfex(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  584. {
  585. AVStream *st;
  586. if (c->fc->nb_streams < 1)
  587. return 0;
  588. st = c->fc->streams[c->fc->nb_streams-1];
  589. if (ff_get_wav_header(pb, st->codec, atom.size) < 0) {
  590. av_log(c->fc, AV_LOG_WARNING, "get_wav_header failed\n");
  591. }
  592. return 0;
  593. }
  594. static int mov_read_pasp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  595. {
  596. const int num = avio_rb32(pb);
  597. const int den = avio_rb32(pb);
  598. AVStream *st;
  599. if (c->fc->nb_streams < 1)
  600. return 0;
  601. st = c->fc->streams[c->fc->nb_streams-1];
  602. if ((st->sample_aspect_ratio.den != 1 || st->sample_aspect_ratio.num) && // default
  603. (den != st->sample_aspect_ratio.den || num != st->sample_aspect_ratio.num)) {
  604. av_log(c->fc, AV_LOG_WARNING,
  605. "sample aspect ratio already set to %d:%d, ignoring 'pasp' atom (%d:%d)\n",
  606. st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
  607. num, den);
  608. } else if (den != 0) {
  609. st->sample_aspect_ratio.num = num;
  610. st->sample_aspect_ratio.den = den;
  611. }
  612. return 0;
  613. }
  614. /* this atom contains actual media data */
  615. static int mov_read_mdat(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  616. {
  617. if (atom.size == 0) /* wrong one (MP4) */
  618. return 0;
  619. c->found_mdat=1;
  620. return 0; /* now go for moov */
  621. }
  622. /* read major brand, minor version and compatible brands and store them as metadata */
  623. static int mov_read_ftyp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  624. {
  625. uint32_t minor_ver;
  626. int comp_brand_size;
  627. char minor_ver_str[11]; /* 32 bit integer -> 10 digits + null */
  628. char* comp_brands_str;
  629. uint8_t type[5] = {0};
  630. avio_read(pb, type, 4);
  631. if (strcmp(type, "qt "))
  632. c->isom = 1;
  633. av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type);
  634. av_dict_set(&c->fc->metadata, "major_brand", type, 0);
  635. minor_ver = avio_rb32(pb); /* minor version */
  636. snprintf(minor_ver_str, sizeof(minor_ver_str), "%d", minor_ver);
  637. av_dict_set(&c->fc->metadata, "minor_version", minor_ver_str, 0);
  638. comp_brand_size = atom.size - 8;
  639. if (comp_brand_size < 0)
  640. return AVERROR_INVALIDDATA;
  641. comp_brands_str = av_malloc(comp_brand_size + 1); /* Add null terminator */
  642. if (!comp_brands_str)
  643. return AVERROR(ENOMEM);
  644. avio_read(pb, comp_brands_str, comp_brand_size);
  645. comp_brands_str[comp_brand_size] = 0;
  646. av_dict_set(&c->fc->metadata, "compatible_brands", comp_brands_str, 0);
  647. av_freep(&comp_brands_str);
  648. return 0;
  649. }
  650. /* this atom should contain all header atoms */
  651. static int mov_read_moov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  652. {
  653. int ret;
  654. if ((ret = mov_read_default(c, pb, atom)) < 0)
  655. return ret;
  656. /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
  657. /* so we don't parse the whole file if over a network */
  658. c->found_moov=1;
  659. return 0; /* now go for mdat */
  660. }
  661. static int mov_read_moof(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  662. {
  663. c->fragment.moof_offset = avio_tell(pb) - 8;
  664. av_dlog(c->fc, "moof offset %"PRIx64"\n", c->fragment.moof_offset);
  665. return mov_read_default(c, pb, atom);
  666. }
  667. static void mov_metadata_creation_time(AVDictionary **metadata, int64_t time)
  668. {
  669. char buffer[32];
  670. if (time) {
  671. struct tm *ptm;
  672. time_t timet;
  673. if(time >= 2082844800)
  674. time -= 2082844800; /* seconds between 1904-01-01 and Epoch */
  675. timet = time;
  676. ptm = gmtime(&timet);
  677. if (!ptm) return;
  678. strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", ptm);
  679. av_dict_set(metadata, "creation_time", buffer, 0);
  680. }
  681. }
  682. static int mov_read_mdhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  683. {
  684. AVStream *st;
  685. MOVStreamContext *sc;
  686. int version;
  687. char language[4] = {0};
  688. unsigned lang;
  689. int64_t creation_time;
  690. if (c->fc->nb_streams < 1)
  691. return 0;
  692. st = c->fc->streams[c->fc->nb_streams-1];
  693. sc = st->priv_data;
  694. version = avio_r8(pb);
  695. if (version > 1) {
  696. av_log_ask_for_sample(c, "unsupported version %d\n", version);
  697. return AVERROR_PATCHWELCOME;
  698. }
  699. avio_rb24(pb); /* flags */
  700. if (version == 1) {
  701. creation_time = avio_rb64(pb);
  702. avio_rb64(pb);
  703. } else {
  704. creation_time = avio_rb32(pb);
  705. avio_rb32(pb); /* modification time */
  706. }
  707. mov_metadata_creation_time(&st->metadata, creation_time);
  708. sc->time_scale = avio_rb32(pb);
  709. st->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
  710. lang = avio_rb16(pb); /* language */
  711. if (ff_mov_lang_to_iso639(lang, language))
  712. av_dict_set(&st->metadata, "language", language, 0);
  713. avio_rb16(pb); /* quality */
  714. return 0;
  715. }
  716. static int mov_read_mvhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  717. {
  718. int64_t creation_time;
  719. int version = avio_r8(pb); /* version */
  720. avio_rb24(pb); /* flags */
  721. if (version == 1) {
  722. creation_time = avio_rb64(pb);
  723. avio_rb64(pb);
  724. } else {
  725. creation_time = avio_rb32(pb);
  726. avio_rb32(pb); /* modification time */
  727. }
  728. mov_metadata_creation_time(&c->fc->metadata, creation_time);
  729. c->time_scale = avio_rb32(pb); /* time scale */
  730. av_dlog(c->fc, "time scale = %i\n", c->time_scale);
  731. c->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
  732. // set the AVCodecContext duration because the duration of individual tracks
  733. // may be inaccurate
  734. if (c->time_scale > 0)
  735. c->fc->duration = av_rescale(c->duration, AV_TIME_BASE, c->time_scale);
  736. avio_rb32(pb); /* preferred scale */
  737. avio_rb16(pb); /* preferred volume */
  738. avio_skip(pb, 10); /* reserved */
  739. avio_skip(pb, 36); /* display matrix */
  740. avio_rb32(pb); /* preview time */
  741. avio_rb32(pb); /* preview duration */
  742. avio_rb32(pb); /* poster time */
  743. avio_rb32(pb); /* selection time */
  744. avio_rb32(pb); /* selection duration */
  745. avio_rb32(pb); /* current time */
  746. avio_rb32(pb); /* next track ID */
  747. return 0;
  748. }
  749. static int mov_read_enda(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  750. {
  751. AVStream *st;
  752. int little_endian;
  753. if (c->fc->nb_streams < 1)
  754. return 0;
  755. st = c->fc->streams[c->fc->nb_streams-1];
  756. little_endian = avio_rb16(pb) & 0xFF;
  757. av_dlog(c->fc, "enda %d\n", little_endian);
  758. if (little_endian == 1) {
  759. switch (st->codec->codec_id) {
  760. case AV_CODEC_ID_PCM_S24BE:
  761. st->codec->codec_id = AV_CODEC_ID_PCM_S24LE;
  762. break;
  763. case AV_CODEC_ID_PCM_S32BE:
  764. st->codec->codec_id = AV_CODEC_ID_PCM_S32LE;
  765. break;
  766. case AV_CODEC_ID_PCM_F32BE:
  767. st->codec->codec_id = AV_CODEC_ID_PCM_F32LE;
  768. break;
  769. case AV_CODEC_ID_PCM_F64BE:
  770. st->codec->codec_id = AV_CODEC_ID_PCM_F64LE;
  771. break;
  772. default:
  773. break;
  774. }
  775. }
  776. return 0;
  777. }
  778. static int mov_read_fiel(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  779. {
  780. AVStream *st;
  781. unsigned mov_field_order;
  782. enum AVFieldOrder decoded_field_order = AV_FIELD_UNKNOWN;
  783. if (c->fc->nb_streams < 1) // will happen with jp2 files
  784. return 0;
  785. st = c->fc->streams[c->fc->nb_streams-1];
  786. if (atom.size < 2)
  787. return AVERROR_INVALIDDATA;
  788. mov_field_order = avio_rb16(pb);
  789. if ((mov_field_order & 0xFF00) == 0x0100)
  790. decoded_field_order = AV_FIELD_PROGRESSIVE;
  791. else if ((mov_field_order & 0xFF00) == 0x0200) {
  792. switch (mov_field_order & 0xFF) {
  793. case 0x01: decoded_field_order = AV_FIELD_TT;
  794. break;
  795. case 0x06: decoded_field_order = AV_FIELD_BB;
  796. break;
  797. case 0x09: decoded_field_order = AV_FIELD_TB;
  798. break;
  799. case 0x0E: decoded_field_order = AV_FIELD_BT;
  800. break;
  801. }
  802. }
  803. if (decoded_field_order == AV_FIELD_UNKNOWN && mov_field_order) {
  804. av_log(NULL, AV_LOG_ERROR, "Unknown MOV field order 0x%04x\n", mov_field_order);
  805. }
  806. st->codec->field_order = decoded_field_order;
  807. return 0;
  808. }
  809. /* FIXME modify qdm2/svq3/h264 decoders to take full atom as extradata */
  810. static int mov_read_extradata(MOVContext *c, AVIOContext *pb, MOVAtom atom,
  811. enum AVCodecID codec_id)
  812. {
  813. AVStream *st;
  814. uint64_t size;
  815. uint8_t *buf;
  816. if (c->fc->nb_streams < 1) // will happen with jp2 files
  817. return 0;
  818. st= c->fc->streams[c->fc->nb_streams-1];
  819. if (st->codec->codec_id != codec_id)
  820. return 0; /* unexpected codec_id - don't mess with extradata */
  821. size= (uint64_t)st->codec->extradata_size + atom.size + 8 + FF_INPUT_BUFFER_PADDING_SIZE;
  822. if (size > INT_MAX || (uint64_t)atom.size > INT_MAX)
  823. return AVERROR_INVALIDDATA;
  824. buf= av_realloc(st->codec->extradata, size);
  825. if (!buf)
  826. return AVERROR(ENOMEM);
  827. st->codec->extradata= buf;
  828. buf+= st->codec->extradata_size;
  829. st->codec->extradata_size= size - FF_INPUT_BUFFER_PADDING_SIZE;
  830. AV_WB32( buf , atom.size + 8);
  831. AV_WL32( buf + 4, atom.type);
  832. avio_read(pb, buf + 8, atom.size);
  833. return 0;
  834. }
  835. /* wrapper functions for reading ALAC/AVS/MJPEG/MJPEG2000 extradata atoms only for those codecs */
  836. static int mov_read_alac(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  837. {
  838. return mov_read_extradata(c, pb, atom, AV_CODEC_ID_ALAC);
  839. }
  840. static int mov_read_avss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  841. {
  842. return mov_read_extradata(c, pb, atom, AV_CODEC_ID_AVS);
  843. }
  844. static int mov_read_jp2h(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  845. {
  846. return mov_read_extradata(c, pb, atom, AV_CODEC_ID_JPEG2000);
  847. }
  848. static int mov_read_avid(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  849. {
  850. return mov_read_extradata(c, pb, atom, AV_CODEC_ID_AVUI);
  851. }
  852. static int mov_read_svq3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  853. {
  854. return mov_read_extradata(c, pb, atom, AV_CODEC_ID_SVQ3);
  855. }
  856. static int mov_read_wave(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  857. {
  858. AVStream *st;
  859. if (c->fc->nb_streams < 1)
  860. return 0;
  861. st = c->fc->streams[c->fc->nb_streams-1];
  862. if ((uint64_t)atom.size > (1<<30))
  863. return AVERROR_INVALIDDATA;
  864. if (st->codec->codec_id == AV_CODEC_ID_QDM2 || st->codec->codec_id == AV_CODEC_ID_QDMC) {
  865. // pass all frma atom to codec, needed at least for QDMC and QDM2
  866. av_free(st->codec->extradata);
  867. st->codec->extradata_size = 0;
  868. st->codec->extradata = av_mallocz(atom.size + FF_INPUT_BUFFER_PADDING_SIZE);
  869. if (!st->codec->extradata)
  870. return AVERROR(ENOMEM);
  871. st->codec->extradata_size = atom.size;
  872. avio_read(pb, st->codec->extradata, atom.size);
  873. } else if (atom.size > 8) { /* to read frma, esds atoms */
  874. int ret;
  875. if ((ret = mov_read_default(c, pb, atom)) < 0)
  876. return ret;
  877. } else
  878. avio_skip(pb, atom.size);
  879. return 0;
  880. }
  881. /**
  882. * This function reads atom content and puts data in extradata without tag
  883. * nor size unlike mov_read_extradata.
  884. */
  885. static int mov_read_glbl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  886. {
  887. AVStream *st;
  888. if (c->fc->nb_streams < 1)
  889. return 0;
  890. st = c->fc->streams[c->fc->nb_streams-1];
  891. if ((uint64_t)atom.size > (1<<30))
  892. return AVERROR_INVALIDDATA;
  893. if (atom.size >= 10) {
  894. // Broken files created by legacy versions of libavformat will
  895. // wrap a whole fiel atom inside of a glbl atom.
  896. unsigned size = avio_rb32(pb);
  897. unsigned type = avio_rl32(pb);
  898. avio_seek(pb, -8, SEEK_CUR);
  899. if (type == MKTAG('f','i','e','l') && size == atom.size)
  900. return mov_read_default(c, pb, atom);
  901. }
  902. av_free(st->codec->extradata);
  903. st->codec->extradata_size = 0;
  904. st->codec->extradata = av_mallocz(atom.size + FF_INPUT_BUFFER_PADDING_SIZE);
  905. if (!st->codec->extradata)
  906. return AVERROR(ENOMEM);
  907. st->codec->extradata_size = atom.size;
  908. avio_read(pb, st->codec->extradata, atom.size);
  909. return 0;
  910. }
  911. static int mov_read_dvc1(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  912. {
  913. AVStream *st;
  914. uint8_t profile_level;
  915. if (c->fc->nb_streams < 1)
  916. return 0;
  917. st = c->fc->streams[c->fc->nb_streams-1];
  918. if (atom.size >= (1<<28) || atom.size < 7)
  919. return AVERROR_INVALIDDATA;
  920. profile_level = avio_r8(pb);
  921. if ((profile_level & 0xf0) != 0xc0)
  922. return 0;
  923. av_free(st->codec->extradata);
  924. st->codec->extradata_size = 0;
  925. st->codec->extradata = av_mallocz(atom.size - 7 + FF_INPUT_BUFFER_PADDING_SIZE);
  926. if (!st->codec->extradata)
  927. return AVERROR(ENOMEM);
  928. st->codec->extradata_size = atom.size - 7;
  929. avio_seek(pb, 6, SEEK_CUR);
  930. avio_read(pb, st->codec->extradata, st->codec->extradata_size);
  931. return 0;
  932. }
  933. /**
  934. * An strf atom is a BITMAPINFOHEADER struct. This struct is 40 bytes itself,
  935. * but can have extradata appended at the end after the 40 bytes belonging
  936. * to the struct.
  937. */
  938. static int mov_read_strf(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  939. {
  940. AVStream *st;
  941. if (c->fc->nb_streams < 1)
  942. return 0;
  943. if (atom.size <= 40)
  944. return 0;
  945. st = c->fc->streams[c->fc->nb_streams-1];
  946. if ((uint64_t)atom.size > (1<<30))
  947. return AVERROR_INVALIDDATA;
  948. av_free(st->codec->extradata);
  949. st->codec->extradata_size = 0;
  950. st->codec->extradata = av_mallocz(atom.size - 40 + FF_INPUT_BUFFER_PADDING_SIZE);
  951. if (!st->codec->extradata)
  952. return AVERROR(ENOMEM);
  953. st->codec->extradata_size = atom.size - 40;
  954. avio_skip(pb, 40);
  955. avio_read(pb, st->codec->extradata, atom.size - 40);
  956. return 0;
  957. }
  958. static int mov_read_stco(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  959. {
  960. AVStream *st;
  961. MOVStreamContext *sc;
  962. unsigned int i, entries;
  963. if (c->fc->nb_streams < 1)
  964. return 0;
  965. st = c->fc->streams[c->fc->nb_streams-1];
  966. sc = st->priv_data;
  967. avio_r8(pb); /* version */
  968. avio_rb24(pb); /* flags */
  969. entries = avio_rb32(pb);
  970. if (!entries)
  971. return 0;
  972. if (entries >= UINT_MAX/sizeof(int64_t))
  973. return AVERROR_INVALIDDATA;
  974. sc->chunk_offsets = av_malloc(entries * sizeof(int64_t));
  975. if (!sc->chunk_offsets)
  976. return AVERROR(ENOMEM);
  977. sc->chunk_count = entries;
  978. if (atom.type == MKTAG('s','t','c','o'))
  979. for (i = 0; i < entries && !pb->eof_reached; i++)
  980. sc->chunk_offsets[i] = avio_rb32(pb);
  981. else if (atom.type == MKTAG('c','o','6','4'))
  982. for (i = 0; i < entries && !pb->eof_reached; i++)
  983. sc->chunk_offsets[i] = avio_rb64(pb);
  984. else
  985. return AVERROR_INVALIDDATA;
  986. sc->chunk_count = i;
  987. if (pb->eof_reached)
  988. return AVERROR_EOF;
  989. return 0;
  990. }
  991. /**
  992. * Compute codec id for 'lpcm' tag.
  993. * See CoreAudioTypes and AudioStreamBasicDescription at Apple.
  994. */
  995. enum AVCodecID ff_mov_get_lpcm_codec_id(int bps, int flags)
  996. {
  997. /* lpcm flags:
  998. * 0x1 = float
  999. * 0x2 = big-endian
  1000. * 0x4 = signed
  1001. */
  1002. return ff_get_pcm_codec_id(bps, flags & 1, flags & 2, flags & 4 ? -1 : 0);
  1003. }
  1004. int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries)
  1005. {
  1006. AVStream *st;
  1007. MOVStreamContext *sc;
  1008. int j, pseudo_stream_id;
  1009. if (c->fc->nb_streams < 1)
  1010. return 0;
  1011. st = c->fc->streams[c->fc->nb_streams-1];
  1012. sc = st->priv_data;
  1013. for (pseudo_stream_id = 0;
  1014. pseudo_stream_id < entries && !pb->eof_reached;
  1015. pseudo_stream_id++) {
  1016. //Parsing Sample description table
  1017. enum AVCodecID id;
  1018. int dref_id = 1;
  1019. MOVAtom a = { AV_RL32("stsd") };
  1020. int64_t start_pos = avio_tell(pb);
  1021. int64_t size = avio_rb32(pb); /* size */
  1022. uint32_t format = avio_rl32(pb); /* data format */
  1023. if (size >= 16) {
  1024. avio_rb32(pb); /* reserved */
  1025. avio_rb16(pb); /* reserved */
  1026. dref_id = avio_rb16(pb);
  1027. }else if (size <= 7){
  1028. av_log(c->fc, AV_LOG_ERROR, "invalid size %"PRId64" in stsd\n", size);
  1029. return AVERROR_INVALIDDATA;
  1030. }
  1031. if (st->codec->codec_tag &&
  1032. st->codec->codec_tag != format &&
  1033. (c->fc->video_codec_id ? ff_codec_get_id(ff_codec_movvideo_tags, format) != c->fc->video_codec_id
  1034. : st->codec->codec_tag != MKTAG('j','p','e','g'))
  1035. ){
  1036. /* Multiple fourcc, we skip JPEG. This is not correct, we should
  1037. * export it as a separate AVStream but this needs a few changes
  1038. * in the MOV demuxer, patch welcome. */
  1039. av_log(c->fc, AV_LOG_WARNING, "multiple fourcc not supported\n");
  1040. avio_skip(pb, size - (avio_tell(pb) - start_pos));
  1041. continue;
  1042. }
  1043. /* we cannot demux concatenated h264 streams because of different extradata */
  1044. if (st->codec->codec_tag && st->codec->codec_tag == AV_RL32("avc1"))
  1045. av_log(c->fc, AV_LOG_WARNING, "Concatenated H.264 might not play corrently.\n");
  1046. sc->pseudo_stream_id = st->codec->codec_tag ? -1 : pseudo_stream_id;
  1047. sc->dref_id= dref_id;
  1048. st->codec->codec_tag = format;
  1049. id = ff_codec_get_id(ff_codec_movaudio_tags, format);
  1050. if (id<=0 && ((format&0xFFFF) == 'm'+('s'<<8) || (format&0xFFFF) == 'T'+('S'<<8)))
  1051. id = ff_codec_get_id(ff_codec_wav_tags, av_bswap32(format)&0xFFFF);
  1052. if (st->codec->codec_type != AVMEDIA_TYPE_VIDEO && id > 0) {
  1053. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  1054. } else if (st->codec->codec_type != AVMEDIA_TYPE_AUDIO && /* do not overwrite codec type */
  1055. format && format != MKTAG('m','p','4','s')) { /* skip old asf mpeg4 tag */
  1056. id = ff_codec_get_id(ff_codec_movvideo_tags, format);
  1057. if (id <= 0)
  1058. id = ff_codec_get_id(ff_codec_bmp_tags, format);
  1059. if (id > 0)
  1060. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  1061. else if (st->codec->codec_type == AVMEDIA_TYPE_DATA ||
  1062. (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE &&
  1063. st->codec->codec_id == AV_CODEC_ID_NONE)){
  1064. id = ff_codec_get_id(ff_codec_movsubtitle_tags, format);
  1065. if (id > 0)
  1066. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  1067. }
  1068. }
  1069. av_dlog(c->fc, "size=%"PRId64" 4CC= %c%c%c%c codec_type=%d\n", size,
  1070. (format >> 0) & 0xff, (format >> 8) & 0xff, (format >> 16) & 0xff,
  1071. (format >> 24) & 0xff, st->codec->codec_type);
  1072. if (st->codec->codec_type==AVMEDIA_TYPE_VIDEO) {
  1073. unsigned int color_depth, len;
  1074. int color_greyscale;
  1075. int color_table_id;
  1076. avio_rb16(pb); /* version */
  1077. avio_rb16(pb); /* revision level */
  1078. avio_rb32(pb); /* vendor */
  1079. avio_rb32(pb); /* temporal quality */
  1080. avio_rb32(pb); /* spatial quality */
  1081. st->codec->width = avio_rb16(pb); /* width */
  1082. st->codec->height = avio_rb16(pb); /* height */
  1083. avio_rb32(pb); /* horiz resolution */
  1084. avio_rb32(pb); /* vert resolution */
  1085. avio_rb32(pb); /* data size, always 0 */
  1086. avio_rb16(pb); /* frames per samples */
  1087. len = avio_r8(pb); /* codec name, pascal string */
  1088. if (len > 31)
  1089. len = 31;
  1090. mov_read_mac_string(c, pb, len, st->codec->codec_name, 32);
  1091. if (len < 31)
  1092. avio_skip(pb, 31 - len);
  1093. /* codec_tag YV12 triggers an UV swap in rawdec.c */
  1094. if (!memcmp(st->codec->codec_name, "Planar Y'CbCr 8-bit 4:2:0", 25))
  1095. st->codec->codec_tag=MKTAG('I', '4', '2', '0');
  1096. /* Flash Media Server streams files with Sorenson Spark and tag H263 */
  1097. if (!memcmp(st->codec->codec_name, "Sorenson H263", 13)
  1098. && format == MKTAG('H','2','6','3'))
  1099. id = AV_CODEC_ID_FLV1;
  1100. st->codec->codec_id = id;
  1101. st->codec->bits_per_coded_sample = avio_rb16(pb); /* depth */
  1102. color_table_id = avio_rb16(pb); /* colortable id */
  1103. av_dlog(c->fc, "depth %d, ctab id %d\n",
  1104. st->codec->bits_per_coded_sample, color_table_id);
  1105. /* figure out the palette situation */
  1106. color_depth = st->codec->bits_per_coded_sample & 0x1F;
  1107. color_greyscale = st->codec->bits_per_coded_sample & 0x20;
  1108. /* if the depth is 2, 4, or 8 bpp, file is palettized */
  1109. if ((color_depth == 2) || (color_depth == 4) ||
  1110. (color_depth == 8)) {
  1111. /* for palette traversal */
  1112. unsigned int color_start, color_count, color_end;
  1113. unsigned char a, r, g, b;
  1114. if (color_greyscale) {
  1115. int color_index, color_dec;
  1116. /* compute the greyscale palette */
  1117. st->codec->bits_per_coded_sample = color_depth;
  1118. color_count = 1 << color_depth;
  1119. color_index = 255;
  1120. color_dec = 256 / (color_count - 1);
  1121. for (j = 0; j < color_count; j++) {
  1122. if (id == AV_CODEC_ID_CINEPAK){
  1123. r = g = b = color_count - 1 - color_index;
  1124. }else
  1125. r = g = b = color_index;
  1126. sc->palette[j] =
  1127. (0xFFU << 24) | (r << 16) | (g << 8) | (b);
  1128. color_index -= color_dec;
  1129. if (color_index < 0)
  1130. color_index = 0;
  1131. }
  1132. } else if (color_table_id) {
  1133. const uint8_t *color_table;
  1134. /* if flag bit 3 is set, use the default palette */
  1135. color_count = 1 << color_depth;
  1136. if (color_depth == 2)
  1137. color_table = ff_qt_default_palette_4;
  1138. else if (color_depth == 4)
  1139. color_table = ff_qt_default_palette_16;
  1140. else
  1141. color_table = ff_qt_default_palette_256;
  1142. for (j = 0; j < color_count; j++) {
  1143. r = color_table[j * 3 + 0];
  1144. g = color_table[j * 3 + 1];
  1145. b = color_table[j * 3 + 2];
  1146. sc->palette[j] =
  1147. (0xFFU << 24) | (r << 16) | (g << 8) | (b);
  1148. }
  1149. } else {
  1150. /* load the palette from the file */
  1151. color_start = avio_rb32(pb);
  1152. color_count = avio_rb16(pb);
  1153. color_end = avio_rb16(pb);
  1154. if ((color_start <= 255) &&
  1155. (color_end <= 255)) {
  1156. for (j = color_start; j <= color_end; j++) {
  1157. /* each A, R, G, or B component is 16 bits;
  1158. * only use the top 8 bits */
  1159. a = avio_r8(pb);
  1160. avio_r8(pb);
  1161. r = avio_r8(pb);
  1162. avio_r8(pb);
  1163. g = avio_r8(pb);
  1164. avio_r8(pb);
  1165. b = avio_r8(pb);
  1166. avio_r8(pb);
  1167. sc->palette[j] =
  1168. (a << 24 ) | (r << 16) | (g << 8) | (b);
  1169. }
  1170. }
  1171. }
  1172. sc->has_palette = 1;
  1173. }
  1174. } else if (st->codec->codec_type==AVMEDIA_TYPE_AUDIO) {
  1175. int bits_per_sample, flags;
  1176. uint16_t version = avio_rb16(pb);
  1177. AVDictionaryEntry *compatible_brands = av_dict_get(c->fc->metadata, "compatible_brands", NULL, AV_DICT_MATCH_CASE);
  1178. st->codec->codec_id = id;
  1179. avio_rb16(pb); /* revision level */
  1180. avio_rb32(pb); /* vendor */
  1181. st->codec->channels = avio_rb16(pb); /* channel count */
  1182. av_dlog(c->fc, "audio channels %d\n", st->codec->channels);
  1183. st->codec->bits_per_coded_sample = avio_rb16(pb); /* sample size */
  1184. sc->audio_cid = avio_rb16(pb);
  1185. avio_rb16(pb); /* packet size = 0 */
  1186. st->codec->sample_rate = ((avio_rb32(pb) >> 16));
  1187. //Read QT version 1 fields. In version 0 these do not exist.
  1188. av_dlog(c->fc, "version =%d, isom =%d\n",version,c->isom);
  1189. if (!c->isom ||
  1190. (compatible_brands && strstr(compatible_brands->value, "qt "))) {
  1191. if (version==1) {
  1192. sc->samples_per_frame = avio_rb32(pb);
  1193. avio_rb32(pb); /* bytes per packet */
  1194. sc->bytes_per_frame = avio_rb32(pb);
  1195. avio_rb32(pb); /* bytes per sample */
  1196. } else if (version==2) {
  1197. avio_rb32(pb); /* sizeof struct only */
  1198. st->codec->sample_rate = av_int2double(avio_rb64(pb)); /* float 64 */
  1199. st->codec->channels = avio_rb32(pb);
  1200. avio_rb32(pb); /* always 0x7F000000 */
  1201. st->codec->bits_per_coded_sample = avio_rb32(pb); /* bits per channel if sound is uncompressed */
  1202. flags = avio_rb32(pb); /* lpcm format specific flag */
  1203. sc->bytes_per_frame = avio_rb32(pb); /* bytes per audio packet if constant */
  1204. sc->samples_per_frame = avio_rb32(pb); /* lpcm frames per audio packet if constant */
  1205. if (format == MKTAG('l','p','c','m'))
  1206. st->codec->codec_id = ff_mov_get_lpcm_codec_id(st->codec->bits_per_coded_sample, flags);
  1207. }
  1208. }
  1209. switch (st->codec->codec_id) {
  1210. case AV_CODEC_ID_PCM_S8:
  1211. case AV_CODEC_ID_PCM_U8:
  1212. if (st->codec->bits_per_coded_sample == 16)
  1213. st->codec->codec_id = AV_CODEC_ID_PCM_S16BE;
  1214. break;
  1215. case AV_CODEC_ID_PCM_S16LE:
  1216. case AV_CODEC_ID_PCM_S16BE:
  1217. if (st->codec->bits_per_coded_sample == 8)
  1218. st->codec->codec_id = AV_CODEC_ID_PCM_S8;
  1219. else if (st->codec->bits_per_coded_sample == 24)
  1220. st->codec->codec_id =
  1221. st->codec->codec_id == AV_CODEC_ID_PCM_S16BE ?
  1222. AV_CODEC_ID_PCM_S24BE : AV_CODEC_ID_PCM_S24LE;
  1223. break;
  1224. /* set values for old format before stsd version 1 appeared */
  1225. case AV_CODEC_ID_MACE3:
  1226. sc->samples_per_frame = 6;
  1227. sc->bytes_per_frame = 2*st->codec->channels;
  1228. break;
  1229. case AV_CODEC_ID_MACE6:
  1230. sc->samples_per_frame = 6;
  1231. sc->bytes_per_frame = 1*st->codec->channels;
  1232. break;
  1233. case AV_CODEC_ID_ADPCM_IMA_QT:
  1234. sc->samples_per_frame = 64;
  1235. sc->bytes_per_frame = 34*st->codec->channels;
  1236. break;
  1237. case AV_CODEC_ID_GSM:
  1238. sc->samples_per_frame = 160;
  1239. sc->bytes_per_frame = 33;
  1240. break;
  1241. default:
  1242. break;
  1243. }
  1244. bits_per_sample = av_get_bits_per_sample(st->codec->codec_id);
  1245. if (bits_per_sample) {
  1246. st->codec->bits_per_coded_sample = bits_per_sample;
  1247. sc->sample_size = (bits_per_sample >> 3) * st->codec->channels;
  1248. }
  1249. } else if (st->codec->codec_type==AVMEDIA_TYPE_SUBTITLE){
  1250. // ttxt stsd contains display flags, justification, background
  1251. // color, fonts, and default styles, so fake an atom to read it
  1252. MOVAtom fake_atom = { .size = size - (avio_tell(pb) - start_pos) };
  1253. if (format != AV_RL32("mp4s")) // mp4s contains a regular esds atom
  1254. mov_read_glbl(c, pb, fake_atom);
  1255. st->codec->codec_id= id;
  1256. st->codec->width = sc->width;
  1257. st->codec->height = sc->height;
  1258. } else {
  1259. if (st->codec->codec_tag == MKTAG('t','m','c','d')) {
  1260. MOVStreamContext *tmcd_ctx = st->priv_data;
  1261. int val;
  1262. avio_rb32(pb); /* reserved */
  1263. val = avio_rb32(pb); /* flags */
  1264. tmcd_ctx->tmcd_flags = val;
  1265. if (val & 1)
  1266. st->codec->flags2 |= CODEC_FLAG2_DROP_FRAME_TIMECODE;
  1267. avio_rb32(pb); /* time scale */
  1268. avio_rb32(pb); /* frame duration */
  1269. st->codec->time_base.den = avio_r8(pb); /* number of frame */
  1270. st->codec->time_base.num = 1;
  1271. }
  1272. /* other codec type, just skip (rtp, mp4s, ...) */
  1273. avio_skip(pb, size - (avio_tell(pb) - start_pos));
  1274. }
  1275. /* this will read extra atoms at the end (wave, alac, damr, avcC, SMI ...) */
  1276. a.size = size - (avio_tell(pb) - start_pos);
  1277. if (a.size > 8) {
  1278. int ret;
  1279. if ((ret = mov_read_default(c, pb, a)) < 0)
  1280. return ret;
  1281. } else if (a.size > 0)
  1282. avio_skip(pb, a.size);
  1283. }
  1284. if (pb->eof_reached)
  1285. return AVERROR_EOF;
  1286. if (st->codec->codec_type==AVMEDIA_TYPE_AUDIO && st->codec->sample_rate==0 && sc->time_scale>1)
  1287. st->codec->sample_rate= sc->time_scale;
  1288. /* special codec parameters handling */
  1289. switch (st->codec->codec_id) {
  1290. #if CONFIG_DV_DEMUXER
  1291. case AV_CODEC_ID_DVAUDIO:
  1292. c->dv_fctx = avformat_alloc_context();
  1293. c->dv_demux = avpriv_dv_init_demux(c->dv_fctx);
  1294. if (!c->dv_demux) {
  1295. av_log(c->fc, AV_LOG_ERROR, "dv demux context init error\n");
  1296. return AVERROR(ENOMEM);
  1297. }
  1298. sc->dv_audio_container = 1;
  1299. st->codec->codec_id = AV_CODEC_ID_PCM_S16LE;
  1300. break;
  1301. #endif
  1302. /* no ifdef since parameters are always those */
  1303. case AV_CODEC_ID_QCELP:
  1304. // force sample rate for qcelp when not stored in mov
  1305. if (st->codec->codec_tag != MKTAG('Q','c','l','p'))
  1306. st->codec->sample_rate = 8000;
  1307. st->codec->channels= 1; /* really needed */
  1308. break;
  1309. case AV_CODEC_ID_AMR_NB:
  1310. st->codec->channels= 1; /* really needed */
  1311. /* force sample rate for amr, stsd in 3gp does not store sample rate */
  1312. st->codec->sample_rate = 8000;
  1313. break;
  1314. case AV_CODEC_ID_AMR_WB:
  1315. st->codec->channels = 1;
  1316. st->codec->sample_rate = 16000;
  1317. break;
  1318. case AV_CODEC_ID_MP2:
  1319. case AV_CODEC_ID_MP3:
  1320. st->codec->codec_type = AVMEDIA_TYPE_AUDIO; /* force type after stsd for m1a hdlr */
  1321. st->need_parsing = AVSTREAM_PARSE_FULL;
  1322. break;
  1323. case AV_CODEC_ID_GSM:
  1324. case AV_CODEC_ID_ADPCM_MS:
  1325. case AV_CODEC_ID_ADPCM_IMA_WAV:
  1326. case AV_CODEC_ID_ILBC:
  1327. st->codec->block_align = sc->bytes_per_frame;
  1328. break;
  1329. case AV_CODEC_ID_ALAC:
  1330. if (st->codec->extradata_size == 36) {
  1331. st->codec->channels = AV_RB8 (st->codec->extradata+21);
  1332. st->codec->sample_rate = AV_RB32(st->codec->extradata+32);
  1333. }
  1334. break;
  1335. case AV_CODEC_ID_AC3:
  1336. st->need_parsing = AVSTREAM_PARSE_FULL;
  1337. break;
  1338. case AV_CODEC_ID_MPEG1VIDEO:
  1339. st->need_parsing = AVSTREAM_PARSE_FULL;
  1340. break;
  1341. case AV_CODEC_ID_VC1:
  1342. st->need_parsing = AVSTREAM_PARSE_FULL;
  1343. break;
  1344. default:
  1345. break;
  1346. }
  1347. return 0;
  1348. }
  1349. static int mov_read_stsd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1350. {
  1351. int entries;
  1352. avio_r8(pb); /* version */
  1353. avio_rb24(pb); /* flags */
  1354. entries = avio_rb32(pb);
  1355. return ff_mov_read_stsd_entries(c, pb, entries);
  1356. }
  1357. static int mov_read_stsc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1358. {
  1359. AVStream *st;
  1360. MOVStreamContext *sc;
  1361. unsigned int i, entries;
  1362. if (c->fc->nb_streams < 1)
  1363. return 0;
  1364. st = c->fc->streams[c->fc->nb_streams-1];
  1365. sc = st->priv_data;
  1366. avio_r8(pb); /* version */
  1367. avio_rb24(pb); /* flags */
  1368. entries = avio_rb32(pb);
  1369. av_dlog(c->fc, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
  1370. if (!entries)
  1371. return 0;
  1372. if (entries >= UINT_MAX / sizeof(*sc->stsc_data))
  1373. return AVERROR_INVALIDDATA;
  1374. sc->stsc_data = av_malloc(entries * sizeof(*sc->stsc_data));
  1375. if (!sc->stsc_data)
  1376. return AVERROR(ENOMEM);
  1377. for (i = 0; i < entries && !pb->eof_reached; i++) {
  1378. sc->stsc_data[i].first = avio_rb32(pb);
  1379. sc->stsc_data[i].count = avio_rb32(pb);
  1380. sc->stsc_data[i].id = avio_rb32(pb);
  1381. }
  1382. sc->stsc_count = i;
  1383. if (pb->eof_reached)
  1384. return AVERROR_EOF;
  1385. return 0;
  1386. }
  1387. static int mov_read_stps(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1388. {
  1389. AVStream *st;
  1390. MOVStreamContext *sc;
  1391. unsigned i, entries;
  1392. if (c->fc->nb_streams < 1)
  1393. return 0;
  1394. st = c->fc->streams[c->fc->nb_streams-1];
  1395. sc = st->priv_data;
  1396. avio_rb32(pb); // version + flags
  1397. entries = avio_rb32(pb);
  1398. if (entries >= UINT_MAX / sizeof(*sc->stps_data))
  1399. return AVERROR_INVALIDDATA;
  1400. sc->stps_data = av_malloc(entries * sizeof(*sc->stps_data));
  1401. if (!sc->stps_data)
  1402. return AVERROR(ENOMEM);
  1403. for (i = 0; i < entries && !pb->eof_reached; i++) {
  1404. sc->stps_data[i] = avio_rb32(pb);
  1405. //av_dlog(c->fc, "stps %d\n", sc->stps_data[i]);
  1406. }
  1407. sc->stps_count = i;
  1408. if (pb->eof_reached)
  1409. return AVERROR_EOF;
  1410. return 0;
  1411. }
  1412. static int mov_read_stss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1413. {
  1414. AVStream *st;
  1415. MOVStreamContext *sc;
  1416. unsigned int i, entries;
  1417. if (c->fc->nb_streams < 1)
  1418. return 0;
  1419. st = c->fc->streams[c->fc->nb_streams-1];
  1420. sc = st->priv_data;
  1421. avio_r8(pb); /* version */
  1422. avio_rb24(pb); /* flags */
  1423. entries = avio_rb32(pb);
  1424. av_dlog(c->fc, "keyframe_count = %d\n", entries);
  1425. if (!entries)
  1426. {
  1427. sc->keyframe_absent = 1;
  1428. return 0;
  1429. }
  1430. if (entries >= UINT_MAX / sizeof(int))
  1431. return AVERROR_INVALIDDATA;
  1432. sc->keyframes = av_malloc(entries * sizeof(int));
  1433. if (!sc->keyframes)
  1434. return AVERROR(ENOMEM);
  1435. for (i = 0; i < entries && !pb->eof_reached; i++) {
  1436. sc->keyframes[i] = avio_rb32(pb);
  1437. //av_dlog(c->fc, "keyframes[]=%d\n", sc->keyframes[i]);
  1438. }
  1439. sc->keyframe_count = i;
  1440. if (pb->eof_reached)
  1441. return AVERROR_EOF;
  1442. return 0;
  1443. }
  1444. static int mov_read_stsz(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1445. {
  1446. AVStream *st;
  1447. MOVStreamContext *sc;
  1448. unsigned int i, entries, sample_size, field_size, num_bytes;
  1449. GetBitContext gb;
  1450. unsigned char* buf;
  1451. if (c->fc->nb_streams < 1)
  1452. return 0;
  1453. st = c->fc->streams[c->fc->nb_streams-1];
  1454. sc = st->priv_data;
  1455. avio_r8(pb); /* version */
  1456. avio_rb24(pb); /* flags */
  1457. if (atom.type == MKTAG('s','t','s','z')) {
  1458. sample_size = avio_rb32(pb);
  1459. if (!sc->sample_size) /* do not overwrite value computed in stsd */
  1460. sc->sample_size = sample_size;
  1461. sc->alt_sample_size = sample_size;
  1462. field_size = 32;
  1463. } else {
  1464. sample_size = 0;
  1465. avio_rb24(pb); /* reserved */
  1466. field_size = avio_r8(pb);
  1467. }
  1468. entries = avio_rb32(pb);
  1469. av_dlog(c->fc, "sample_size = %d sample_count = %d\n", sc->sample_size, entries);
  1470. sc->sample_count = entries;
  1471. if (sample_size)
  1472. return 0;
  1473. if (field_size != 4 && field_size != 8 && field_size != 16 && field_size != 32) {
  1474. av_log(c->fc, AV_LOG_ERROR, "Invalid sample field size %d\n", field_size);
  1475. return AVERROR_INVALIDDATA;
  1476. }
  1477. if (!entries)
  1478. return 0;
  1479. if (entries >= UINT_MAX / sizeof(int) || entries >= (UINT_MAX - 4) / field_size)
  1480. return AVERROR_INVALIDDATA;
  1481. sc->sample_sizes = av_malloc(entries * sizeof(int));
  1482. if (!sc->sample_sizes)
  1483. return AVERROR(ENOMEM);
  1484. num_bytes = (entries*field_size+4)>>3;
  1485. buf = av_malloc(num_bytes+FF_INPUT_BUFFER_PADDING_SIZE);
  1486. if (!buf) {
  1487. av_freep(&sc->sample_sizes);
  1488. return AVERROR(ENOMEM);
  1489. }
  1490. if (avio_read(pb, buf, num_bytes) < num_bytes) {
  1491. av_freep(&sc->sample_sizes);
  1492. av_free(buf);
  1493. return AVERROR_INVALIDDATA;
  1494. }
  1495. init_get_bits(&gb, buf, 8*num_bytes);
  1496. for (i = 0; i < entries && !pb->eof_reached; i++) {
  1497. sc->sample_sizes[i] = get_bits_long(&gb, field_size);
  1498. sc->data_size += sc->sample_sizes[i];
  1499. }
  1500. sc->sample_count = i;
  1501. if (pb->eof_reached)
  1502. return AVERROR_EOF;
  1503. av_free(buf);
  1504. return 0;
  1505. }
  1506. static int mov_read_stts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1507. {
  1508. AVStream *st;
  1509. MOVStreamContext *sc;
  1510. unsigned int i, entries;
  1511. int64_t duration=0;
  1512. int64_t total_sample_count=0;
  1513. if (c->fc->nb_streams < 1)
  1514. return 0;
  1515. st = c->fc->streams[c->fc->nb_streams-1];
  1516. sc = st->priv_data;
  1517. avio_r8(pb); /* version */
  1518. avio_rb24(pb); /* flags */
  1519. entries = avio_rb32(pb);
  1520. av_dlog(c->fc, "track[%i].stts.entries = %i\n",
  1521. c->fc->nb_streams-1, entries);
  1522. if (entries >= UINT_MAX / sizeof(*sc->stts_data))
  1523. return -1;
  1524. sc->stts_data = av_malloc(entries * sizeof(*sc->stts_data));
  1525. if (!sc->stts_data)
  1526. return AVERROR(ENOMEM);
  1527. for (i = 0; i < entries && !pb->eof_reached; i++) {
  1528. int sample_duration;
  1529. int sample_count;
  1530. sample_count=avio_rb32(pb);
  1531. sample_duration = avio_rb32(pb);
  1532. /* sample_duration < 0 is invalid based on the spec */
  1533. if (sample_duration < 0) {
  1534. av_log(c->fc, AV_LOG_ERROR, "Invalid SampleDelta in STTS %d\n", sample_duration);
  1535. sample_duration = 1;
  1536. }
  1537. sc->stts_data[i].count= sample_count;
  1538. sc->stts_data[i].duration= sample_duration;
  1539. av_dlog(c->fc, "sample_count=%d, sample_duration=%d\n",
  1540. sample_count, sample_duration);
  1541. duration+=(int64_t)sample_duration*sample_count;
  1542. total_sample_count+=sample_count;
  1543. }
  1544. sc->stts_count = i;
  1545. if (pb->eof_reached)
  1546. return AVERROR_EOF;
  1547. st->nb_frames= total_sample_count;
  1548. if (duration)
  1549. st->duration= duration;
  1550. sc->track_end = duration;
  1551. return 0;
  1552. }
  1553. static int mov_read_ctts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1554. {
  1555. AVStream *st;
  1556. MOVStreamContext *sc;
  1557. unsigned int i, entries;
  1558. if (c->fc->nb_streams < 1)
  1559. return 0;
  1560. st = c->fc->streams[c->fc->nb_streams-1];
  1561. sc = st->priv_data;
  1562. avio_r8(pb); /* version */
  1563. avio_rb24(pb); /* flags */
  1564. entries = avio_rb32(pb);
  1565. av_dlog(c->fc, "track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries);
  1566. if (!entries)
  1567. return 0;
  1568. if (entries >= UINT_MAX / sizeof(*sc->ctts_data))
  1569. return AVERROR_INVALIDDATA;
  1570. sc->ctts_data = av_malloc(entries * sizeof(*sc->ctts_data));
  1571. if (!sc->ctts_data)
  1572. return AVERROR(ENOMEM);
  1573. for (i = 0; i < entries && !pb->eof_reached; i++) {
  1574. int count =avio_rb32(pb);
  1575. int duration =avio_rb32(pb);
  1576. sc->ctts_data[i].count = count;
  1577. sc->ctts_data[i].duration= duration;
  1578. av_dlog(c->fc, "count=%d, duration=%d\n",
  1579. count, duration);
  1580. if (FFABS(duration) > (1<<28) && i+2<entries) {
  1581. av_log(c->fc, AV_LOG_WARNING, "CTTS invalid\n");
  1582. av_freep(&sc->ctts_data);
  1583. sc->ctts_count = 0;
  1584. return 0;
  1585. }
  1586. if (duration < 0 && i+2<entries)
  1587. sc->dts_shift = FFMAX(sc->dts_shift, -duration);
  1588. }
  1589. sc->ctts_count = i;
  1590. if (pb->eof_reached)
  1591. return AVERROR_EOF;
  1592. av_dlog(c->fc, "dts shift %d\n", sc->dts_shift);
  1593. return 0;
  1594. }
  1595. static int mov_read_sbgp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1596. {
  1597. AVStream *st;
  1598. MOVStreamContext *sc;
  1599. unsigned int i, entries;
  1600. uint8_t version;
  1601. uint32_t grouping_type;
  1602. if (c->fc->nb_streams < 1)
  1603. return 0;
  1604. st = c->fc->streams[c->fc->nb_streams-1];
  1605. sc = st->priv_data;
  1606. version = avio_r8(pb); /* version */
  1607. avio_rb24(pb); /* flags */
  1608. grouping_type = avio_rl32(pb);
  1609. if (grouping_type != MKTAG( 'r','a','p',' '))
  1610. return 0; /* only support 'rap ' grouping */
  1611. if (version == 1)
  1612. avio_rb32(pb); /* grouping_type_parameter */
  1613. entries = avio_rb32(pb);
  1614. if (!entries)
  1615. return 0;
  1616. if (entries >= UINT_MAX / sizeof(*sc->rap_group))
  1617. return AVERROR_INVALIDDATA;
  1618. sc->rap_group = av_malloc(entries * sizeof(*sc->rap_group));
  1619. if (!sc->rap_group)
  1620. return AVERROR(ENOMEM);
  1621. for (i = 0; i < entries && !pb->eof_reached; i++) {
  1622. sc->rap_group[i].count = avio_rb32(pb); /* sample_count */
  1623. sc->rap_group[i].index = avio_rb32(pb); /* group_description_index */
  1624. }
  1625. sc->rap_group_count = i;
  1626. return pb->eof_reached ? AVERROR_EOF : 0;
  1627. }
  1628. static void mov_build_index(MOVContext *mov, AVStream *st)
  1629. {
  1630. MOVStreamContext *sc = st->priv_data;
  1631. int64_t current_offset;
  1632. int64_t current_dts = 0;
  1633. unsigned int stts_index = 0;
  1634. unsigned int stsc_index = 0;
  1635. unsigned int stss_index = 0;
  1636. unsigned int stps_index = 0;
  1637. unsigned int i, j;
  1638. uint64_t stream_size = 0;
  1639. AVIndexEntry *mem;
  1640. /* adjust first dts according to edit list */
  1641. if ((sc->empty_duration || sc->start_time) && mov->time_scale > 0) {
  1642. if (sc->empty_duration)
  1643. sc->empty_duration = av_rescale(sc->empty_duration, sc->time_scale, mov->time_scale);
  1644. sc->time_offset = sc->start_time - sc->empty_duration;
  1645. current_dts = -sc->time_offset;
  1646. if (sc->ctts_count>0 && sc->stts_count>0 &&
  1647. sc->ctts_data[0].duration / FFMAX(sc->stts_data[0].duration, 1) > 16) {
  1648. /* more than 16 frames delay, dts are likely wrong
  1649. this happens with files created by iMovie */
  1650. sc->wrong_dts = 1;
  1651. st->codec->has_b_frames = 1;
  1652. }
  1653. }
  1654. /* only use old uncompressed audio chunk demuxing when stts specifies it */
  1655. if (!(st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
  1656. sc->stts_count == 1 && sc->stts_data[0].duration == 1)) {
  1657. unsigned int current_sample = 0;
  1658. unsigned int stts_sample = 0;
  1659. unsigned int sample_size;
  1660. unsigned int distance = 0;
  1661. unsigned int rap_group_index = 0;
  1662. unsigned int rap_group_sample = 0;
  1663. int rap_group_present = sc->rap_group_count && sc->rap_group;
  1664. int key_off = (sc->keyframe_count && sc->keyframes[0] > 0) || (sc->stps_count && sc->stps_data[0] > 0);
  1665. current_dts -= sc->dts_shift;
  1666. if (!sc->sample_count || st->nb_index_entries)
  1667. return;
  1668. if (sc->sample_count >= UINT_MAX / sizeof(*st->index_entries) - st->nb_index_entries)
  1669. return;
  1670. mem = av_realloc(st->index_entries, (st->nb_index_entries + sc->sample_count) * sizeof(*st->index_entries));
  1671. if (!mem)
  1672. return;
  1673. st->index_entries = mem;
  1674. st->index_entries_allocated_size = (st->nb_index_entries + sc->sample_count) * sizeof(*st->index_entries);
  1675. for (i = 0; i < sc->chunk_count; i++) {
  1676. current_offset = sc->chunk_offsets[i];
  1677. while (stsc_index + 1 < sc->stsc_count &&
  1678. i + 1 == sc->stsc_data[stsc_index + 1].first)
  1679. stsc_index++;
  1680. for (j = 0; j < sc->stsc_data[stsc_index].count; j++) {
  1681. int keyframe = 0;
  1682. if (current_sample >= sc->sample_count) {
  1683. av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n");
  1684. return;
  1685. }
  1686. if (!sc->keyframe_absent && (!sc->keyframe_count || current_sample+key_off == sc->keyframes[stss_index])) {
  1687. keyframe = 1;
  1688. if (stss_index + 1 < sc->keyframe_count)
  1689. stss_index++;
  1690. } else if (sc->stps_count && current_sample+key_off == sc->stps_data[stps_index]) {
  1691. keyframe = 1;
  1692. if (stps_index + 1 < sc->stps_count)
  1693. stps_index++;
  1694. }
  1695. if (rap_group_present && rap_group_index < sc->rap_group_count) {
  1696. if (sc->rap_group[rap_group_index].index > 0)
  1697. keyframe = 1;
  1698. if (++rap_group_sample == sc->rap_group[rap_group_index].count) {
  1699. rap_group_sample = 0;
  1700. rap_group_index++;
  1701. }
  1702. }
  1703. if (keyframe)
  1704. distance = 0;
  1705. sample_size = sc->alt_sample_size > 0 ? sc->alt_sample_size : sc->sample_sizes[current_sample];
  1706. if (sc->pseudo_stream_id == -1 ||
  1707. sc->stsc_data[stsc_index].id - 1 == sc->pseudo_stream_id) {
  1708. AVIndexEntry *e = &st->index_entries[st->nb_index_entries++];
  1709. e->pos = current_offset;
  1710. e->timestamp = current_dts;
  1711. e->size = sample_size;
  1712. e->min_distance = distance;
  1713. e->flags = keyframe ? AVINDEX_KEYFRAME : 0;
  1714. av_dlog(mov->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
  1715. "size %d, distance %d, keyframe %d\n", st->index, current_sample,
  1716. current_offset, current_dts, sample_size, distance, keyframe);
  1717. }
  1718. current_offset += sample_size;
  1719. stream_size += sample_size;
  1720. current_dts += sc->stts_data[stts_index].duration;
  1721. distance++;
  1722. stts_sample++;
  1723. current_sample++;
  1724. if (stts_index + 1 < sc->stts_count && stts_sample == sc->stts_data[stts_index].count) {
  1725. stts_sample = 0;
  1726. stts_index++;
  1727. }
  1728. }
  1729. }
  1730. if (st->duration > 0)
  1731. st->codec->bit_rate = stream_size*8*sc->time_scale/st->duration;
  1732. } else {
  1733. unsigned chunk_samples, total = 0;
  1734. // compute total chunk count
  1735. for (i = 0; i < sc->stsc_count; i++) {
  1736. unsigned count, chunk_count;
  1737. chunk_samples = sc->stsc_data[i].count;
  1738. if (i != sc->stsc_count - 1 &&
  1739. sc->samples_per_frame && chunk_samples % sc->samples_per_frame) {
  1740. av_log(mov->fc, AV_LOG_ERROR, "error unaligned chunk\n");
  1741. return;
  1742. }
  1743. if (sc->samples_per_frame >= 160) { // gsm
  1744. count = chunk_samples / sc->samples_per_frame;
  1745. } else if (sc->samples_per_frame > 1) {
  1746. unsigned samples = (1024/sc->samples_per_frame)*sc->samples_per_frame;
  1747. count = (chunk_samples+samples-1) / samples;
  1748. } else {
  1749. count = (chunk_samples+1023) / 1024;
  1750. }
  1751. if (i < sc->stsc_count - 1)
  1752. chunk_count = sc->stsc_data[i+1].first - sc->stsc_data[i].first;
  1753. else
  1754. chunk_count = sc->chunk_count - (sc->stsc_data[i].first - 1);
  1755. total += chunk_count * count;
  1756. }
  1757. av_dlog(mov->fc, "chunk count %d\n", total);
  1758. if (total >= UINT_MAX / sizeof(*st->index_entries) - st->nb_index_entries)
  1759. return;
  1760. mem = av_realloc(st->index_entries, (st->nb_index_entries + total) * sizeof(*st->index_entries));
  1761. if (!mem)
  1762. return;
  1763. st->index_entries = mem;
  1764. st->index_entries_allocated_size = (st->nb_index_entries + total) * sizeof(*st->index_entries);
  1765. // populate index
  1766. for (i = 0; i < sc->chunk_count; i++) {
  1767. current_offset = sc->chunk_offsets[i];
  1768. if (stsc_index + 1 < sc->stsc_count &&
  1769. i + 1 == sc->stsc_data[stsc_index + 1].first)
  1770. stsc_index++;
  1771. chunk_samples = sc->stsc_data[stsc_index].count;
  1772. while (chunk_samples > 0) {
  1773. AVIndexEntry *e;
  1774. unsigned size, samples;
  1775. if (sc->samples_per_frame >= 160) { // gsm
  1776. samples = sc->samples_per_frame;
  1777. size = sc->bytes_per_frame;
  1778. } else {
  1779. if (sc->samples_per_frame > 1) {
  1780. samples = FFMIN((1024 / sc->samples_per_frame)*
  1781. sc->samples_per_frame, chunk_samples);
  1782. size = (samples / sc->samples_per_frame) * sc->bytes_per_frame;
  1783. } else {
  1784. samples = FFMIN(1024, chunk_samples);
  1785. size = samples * sc->sample_size;
  1786. }
  1787. }
  1788. if (st->nb_index_entries >= total) {
  1789. av_log(mov->fc, AV_LOG_ERROR, "wrong chunk count %d\n", total);
  1790. return;
  1791. }
  1792. e = &st->index_entries[st->nb_index_entries++];
  1793. e->pos = current_offset;
  1794. e->timestamp = current_dts;
  1795. e->size = size;
  1796. e->min_distance = 0;
  1797. e->flags = AVINDEX_KEYFRAME;
  1798. av_dlog(mov->fc, "AVIndex stream %d, chunk %d, offset %"PRIx64", dts %"PRId64", "
  1799. "size %d, duration %d\n", st->index, i, current_offset, current_dts,
  1800. size, samples);
  1801. current_offset += size;
  1802. current_dts += samples;
  1803. chunk_samples -= samples;
  1804. }
  1805. }
  1806. }
  1807. }
  1808. static int mov_open_dref(AVIOContext **pb, const char *src, MOVDref *ref,
  1809. AVIOInterruptCB *int_cb, int use_absolute_path, AVFormatContext *fc)
  1810. {
  1811. /* try relative path, we do not try the absolute because it can leak information about our
  1812. system to an attacker */
  1813. if (ref->nlvl_to > 0 && ref->nlvl_from > 0) {
  1814. char filename[1024];
  1815. const char *src_path;
  1816. int i, l;
  1817. /* find a source dir */
  1818. src_path = strrchr(src, '/');
  1819. if (src_path)
  1820. src_path++;
  1821. else
  1822. src_path = src;
  1823. /* find a next level down to target */
  1824. for (i = 0, l = strlen(ref->path) - 1; l >= 0; l--)
  1825. if (ref->path[l] == '/') {
  1826. if (i == ref->nlvl_to - 1)
  1827. break;
  1828. else
  1829. i++;
  1830. }
  1831. /* compose filename if next level down to target was found */
  1832. if (i == ref->nlvl_to - 1 && src_path - src < sizeof(filename)) {
  1833. memcpy(filename, src, src_path - src);
  1834. filename[src_path - src] = 0;
  1835. for (i = 1; i < ref->nlvl_from; i++)
  1836. av_strlcat(filename, "../", 1024);
  1837. av_strlcat(filename, ref->path + l + 1, 1024);
  1838. if (!avio_open2(pb, filename, AVIO_FLAG_READ, int_cb, NULL))
  1839. return 0;
  1840. }
  1841. } else if (use_absolute_path) {
  1842. av_log(fc, AV_LOG_WARNING, "Using absolute path on user request, "
  1843. "this is a possible security issue\n");
  1844. if (!avio_open2(pb, ref->path, AVIO_FLAG_READ, int_cb, NULL))
  1845. return 0;
  1846. }
  1847. return AVERROR(ENOENT);
  1848. }
  1849. static void fix_timescale(MOVContext *c, MOVStreamContext *sc)
  1850. {
  1851. if (sc->time_scale <= 0) {
  1852. av_log(c->fc, AV_LOG_WARNING, "stream %d, timescale not set\n", sc->ffindex);
  1853. sc->time_scale = c->time_scale;
  1854. if (sc->time_scale <= 0)
  1855. sc->time_scale = 1;
  1856. }
  1857. }
  1858. static int mov_read_trak(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1859. {
  1860. AVStream *st;
  1861. MOVStreamContext *sc;
  1862. int ret;
  1863. st = avformat_new_stream(c->fc, NULL);
  1864. if (!st) return AVERROR(ENOMEM);
  1865. st->id = c->fc->nb_streams;
  1866. sc = av_mallocz(sizeof(MOVStreamContext));
  1867. if (!sc) return AVERROR(ENOMEM);
  1868. st->priv_data = sc;
  1869. st->codec->codec_type = AVMEDIA_TYPE_DATA;
  1870. sc->ffindex = st->index;
  1871. if ((ret = mov_read_default(c, pb, atom)) < 0)
  1872. return ret;
  1873. /* sanity checks */
  1874. if (sc->chunk_count && (!sc->stts_count || !sc->stsc_count ||
  1875. (!sc->sample_size && !sc->sample_count))) {
  1876. av_log(c->fc, AV_LOG_ERROR, "stream %d, missing mandatory atoms, broken header\n",
  1877. st->index);
  1878. return 0;
  1879. }
  1880. fix_timescale(c, sc);
  1881. avpriv_set_pts_info(st, 64, 1, sc->time_scale);
  1882. mov_build_index(c, st);
  1883. if (sc->dref_id-1 < sc->drefs_count && sc->drefs[sc->dref_id-1].path) {
  1884. MOVDref *dref = &sc->drefs[sc->dref_id - 1];
  1885. if (mov_open_dref(&sc->pb, c->fc->filename, dref, &c->fc->interrupt_callback,
  1886. c->use_absolute_path, c->fc) < 0)
  1887. av_log(c->fc, AV_LOG_ERROR,
  1888. "stream %d, error opening alias: path='%s', dir='%s', "
  1889. "filename='%s', volume='%s', nlvl_from=%d, nlvl_to=%d\n",
  1890. st->index, dref->path, dref->dir, dref->filename,
  1891. dref->volume, dref->nlvl_from, dref->nlvl_to);
  1892. } else
  1893. sc->pb = c->fc->pb;
  1894. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  1895. if (!st->sample_aspect_ratio.num &&
  1896. (st->codec->width != sc->width || st->codec->height != sc->height)) {
  1897. st->sample_aspect_ratio = av_d2q(((double)st->codec->height * sc->width) /
  1898. ((double)st->codec->width * sc->height), INT_MAX);
  1899. }
  1900. if (st->duration > 0)
  1901. av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
  1902. sc->time_scale*st->nb_frames, st->duration, INT_MAX);
  1903. #if FF_API_R_FRAME_RATE
  1904. if (sc->stts_count == 1 || (sc->stts_count == 2 && sc->stts_data[1].count == 1))
  1905. av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den,
  1906. sc->time_scale, sc->stts_data[0].duration, INT_MAX);
  1907. #endif
  1908. }
  1909. switch (st->codec->codec_id) {
  1910. #if CONFIG_H261_DECODER
  1911. case AV_CODEC_ID_H261:
  1912. #endif
  1913. #if CONFIG_H263_DECODER
  1914. case AV_CODEC_ID_H263:
  1915. #endif
  1916. #if CONFIG_MPEG4_DECODER
  1917. case AV_CODEC_ID_MPEG4:
  1918. #endif
  1919. st->codec->width = 0; /* let decoder init width/height */
  1920. st->codec->height= 0;
  1921. break;
  1922. }
  1923. /* Do not need those anymore. */
  1924. av_freep(&sc->chunk_offsets);
  1925. av_freep(&sc->stsc_data);
  1926. av_freep(&sc->sample_sizes);
  1927. av_freep(&sc->keyframes);
  1928. av_freep(&sc->stts_data);
  1929. av_freep(&sc->stps_data);
  1930. av_freep(&sc->rap_group);
  1931. return 0;
  1932. }
  1933. static int mov_read_ilst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1934. {
  1935. int ret;
  1936. c->itunes_metadata = 1;
  1937. ret = mov_read_default(c, pb, atom);
  1938. c->itunes_metadata = 0;
  1939. return ret;
  1940. }
  1941. static int mov_read_meta(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1942. {
  1943. while (atom.size > 8) {
  1944. uint32_t tag = avio_rl32(pb);
  1945. atom.size -= 4;
  1946. if (tag == MKTAG('h','d','l','r')) {
  1947. avio_seek(pb, -8, SEEK_CUR);
  1948. atom.size += 8;
  1949. return mov_read_default(c, pb, atom);
  1950. }
  1951. }
  1952. return 0;
  1953. }
  1954. static int mov_read_tkhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1955. {
  1956. int i;
  1957. int width;
  1958. int height;
  1959. int64_t disp_transform[2];
  1960. int display_matrix[3][2];
  1961. AVStream *st;
  1962. MOVStreamContext *sc;
  1963. int version;
  1964. if (c->fc->nb_streams < 1)
  1965. return 0;
  1966. st = c->fc->streams[c->fc->nb_streams-1];
  1967. sc = st->priv_data;
  1968. version = avio_r8(pb);
  1969. avio_rb24(pb); /* flags */
  1970. /*
  1971. MOV_TRACK_ENABLED 0x0001
  1972. MOV_TRACK_IN_MOVIE 0x0002
  1973. MOV_TRACK_IN_PREVIEW 0x0004
  1974. MOV_TRACK_IN_POSTER 0x0008
  1975. */
  1976. if (version == 1) {
  1977. avio_rb64(pb);
  1978. avio_rb64(pb);
  1979. } else {
  1980. avio_rb32(pb); /* creation time */
  1981. avio_rb32(pb); /* modification time */
  1982. }
  1983. st->id = (int)avio_rb32(pb); /* track id (NOT 0 !)*/
  1984. avio_rb32(pb); /* reserved */
  1985. /* highlevel (considering edits) duration in movie timebase */
  1986. (version == 1) ? avio_rb64(pb) : avio_rb32(pb);
  1987. avio_rb32(pb); /* reserved */
  1988. avio_rb32(pb); /* reserved */
  1989. avio_rb16(pb); /* layer */
  1990. avio_rb16(pb); /* alternate group */
  1991. avio_rb16(pb); /* volume */
  1992. avio_rb16(pb); /* reserved */
  1993. //read in the display matrix (outlined in ISO 14496-12, Section 6.2.2)
  1994. // they're kept in fixed point format through all calculations
  1995. // ignore u,v,z b/c we don't need the scale factor to calc aspect ratio
  1996. for (i = 0; i < 3; i++) {
  1997. display_matrix[i][0] = avio_rb32(pb); // 16.16 fixed point
  1998. display_matrix[i][1] = avio_rb32(pb); // 16.16 fixed point
  1999. avio_rb32(pb); // 2.30 fixed point (not used)
  2000. }
  2001. width = avio_rb32(pb); // 16.16 fixed point track width
  2002. height = avio_rb32(pb); // 16.16 fixed point track height
  2003. sc->width = width >> 16;
  2004. sc->height = height >> 16;
  2005. //Assign clockwise rotate values based on transform matrix so that
  2006. //we can compensate for iPhone orientation during capture.
  2007. if (display_matrix[1][0] == -65536 && display_matrix[0][1] == 65536) {
  2008. av_dict_set(&st->metadata, "rotate", "90", 0);
  2009. }
  2010. if (display_matrix[0][0] == -65536 && display_matrix[1][1] == -65536) {
  2011. av_dict_set(&st->metadata, "rotate", "180", 0);
  2012. }
  2013. if (display_matrix[1][0] == 65536 && display_matrix[0][1] == -65536) {
  2014. av_dict_set(&st->metadata, "rotate", "270", 0);
  2015. }
  2016. // transform the display width/height according to the matrix
  2017. // skip this if the display matrix is the default identity matrix
  2018. // or if it is rotating the picture, ex iPhone 3GS
  2019. // to keep the same scale, use [width height 1<<16]
  2020. if (width && height &&
  2021. ((display_matrix[0][0] != 65536 ||
  2022. display_matrix[1][1] != 65536) &&
  2023. !display_matrix[0][1] &&
  2024. !display_matrix[1][0] &&
  2025. !display_matrix[2][0] && !display_matrix[2][1])) {
  2026. for (i = 0; i < 2; i++)
  2027. disp_transform[i] =
  2028. (int64_t) width * display_matrix[0][i] +
  2029. (int64_t) height * display_matrix[1][i] +
  2030. ((int64_t) display_matrix[2][i] << 16);
  2031. //sample aspect ratio is new width/height divided by old width/height
  2032. st->sample_aspect_ratio = av_d2q(
  2033. ((double) disp_transform[0] * height) /
  2034. ((double) disp_transform[1] * width), INT_MAX);
  2035. }
  2036. return 0;
  2037. }
  2038. static int mov_read_tfhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2039. {
  2040. MOVFragment *frag = &c->fragment;
  2041. MOVTrackExt *trex = NULL;
  2042. int flags, track_id, i;
  2043. avio_r8(pb); /* version */
  2044. flags = avio_rb24(pb);
  2045. track_id = avio_rb32(pb);
  2046. if (!track_id)
  2047. return AVERROR_INVALIDDATA;
  2048. frag->track_id = track_id;
  2049. for (i = 0; i < c->trex_count; i++)
  2050. if (c->trex_data[i].track_id == frag->track_id) {
  2051. trex = &c->trex_data[i];
  2052. break;
  2053. }
  2054. if (!trex) {
  2055. av_log(c->fc, AV_LOG_ERROR, "could not find corresponding trex\n");
  2056. return AVERROR_INVALIDDATA;
  2057. }
  2058. frag->base_data_offset = flags & MOV_TFHD_BASE_DATA_OFFSET ?
  2059. avio_rb64(pb) : frag->moof_offset;
  2060. frag->stsd_id = flags & MOV_TFHD_STSD_ID ? avio_rb32(pb) : trex->stsd_id;
  2061. frag->duration = flags & MOV_TFHD_DEFAULT_DURATION ?
  2062. avio_rb32(pb) : trex->duration;
  2063. frag->size = flags & MOV_TFHD_DEFAULT_SIZE ?
  2064. avio_rb32(pb) : trex->size;
  2065. frag->flags = flags & MOV_TFHD_DEFAULT_FLAGS ?
  2066. avio_rb32(pb) : trex->flags;
  2067. av_dlog(c->fc, "frag flags 0x%x\n", frag->flags);
  2068. return 0;
  2069. }
  2070. static int mov_read_chap(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2071. {
  2072. c->chapter_track = avio_rb32(pb);
  2073. return 0;
  2074. }
  2075. static int mov_read_trex(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2076. {
  2077. MOVTrackExt *trex;
  2078. if ((uint64_t)c->trex_count+1 >= UINT_MAX / sizeof(*c->trex_data))
  2079. return AVERROR_INVALIDDATA;
  2080. trex = av_realloc(c->trex_data, (c->trex_count+1)*sizeof(*c->trex_data));
  2081. if (!trex)
  2082. return AVERROR(ENOMEM);
  2083. c->fc->duration = AV_NOPTS_VALUE; // the duration from mvhd is not representing the whole file when fragments are used.
  2084. c->trex_data = trex;
  2085. trex = &c->trex_data[c->trex_count++];
  2086. avio_r8(pb); /* version */
  2087. avio_rb24(pb); /* flags */
  2088. trex->track_id = avio_rb32(pb);
  2089. trex->stsd_id = avio_rb32(pb);
  2090. trex->duration = avio_rb32(pb);
  2091. trex->size = avio_rb32(pb);
  2092. trex->flags = avio_rb32(pb);
  2093. return 0;
  2094. }
  2095. static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2096. {
  2097. MOVFragment *frag = &c->fragment;
  2098. AVStream *st = NULL;
  2099. MOVStreamContext *sc;
  2100. MOVStts *ctts_data;
  2101. uint64_t offset;
  2102. int64_t dts;
  2103. int data_offset = 0;
  2104. unsigned entries, first_sample_flags = frag->flags;
  2105. int flags, distance, i, found_keyframe = 0;
  2106. for (i = 0; i < c->fc->nb_streams; i++) {
  2107. if (c->fc->streams[i]->id == frag->track_id) {
  2108. st = c->fc->streams[i];
  2109. break;
  2110. }
  2111. }
  2112. if (!st) {
  2113. av_log(c->fc, AV_LOG_ERROR, "could not find corresponding track id %d\n", frag->track_id);
  2114. return AVERROR_INVALIDDATA;
  2115. }
  2116. sc = st->priv_data;
  2117. if (sc->pseudo_stream_id+1 != frag->stsd_id)
  2118. return 0;
  2119. avio_r8(pb); /* version */
  2120. flags = avio_rb24(pb);
  2121. entries = avio_rb32(pb);
  2122. av_dlog(c->fc, "flags 0x%x entries %d\n", flags, entries);
  2123. /* Always assume the presence of composition time offsets.
  2124. * Without this assumption, for instance, we cannot deal with a track in fragmented movies that meet the following.
  2125. * 1) in the initial movie, there are no samples.
  2126. * 2) in the first movie fragment, there is only one sample without composition time offset.
  2127. * 3) in the subsequent movie fragments, there are samples with composition time offset. */
  2128. if (!sc->ctts_count && sc->sample_count)
  2129. {
  2130. /* Complement ctts table if moov atom doesn't have ctts atom. */
  2131. ctts_data = av_malloc(sizeof(*sc->ctts_data));
  2132. if (!ctts_data)
  2133. return AVERROR(ENOMEM);
  2134. sc->ctts_data = ctts_data;
  2135. sc->ctts_data[sc->ctts_count].count = sc->sample_count;
  2136. sc->ctts_data[sc->ctts_count].duration = 0;
  2137. sc->ctts_count++;
  2138. }
  2139. if ((uint64_t)entries+sc->ctts_count >= UINT_MAX/sizeof(*sc->ctts_data))
  2140. return AVERROR_INVALIDDATA;
  2141. ctts_data = av_realloc(sc->ctts_data,
  2142. (entries+sc->ctts_count)*sizeof(*sc->ctts_data));
  2143. if (!ctts_data)
  2144. return AVERROR(ENOMEM);
  2145. sc->ctts_data = ctts_data;
  2146. if (flags & MOV_TRUN_DATA_OFFSET) data_offset = avio_rb32(pb);
  2147. if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS) first_sample_flags = avio_rb32(pb);
  2148. dts = sc->track_end - sc->time_offset;
  2149. offset = frag->base_data_offset + data_offset;
  2150. distance = 0;
  2151. av_dlog(c->fc, "first sample flags 0x%x\n", first_sample_flags);
  2152. for (i = 0; i < entries && !pb->eof_reached; i++) {
  2153. unsigned sample_size = frag->size;
  2154. int sample_flags = i ? frag->flags : first_sample_flags;
  2155. unsigned sample_duration = frag->duration;
  2156. int keyframe = 0;
  2157. if (flags & MOV_TRUN_SAMPLE_DURATION) sample_duration = avio_rb32(pb);
  2158. if (flags & MOV_TRUN_SAMPLE_SIZE) sample_size = avio_rb32(pb);
  2159. if (flags & MOV_TRUN_SAMPLE_FLAGS) sample_flags = avio_rb32(pb);
  2160. sc->ctts_data[sc->ctts_count].count = 1;
  2161. sc->ctts_data[sc->ctts_count].duration = (flags & MOV_TRUN_SAMPLE_CTS) ?
  2162. avio_rb32(pb) : 0;
  2163. sc->ctts_count++;
  2164. if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
  2165. keyframe = 1;
  2166. else if (!found_keyframe)
  2167. keyframe = found_keyframe =
  2168. !(sample_flags & (MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC |
  2169. MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES));
  2170. if (keyframe)
  2171. distance = 0;
  2172. av_add_index_entry(st, offset, dts, sample_size, distance,
  2173. keyframe ? AVINDEX_KEYFRAME : 0);
  2174. av_dlog(c->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
  2175. "size %d, distance %d, keyframe %d\n", st->index, sc->sample_count+i,
  2176. offset, dts, sample_size, distance, keyframe);
  2177. distance++;
  2178. dts += sample_duration;
  2179. offset += sample_size;
  2180. sc->data_size += sample_size;
  2181. }
  2182. if (pb->eof_reached)
  2183. return AVERROR_EOF;
  2184. frag->moof_offset = offset;
  2185. st->duration = sc->track_end = dts + sc->time_offset;
  2186. return 0;
  2187. }
  2188. /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
  2189. /* like the files created with Adobe Premiere 5.0, for samples see */
  2190. /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
  2191. static int mov_read_wide(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2192. {
  2193. int err;
  2194. if (atom.size < 8)
  2195. return 0; /* continue */
  2196. if (avio_rb32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
  2197. avio_skip(pb, atom.size - 4);
  2198. return 0;
  2199. }
  2200. atom.type = avio_rl32(pb);
  2201. atom.size -= 8;
  2202. if (atom.type != MKTAG('m','d','a','t')) {
  2203. avio_skip(pb, atom.size);
  2204. return 0;
  2205. }
  2206. err = mov_read_mdat(c, pb, atom);
  2207. return err;
  2208. }
  2209. static int mov_read_cmov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2210. {
  2211. #if CONFIG_ZLIB
  2212. AVIOContext ctx;
  2213. uint8_t *cmov_data;
  2214. uint8_t *moov_data; /* uncompressed data */
  2215. long cmov_len, moov_len;
  2216. int ret = -1;
  2217. avio_rb32(pb); /* dcom atom */
  2218. if (avio_rl32(pb) != MKTAG('d','c','o','m'))
  2219. return AVERROR_INVALIDDATA;
  2220. if (avio_rl32(pb) != MKTAG('z','l','i','b')) {
  2221. av_log(c->fc, AV_LOG_ERROR, "unknown compression for cmov atom !\n");
  2222. return AVERROR_INVALIDDATA;
  2223. }
  2224. avio_rb32(pb); /* cmvd atom */
  2225. if (avio_rl32(pb) != MKTAG('c','m','v','d'))
  2226. return AVERROR_INVALIDDATA;
  2227. moov_len = avio_rb32(pb); /* uncompressed size */
  2228. cmov_len = atom.size - 6 * 4;
  2229. cmov_data = av_malloc(cmov_len);
  2230. if (!cmov_data)
  2231. return AVERROR(ENOMEM);
  2232. moov_data = av_malloc(moov_len);
  2233. if (!moov_data) {
  2234. av_free(cmov_data);
  2235. return AVERROR(ENOMEM);
  2236. }
  2237. avio_read(pb, cmov_data, cmov_len);
  2238. if (uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
  2239. goto free_and_return;
  2240. if (ffio_init_context(&ctx, moov_data, moov_len, 0, NULL, NULL, NULL, NULL) != 0)
  2241. goto free_and_return;
  2242. atom.type = MKTAG('m','o','o','v');
  2243. atom.size = moov_len;
  2244. ret = mov_read_default(c, &ctx, atom);
  2245. free_and_return:
  2246. av_free(moov_data);
  2247. av_free(cmov_data);
  2248. return ret;
  2249. #else
  2250. av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n");
  2251. return AVERROR(ENOSYS);
  2252. #endif
  2253. }
  2254. /* edit list atom */
  2255. static int mov_read_elst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2256. {
  2257. MOVStreamContext *sc;
  2258. int i, edit_count, version, edit_start_index = 0;
  2259. int unsupported = 0;
  2260. if (c->fc->nb_streams < 1 || c->ignore_editlist)
  2261. return 0;
  2262. sc = c->fc->streams[c->fc->nb_streams-1]->priv_data;
  2263. version = avio_r8(pb); /* version */
  2264. avio_rb24(pb); /* flags */
  2265. edit_count = avio_rb32(pb); /* entries */
  2266. if ((uint64_t)edit_count*12+8 > atom.size)
  2267. return AVERROR_INVALIDDATA;
  2268. av_dlog(c->fc, "track[%i].edit_count = %i\n", c->fc->nb_streams-1, edit_count);
  2269. for (i=0; i<edit_count; i++){
  2270. int64_t time;
  2271. int64_t duration;
  2272. int rate;
  2273. if (version == 1) {
  2274. duration = avio_rb64(pb);
  2275. time = avio_rb64(pb);
  2276. } else {
  2277. duration = avio_rb32(pb); /* segment duration */
  2278. time = (int32_t)avio_rb32(pb); /* media time */
  2279. }
  2280. rate = avio_rb32(pb);
  2281. if (i == 0 && time == -1) {
  2282. sc->empty_duration = duration;
  2283. edit_start_index = 1;
  2284. } else if (i == edit_start_index && time >= 0)
  2285. sc->start_time = time;
  2286. else
  2287. unsupported = 1;
  2288. av_dlog(c->fc, "duration=%"PRId64" time=%"PRId64" rate=%f\n",
  2289. duration, time, rate / 65536.0);
  2290. }
  2291. if (unsupported)
  2292. av_log(c->fc, AV_LOG_WARNING, "multiple edit list entries, "
  2293. "a/v desync might occur, patch welcome\n");
  2294. return 0;
  2295. }
  2296. static int mov_read_chan2(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2297. {
  2298. if (atom.size < 16)
  2299. return 0;
  2300. avio_skip(pb, 4);
  2301. ff_mov_read_chan(c->fc, pb, c->fc->streams[0], atom.size - 4);
  2302. return 0;
  2303. }
  2304. static int mov_read_tref(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2305. {
  2306. uint32_t i, size;
  2307. MOVStreamContext *sc;
  2308. if (c->fc->nb_streams < 1)
  2309. return AVERROR_INVALIDDATA;
  2310. sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
  2311. size = avio_rb32(pb);
  2312. if (size < 12)
  2313. return 0;
  2314. sc->trefs_count = (size - 4) / 8;
  2315. sc->trefs = av_malloc(sc->trefs_count * sizeof(*sc->trefs));
  2316. if (!sc->trefs)
  2317. return AVERROR(ENOMEM);
  2318. sc->tref_type = avio_rl32(pb);
  2319. for (i = 0; i < sc->trefs_count; i++)
  2320. sc->trefs[i] = avio_rb32(pb);
  2321. return 0;
  2322. }
  2323. static const MOVParseTableEntry mov_default_parse_table[] = {
  2324. { MKTAG('A','C','L','R'), mov_read_avid },
  2325. { MKTAG('A','P','R','G'), mov_read_avid },
  2326. { MKTAG('A','A','L','P'), mov_read_avid },
  2327. { MKTAG('A','R','E','S'), mov_read_avid },
  2328. { MKTAG('a','v','s','s'), mov_read_avss },
  2329. { MKTAG('c','h','p','l'), mov_read_chpl },
  2330. { MKTAG('c','o','6','4'), mov_read_stco },
  2331. { MKTAG('c','t','t','s'), mov_read_ctts }, /* composition time to sample */
  2332. { MKTAG('d','i','n','f'), mov_read_default },
  2333. { MKTAG('d','r','e','f'), mov_read_dref },
  2334. { MKTAG('e','d','t','s'), mov_read_default },
  2335. { MKTAG('e','l','s','t'), mov_read_elst },
  2336. { MKTAG('e','n','d','a'), mov_read_enda },
  2337. { MKTAG('f','i','e','l'), mov_read_fiel },
  2338. { MKTAG('f','t','y','p'), mov_read_ftyp },
  2339. { MKTAG('g','l','b','l'), mov_read_glbl },
  2340. { MKTAG('h','d','l','r'), mov_read_hdlr },
  2341. { MKTAG('i','l','s','t'), mov_read_ilst },
  2342. { MKTAG('j','p','2','h'), mov_read_jp2h },
  2343. { MKTAG('m','d','a','t'), mov_read_mdat },
  2344. { MKTAG('m','d','h','d'), mov_read_mdhd },
  2345. { MKTAG('m','d','i','a'), mov_read_default },
  2346. { MKTAG('m','e','t','a'), mov_read_meta },
  2347. { MKTAG('m','i','n','f'), mov_read_default },
  2348. { MKTAG('m','o','o','f'), mov_read_moof },
  2349. { MKTAG('m','o','o','v'), mov_read_moov },
  2350. { MKTAG('m','v','e','x'), mov_read_default },
  2351. { MKTAG('m','v','h','d'), mov_read_mvhd },
  2352. { MKTAG('S','M','I',' '), mov_read_svq3 },
  2353. { MKTAG('a','l','a','c'), mov_read_alac }, /* alac specific atom */
  2354. { MKTAG('a','v','c','C'), mov_read_glbl },
  2355. { MKTAG('p','a','s','p'), mov_read_pasp },
  2356. { MKTAG('s','t','b','l'), mov_read_default },
  2357. { MKTAG('s','t','c','o'), mov_read_stco },
  2358. { MKTAG('s','t','p','s'), mov_read_stps },
  2359. { MKTAG('s','t','r','f'), mov_read_strf },
  2360. { MKTAG('s','t','s','c'), mov_read_stsc },
  2361. { MKTAG('s','t','s','d'), mov_read_stsd }, /* sample description */
  2362. { MKTAG('s','t','s','s'), mov_read_stss }, /* sync sample */
  2363. { MKTAG('s','t','s','z'), mov_read_stsz }, /* sample size */
  2364. { MKTAG('s','t','t','s'), mov_read_stts },
  2365. { MKTAG('s','t','z','2'), mov_read_stsz }, /* compact sample size */
  2366. { MKTAG('t','k','h','d'), mov_read_tkhd }, /* track header */
  2367. { MKTAG('t','f','h','d'), mov_read_tfhd }, /* track fragment header */
  2368. { MKTAG('t','r','a','k'), mov_read_trak },
  2369. { MKTAG('t','r','a','f'), mov_read_default },
  2370. { MKTAG('t','r','e','f'), mov_read_tref },
  2371. { MKTAG('c','h','a','p'), mov_read_chap },
  2372. { MKTAG('t','r','e','x'), mov_read_trex },
  2373. { MKTAG('t','r','u','n'), mov_read_trun },
  2374. { MKTAG('u','d','t','a'), mov_read_default },
  2375. { MKTAG('w','a','v','e'), mov_read_wave },
  2376. { MKTAG('e','s','d','s'), mov_read_esds },
  2377. { MKTAG('d','a','c','3'), mov_read_dac3 }, /* AC-3 info */
  2378. { MKTAG('d','e','c','3'), mov_read_dec3 }, /* EAC-3 info */
  2379. { MKTAG('w','i','d','e'), mov_read_wide }, /* place holder */
  2380. { MKTAG('w','f','e','x'), mov_read_wfex },
  2381. { MKTAG('c','m','o','v'), mov_read_cmov },
  2382. { MKTAG('c','h','a','n'), mov_read_chan }, /* channel layout */
  2383. { MKTAG('d','v','c','1'), mov_read_dvc1 },
  2384. { MKTAG('s','b','g','p'), mov_read_sbgp },
  2385. { 0, NULL }
  2386. };
  2387. static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2388. {
  2389. int64_t total_size = 0;
  2390. MOVAtom a;
  2391. int i;
  2392. if (atom.size < 0)
  2393. atom.size = INT64_MAX;
  2394. while (total_size + 8 <= atom.size && !url_feof(pb)) {
  2395. int (*parse)(MOVContext*, AVIOContext*, MOVAtom) = NULL;
  2396. a.size = atom.size;
  2397. a.type=0;
  2398. if (atom.size >= 8) {
  2399. a.size = avio_rb32(pb);
  2400. a.type = avio_rl32(pb);
  2401. if (atom.type != MKTAG('r','o','o','t') &&
  2402. atom.type != MKTAG('m','o','o','v'))
  2403. {
  2404. if (a.type == MKTAG('t','r','a','k') || a.type == MKTAG('m','d','a','t'))
  2405. {
  2406. av_log(c->fc, AV_LOG_ERROR, "Broken file, trak/mdat not at top-level\n");
  2407. avio_skip(pb, -8);
  2408. return 0;
  2409. }
  2410. }
  2411. total_size += 8;
  2412. if (a.size == 1) { /* 64 bit extended size */
  2413. a.size = avio_rb64(pb) - 8;
  2414. total_size += 8;
  2415. }
  2416. }
  2417. av_dlog(c->fc, "type: %08x '%.4s' parent:'%.4s' sz: %"PRId64" %"PRId64" %"PRId64"\n",
  2418. a.type, (char*)&a.type, (char*)&atom.type, a.size, total_size, atom.size);
  2419. if (a.size == 0) {
  2420. a.size = atom.size - total_size + 8;
  2421. }
  2422. a.size -= 8;
  2423. if (a.size < 0)
  2424. break;
  2425. a.size = FFMIN(a.size, atom.size - total_size);
  2426. for (i = 0; mov_default_parse_table[i].type; i++)
  2427. if (mov_default_parse_table[i].type == a.type) {
  2428. parse = mov_default_parse_table[i].parse;
  2429. break;
  2430. }
  2431. // container is user data
  2432. if (!parse && (atom.type == MKTAG('u','d','t','a') ||
  2433. atom.type == MKTAG('i','l','s','t')))
  2434. parse = mov_read_udta_string;
  2435. if (!parse) { /* skip leaf atoms data */
  2436. avio_skip(pb, a.size);
  2437. } else {
  2438. int64_t start_pos = avio_tell(pb);
  2439. int64_t left;
  2440. int err = parse(c, pb, a);
  2441. if (err < 0)
  2442. return err;
  2443. if (c->found_moov && c->found_mdat &&
  2444. ((!pb->seekable || c->fc->flags & AVFMT_FLAG_IGNIDX) ||
  2445. start_pos + a.size == avio_size(pb))) {
  2446. if (!pb->seekable || c->fc->flags & AVFMT_FLAG_IGNIDX)
  2447. c->next_root_atom = start_pos + a.size;
  2448. return 0;
  2449. }
  2450. left = a.size - avio_tell(pb) + start_pos;
  2451. if (left > 0) /* skip garbage at atom end */
  2452. avio_skip(pb, left);
  2453. else if(left < 0) {
  2454. av_log(c->fc, AV_LOG_DEBUG, "undoing overread of %"PRId64" in '%.4s'\n", -left, (char*)&a.type);
  2455. avio_seek(pb, left, SEEK_CUR);
  2456. }
  2457. }
  2458. total_size += a.size;
  2459. }
  2460. if (total_size < atom.size && atom.size < 0x7ffff)
  2461. avio_skip(pb, atom.size - total_size);
  2462. return 0;
  2463. }
  2464. static int mov_probe(AVProbeData *p)
  2465. {
  2466. int64_t offset;
  2467. uint32_t tag;
  2468. int score = 0;
  2469. int moov_offset = -1;
  2470. /* check file header */
  2471. offset = 0;
  2472. for (;;) {
  2473. /* ignore invalid offset */
  2474. if ((offset + 8) > (unsigned int)p->buf_size)
  2475. break;
  2476. tag = AV_RL32(p->buf + offset + 4);
  2477. switch(tag) {
  2478. /* check for obvious tags */
  2479. case MKTAG('m','o','o','v'):
  2480. moov_offset = offset + 4;
  2481. case MKTAG('j','P',' ',' '): /* jpeg 2000 signature */
  2482. case MKTAG('m','d','a','t'):
  2483. case MKTAG('p','n','o','t'): /* detect movs with preview pics like ew.mov and april.mov */
  2484. case MKTAG('u','d','t','a'): /* Packet Video PVAuthor adds this and a lot of more junk */
  2485. case MKTAG('f','t','y','p'):
  2486. if (AV_RB32(p->buf+offset) < 8 &&
  2487. (AV_RB32(p->buf+offset) != 1 ||
  2488. offset + 12 > (unsigned int)p->buf_size ||
  2489. AV_RB64(p->buf+offset + 8) == 0)) {
  2490. score = FFMAX(score, AVPROBE_SCORE_MAX - 50);
  2491. } else {
  2492. score = AVPROBE_SCORE_MAX;
  2493. }
  2494. offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset;
  2495. break;
  2496. /* those are more common words, so rate then a bit less */
  2497. case MKTAG('e','d','i','w'): /* xdcam files have reverted first tags */
  2498. case MKTAG('w','i','d','e'):
  2499. case MKTAG('f','r','e','e'):
  2500. case MKTAG('j','u','n','k'):
  2501. case MKTAG('p','i','c','t'):
  2502. score = FFMAX(score, AVPROBE_SCORE_MAX - 5);
  2503. offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset;
  2504. break;
  2505. case MKTAG(0x82,0x82,0x7f,0x7d):
  2506. case MKTAG('s','k','i','p'):
  2507. case MKTAG('u','u','i','d'):
  2508. case MKTAG('p','r','f','l'):
  2509. /* if we only find those cause probedata is too small at least rate them */
  2510. score = FFMAX(score, AVPROBE_SCORE_MAX - 50);
  2511. offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset;
  2512. break;
  2513. default:
  2514. offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset;
  2515. }
  2516. }
  2517. if(score > AVPROBE_SCORE_MAX - 50 && moov_offset != -1) {
  2518. /* moov atom in the header - we should make sure that this is not a
  2519. * MOV-packed MPEG-PS */
  2520. offset = moov_offset;
  2521. while(offset < (p->buf_size - 16)){ /* Sufficient space */
  2522. /* We found an actual hdlr atom */
  2523. if(AV_RL32(p->buf + offset ) == MKTAG('h','d','l','r') &&
  2524. AV_RL32(p->buf + offset + 8) == MKTAG('m','h','l','r') &&
  2525. AV_RL32(p->buf + offset + 12) == MKTAG('M','P','E','G')){
  2526. av_log(NULL, AV_LOG_WARNING, "Found media data tag MPEG indicating this is a MOV-packed MPEG-PS.\n");
  2527. /* We found a media handler reference atom describing an
  2528. * MPEG-PS-in-MOV, return a
  2529. * low score to force expanding the probe window until
  2530. * mpegps_probe finds what it needs */
  2531. return 5;
  2532. }else
  2533. /* Keep looking */
  2534. offset+=2;
  2535. }
  2536. }
  2537. return score;
  2538. }
  2539. // must be done after parsing all trak because there's no order requirement
  2540. static void mov_read_chapters(AVFormatContext *s)
  2541. {
  2542. MOVContext *mov = s->priv_data;
  2543. AVStream *st = NULL;
  2544. MOVStreamContext *sc;
  2545. int64_t cur_pos;
  2546. int i;
  2547. for (i = 0; i < s->nb_streams; i++)
  2548. if (s->streams[i]->id == mov->chapter_track) {
  2549. st = s->streams[i];
  2550. break;
  2551. }
  2552. if (!st) {
  2553. av_log(s, AV_LOG_ERROR, "Referenced QT chapter track not found\n");
  2554. return;
  2555. }
  2556. st->discard = AVDISCARD_ALL;
  2557. sc = st->priv_data;
  2558. cur_pos = avio_tell(sc->pb);
  2559. for (i = 0; i < st->nb_index_entries; i++) {
  2560. AVIndexEntry *sample = &st->index_entries[i];
  2561. int64_t end = i+1 < st->nb_index_entries ? st->index_entries[i+1].timestamp : st->duration;
  2562. uint8_t *title;
  2563. uint16_t ch;
  2564. int len, title_len;
  2565. if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
  2566. av_log(s, AV_LOG_ERROR, "Chapter %d not found in file\n", i);
  2567. goto finish;
  2568. }
  2569. // the first two bytes are the length of the title
  2570. len = avio_rb16(sc->pb);
  2571. if (len > sample->size-2)
  2572. continue;
  2573. title_len = 2*len + 1;
  2574. if (!(title = av_mallocz(title_len)))
  2575. goto finish;
  2576. // The samples could theoretically be in any encoding if there's an encd
  2577. // atom following, but in practice are only utf-8 or utf-16, distinguished
  2578. // instead by the presence of a BOM
  2579. if (!len) {
  2580. title[0] = 0;
  2581. } else {
  2582. ch = avio_rb16(sc->pb);
  2583. if (ch == 0xfeff)
  2584. avio_get_str16be(sc->pb, len, title, title_len);
  2585. else if (ch == 0xfffe)
  2586. avio_get_str16le(sc->pb, len, title, title_len);
  2587. else {
  2588. AV_WB16(title, ch);
  2589. if (len == 1 || len == 2)
  2590. title[len] = 0;
  2591. else
  2592. avio_get_str(sc->pb, INT_MAX, title + 2, len - 1);
  2593. }
  2594. }
  2595. avpriv_new_chapter(s, i, st->time_base, sample->timestamp, end, title);
  2596. av_freep(&title);
  2597. }
  2598. finish:
  2599. avio_seek(sc->pb, cur_pos, SEEK_SET);
  2600. }
  2601. static int parse_timecode_in_framenum_format(AVFormatContext *s, AVStream *st,
  2602. uint32_t value, int flags)
  2603. {
  2604. AVTimecode tc;
  2605. char buf[AV_TIMECODE_STR_SIZE];
  2606. AVRational rate = {st->codec->time_base.den,
  2607. st->codec->time_base.num};
  2608. int ret = av_timecode_init(&tc, rate, flags, 0, s);
  2609. if (ret < 0)
  2610. return ret;
  2611. av_dict_set(&st->metadata, "timecode",
  2612. av_timecode_make_string(&tc, buf, value), 0);
  2613. return 0;
  2614. }
  2615. static int mov_read_timecode_track(AVFormatContext *s, AVStream *st)
  2616. {
  2617. MOVStreamContext *sc = st->priv_data;
  2618. int flags = 0;
  2619. int64_t cur_pos = avio_tell(sc->pb);
  2620. uint32_t value;
  2621. if (!st->nb_index_entries)
  2622. return -1;
  2623. avio_seek(sc->pb, st->index_entries->pos, SEEK_SET);
  2624. value = avio_rb32(s->pb);
  2625. if (sc->tmcd_flags & 0x0001) flags |= AV_TIMECODE_FLAG_DROPFRAME;
  2626. if (sc->tmcd_flags & 0x0002) flags |= AV_TIMECODE_FLAG_24HOURSMAX;
  2627. if (sc->tmcd_flags & 0x0004) flags |= AV_TIMECODE_FLAG_ALLOWNEGATIVE;
  2628. /* Assume Counter flag is set to 1 in tmcd track (even though it is likely
  2629. * not the case) and thus assume "frame number format" instead of QT one.
  2630. * No sample with tmcd track can be found with a QT timecode at the moment,
  2631. * despite what the tmcd track "suggests" (Counter flag set to 0 means QT
  2632. * format). */
  2633. parse_timecode_in_framenum_format(s, st, value, flags);
  2634. avio_seek(sc->pb, cur_pos, SEEK_SET);
  2635. return 0;
  2636. }
  2637. static int mov_read_close(AVFormatContext *s)
  2638. {
  2639. MOVContext *mov = s->priv_data;
  2640. int i, j;
  2641. for (i = 0; i < s->nb_streams; i++) {
  2642. AVStream *st = s->streams[i];
  2643. MOVStreamContext *sc = st->priv_data;
  2644. av_freep(&sc->ctts_data);
  2645. for (j = 0; j < sc->drefs_count; j++) {
  2646. av_freep(&sc->drefs[j].path);
  2647. av_freep(&sc->drefs[j].dir);
  2648. }
  2649. av_freep(&sc->drefs);
  2650. av_freep(&sc->trefs);
  2651. if (sc->pb && sc->pb != s->pb)
  2652. avio_close(sc->pb);
  2653. sc->pb = NULL;
  2654. av_freep(&sc->chunk_offsets);
  2655. av_freep(&sc->keyframes);
  2656. av_freep(&sc->sample_sizes);
  2657. av_freep(&sc->stps_data);
  2658. av_freep(&sc->stsc_data);
  2659. av_freep(&sc->stts_data);
  2660. }
  2661. if (mov->dv_demux) {
  2662. for (i = 0; i < mov->dv_fctx->nb_streams; i++) {
  2663. av_freep(&mov->dv_fctx->streams[i]->codec);
  2664. av_freep(&mov->dv_fctx->streams[i]);
  2665. }
  2666. av_freep(&mov->dv_fctx);
  2667. av_freep(&mov->dv_demux);
  2668. }
  2669. av_freep(&mov->trex_data);
  2670. return 0;
  2671. }
  2672. static int tmcd_is_referenced(AVFormatContext *s, int tmcd_id)
  2673. {
  2674. int i, j;
  2675. for (i = 0; i < s->nb_streams; i++) {
  2676. AVStream *st = s->streams[i];
  2677. MOVStreamContext *sc = st->priv_data;
  2678. if (s->streams[i]->codec->codec_type != AVMEDIA_TYPE_VIDEO)
  2679. continue;
  2680. for (j = 0; j < sc->trefs_count; j++)
  2681. if (tmcd_id == sc->trefs[j])
  2682. return 1;
  2683. }
  2684. return 0;
  2685. }
  2686. /* look for a tmcd track not referenced by any video track, and export it globally */
  2687. static void export_orphan_timecode(AVFormatContext *s)
  2688. {
  2689. int i;
  2690. for (i = 0; i < s->nb_streams; i++) {
  2691. AVStream *st = s->streams[i];
  2692. if (st->codec->codec_tag == MKTAG('t','m','c','d') &&
  2693. !tmcd_is_referenced(s, i + 1)) {
  2694. AVDictionaryEntry *tcr = av_dict_get(st->metadata, "timecode", NULL, 0);
  2695. if (tcr) {
  2696. av_dict_set(&s->metadata, "timecode", tcr->value, 0);
  2697. break;
  2698. }
  2699. }
  2700. }
  2701. }
  2702. static int mov_read_header(AVFormatContext *s)
  2703. {
  2704. MOVContext *mov = s->priv_data;
  2705. AVIOContext *pb = s->pb;
  2706. int i, err;
  2707. MOVAtom atom = { AV_RL32("root") };
  2708. mov->fc = s;
  2709. /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
  2710. if (pb->seekable)
  2711. atom.size = avio_size(pb);
  2712. else
  2713. atom.size = INT64_MAX;
  2714. /* check MOV header */
  2715. if ((err = mov_read_default(mov, pb, atom)) < 0) {
  2716. av_log(s, AV_LOG_ERROR, "error reading header: %d\n", err);
  2717. mov_read_close(s);
  2718. return err;
  2719. }
  2720. if (!mov->found_moov) {
  2721. av_log(s, AV_LOG_ERROR, "moov atom not found\n");
  2722. mov_read_close(s);
  2723. return AVERROR_INVALIDDATA;
  2724. }
  2725. av_dlog(mov->fc, "on_parse_exit_offset=%"PRId64"\n", avio_tell(pb));
  2726. if (pb->seekable) {
  2727. if (mov->chapter_track > 0)
  2728. mov_read_chapters(s);
  2729. for (i = 0; i < s->nb_streams; i++)
  2730. if (s->streams[i]->codec->codec_tag == AV_RL32("tmcd"))
  2731. mov_read_timecode_track(s, s->streams[i]);
  2732. }
  2733. /* copy timecode metadata from tmcd tracks to the related video streams */
  2734. for (i = 0; i < s->nb_streams; i++) {
  2735. AVStream *st = s->streams[i];
  2736. MOVStreamContext *sc = st->priv_data;
  2737. if (sc->tref_type == AV_RL32("tmcd") && sc->trefs_count) {
  2738. AVDictionaryEntry *tcr;
  2739. int tmcd_st_id = sc->trefs[0] - 1;
  2740. if (tmcd_st_id < 0 || tmcd_st_id >= s->nb_streams)
  2741. continue;
  2742. tcr = av_dict_get(s->streams[tmcd_st_id]->metadata, "timecode", NULL, 0);
  2743. if (tcr)
  2744. av_dict_set(&st->metadata, "timecode", tcr->value, 0);
  2745. }
  2746. }
  2747. export_orphan_timecode(s);
  2748. for (i = 0; i < s->nb_streams; i++) {
  2749. AVStream *st = s->streams[i];
  2750. MOVStreamContext *sc = st->priv_data;
  2751. fix_timescale(mov, sc);
  2752. if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->codec->codec_id == AV_CODEC_ID_AAC) {
  2753. st->skip_samples = sc->start_pad;
  2754. }
  2755. }
  2756. if (mov->trex_data) {
  2757. for (i = 0; i < s->nb_streams; i++) {
  2758. AVStream *st = s->streams[i];
  2759. MOVStreamContext *sc = st->priv_data;
  2760. if (st->duration)
  2761. st->codec->bit_rate = sc->data_size * 8 * sc->time_scale / st->duration;
  2762. }
  2763. }
  2764. return 0;
  2765. }
  2766. static AVIndexEntry *mov_find_next_sample(AVFormatContext *s, AVStream **st)
  2767. {
  2768. AVIndexEntry *sample = NULL;
  2769. int64_t best_dts = INT64_MAX;
  2770. int i;
  2771. for (i = 0; i < s->nb_streams; i++) {
  2772. AVStream *avst = s->streams[i];
  2773. MOVStreamContext *msc = avst->priv_data;
  2774. if (msc->pb && msc->current_sample < avst->nb_index_entries) {
  2775. AVIndexEntry *current_sample = &avst->index_entries[msc->current_sample];
  2776. int64_t dts = av_rescale(current_sample->timestamp, AV_TIME_BASE, msc->time_scale);
  2777. av_dlog(s, "stream %d, sample %d, dts %"PRId64"\n", i, msc->current_sample, dts);
  2778. if (!sample || (!s->pb->seekable && current_sample->pos < sample->pos) ||
  2779. (s->pb->seekable &&
  2780. ((msc->pb != s->pb && dts < best_dts) || (msc->pb == s->pb &&
  2781. ((FFABS(best_dts - dts) <= AV_TIME_BASE && current_sample->pos < sample->pos) ||
  2782. (FFABS(best_dts - dts) > AV_TIME_BASE && dts < best_dts)))))) {
  2783. sample = current_sample;
  2784. best_dts = dts;
  2785. *st = avst;
  2786. }
  2787. }
  2788. }
  2789. return sample;
  2790. }
  2791. static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
  2792. {
  2793. MOVContext *mov = s->priv_data;
  2794. MOVStreamContext *sc;
  2795. AVIndexEntry *sample;
  2796. AVStream *st = NULL;
  2797. int ret;
  2798. mov->fc = s;
  2799. retry:
  2800. sample = mov_find_next_sample(s, &st);
  2801. if (!sample) {
  2802. mov->found_mdat = 0;
  2803. if (!mov->next_root_atom)
  2804. return AVERROR_EOF;
  2805. avio_seek(s->pb, mov->next_root_atom, SEEK_SET);
  2806. mov->next_root_atom = 0;
  2807. if (mov_read_default(mov, s->pb, (MOVAtom){ AV_RL32("root"), INT64_MAX }) < 0 ||
  2808. url_feof(s->pb))
  2809. return AVERROR_EOF;
  2810. av_dlog(s, "read fragments, offset 0x%"PRIx64"\n", avio_tell(s->pb));
  2811. goto retry;
  2812. }
  2813. sc = st->priv_data;
  2814. /* must be done just before reading, to avoid infinite loop on sample */
  2815. sc->current_sample++;
  2816. if (st->discard != AVDISCARD_ALL) {
  2817. if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
  2818. av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n",
  2819. sc->ffindex, sample->pos);
  2820. return AVERROR_INVALIDDATA;
  2821. }
  2822. ret = av_get_packet(sc->pb, pkt, sample->size);
  2823. if (ret < 0)
  2824. return ret;
  2825. if (sc->has_palette) {
  2826. uint8_t *pal;
  2827. pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE);
  2828. if (!pal) {
  2829. av_log(mov->fc, AV_LOG_ERROR, "Cannot append palette to packet\n");
  2830. } else {
  2831. memcpy(pal, sc->palette, AVPALETTE_SIZE);
  2832. sc->has_palette = 0;
  2833. }
  2834. }
  2835. #if CONFIG_DV_DEMUXER
  2836. if (mov->dv_demux && sc->dv_audio_container) {
  2837. avpriv_dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size, pkt->pos);
  2838. av_free(pkt->data);
  2839. pkt->size = 0;
  2840. ret = avpriv_dv_get_packet(mov->dv_demux, pkt);
  2841. if (ret < 0)
  2842. return ret;
  2843. }
  2844. #endif
  2845. }
  2846. pkt->stream_index = sc->ffindex;
  2847. pkt->dts = sample->timestamp;
  2848. if (sc->ctts_data && sc->ctts_index < sc->ctts_count) {
  2849. pkt->pts = pkt->dts + sc->dts_shift + sc->ctts_data[sc->ctts_index].duration;
  2850. /* update ctts context */
  2851. sc->ctts_sample++;
  2852. if (sc->ctts_index < sc->ctts_count &&
  2853. sc->ctts_data[sc->ctts_index].count == sc->ctts_sample) {
  2854. sc->ctts_index++;
  2855. sc->ctts_sample = 0;
  2856. }
  2857. if (sc->wrong_dts)
  2858. pkt->dts = AV_NOPTS_VALUE;
  2859. } else {
  2860. int64_t next_dts = (sc->current_sample < st->nb_index_entries) ?
  2861. st->index_entries[sc->current_sample].timestamp : st->duration;
  2862. pkt->duration = next_dts - pkt->dts;
  2863. pkt->pts = pkt->dts;
  2864. }
  2865. if (st->discard == AVDISCARD_ALL)
  2866. goto retry;
  2867. pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? AV_PKT_FLAG_KEY : 0;
  2868. pkt->pos = sample->pos;
  2869. av_dlog(s, "stream %d, pts %"PRId64", dts %"PRId64", pos 0x%"PRIx64", duration %d\n",
  2870. pkt->stream_index, pkt->pts, pkt->dts, pkt->pos, pkt->duration);
  2871. return 0;
  2872. }
  2873. static int mov_seek_stream(AVFormatContext *s, AVStream *st, int64_t timestamp, int flags)
  2874. {
  2875. MOVStreamContext *sc = st->priv_data;
  2876. int sample, time_sample;
  2877. int i;
  2878. sample = av_index_search_timestamp(st, timestamp, flags);
  2879. av_dlog(s, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample);
  2880. if (sample < 0 && st->nb_index_entries && timestamp < st->index_entries[0].timestamp)
  2881. sample = 0;
  2882. if (sample < 0) /* not sure what to do */
  2883. return AVERROR_INVALIDDATA;
  2884. sc->current_sample = sample;
  2885. av_dlog(s, "stream %d, found sample %d\n", st->index, sc->current_sample);
  2886. /* adjust ctts index */
  2887. if (sc->ctts_data) {
  2888. time_sample = 0;
  2889. for (i = 0; i < sc->ctts_count; i++) {
  2890. int next = time_sample + sc->ctts_data[i].count;
  2891. if (next > sc->current_sample) {
  2892. sc->ctts_index = i;
  2893. sc->ctts_sample = sc->current_sample - time_sample;
  2894. break;
  2895. }
  2896. time_sample = next;
  2897. }
  2898. }
  2899. return sample;
  2900. }
  2901. static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
  2902. {
  2903. AVStream *st;
  2904. int64_t seek_timestamp, timestamp;
  2905. int sample;
  2906. int i;
  2907. if (stream_index >= s->nb_streams)
  2908. return AVERROR_INVALIDDATA;
  2909. st = s->streams[stream_index];
  2910. sample = mov_seek_stream(s, st, sample_time, flags);
  2911. if (sample < 0)
  2912. return sample;
  2913. /* adjust seek timestamp to found sample timestamp */
  2914. seek_timestamp = st->index_entries[sample].timestamp;
  2915. for (i = 0; i < s->nb_streams; i++) {
  2916. MOVStreamContext *sc = s->streams[i]->priv_data;
  2917. st = s->streams[i];
  2918. st->skip_samples = (sample_time <= 0) ? sc->start_pad : 0;
  2919. if (stream_index == i)
  2920. continue;
  2921. timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base);
  2922. mov_seek_stream(s, st, timestamp, flags);
  2923. }
  2924. return 0;
  2925. }
  2926. static const AVOption options[] = {
  2927. {"use_absolute_path",
  2928. "allow using absolute path when opening alias, this is a possible security issue",
  2929. offsetof(MOVContext, use_absolute_path), FF_OPT_TYPE_INT, {.i64 = 0},
  2930. 0, 1, AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_DECODING_PARAM},
  2931. {"ignore_editlist", "", offsetof(MOVContext, ignore_editlist), FF_OPT_TYPE_INT, {.i64 = 0},
  2932. 0, 1, AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_DECODING_PARAM},
  2933. {NULL}
  2934. };
  2935. static const AVClass class = {
  2936. .class_name = "mov,mp4,m4a,3gp,3g2,mj2",
  2937. .item_name = av_default_item_name,
  2938. .option = options,
  2939. .version = LIBAVUTIL_VERSION_INT,
  2940. };
  2941. AVInputFormat ff_mov_demuxer = {
  2942. .name = "mov,mp4,m4a,3gp,3g2,mj2",
  2943. .long_name = NULL_IF_CONFIG_SMALL("QuickTime / MOV"),
  2944. .priv_data_size = sizeof(MOVContext),
  2945. .read_probe = mov_probe,
  2946. .read_header = mov_read_header,
  2947. .read_packet = mov_read_packet,
  2948. .read_close = mov_read_close,
  2949. .read_seek = mov_read_seek,
  2950. .priv_class = &class,
  2951. .flags = AVFMT_NO_BYTE_SEEK,
  2952. };