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.

3402 lines
114KB

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