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.

3501 lines
115KB

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