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.

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