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.

3453 lines
112KB

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