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.

3962 lines
131KB

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