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.

2736 lines
91KB

  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/intreadwrite.h"
  26. #include "libavutil/intfloat_readwrite.h"
  27. #include "libavutil/mathematics.h"
  28. #include "libavutil/avstring.h"
  29. #include "libavutil/dict.h"
  30. #include "avformat.h"
  31. #include "avio_internal.h"
  32. #include "riff.h"
  33. #include "isom.h"
  34. #include "libavcodec/get_bits.h"
  35. #if CONFIG_ZLIB
  36. #include <zlib.h>
  37. #endif
  38. /*
  39. * First version by Francois Revol revol@free.fr
  40. * Seek function by Gael Chardon gael.dev@4now.net
  41. *
  42. * Features and limitations:
  43. * - reads most of the QT files I have (at least the structure),
  44. * Sample QuickTime files with mp3 audio can be found at: http://www.3ivx.com/showcase.html
  45. * - the code is quite ugly... maybe I won't do it recursive next time :-)
  46. *
  47. * Funny I didn't know about http://sourceforge.net/projects/qt-ffmpeg/
  48. * when coding this :) (it's a writer anyway)
  49. *
  50. * Reference documents:
  51. * http://www.geocities.com/xhelmboyx/quicktime/formats/qtm-layout.txt
  52. * Apple:
  53. * http://developer.apple.com/documentation/QuickTime/QTFF/
  54. * http://developer.apple.com/documentation/QuickTime/QTFF/qtff.pdf
  55. * QuickTime is a trademark of Apple (AFAIK :))
  56. */
  57. #include "qtpalette.h"
  58. #undef NDEBUG
  59. #include <assert.h>
  60. /* XXX: it's the first time I make a recursive parser I think... sorry if it's ugly :P */
  61. /* those functions parse an atom */
  62. /* return code:
  63. 0: continue to parse next atom
  64. <0: error occurred, exit
  65. */
  66. /* links atom IDs to parse functions */
  67. typedef struct MOVParseTableEntry {
  68. uint32_t type;
  69. int (*parse)(MOVContext *ctx, AVIOContext *pb, MOVAtom atom);
  70. } MOVParseTableEntry;
  71. static const MOVParseTableEntry mov_default_parse_table[];
  72. static int mov_metadata_track_or_disc_number(MOVContext *c, AVIOContext *pb,
  73. unsigned len, const char *key)
  74. {
  75. char buf[16];
  76. short current, total;
  77. avio_rb16(pb); // unknown
  78. current = avio_rb16(pb);
  79. total = avio_rb16(pb);
  80. if (!total)
  81. snprintf(buf, sizeof(buf), "%d", current);
  82. else
  83. snprintf(buf, sizeof(buf), "%d/%d", current, total);
  84. av_dict_set(&c->fc->metadata, key, buf, 0);
  85. return 0;
  86. }
  87. static int mov_metadata_int8(MOVContext *c, AVIOContext *pb,
  88. unsigned len, const char *key)
  89. {
  90. char buf[16];
  91. /* bypass padding bytes */
  92. avio_r8(pb);
  93. avio_r8(pb);
  94. avio_r8(pb);
  95. snprintf(buf, sizeof(buf), "%hu", avio_r8(pb));
  96. av_dict_set(&c->fc->metadata, key, buf, 0);
  97. return 0;
  98. }
  99. static int mov_metadata_stik(MOVContext *c, AVIOContext *pb,
  100. unsigned len, const char *key)
  101. {
  102. char buf[16];
  103. snprintf(buf, sizeof(buf), "%hu", avio_r8(pb));
  104. av_dict_set(&c->fc->metadata, key, buf, 0);
  105. return 0;
  106. }
  107. static const uint32_t mac_to_unicode[128] = {
  108. 0x00C4,0x00C5,0x00C7,0x00C9,0x00D1,0x00D6,0x00DC,0x00E1,
  109. 0x00E0,0x00E2,0x00E4,0x00E3,0x00E5,0x00E7,0x00E9,0x00E8,
  110. 0x00EA,0x00EB,0x00ED,0x00EC,0x00EE,0x00EF,0x00F1,0x00F3,
  111. 0x00F2,0x00F4,0x00F6,0x00F5,0x00FA,0x00F9,0x00FB,0x00FC,
  112. 0x2020,0x00B0,0x00A2,0x00A3,0x00A7,0x2022,0x00B6,0x00DF,
  113. 0x00AE,0x00A9,0x2122,0x00B4,0x00A8,0x2260,0x00C6,0x00D8,
  114. 0x221E,0x00B1,0x2264,0x2265,0x00A5,0x00B5,0x2202,0x2211,
  115. 0x220F,0x03C0,0x222B,0x00AA,0x00BA,0x03A9,0x00E6,0x00F8,
  116. 0x00BF,0x00A1,0x00AC,0x221A,0x0192,0x2248,0x2206,0x00AB,
  117. 0x00BB,0x2026,0x00A0,0x00C0,0x00C3,0x00D5,0x0152,0x0153,
  118. 0x2013,0x2014,0x201C,0x201D,0x2018,0x2019,0x00F7,0x25CA,
  119. 0x00FF,0x0178,0x2044,0x20AC,0x2039,0x203A,0xFB01,0xFB02,
  120. 0x2021,0x00B7,0x201A,0x201E,0x2030,0x00C2,0x00CA,0x00C1,
  121. 0x00CB,0x00C8,0x00CD,0x00CE,0x00CF,0x00CC,0x00D3,0x00D4,
  122. 0xF8FF,0x00D2,0x00DA,0x00DB,0x00D9,0x0131,0x02C6,0x02DC,
  123. 0x00AF,0x02D8,0x02D9,0x02DA,0x00B8,0x02DD,0x02DB,0x02C7,
  124. };
  125. static int mov_read_mac_string(MOVContext *c, AVIOContext *pb, int len,
  126. char *dst, int dstlen)
  127. {
  128. char *p = dst;
  129. char *end = dst+dstlen-1;
  130. int i;
  131. for (i = 0; i < len; i++) {
  132. uint8_t t, c = avio_r8(pb);
  133. if (c < 0x80 && p < end)
  134. *p++ = c;
  135. else
  136. PUT_UTF8(mac_to_unicode[c-0x80], t, if (p < end) *p++ = t;);
  137. }
  138. *p = 0;
  139. return p - dst;
  140. }
  141. static int mov_read_udta_string(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  142. {
  143. #ifdef MOV_EXPORT_ALL_METADATA
  144. char tmp_key[5];
  145. #endif
  146. char str[1024], key2[16], language[4] = {0};
  147. const char *key = NULL;
  148. uint16_t str_size, langcode = 0;
  149. uint32_t data_type = 0;
  150. int (*parse)(MOVContext*, AVIOContext*, unsigned, const char*) = NULL;
  151. switch (atom.type) {
  152. case MKTAG(0xa9,'n','a','m'): key = "title"; break;
  153. case MKTAG(0xa9,'a','u','t'):
  154. case MKTAG(0xa9,'A','R','T'): key = "artist"; break;
  155. case MKTAG( 'a','A','R','T'): key = "album_artist"; break;
  156. case MKTAG(0xa9,'w','r','t'): key = "composer"; break;
  157. case MKTAG( 'c','p','r','t'):
  158. case MKTAG(0xa9,'c','p','y'): key = "copyright"; break;
  159. case MKTAG(0xa9,'g','r','p'): key = "grouping"; break;
  160. case MKTAG(0xa9,'l','y','r'): key = "lyrics"; break;
  161. case MKTAG(0xa9,'c','m','t'):
  162. case MKTAG(0xa9,'i','n','f'): key = "comment"; break;
  163. case MKTAG(0xa9,'a','l','b'): key = "album"; break;
  164. case MKTAG(0xa9,'d','a','y'): key = "date"; break;
  165. case MKTAG(0xa9,'g','e','n'): key = "genre"; break;
  166. case MKTAG(0xa9,'t','o','o'):
  167. case MKTAG(0xa9,'s','w','r'): key = "encoder"; break;
  168. case MKTAG(0xa9,'e','n','c'): key = "encoder"; break;
  169. case MKTAG( 'd','e','s','c'): key = "description";break;
  170. case MKTAG( 'l','d','e','s'): key = "synopsis"; break;
  171. case MKTAG( 't','v','s','h'): key = "show"; break;
  172. case MKTAG( 't','v','e','n'): key = "episode_id";break;
  173. case MKTAG( 't','v','n','n'): key = "network"; break;
  174. case MKTAG( 't','r','k','n'): key = "track";
  175. parse = mov_metadata_track_or_disc_number; break;
  176. case MKTAG( 'd','i','s','k'): key = "disc";
  177. parse = mov_metadata_track_or_disc_number; break;
  178. case MKTAG( 't','v','e','s'): key = "episode_sort";
  179. parse = mov_metadata_int8; break;
  180. case MKTAG( 't','v','s','n'): key = "season_number";
  181. parse = mov_metadata_int8; break;
  182. case MKTAG( 's','t','i','k'): key = "media_type";
  183. parse = mov_metadata_stik; break;
  184. }
  185. if (c->itunes_metadata && atom.size > 8) {
  186. int data_size = avio_rb32(pb);
  187. int tag = avio_rl32(pb);
  188. if (tag == MKTAG('d','a','t','a')) {
  189. data_type = avio_rb32(pb); // type
  190. avio_rb32(pb); // unknown
  191. str_size = data_size - 16;
  192. atom.size -= 16;
  193. } else return 0;
  194. } else if (atom.size > 4 && key && !c->itunes_metadata) {
  195. str_size = avio_rb16(pb); // string length
  196. langcode = avio_rb16(pb);
  197. ff_mov_lang_to_iso639(langcode, language);
  198. atom.size -= 4;
  199. } else
  200. str_size = atom.size;
  201. #ifdef MOV_EXPORT_ALL_METADATA
  202. if (!key) {
  203. snprintf(tmp_key, 5, "%.4s", (char*)&atom.type);
  204. key = tmp_key;
  205. }
  206. #endif
  207. if (!key)
  208. return 0;
  209. if (atom.size < 0)
  210. return -1;
  211. str_size = FFMIN3(sizeof(str)-1, str_size, atom.size);
  212. if (parse)
  213. parse(c, pb, str_size, key);
  214. else {
  215. if (data_type == 3 || (data_type == 0 && langcode < 0x800)) { // MAC Encoded
  216. mov_read_mac_string(c, pb, str_size, str, sizeof(str));
  217. } else {
  218. avio_read(pb, str, str_size);
  219. str[str_size] = 0;
  220. }
  221. av_dict_set(&c->fc->metadata, key, str, 0);
  222. if (*language && strcmp(language, "und")) {
  223. snprintf(key2, sizeof(key2), "%s-%s", key, language);
  224. av_dict_set(&c->fc->metadata, key2, str, 0);
  225. }
  226. }
  227. av_dlog(c->fc, "lang \"%3s\" ", language);
  228. av_dlog(c->fc, "tag \"%s\" value \"%s\" atom \"%.4s\" %d %"PRId64"\n",
  229. key, str, (char*)&atom.type, str_size, atom.size);
  230. return 0;
  231. }
  232. static int mov_read_chpl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  233. {
  234. int64_t start;
  235. int i, nb_chapters, str_len, version;
  236. char str[256+1];
  237. if ((atom.size -= 5) < 0)
  238. return 0;
  239. version = avio_r8(pb);
  240. avio_rb24(pb);
  241. if (version)
  242. avio_rb32(pb); // ???
  243. nb_chapters = avio_r8(pb);
  244. for (i = 0; i < nb_chapters; i++) {
  245. if (atom.size < 9)
  246. return 0;
  247. start = avio_rb64(pb);
  248. str_len = avio_r8(pb);
  249. if ((atom.size -= 9+str_len) < 0)
  250. return 0;
  251. avio_read(pb, str, str_len);
  252. str[str_len] = 0;
  253. ff_new_chapter(c->fc, i, (AVRational){1,10000000}, start, AV_NOPTS_VALUE, str);
  254. }
  255. return 0;
  256. }
  257. static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  258. {
  259. int64_t total_size = 0;
  260. MOVAtom a;
  261. int i;
  262. if (atom.size < 0)
  263. atom.size = INT64_MAX;
  264. while (total_size + 8 < atom.size && !url_feof(pb)) {
  265. int (*parse)(MOVContext*, AVIOContext*, MOVAtom) = NULL;
  266. a.size = atom.size;
  267. a.type=0;
  268. if (atom.size >= 8) {
  269. a.size = avio_rb32(pb);
  270. a.type = avio_rl32(pb);
  271. }
  272. av_dlog(c->fc, "type: %08x '%.4s' parent:'%.4s' sz: %"PRId64" %"PRId64" %"PRId64"\n",
  273. a.type, (char*)&a.type, (char*)&atom.type, a.size, total_size, atom.size);
  274. total_size += 8;
  275. if (a.size == 1) { /* 64 bit extended size */
  276. a.size = avio_rb64(pb) - 8;
  277. total_size += 8;
  278. }
  279. if (a.size == 0) {
  280. a.size = atom.size - total_size;
  281. if (a.size <= 8)
  282. break;
  283. }
  284. a.size -= 8;
  285. if (a.size < 0)
  286. break;
  287. a.size = FFMIN(a.size, atom.size - total_size);
  288. for (i = 0; mov_default_parse_table[i].type; i++)
  289. if (mov_default_parse_table[i].type == a.type) {
  290. parse = mov_default_parse_table[i].parse;
  291. break;
  292. }
  293. // container is user data
  294. if (!parse && (atom.type == MKTAG('u','d','t','a') ||
  295. atom.type == MKTAG('i','l','s','t')))
  296. parse = mov_read_udta_string;
  297. if (!parse) { /* skip leaf atoms data */
  298. avio_skip(pb, a.size);
  299. } else {
  300. int64_t start_pos = avio_tell(pb);
  301. int64_t left;
  302. int err = parse(c, pb, a);
  303. if (err < 0)
  304. return err;
  305. if (c->found_moov && c->found_mdat &&
  306. (!pb->seekable || start_pos + a.size == avio_size(pb)))
  307. return 0;
  308. left = a.size - avio_tell(pb) + start_pos;
  309. if (left > 0) /* skip garbage at atom end */
  310. avio_skip(pb, left);
  311. }
  312. total_size += a.size;
  313. }
  314. if (total_size < atom.size && atom.size < 0x7ffff)
  315. avio_skip(pb, atom.size - total_size);
  316. return 0;
  317. }
  318. static int mov_read_dref(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  319. {
  320. AVStream *st;
  321. MOVStreamContext *sc;
  322. int entries, i, j;
  323. if (c->fc->nb_streams < 1)
  324. return 0;
  325. st = c->fc->streams[c->fc->nb_streams-1];
  326. sc = st->priv_data;
  327. avio_rb32(pb); // version + flags
  328. entries = avio_rb32(pb);
  329. if (entries >= UINT_MAX / sizeof(*sc->drefs))
  330. return -1;
  331. sc->drefs = av_mallocz(entries * sizeof(*sc->drefs));
  332. if (!sc->drefs)
  333. return AVERROR(ENOMEM);
  334. sc->drefs_count = entries;
  335. for (i = 0; i < sc->drefs_count; i++) {
  336. MOVDref *dref = &sc->drefs[i];
  337. uint32_t size = avio_rb32(pb);
  338. int64_t next = avio_tell(pb) + size - 4;
  339. if (size < 12)
  340. return -1;
  341. dref->type = avio_rl32(pb);
  342. avio_rb32(pb); // version + flags
  343. av_dlog(c->fc, "type %.4s size %d\n", (char*)&dref->type, size);
  344. if (dref->type == MKTAG('a','l','i','s') && size > 150) {
  345. /* macintosh alias record */
  346. uint16_t volume_len, len;
  347. int16_t type;
  348. avio_skip(pb, 10);
  349. volume_len = avio_r8(pb);
  350. volume_len = FFMIN(volume_len, 27);
  351. avio_read(pb, dref->volume, 27);
  352. dref->volume[volume_len] = 0;
  353. av_log(c->fc, AV_LOG_DEBUG, "volume %s, len %d\n", dref->volume, volume_len);
  354. avio_skip(pb, 12);
  355. len = avio_r8(pb);
  356. len = FFMIN(len, 63);
  357. avio_read(pb, dref->filename, 63);
  358. dref->filename[len] = 0;
  359. av_log(c->fc, AV_LOG_DEBUG, "filename %s, len %d\n", dref->filename, len);
  360. avio_skip(pb, 16);
  361. /* read next level up_from_alias/down_to_target */
  362. dref->nlvl_from = avio_rb16(pb);
  363. dref->nlvl_to = avio_rb16(pb);
  364. av_log(c->fc, AV_LOG_DEBUG, "nlvl from %d, nlvl to %d\n",
  365. dref->nlvl_from, dref->nlvl_to);
  366. avio_skip(pb, 16);
  367. for (type = 0; type != -1 && avio_tell(pb) < next; ) {
  368. type = avio_rb16(pb);
  369. len = avio_rb16(pb);
  370. av_log(c->fc, AV_LOG_DEBUG, "type %d, len %d\n", type, len);
  371. if (len&1)
  372. len += 1;
  373. if (type == 2) { // absolute path
  374. av_free(dref->path);
  375. dref->path = av_mallocz(len+1);
  376. if (!dref->path)
  377. return AVERROR(ENOMEM);
  378. avio_read(pb, dref->path, len);
  379. if (len > volume_len && !strncmp(dref->path, dref->volume, volume_len)) {
  380. len -= volume_len;
  381. memmove(dref->path, dref->path+volume_len, len);
  382. dref->path[len] = 0;
  383. }
  384. for (j = 0; j < len; j++)
  385. if (dref->path[j] == ':')
  386. dref->path[j] = '/';
  387. av_log(c->fc, AV_LOG_DEBUG, "path %s\n", dref->path);
  388. } else if (type == 0) { // directory name
  389. av_free(dref->dir);
  390. dref->dir = av_malloc(len+1);
  391. if (!dref->dir)
  392. return AVERROR(ENOMEM);
  393. avio_read(pb, dref->dir, len);
  394. dref->dir[len] = 0;
  395. for (j = 0; j < len; j++)
  396. if (dref->dir[j] == ':')
  397. dref->dir[j] = '/';
  398. av_log(c->fc, AV_LOG_DEBUG, "dir %s\n", dref->dir);
  399. } else
  400. avio_skip(pb, len);
  401. }
  402. }
  403. avio_seek(pb, next, SEEK_SET);
  404. }
  405. return 0;
  406. }
  407. static int mov_read_hdlr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  408. {
  409. AVStream *st;
  410. uint32_t type;
  411. uint32_t av_unused ctype;
  412. if (c->fc->nb_streams < 1) // meta before first trak
  413. return 0;
  414. st = c->fc->streams[c->fc->nb_streams-1];
  415. avio_r8(pb); /* version */
  416. avio_rb24(pb); /* flags */
  417. /* component type */
  418. ctype = avio_rl32(pb);
  419. type = avio_rl32(pb); /* component subtype */
  420. av_dlog(c->fc, "ctype= %.4s (0x%08x)\n", (char*)&ctype, ctype);
  421. av_dlog(c->fc, "stype= %.4s\n", (char*)&type);
  422. if (type == MKTAG('v','i','d','e'))
  423. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  424. else if (type == MKTAG('s','o','u','n'))
  425. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  426. else if (type == MKTAG('m','1','a',' '))
  427. st->codec->codec_id = CODEC_ID_MP2;
  428. else if ((type == MKTAG('s','u','b','p')) || (type == MKTAG('c','l','c','p')))
  429. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  430. avio_rb32(pb); /* component manufacture */
  431. avio_rb32(pb); /* component flags */
  432. avio_rb32(pb); /* component flags mask */
  433. return 0;
  434. }
  435. int ff_mov_read_esds(AVFormatContext *fc, AVIOContext *pb, MOVAtom atom)
  436. {
  437. AVStream *st;
  438. int tag;
  439. if (fc->nb_streams < 1)
  440. return 0;
  441. st = fc->streams[fc->nb_streams-1];
  442. avio_rb32(pb); /* version + flags */
  443. ff_mp4_read_descr(fc, pb, &tag);
  444. if (tag == MP4ESDescrTag) {
  445. ff_mp4_parse_es_descr(pb, NULL);
  446. } else
  447. avio_rb16(pb); /* ID */
  448. ff_mp4_read_descr(fc, pb, &tag);
  449. if (tag == MP4DecConfigDescrTag)
  450. ff_mp4_read_dec_config_descr(fc, st, pb);
  451. return 0;
  452. }
  453. static int mov_read_esds(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  454. {
  455. return ff_mov_read_esds(c->fc, pb, atom);
  456. }
  457. static int mov_read_dac3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  458. {
  459. AVStream *st;
  460. int ac3info, acmod, lfeon, bsmod;
  461. if (c->fc->nb_streams < 1)
  462. return 0;
  463. st = c->fc->streams[c->fc->nb_streams-1];
  464. ac3info = avio_rb24(pb);
  465. bsmod = (ac3info >> 14) & 0x7;
  466. acmod = (ac3info >> 11) & 0x7;
  467. lfeon = (ac3info >> 10) & 0x1;
  468. st->codec->channels = ((int[]){2,1,2,3,3,4,4,5})[acmod] + lfeon;
  469. st->codec->audio_service_type = bsmod;
  470. if (st->codec->channels > 1 && bsmod == 0x7)
  471. st->codec->audio_service_type = AV_AUDIO_SERVICE_TYPE_KARAOKE;
  472. return 0;
  473. }
  474. static int mov_read_wfex(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  475. {
  476. AVStream *st;
  477. if (c->fc->nb_streams < 1)
  478. return 0;
  479. st = c->fc->streams[c->fc->nb_streams-1];
  480. ff_get_wav_header(pb, st->codec, atom.size);
  481. return 0;
  482. }
  483. static int mov_read_pasp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  484. {
  485. const int num = avio_rb32(pb);
  486. const int den = avio_rb32(pb);
  487. AVStream *st;
  488. if (c->fc->nb_streams < 1)
  489. return 0;
  490. st = c->fc->streams[c->fc->nb_streams-1];
  491. if ((st->sample_aspect_ratio.den != 1 || st->sample_aspect_ratio.num) && // default
  492. (den != st->sample_aspect_ratio.den || num != st->sample_aspect_ratio.num)) {
  493. av_log(c->fc, AV_LOG_WARNING,
  494. "sample aspect ratio already set to %d:%d, ignoring 'pasp' atom (%d:%d)\n",
  495. st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
  496. num, den);
  497. } else if (den != 0) {
  498. st->sample_aspect_ratio.num = num;
  499. st->sample_aspect_ratio.den = den;
  500. }
  501. return 0;
  502. }
  503. /* this atom contains actual media data */
  504. static int mov_read_mdat(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  505. {
  506. if (atom.size == 0) /* wrong one (MP4) */
  507. return 0;
  508. c->found_mdat=1;
  509. return 0; /* now go for moov */
  510. }
  511. /* read major brand, minor version and compatible brands and store them as metadata */
  512. static int mov_read_ftyp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  513. {
  514. uint32_t minor_ver;
  515. int comp_brand_size;
  516. char minor_ver_str[11]; /* 32 bit integer -> 10 digits + null */
  517. char* comp_brands_str;
  518. uint8_t type[5] = {0};
  519. avio_read(pb, type, 4);
  520. if (strcmp(type, "qt "))
  521. c->isom = 1;
  522. av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type);
  523. av_dict_set(&c->fc->metadata, "major_brand", type, 0);
  524. minor_ver = avio_rb32(pb); /* minor version */
  525. snprintf(minor_ver_str, sizeof(minor_ver_str), "%d", minor_ver);
  526. av_dict_set(&c->fc->metadata, "minor_version", minor_ver_str, 0);
  527. comp_brand_size = atom.size - 8;
  528. if (comp_brand_size < 0)
  529. return -1;
  530. comp_brands_str = av_malloc(comp_brand_size + 1); /* Add null terminator */
  531. if (!comp_brands_str)
  532. return AVERROR(ENOMEM);
  533. avio_read(pb, comp_brands_str, comp_brand_size);
  534. comp_brands_str[comp_brand_size] = 0;
  535. av_dict_set(&c->fc->metadata, "compatible_brands", comp_brands_str, 0);
  536. av_freep(&comp_brands_str);
  537. return 0;
  538. }
  539. /* this atom should contain all header atoms */
  540. static int mov_read_moov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  541. {
  542. if (mov_read_default(c, pb, atom) < 0)
  543. return -1;
  544. /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
  545. /* so we don't parse the whole file if over a network */
  546. c->found_moov=1;
  547. return 0; /* now go for mdat */
  548. }
  549. static int mov_read_moof(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  550. {
  551. c->fragment.moof_offset = avio_tell(pb) - 8;
  552. av_dlog(c->fc, "moof offset %"PRIx64"\n", c->fragment.moof_offset);
  553. return mov_read_default(c, pb, atom);
  554. }
  555. static void mov_metadata_creation_time(AVDictionary **metadata, time_t time)
  556. {
  557. char buffer[32];
  558. if (time) {
  559. struct tm *ptm;
  560. time -= 2082844800; /* seconds between 1904-01-01 and Epoch */
  561. ptm = gmtime(&time);
  562. if (!ptm) return;
  563. strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", ptm);
  564. av_dict_set(metadata, "creation_time", buffer, 0);
  565. }
  566. }
  567. static int mov_read_mdhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  568. {
  569. AVStream *st;
  570. MOVStreamContext *sc;
  571. int version;
  572. char language[4] = {0};
  573. unsigned lang;
  574. time_t creation_time;
  575. if (c->fc->nb_streams < 1)
  576. return 0;
  577. st = c->fc->streams[c->fc->nb_streams-1];
  578. sc = st->priv_data;
  579. version = avio_r8(pb);
  580. if (version > 1)
  581. return -1; /* unsupported */
  582. avio_rb24(pb); /* flags */
  583. if (version == 1) {
  584. creation_time = avio_rb64(pb);
  585. avio_rb64(pb);
  586. } else {
  587. creation_time = avio_rb32(pb);
  588. avio_rb32(pb); /* modification time */
  589. }
  590. mov_metadata_creation_time(&st->metadata, creation_time);
  591. sc->time_scale = avio_rb32(pb);
  592. st->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
  593. lang = avio_rb16(pb); /* language */
  594. if (ff_mov_lang_to_iso639(lang, language))
  595. av_dict_set(&st->metadata, "language", language, 0);
  596. avio_rb16(pb); /* quality */
  597. return 0;
  598. }
  599. static int mov_read_mvhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  600. {
  601. time_t creation_time;
  602. int version = avio_r8(pb); /* version */
  603. avio_rb24(pb); /* flags */
  604. if (version == 1) {
  605. creation_time = avio_rb64(pb);
  606. avio_rb64(pb);
  607. } else {
  608. creation_time = avio_rb32(pb);
  609. avio_rb32(pb); /* modification time */
  610. }
  611. mov_metadata_creation_time(&c->fc->metadata, creation_time);
  612. c->time_scale = avio_rb32(pb); /* time scale */
  613. av_dlog(c->fc, "time scale = %i\n", c->time_scale);
  614. c->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
  615. avio_rb32(pb); /* preferred scale */
  616. avio_rb16(pb); /* preferred volume */
  617. avio_skip(pb, 10); /* reserved */
  618. avio_skip(pb, 36); /* display matrix */
  619. avio_rb32(pb); /* preview time */
  620. avio_rb32(pb); /* preview duration */
  621. avio_rb32(pb); /* poster time */
  622. avio_rb32(pb); /* selection time */
  623. avio_rb32(pb); /* selection duration */
  624. avio_rb32(pb); /* current time */
  625. avio_rb32(pb); /* next track ID */
  626. return 0;
  627. }
  628. static int mov_read_smi(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  629. {
  630. AVStream *st;
  631. if (c->fc->nb_streams < 1)
  632. return 0;
  633. st = c->fc->streams[c->fc->nb_streams-1];
  634. if ((uint64_t)atom.size > (1<<30))
  635. return -1;
  636. // currently SVQ3 decoder expect full STSD header - so let's fake it
  637. // this should be fixed and just SMI header should be passed
  638. av_free(st->codec->extradata);
  639. st->codec->extradata = av_mallocz(atom.size + 0x5a + FF_INPUT_BUFFER_PADDING_SIZE);
  640. if (!st->codec->extradata)
  641. return AVERROR(ENOMEM);
  642. st->codec->extradata_size = 0x5a + atom.size;
  643. memcpy(st->codec->extradata, "SVQ3", 4); // fake
  644. avio_read(pb, st->codec->extradata + 0x5a, atom.size);
  645. av_dlog(c->fc, "Reading SMI %"PRId64" %s\n", atom.size, st->codec->extradata + 0x5a);
  646. return 0;
  647. }
  648. static int mov_read_enda(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  649. {
  650. AVStream *st;
  651. int little_endian;
  652. if (c->fc->nb_streams < 1)
  653. return 0;
  654. st = c->fc->streams[c->fc->nb_streams-1];
  655. little_endian = avio_rb16(pb) & 0xFF;
  656. av_dlog(c->fc, "enda %d\n", little_endian);
  657. if (little_endian == 1) {
  658. switch (st->codec->codec_id) {
  659. case CODEC_ID_PCM_S24BE:
  660. st->codec->codec_id = CODEC_ID_PCM_S24LE;
  661. break;
  662. case CODEC_ID_PCM_S32BE:
  663. st->codec->codec_id = CODEC_ID_PCM_S32LE;
  664. break;
  665. case CODEC_ID_PCM_F32BE:
  666. st->codec->codec_id = CODEC_ID_PCM_F32LE;
  667. break;
  668. case CODEC_ID_PCM_F64BE:
  669. st->codec->codec_id = CODEC_ID_PCM_F64LE;
  670. break;
  671. default:
  672. break;
  673. }
  674. }
  675. return 0;
  676. }
  677. /* FIXME modify qdm2/svq3/h264 decoders to take full atom as extradata */
  678. static int mov_read_extradata(MOVContext *c, AVIOContext *pb, MOVAtom atom,
  679. enum CodecID codec_id)
  680. {
  681. AVStream *st;
  682. uint64_t size;
  683. uint8_t *buf;
  684. if (c->fc->nb_streams < 1) // will happen with jp2 files
  685. return 0;
  686. st= c->fc->streams[c->fc->nb_streams-1];
  687. if (st->codec->codec_id != codec_id)
  688. return 0; /* unexpected codec_id - don't mess with extradata */
  689. size= (uint64_t)st->codec->extradata_size + atom.size + 8 + FF_INPUT_BUFFER_PADDING_SIZE;
  690. if (size > INT_MAX || (uint64_t)atom.size > INT_MAX)
  691. return -1;
  692. buf= av_realloc(st->codec->extradata, size);
  693. if (!buf)
  694. return -1;
  695. st->codec->extradata= buf;
  696. buf+= st->codec->extradata_size;
  697. st->codec->extradata_size= size - FF_INPUT_BUFFER_PADDING_SIZE;
  698. AV_WB32( buf , atom.size + 8);
  699. AV_WL32( buf + 4, atom.type);
  700. avio_read(pb, buf + 8, atom.size);
  701. return 0;
  702. }
  703. /* wrapper functions for reading ALAC/AVS/MJPEG/MJPEG2000 extradata atoms only for those codecs */
  704. static int mov_read_alac(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  705. {
  706. return mov_read_extradata(c, pb, atom, CODEC_ID_ALAC);
  707. }
  708. static int mov_read_avss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  709. {
  710. return mov_read_extradata(c, pb, atom, CODEC_ID_AVS);
  711. }
  712. static int mov_read_fiel(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  713. {
  714. return mov_read_extradata(c, pb, atom, CODEC_ID_MJPEG);
  715. }
  716. static int mov_read_jp2h(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  717. {
  718. return mov_read_extradata(c, pb, atom, CODEC_ID_JPEG2000);
  719. }
  720. static int mov_read_wave(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  721. {
  722. AVStream *st;
  723. if (c->fc->nb_streams < 1)
  724. return 0;
  725. st = c->fc->streams[c->fc->nb_streams-1];
  726. if ((uint64_t)atom.size > (1<<30))
  727. return -1;
  728. if (st->codec->codec_id == CODEC_ID_QDM2 || st->codec->codec_id == CODEC_ID_QDMC) {
  729. // pass all frma atom to codec, needed at least for QDMC and QDM2
  730. av_free(st->codec->extradata);
  731. st->codec->extradata = av_mallocz(atom.size + FF_INPUT_BUFFER_PADDING_SIZE);
  732. if (!st->codec->extradata)
  733. return AVERROR(ENOMEM);
  734. st->codec->extradata_size = atom.size;
  735. avio_read(pb, st->codec->extradata, atom.size);
  736. } else if (atom.size > 8) { /* to read frma, esds atoms */
  737. if (mov_read_default(c, pb, atom) < 0)
  738. return -1;
  739. } else
  740. avio_skip(pb, atom.size);
  741. return 0;
  742. }
  743. /**
  744. * This function reads atom content and puts data in extradata without tag
  745. * nor size unlike mov_read_extradata.
  746. */
  747. static int mov_read_glbl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  748. {
  749. AVStream *st;
  750. if (c->fc->nb_streams < 1)
  751. return 0;
  752. st = c->fc->streams[c->fc->nb_streams-1];
  753. if ((uint64_t)atom.size > (1<<30))
  754. return -1;
  755. av_free(st->codec->extradata);
  756. st->codec->extradata = av_mallocz(atom.size + FF_INPUT_BUFFER_PADDING_SIZE);
  757. if (!st->codec->extradata)
  758. return AVERROR(ENOMEM);
  759. st->codec->extradata_size = atom.size;
  760. avio_read(pb, st->codec->extradata, atom.size);
  761. return 0;
  762. }
  763. /**
  764. * An strf atom is a BITMAPINFOHEADER struct. This struct is 40 bytes itself,
  765. * but can have extradata appended at the end after the 40 bytes belonging
  766. * to the struct.
  767. */
  768. static int mov_read_strf(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  769. {
  770. AVStream *st;
  771. if (c->fc->nb_streams < 1)
  772. return 0;
  773. if (atom.size <= 40)
  774. return 0;
  775. st = c->fc->streams[c->fc->nb_streams-1];
  776. if ((uint64_t)atom.size > (1<<30))
  777. return -1;
  778. av_free(st->codec->extradata);
  779. st->codec->extradata = av_mallocz(atom.size - 40 + FF_INPUT_BUFFER_PADDING_SIZE);
  780. if (!st->codec->extradata)
  781. return AVERROR(ENOMEM);
  782. st->codec->extradata_size = atom.size - 40;
  783. avio_skip(pb, 40);
  784. avio_read(pb, st->codec->extradata, atom.size - 40);
  785. return 0;
  786. }
  787. static int mov_read_stco(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  788. {
  789. AVStream *st;
  790. MOVStreamContext *sc;
  791. unsigned int i, entries;
  792. if (c->fc->nb_streams < 1)
  793. return 0;
  794. st = c->fc->streams[c->fc->nb_streams-1];
  795. sc = st->priv_data;
  796. avio_r8(pb); /* version */
  797. avio_rb24(pb); /* flags */
  798. entries = avio_rb32(pb);
  799. if (entries >= UINT_MAX/sizeof(int64_t))
  800. return -1;
  801. sc->chunk_offsets = av_malloc(entries * sizeof(int64_t));
  802. if (!sc->chunk_offsets)
  803. return AVERROR(ENOMEM);
  804. sc->chunk_count = entries;
  805. if (atom.type == MKTAG('s','t','c','o'))
  806. for (i=0; i<entries; i++)
  807. sc->chunk_offsets[i] = avio_rb32(pb);
  808. else if (atom.type == MKTAG('c','o','6','4'))
  809. for (i=0; i<entries; i++)
  810. sc->chunk_offsets[i] = avio_rb64(pb);
  811. else
  812. return -1;
  813. return 0;
  814. }
  815. /**
  816. * Compute codec id for 'lpcm' tag.
  817. * See CoreAudioTypes and AudioStreamBasicDescription at Apple.
  818. */
  819. enum CodecID ff_mov_get_lpcm_codec_id(int bps, int flags)
  820. {
  821. if (flags & 1) { // floating point
  822. if (flags & 2) { // big endian
  823. if (bps == 32) return CODEC_ID_PCM_F32BE;
  824. else if (bps == 64) return CODEC_ID_PCM_F64BE;
  825. } else {
  826. if (bps == 32) return CODEC_ID_PCM_F32LE;
  827. else if (bps == 64) return CODEC_ID_PCM_F64LE;
  828. }
  829. } else {
  830. if (flags & 2) {
  831. if (bps == 8)
  832. // signed integer
  833. if (flags & 4) return CODEC_ID_PCM_S8;
  834. else return CODEC_ID_PCM_U8;
  835. else if (bps == 16) return CODEC_ID_PCM_S16BE;
  836. else if (bps == 24) return CODEC_ID_PCM_S24BE;
  837. else if (bps == 32) return CODEC_ID_PCM_S32BE;
  838. } else {
  839. if (bps == 8)
  840. if (flags & 4) return CODEC_ID_PCM_S8;
  841. else return CODEC_ID_PCM_U8;
  842. else if (bps == 16) return CODEC_ID_PCM_S16LE;
  843. else if (bps == 24) return CODEC_ID_PCM_S24LE;
  844. else if (bps == 32) return CODEC_ID_PCM_S32LE;
  845. }
  846. }
  847. return CODEC_ID_NONE;
  848. }
  849. int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries)
  850. {
  851. AVStream *st;
  852. MOVStreamContext *sc;
  853. int j, pseudo_stream_id;
  854. if (c->fc->nb_streams < 1)
  855. return 0;
  856. st = c->fc->streams[c->fc->nb_streams-1];
  857. sc = st->priv_data;
  858. for (pseudo_stream_id=0; pseudo_stream_id<entries; pseudo_stream_id++) {
  859. //Parsing Sample description table
  860. enum CodecID id;
  861. int dref_id = 1;
  862. MOVAtom a = { AV_RL32("stsd") };
  863. int64_t start_pos = avio_tell(pb);
  864. int size = avio_rb32(pb); /* size */
  865. uint32_t format = avio_rl32(pb); /* data format */
  866. if (size >= 16) {
  867. avio_rb32(pb); /* reserved */
  868. avio_rb16(pb); /* reserved */
  869. dref_id = avio_rb16(pb);
  870. }
  871. if (st->codec->codec_tag &&
  872. st->codec->codec_tag != format &&
  873. (c->fc->video_codec_id ? ff_codec_get_id(codec_movvideo_tags, format) != c->fc->video_codec_id
  874. : st->codec->codec_tag != MKTAG('j','p','e','g'))
  875. ){
  876. /* Multiple fourcc, we skip JPEG. This is not correct, we should
  877. * export it as a separate AVStream but this needs a few changes
  878. * in the MOV demuxer, patch welcome. */
  879. multiple_stsd:
  880. av_log(c->fc, AV_LOG_WARNING, "multiple fourcc not supported\n");
  881. avio_skip(pb, size - (avio_tell(pb) - start_pos));
  882. continue;
  883. }
  884. /* we cannot demux concatenated h264 streams because of different extradata */
  885. if (st->codec->codec_tag && st->codec->codec_tag == AV_RL32("avc1"))
  886. goto multiple_stsd;
  887. sc->pseudo_stream_id = st->codec->codec_tag ? -1 : pseudo_stream_id;
  888. sc->dref_id= dref_id;
  889. st->codec->codec_tag = format;
  890. id = ff_codec_get_id(codec_movaudio_tags, format);
  891. if (id<=0 && ((format&0xFFFF) == 'm'+('s'<<8) || (format&0xFFFF) == 'T'+('S'<<8)))
  892. id = ff_codec_get_id(ff_codec_wav_tags, av_bswap32(format)&0xFFFF);
  893. if (st->codec->codec_type != AVMEDIA_TYPE_VIDEO && id > 0) {
  894. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  895. } else if (st->codec->codec_type != AVMEDIA_TYPE_AUDIO && /* do not overwrite codec type */
  896. format && format != MKTAG('m','p','4','s')) { /* skip old asf mpeg4 tag */
  897. id = ff_codec_get_id(codec_movvideo_tags, format);
  898. if (id <= 0)
  899. id = ff_codec_get_id(ff_codec_bmp_tags, format);
  900. if (id > 0)
  901. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  902. else if (st->codec->codec_type == AVMEDIA_TYPE_DATA){
  903. id = ff_codec_get_id(ff_codec_movsubtitle_tags, format);
  904. if (id > 0)
  905. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  906. }
  907. }
  908. av_dlog(c->fc, "size=%d 4CC= %c%c%c%c codec_type=%d\n", size,
  909. (format >> 0) & 0xff, (format >> 8) & 0xff, (format >> 16) & 0xff,
  910. (format >> 24) & 0xff, st->codec->codec_type);
  911. if (st->codec->codec_type==AVMEDIA_TYPE_VIDEO) {
  912. unsigned int color_depth, len;
  913. int color_greyscale;
  914. st->codec->codec_id = id;
  915. avio_rb16(pb); /* version */
  916. avio_rb16(pb); /* revision level */
  917. avio_rb32(pb); /* vendor */
  918. avio_rb32(pb); /* temporal quality */
  919. avio_rb32(pb); /* spatial quality */
  920. st->codec->width = avio_rb16(pb); /* width */
  921. st->codec->height = avio_rb16(pb); /* height */
  922. avio_rb32(pb); /* horiz resolution */
  923. avio_rb32(pb); /* vert resolution */
  924. avio_rb32(pb); /* data size, always 0 */
  925. avio_rb16(pb); /* frames per samples */
  926. len = avio_r8(pb); /* codec name, pascal string */
  927. if (len > 31)
  928. len = 31;
  929. mov_read_mac_string(c, pb, len, st->codec->codec_name, 32);
  930. if (len < 31)
  931. avio_skip(pb, 31 - len);
  932. /* codec_tag YV12 triggers an UV swap in rawdec.c */
  933. if (!memcmp(st->codec->codec_name, "Planar Y'CbCr 8-bit 4:2:0", 25))
  934. st->codec->codec_tag=MKTAG('I', '4', '2', '0');
  935. st->codec->bits_per_coded_sample = avio_rb16(pb); /* depth */
  936. st->codec->color_table_id = avio_rb16(pb); /* colortable id */
  937. av_dlog(c->fc, "depth %d, ctab id %d\n",
  938. st->codec->bits_per_coded_sample, st->codec->color_table_id);
  939. /* figure out the palette situation */
  940. color_depth = st->codec->bits_per_coded_sample & 0x1F;
  941. color_greyscale = st->codec->bits_per_coded_sample & 0x20;
  942. /* if the depth is 2, 4, or 8 bpp, file is palettized */
  943. if ((color_depth == 2) || (color_depth == 4) ||
  944. (color_depth == 8)) {
  945. /* for palette traversal */
  946. unsigned int color_start, color_count, color_end;
  947. unsigned char r, g, b;
  948. if (color_greyscale) {
  949. int color_index, color_dec;
  950. /* compute the greyscale palette */
  951. st->codec->bits_per_coded_sample = color_depth;
  952. color_count = 1 << color_depth;
  953. color_index = 255;
  954. color_dec = 256 / (color_count - 1);
  955. for (j = 0; j < color_count; j++) {
  956. if (id == CODEC_ID_CINEPAK){
  957. r = g = b = color_count - 1 - color_index;
  958. }else
  959. r = g = b = color_index;
  960. sc->palette[j] =
  961. (r << 16) | (g << 8) | (b);
  962. color_index -= color_dec;
  963. if (color_index < 0)
  964. color_index = 0;
  965. }
  966. } else if (st->codec->color_table_id) {
  967. const uint8_t *color_table;
  968. /* if flag bit 3 is set, use the default palette */
  969. color_count = 1 << color_depth;
  970. if (color_depth == 2)
  971. color_table = ff_qt_default_palette_4;
  972. else if (color_depth == 4)
  973. color_table = ff_qt_default_palette_16;
  974. else
  975. color_table = ff_qt_default_palette_256;
  976. for (j = 0; j < color_count; j++) {
  977. r = color_table[j * 3 + 0];
  978. g = color_table[j * 3 + 1];
  979. b = color_table[j * 3 + 2];
  980. sc->palette[j] =
  981. (r << 16) | (g << 8) | (b);
  982. }
  983. } else {
  984. /* load the palette from the file */
  985. color_start = avio_rb32(pb);
  986. color_count = avio_rb16(pb);
  987. color_end = avio_rb16(pb);
  988. if ((color_start <= 255) &&
  989. (color_end <= 255)) {
  990. for (j = color_start; j <= color_end; j++) {
  991. /* each R, G, or B component is 16 bits;
  992. * only use the top 8 bits; skip alpha bytes
  993. * up front */
  994. avio_r8(pb);
  995. avio_r8(pb);
  996. r = avio_r8(pb);
  997. avio_r8(pb);
  998. g = avio_r8(pb);
  999. avio_r8(pb);
  1000. b = avio_r8(pb);
  1001. avio_r8(pb);
  1002. sc->palette[j] =
  1003. (r << 16) | (g << 8) | (b);
  1004. }
  1005. }
  1006. }
  1007. sc->has_palette = 1;
  1008. }
  1009. } else if (st->codec->codec_type==AVMEDIA_TYPE_AUDIO) {
  1010. int bits_per_sample, flags;
  1011. uint16_t version = avio_rb16(pb);
  1012. st->codec->codec_id = id;
  1013. avio_rb16(pb); /* revision level */
  1014. avio_rb32(pb); /* vendor */
  1015. st->codec->channels = avio_rb16(pb); /* channel count */
  1016. av_dlog(c->fc, "audio channels %d\n", st->codec->channels);
  1017. st->codec->bits_per_coded_sample = avio_rb16(pb); /* sample size */
  1018. sc->audio_cid = avio_rb16(pb);
  1019. avio_rb16(pb); /* packet size = 0 */
  1020. st->codec->sample_rate = ((avio_rb32(pb) >> 16));
  1021. //Read QT version 1 fields. In version 0 these do not exist.
  1022. av_dlog(c->fc, "version =%d, isom =%d\n",version,c->isom);
  1023. if (!c->isom) {
  1024. if (version==1) {
  1025. sc->samples_per_frame = avio_rb32(pb);
  1026. avio_rb32(pb); /* bytes per packet */
  1027. sc->bytes_per_frame = avio_rb32(pb);
  1028. avio_rb32(pb); /* bytes per sample */
  1029. } else if (version==2) {
  1030. avio_rb32(pb); /* sizeof struct only */
  1031. st->codec->sample_rate = av_int2dbl(avio_rb64(pb)); /* float 64 */
  1032. st->codec->channels = avio_rb32(pb);
  1033. avio_rb32(pb); /* always 0x7F000000 */
  1034. st->codec->bits_per_coded_sample = avio_rb32(pb); /* bits per channel if sound is uncompressed */
  1035. flags = avio_rb32(pb); /* lpcm format specific flag */
  1036. sc->bytes_per_frame = avio_rb32(pb); /* bytes per audio packet if constant */
  1037. sc->samples_per_frame = avio_rb32(pb); /* lpcm frames per audio packet if constant */
  1038. if (format == MKTAG('l','p','c','m'))
  1039. st->codec->codec_id = ff_mov_get_lpcm_codec_id(st->codec->bits_per_coded_sample, flags);
  1040. }
  1041. }
  1042. switch (st->codec->codec_id) {
  1043. case CODEC_ID_PCM_S8:
  1044. case CODEC_ID_PCM_U8:
  1045. if (st->codec->bits_per_coded_sample == 16)
  1046. st->codec->codec_id = CODEC_ID_PCM_S16BE;
  1047. break;
  1048. case CODEC_ID_PCM_S16LE:
  1049. case CODEC_ID_PCM_S16BE:
  1050. if (st->codec->bits_per_coded_sample == 8)
  1051. st->codec->codec_id = CODEC_ID_PCM_S8;
  1052. else if (st->codec->bits_per_coded_sample == 24)
  1053. st->codec->codec_id =
  1054. st->codec->codec_id == CODEC_ID_PCM_S16BE ?
  1055. CODEC_ID_PCM_S24BE : CODEC_ID_PCM_S24LE;
  1056. break;
  1057. /* set values for old format before stsd version 1 appeared */
  1058. case CODEC_ID_MACE3:
  1059. sc->samples_per_frame = 6;
  1060. sc->bytes_per_frame = 2*st->codec->channels;
  1061. break;
  1062. case CODEC_ID_MACE6:
  1063. sc->samples_per_frame = 6;
  1064. sc->bytes_per_frame = 1*st->codec->channels;
  1065. break;
  1066. case CODEC_ID_ADPCM_IMA_QT:
  1067. sc->samples_per_frame = 64;
  1068. sc->bytes_per_frame = 34*st->codec->channels;
  1069. break;
  1070. case CODEC_ID_GSM:
  1071. sc->samples_per_frame = 160;
  1072. sc->bytes_per_frame = 33;
  1073. break;
  1074. default:
  1075. break;
  1076. }
  1077. bits_per_sample = av_get_bits_per_sample(st->codec->codec_id);
  1078. if (bits_per_sample) {
  1079. st->codec->bits_per_coded_sample = bits_per_sample;
  1080. sc->sample_size = (bits_per_sample >> 3) * st->codec->channels;
  1081. }
  1082. } else if (st->codec->codec_type==AVMEDIA_TYPE_SUBTITLE){
  1083. // ttxt stsd contains display flags, justification, background
  1084. // color, fonts, and default styles, so fake an atom to read it
  1085. MOVAtom fake_atom = { .size = size - (avio_tell(pb) - start_pos) };
  1086. if (format != AV_RL32("mp4s")) // mp4s contains a regular esds atom
  1087. mov_read_glbl(c, pb, fake_atom);
  1088. st->codec->codec_id= id;
  1089. st->codec->width = sc->width;
  1090. st->codec->height = sc->height;
  1091. } else {
  1092. if (st->codec->codec_tag == MKTAG('t','m','c','d')) {
  1093. int val;
  1094. avio_rb32(pb); /* reserved */
  1095. val = avio_rb32(pb); /* flags */
  1096. if (val & 1)
  1097. st->codec->flags2 |= CODEC_FLAG2_DROP_FRAME_TIMECODE;
  1098. avio_rb32(pb);
  1099. avio_rb32(pb);
  1100. st->codec->time_base.den = get_byte(pb);
  1101. st->codec->time_base.num = 1;
  1102. }
  1103. /* other codec type, just skip (rtp, mp4s, ...) */
  1104. avio_skip(pb, size - (avio_tell(pb) - start_pos));
  1105. }
  1106. /* this will read extra atoms at the end (wave, alac, damr, avcC, SMI ...) */
  1107. a.size = size - (avio_tell(pb) - start_pos);
  1108. if (a.size > 8) {
  1109. if (mov_read_default(c, pb, a) < 0)
  1110. return -1;
  1111. } else if (a.size > 0)
  1112. avio_skip(pb, a.size);
  1113. }
  1114. if (st->codec->codec_type==AVMEDIA_TYPE_AUDIO && st->codec->sample_rate==0 && sc->time_scale>1)
  1115. st->codec->sample_rate= sc->time_scale;
  1116. /* special codec parameters handling */
  1117. switch (st->codec->codec_id) {
  1118. #if CONFIG_DV_DEMUXER
  1119. case CODEC_ID_DVAUDIO:
  1120. c->dv_fctx = avformat_alloc_context();
  1121. c->dv_demux = dv_init_demux(c->dv_fctx);
  1122. if (!c->dv_demux) {
  1123. av_log(c->fc, AV_LOG_ERROR, "dv demux context init error\n");
  1124. return -1;
  1125. }
  1126. sc->dv_audio_container = 1;
  1127. st->codec->codec_id = CODEC_ID_PCM_S16LE;
  1128. break;
  1129. #endif
  1130. /* no ifdef since parameters are always those */
  1131. case CODEC_ID_QCELP:
  1132. // force sample rate for qcelp when not stored in mov
  1133. if (st->codec->codec_tag != MKTAG('Q','c','l','p'))
  1134. st->codec->sample_rate = 8000;
  1135. st->codec->frame_size= 160;
  1136. st->codec->channels= 1; /* really needed */
  1137. break;
  1138. case CODEC_ID_AMR_NB:
  1139. case CODEC_ID_AMR_WB:
  1140. st->codec->frame_size= sc->samples_per_frame;
  1141. st->codec->channels= 1; /* really needed */
  1142. /* force sample rate for amr, stsd in 3gp does not store sample rate */
  1143. if (st->codec->codec_id == CODEC_ID_AMR_NB)
  1144. st->codec->sample_rate = 8000;
  1145. else if (st->codec->codec_id == CODEC_ID_AMR_WB)
  1146. st->codec->sample_rate = 16000;
  1147. break;
  1148. case CODEC_ID_MP2:
  1149. case CODEC_ID_MP3:
  1150. st->codec->codec_type = AVMEDIA_TYPE_AUDIO; /* force type after stsd for m1a hdlr */
  1151. st->need_parsing = AVSTREAM_PARSE_FULL;
  1152. break;
  1153. case CODEC_ID_GSM:
  1154. case CODEC_ID_ADPCM_MS:
  1155. case CODEC_ID_ADPCM_IMA_WAV:
  1156. st->codec->frame_size = sc->samples_per_frame;
  1157. st->codec->block_align = sc->bytes_per_frame;
  1158. break;
  1159. case CODEC_ID_ALAC:
  1160. if (st->codec->extradata_size == 36) {
  1161. st->codec->frame_size = AV_RB32(st->codec->extradata+12);
  1162. st->codec->channels = AV_RB8 (st->codec->extradata+21);
  1163. st->codec->sample_rate = AV_RB32(st->codec->extradata+32);
  1164. }
  1165. break;
  1166. case CODEC_ID_AC3:
  1167. st->need_parsing = AVSTREAM_PARSE_FULL;
  1168. break;
  1169. default:
  1170. break;
  1171. }
  1172. return 0;
  1173. }
  1174. static int mov_read_stsd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1175. {
  1176. int entries;
  1177. avio_r8(pb); /* version */
  1178. avio_rb24(pb); /* flags */
  1179. entries = avio_rb32(pb);
  1180. return ff_mov_read_stsd_entries(c, pb, entries);
  1181. }
  1182. static int mov_read_stsc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1183. {
  1184. AVStream *st;
  1185. MOVStreamContext *sc;
  1186. unsigned int i, entries;
  1187. if (c->fc->nb_streams < 1)
  1188. return 0;
  1189. st = c->fc->streams[c->fc->nb_streams-1];
  1190. sc = st->priv_data;
  1191. avio_r8(pb); /* version */
  1192. avio_rb24(pb); /* flags */
  1193. entries = avio_rb32(pb);
  1194. av_dlog(c->fc, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
  1195. if (entries >= UINT_MAX / sizeof(*sc->stsc_data))
  1196. return -1;
  1197. sc->stsc_data = av_malloc(entries * sizeof(*sc->stsc_data));
  1198. if (!sc->stsc_data)
  1199. return AVERROR(ENOMEM);
  1200. sc->stsc_count = entries;
  1201. for (i=0; i<entries; i++) {
  1202. sc->stsc_data[i].first = avio_rb32(pb);
  1203. sc->stsc_data[i].count = avio_rb32(pb);
  1204. sc->stsc_data[i].id = avio_rb32(pb);
  1205. }
  1206. return 0;
  1207. }
  1208. static int mov_read_stps(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1209. {
  1210. AVStream *st;
  1211. MOVStreamContext *sc;
  1212. unsigned i, entries;
  1213. if (c->fc->nb_streams < 1)
  1214. return 0;
  1215. st = c->fc->streams[c->fc->nb_streams-1];
  1216. sc = st->priv_data;
  1217. avio_rb32(pb); // version + flags
  1218. entries = avio_rb32(pb);
  1219. if (entries >= UINT_MAX / sizeof(*sc->stps_data))
  1220. return -1;
  1221. sc->stps_data = av_malloc(entries * sizeof(*sc->stps_data));
  1222. if (!sc->stps_data)
  1223. return AVERROR(ENOMEM);
  1224. sc->stps_count = entries;
  1225. for (i = 0; i < entries; i++) {
  1226. sc->stps_data[i] = avio_rb32(pb);
  1227. //av_dlog(c->fc, "stps %d\n", sc->stps_data[i]);
  1228. }
  1229. return 0;
  1230. }
  1231. static int mov_read_stss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1232. {
  1233. AVStream *st;
  1234. MOVStreamContext *sc;
  1235. unsigned int i, entries;
  1236. if (c->fc->nb_streams < 1)
  1237. return 0;
  1238. st = c->fc->streams[c->fc->nb_streams-1];
  1239. sc = st->priv_data;
  1240. avio_r8(pb); /* version */
  1241. avio_rb24(pb); /* flags */
  1242. entries = avio_rb32(pb);
  1243. av_dlog(c->fc, "keyframe_count = %d\n", entries);
  1244. if (entries >= UINT_MAX / sizeof(int))
  1245. return -1;
  1246. sc->keyframes = av_malloc(entries * sizeof(int));
  1247. if (!sc->keyframes)
  1248. return AVERROR(ENOMEM);
  1249. sc->keyframe_count = entries;
  1250. for (i=0; i<entries; i++) {
  1251. sc->keyframes[i] = avio_rb32(pb);
  1252. //av_dlog(c->fc, "keyframes[]=%d\n", sc->keyframes[i]);
  1253. }
  1254. return 0;
  1255. }
  1256. static int mov_read_stsz(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1257. {
  1258. AVStream *st;
  1259. MOVStreamContext *sc;
  1260. unsigned int i, entries, sample_size, field_size, num_bytes;
  1261. GetBitContext gb;
  1262. unsigned char* buf;
  1263. if (c->fc->nb_streams < 1)
  1264. return 0;
  1265. st = c->fc->streams[c->fc->nb_streams-1];
  1266. sc = st->priv_data;
  1267. avio_r8(pb); /* version */
  1268. avio_rb24(pb); /* flags */
  1269. if (atom.type == MKTAG('s','t','s','z')) {
  1270. sample_size = avio_rb32(pb);
  1271. if (!sc->sample_size) /* do not overwrite value computed in stsd */
  1272. sc->sample_size = sample_size;
  1273. field_size = 32;
  1274. } else {
  1275. sample_size = 0;
  1276. avio_rb24(pb); /* reserved */
  1277. field_size = avio_r8(pb);
  1278. }
  1279. entries = avio_rb32(pb);
  1280. av_dlog(c->fc, "sample_size = %d sample_count = %d\n", sc->sample_size, entries);
  1281. sc->sample_count = entries;
  1282. if (sample_size)
  1283. return 0;
  1284. if (field_size != 4 && field_size != 8 && field_size != 16 && field_size != 32) {
  1285. av_log(c->fc, AV_LOG_ERROR, "Invalid sample field size %d\n", field_size);
  1286. return -1;
  1287. }
  1288. if (entries >= UINT_MAX / sizeof(int) || entries >= (UINT_MAX - 4) / field_size)
  1289. return -1;
  1290. sc->sample_sizes = av_malloc(entries * sizeof(int));
  1291. if (!sc->sample_sizes)
  1292. return AVERROR(ENOMEM);
  1293. num_bytes = (entries*field_size+4)>>3;
  1294. buf = av_malloc(num_bytes+FF_INPUT_BUFFER_PADDING_SIZE);
  1295. if (!buf) {
  1296. av_freep(&sc->sample_sizes);
  1297. return AVERROR(ENOMEM);
  1298. }
  1299. if (avio_read(pb, buf, num_bytes) < num_bytes) {
  1300. av_freep(&sc->sample_sizes);
  1301. av_free(buf);
  1302. return -1;
  1303. }
  1304. init_get_bits(&gb, buf, 8*num_bytes);
  1305. for (i=0; i<entries; i++)
  1306. sc->sample_sizes[i] = get_bits_long(&gb, field_size);
  1307. av_free(buf);
  1308. return 0;
  1309. }
  1310. static int mov_read_stts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1311. {
  1312. AVStream *st;
  1313. MOVStreamContext *sc;
  1314. unsigned int i, entries;
  1315. int64_t duration=0;
  1316. int64_t total_sample_count=0;
  1317. if (c->fc->nb_streams < 1)
  1318. return 0;
  1319. st = c->fc->streams[c->fc->nb_streams-1];
  1320. sc = st->priv_data;
  1321. avio_r8(pb); /* version */
  1322. avio_rb24(pb); /* flags */
  1323. entries = avio_rb32(pb);
  1324. av_dlog(c->fc, "track[%i].stts.entries = %i\n",
  1325. c->fc->nb_streams-1, entries);
  1326. if (!entries || entries >= UINT_MAX / sizeof(*sc->stts_data))
  1327. return AVERROR(EINVAL);
  1328. sc->stts_data = av_malloc(entries * sizeof(*sc->stts_data));
  1329. if (!sc->stts_data)
  1330. return AVERROR(ENOMEM);
  1331. sc->stts_count = entries;
  1332. for (i=0; i<entries; i++) {
  1333. int sample_duration;
  1334. int sample_count;
  1335. sample_count=avio_rb32(pb);
  1336. sample_duration = avio_rb32(pb);
  1337. /* sample_duration < 0 is invalid based on the spec */
  1338. if (sample_duration < 0) {
  1339. av_log(c->fc, AV_LOG_ERROR, "Invalid SampleDelta in STTS %d", sample_duration);
  1340. sample_duration = 1;
  1341. }
  1342. sc->stts_data[i].count= sample_count;
  1343. sc->stts_data[i].duration= sample_duration;
  1344. av_dlog(c->fc, "sample_count=%d, sample_duration=%d\n",
  1345. sample_count, sample_duration);
  1346. duration+=(int64_t)sample_duration*sample_count;
  1347. total_sample_count+=sample_count;
  1348. }
  1349. st->nb_frames= total_sample_count;
  1350. if (duration)
  1351. st->duration= duration;
  1352. return 0;
  1353. }
  1354. static int mov_read_ctts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1355. {
  1356. AVStream *st;
  1357. MOVStreamContext *sc;
  1358. unsigned int i, entries;
  1359. if (c->fc->nb_streams < 1)
  1360. return 0;
  1361. st = c->fc->streams[c->fc->nb_streams-1];
  1362. sc = st->priv_data;
  1363. avio_r8(pb); /* version */
  1364. avio_rb24(pb); /* flags */
  1365. entries = avio_rb32(pb);
  1366. av_dlog(c->fc, "track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries);
  1367. if (entries >= UINT_MAX / sizeof(*sc->ctts_data))
  1368. return -1;
  1369. sc->ctts_data = av_malloc(entries * sizeof(*sc->ctts_data));
  1370. if (!sc->ctts_data)
  1371. return AVERROR(ENOMEM);
  1372. sc->ctts_count = entries;
  1373. for (i=0; i<entries; i++) {
  1374. int count =avio_rb32(pb);
  1375. int duration =avio_rb32(pb);
  1376. sc->ctts_data[i].count = count;
  1377. sc->ctts_data[i].duration= duration;
  1378. if (duration < 0 && i+1<entries)
  1379. sc->dts_shift = FFMAX(sc->dts_shift, -duration);
  1380. }
  1381. av_dlog(c->fc, "dts shift %d\n", sc->dts_shift);
  1382. return 0;
  1383. }
  1384. static void mov_build_index(MOVContext *mov, AVStream *st)
  1385. {
  1386. MOVStreamContext *sc = st->priv_data;
  1387. int64_t current_offset;
  1388. int64_t current_dts = 0;
  1389. unsigned int stts_index = 0;
  1390. unsigned int stsc_index = 0;
  1391. unsigned int stss_index = 0;
  1392. unsigned int stps_index = 0;
  1393. unsigned int i, j;
  1394. uint64_t stream_size = 0;
  1395. /* adjust first dts according to edit list */
  1396. if (sc->time_offset && mov->time_scale > 0) {
  1397. if (sc->time_offset < 0)
  1398. sc->time_offset = av_rescale(sc->time_offset, sc->time_scale, mov->time_scale);
  1399. current_dts = -sc->time_offset;
  1400. if (sc->ctts_data && sc->stts_data &&
  1401. sc->ctts_data[0].duration / FFMAX(sc->stts_data[0].duration, 1) > 16) {
  1402. /* more than 16 frames delay, dts are likely wrong
  1403. this happens with files created by iMovie */
  1404. sc->wrong_dts = 1;
  1405. st->codec->has_b_frames = 1;
  1406. }
  1407. }
  1408. /* only use old uncompressed audio chunk demuxing when stts specifies it */
  1409. if (!(st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
  1410. sc->stts_count == 1 && sc->stts_data[0].duration == 1)) {
  1411. unsigned int current_sample = 0;
  1412. unsigned int stts_sample = 0;
  1413. unsigned int sample_size;
  1414. unsigned int distance = 0;
  1415. int key_off = sc->keyframes && sc->keyframes[0] == 1;
  1416. current_dts -= sc->dts_shift;
  1417. if (sc->sample_count >= UINT_MAX / sizeof(*st->index_entries))
  1418. return;
  1419. st->index_entries = av_malloc(sc->sample_count*sizeof(*st->index_entries));
  1420. if (!st->index_entries)
  1421. return;
  1422. st->index_entries_allocated_size = sc->sample_count*sizeof(*st->index_entries);
  1423. for (i = 0; i < sc->chunk_count; i++) {
  1424. current_offset = sc->chunk_offsets[i];
  1425. while (stsc_index + 1 < sc->stsc_count &&
  1426. i + 1 == sc->stsc_data[stsc_index + 1].first)
  1427. stsc_index++;
  1428. for (j = 0; j < sc->stsc_data[stsc_index].count; j++) {
  1429. int keyframe = 0;
  1430. if (current_sample >= sc->sample_count) {
  1431. av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n");
  1432. return;
  1433. }
  1434. if (!sc->keyframe_count || current_sample+key_off == sc->keyframes[stss_index]) {
  1435. keyframe = 1;
  1436. if (stss_index + 1 < sc->keyframe_count)
  1437. stss_index++;
  1438. } else if (sc->stps_count && current_sample+key_off == sc->stps_data[stps_index]) {
  1439. keyframe = 1;
  1440. if (stps_index + 1 < sc->stps_count)
  1441. stps_index++;
  1442. }
  1443. if (keyframe)
  1444. distance = 0;
  1445. sample_size = sc->sample_size > 0 ? sc->sample_size : sc->sample_sizes[current_sample];
  1446. if (sc->pseudo_stream_id == -1 ||
  1447. sc->stsc_data[stsc_index].id - 1 == sc->pseudo_stream_id) {
  1448. AVIndexEntry *e = &st->index_entries[st->nb_index_entries++];
  1449. e->pos = current_offset;
  1450. e->timestamp = current_dts;
  1451. e->size = sample_size;
  1452. e->min_distance = distance;
  1453. e->flags = keyframe ? AVINDEX_KEYFRAME : 0;
  1454. av_dlog(mov->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
  1455. "size %d, distance %d, keyframe %d\n", st->index, current_sample,
  1456. current_offset, current_dts, sample_size, distance, keyframe);
  1457. }
  1458. current_offset += sample_size;
  1459. stream_size += sample_size;
  1460. current_dts += sc->stts_data[stts_index].duration;
  1461. distance++;
  1462. stts_sample++;
  1463. current_sample++;
  1464. if (stts_index + 1 < sc->stts_count && stts_sample == sc->stts_data[stts_index].count) {
  1465. stts_sample = 0;
  1466. stts_index++;
  1467. }
  1468. }
  1469. }
  1470. if (st->duration > 0)
  1471. st->codec->bit_rate = stream_size*8*sc->time_scale/st->duration;
  1472. } else {
  1473. unsigned chunk_samples, total = 0;
  1474. // compute total chunk count
  1475. for (i = 0; i < sc->stsc_count; i++) {
  1476. unsigned count, chunk_count;
  1477. chunk_samples = sc->stsc_data[i].count;
  1478. if (sc->samples_per_frame && chunk_samples % sc->samples_per_frame) {
  1479. av_log(mov->fc, AV_LOG_ERROR, "error unaligned chunk\n");
  1480. return;
  1481. }
  1482. if (sc->samples_per_frame >= 160) { // gsm
  1483. count = chunk_samples / sc->samples_per_frame;
  1484. } else if (sc->samples_per_frame > 1) {
  1485. unsigned samples = (1024/sc->samples_per_frame)*sc->samples_per_frame;
  1486. count = (chunk_samples+samples-1) / samples;
  1487. } else {
  1488. count = (chunk_samples+1023) / 1024;
  1489. }
  1490. if (i < sc->stsc_count - 1)
  1491. chunk_count = sc->stsc_data[i+1].first - sc->stsc_data[i].first;
  1492. else
  1493. chunk_count = sc->chunk_count - (sc->stsc_data[i].first - 1);
  1494. total += chunk_count * count;
  1495. }
  1496. av_dlog(mov->fc, "chunk count %d\n", total);
  1497. if (total >= UINT_MAX / sizeof(*st->index_entries))
  1498. return;
  1499. st->index_entries = av_malloc(total*sizeof(*st->index_entries));
  1500. if (!st->index_entries)
  1501. return;
  1502. st->index_entries_allocated_size = total*sizeof(*st->index_entries);
  1503. // populate index
  1504. for (i = 0; i < sc->chunk_count; i++) {
  1505. current_offset = sc->chunk_offsets[i];
  1506. if (stsc_index + 1 < sc->stsc_count &&
  1507. i + 1 == sc->stsc_data[stsc_index + 1].first)
  1508. stsc_index++;
  1509. chunk_samples = sc->stsc_data[stsc_index].count;
  1510. while (chunk_samples > 0) {
  1511. AVIndexEntry *e;
  1512. unsigned size, samples;
  1513. if (sc->samples_per_frame >= 160) { // gsm
  1514. samples = sc->samples_per_frame;
  1515. size = sc->bytes_per_frame;
  1516. } else {
  1517. if (sc->samples_per_frame > 1) {
  1518. samples = FFMIN((1024 / sc->samples_per_frame)*
  1519. sc->samples_per_frame, chunk_samples);
  1520. size = (samples / sc->samples_per_frame) * sc->bytes_per_frame;
  1521. } else {
  1522. samples = FFMIN(1024, chunk_samples);
  1523. size = samples * sc->sample_size;
  1524. }
  1525. }
  1526. if (st->nb_index_entries >= total) {
  1527. av_log(mov->fc, AV_LOG_ERROR, "wrong chunk count %d\n", total);
  1528. return;
  1529. }
  1530. e = &st->index_entries[st->nb_index_entries++];
  1531. e->pos = current_offset;
  1532. e->timestamp = current_dts;
  1533. e->size = size;
  1534. e->min_distance = 0;
  1535. e->flags = AVINDEX_KEYFRAME;
  1536. av_dlog(mov->fc, "AVIndex stream %d, chunk %d, offset %"PRIx64", dts %"PRId64", "
  1537. "size %d, duration %d\n", st->index, i, current_offset, current_dts,
  1538. size, samples);
  1539. current_offset += size;
  1540. current_dts += samples;
  1541. chunk_samples -= samples;
  1542. }
  1543. }
  1544. }
  1545. }
  1546. static int mov_open_dref(AVIOContext **pb, const char *src, MOVDref *ref)
  1547. {
  1548. /* try relative path, we do not try the absolute because it can leak information about our
  1549. system to an attacker */
  1550. if (ref->nlvl_to > 0 && ref->nlvl_from > 0) {
  1551. char filename[1024];
  1552. const char *src_path;
  1553. int i, l;
  1554. /* find a source dir */
  1555. src_path = strrchr(src, '/');
  1556. if (src_path)
  1557. src_path++;
  1558. else
  1559. src_path = src;
  1560. /* find a next level down to target */
  1561. for (i = 0, l = strlen(ref->path) - 1; l >= 0; l--)
  1562. if (ref->path[l] == '/') {
  1563. if (i == ref->nlvl_to - 1)
  1564. break;
  1565. else
  1566. i++;
  1567. }
  1568. /* compose filename if next level down to target was found */
  1569. if (i == ref->nlvl_to - 1 && src_path - src < sizeof(filename)) {
  1570. memcpy(filename, src, src_path - src);
  1571. filename[src_path - src] = 0;
  1572. for (i = 1; i < ref->nlvl_from; i++)
  1573. av_strlcat(filename, "../", 1024);
  1574. av_strlcat(filename, ref->path + l + 1, 1024);
  1575. if (!avio_open(pb, filename, AVIO_FLAG_READ))
  1576. return 0;
  1577. }
  1578. }
  1579. return AVERROR(ENOENT);
  1580. }
  1581. static int mov_read_trak(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1582. {
  1583. AVStream *st;
  1584. MOVStreamContext *sc;
  1585. int ret;
  1586. st = av_new_stream(c->fc, c->fc->nb_streams);
  1587. if (!st) return AVERROR(ENOMEM);
  1588. sc = av_mallocz(sizeof(MOVStreamContext));
  1589. if (!sc) return AVERROR(ENOMEM);
  1590. st->priv_data = sc;
  1591. st->codec->codec_type = AVMEDIA_TYPE_DATA;
  1592. sc->ffindex = st->index;
  1593. if ((ret = mov_read_default(c, pb, atom)) < 0)
  1594. return ret;
  1595. /* sanity checks */
  1596. if (sc->chunk_count && (!sc->stts_count || !sc->stsc_count ||
  1597. (!sc->sample_size && !sc->sample_count))) {
  1598. av_log(c->fc, AV_LOG_ERROR, "stream %d, missing mandatory atoms, broken header\n",
  1599. st->index);
  1600. return 0;
  1601. }
  1602. if (sc->time_scale <= 0) {
  1603. av_log(c->fc, AV_LOG_WARNING, "stream %d, timescale not set\n", st->index);
  1604. sc->time_scale = c->time_scale;
  1605. if (sc->time_scale <= 0)
  1606. sc->time_scale = 1;
  1607. }
  1608. av_set_pts_info(st, 64, 1, sc->time_scale);
  1609. if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
  1610. !st->codec->frame_size && sc->stts_count == 1) {
  1611. st->codec->frame_size = av_rescale(sc->stts_data[0].duration,
  1612. st->codec->sample_rate, sc->time_scale);
  1613. av_dlog(c->fc, "frame size %d\n", st->codec->frame_size);
  1614. }
  1615. mov_build_index(c, st);
  1616. if (sc->dref_id-1 < sc->drefs_count && sc->drefs[sc->dref_id-1].path) {
  1617. MOVDref *dref = &sc->drefs[sc->dref_id - 1];
  1618. if (mov_open_dref(&sc->pb, c->fc->filename, dref) < 0)
  1619. av_log(c->fc, AV_LOG_ERROR,
  1620. "stream %d, error opening alias: path='%s', dir='%s', "
  1621. "filename='%s', volume='%s', nlvl_from=%d, nlvl_to=%d\n",
  1622. st->index, dref->path, dref->dir, dref->filename,
  1623. dref->volume, dref->nlvl_from, dref->nlvl_to);
  1624. } else
  1625. sc->pb = c->fc->pb;
  1626. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  1627. if (!st->sample_aspect_ratio.num &&
  1628. (st->codec->width != sc->width || st->codec->height != sc->height)) {
  1629. st->sample_aspect_ratio = av_d2q(((double)st->codec->height * sc->width) /
  1630. ((double)st->codec->width * sc->height), INT_MAX);
  1631. }
  1632. av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
  1633. sc->time_scale*st->nb_frames, st->duration, INT_MAX);
  1634. if (sc->stts_count == 1 || (sc->stts_count == 2 && sc->stts_data[1].count == 1))
  1635. av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den,
  1636. sc->time_scale, sc->stts_data[0].duration, INT_MAX);
  1637. }
  1638. switch (st->codec->codec_id) {
  1639. #if CONFIG_H261_DECODER
  1640. case CODEC_ID_H261:
  1641. #endif
  1642. #if CONFIG_H263_DECODER
  1643. case CODEC_ID_H263:
  1644. #endif
  1645. #if CONFIG_H264_DECODER
  1646. case CODEC_ID_H264:
  1647. #endif
  1648. #if CONFIG_MPEG4_DECODER
  1649. case CODEC_ID_MPEG4:
  1650. #endif
  1651. st->codec->width = 0; /* let decoder init width/height */
  1652. st->codec->height= 0;
  1653. break;
  1654. }
  1655. /* Do not need those anymore. */
  1656. av_freep(&sc->chunk_offsets);
  1657. av_freep(&sc->stsc_data);
  1658. av_freep(&sc->sample_sizes);
  1659. av_freep(&sc->keyframes);
  1660. av_freep(&sc->stts_data);
  1661. av_freep(&sc->stps_data);
  1662. return 0;
  1663. }
  1664. static int mov_read_ilst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1665. {
  1666. int ret;
  1667. c->itunes_metadata = 1;
  1668. ret = mov_read_default(c, pb, atom);
  1669. c->itunes_metadata = 0;
  1670. return ret;
  1671. }
  1672. static int mov_read_meta(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1673. {
  1674. while (atom.size > 8) {
  1675. uint32_t tag = avio_rl32(pb);
  1676. atom.size -= 4;
  1677. if (tag == MKTAG('h','d','l','r')) {
  1678. avio_seek(pb, -8, SEEK_CUR);
  1679. atom.size += 8;
  1680. return mov_read_default(c, pb, atom);
  1681. }
  1682. }
  1683. return 0;
  1684. }
  1685. static int mov_read_tkhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1686. {
  1687. int i;
  1688. int width;
  1689. int height;
  1690. int64_t disp_transform[2];
  1691. int display_matrix[3][2];
  1692. AVStream *st;
  1693. MOVStreamContext *sc;
  1694. int version;
  1695. if (c->fc->nb_streams < 1)
  1696. return 0;
  1697. st = c->fc->streams[c->fc->nb_streams-1];
  1698. sc = st->priv_data;
  1699. version = avio_r8(pb);
  1700. avio_rb24(pb); /* flags */
  1701. /*
  1702. MOV_TRACK_ENABLED 0x0001
  1703. MOV_TRACK_IN_MOVIE 0x0002
  1704. MOV_TRACK_IN_PREVIEW 0x0004
  1705. MOV_TRACK_IN_POSTER 0x0008
  1706. */
  1707. if (version == 1) {
  1708. avio_rb64(pb);
  1709. avio_rb64(pb);
  1710. } else {
  1711. avio_rb32(pb); /* creation time */
  1712. avio_rb32(pb); /* modification time */
  1713. }
  1714. st->id = (int)avio_rb32(pb); /* track id (NOT 0 !)*/
  1715. avio_rb32(pb); /* reserved */
  1716. /* highlevel (considering edits) duration in movie timebase */
  1717. (version == 1) ? avio_rb64(pb) : avio_rb32(pb);
  1718. avio_rb32(pb); /* reserved */
  1719. avio_rb32(pb); /* reserved */
  1720. avio_rb16(pb); /* layer */
  1721. avio_rb16(pb); /* alternate group */
  1722. avio_rb16(pb); /* volume */
  1723. avio_rb16(pb); /* reserved */
  1724. //read in the display matrix (outlined in ISO 14496-12, Section 6.2.2)
  1725. // they're kept in fixed point format through all calculations
  1726. // ignore u,v,z b/c we don't need the scale factor to calc aspect ratio
  1727. for (i = 0; i < 3; i++) {
  1728. display_matrix[i][0] = avio_rb32(pb); // 16.16 fixed point
  1729. display_matrix[i][1] = avio_rb32(pb); // 16.16 fixed point
  1730. avio_rb32(pb); // 2.30 fixed point (not used)
  1731. }
  1732. width = avio_rb32(pb); // 16.16 fixed point track width
  1733. height = avio_rb32(pb); // 16.16 fixed point track height
  1734. sc->width = width >> 16;
  1735. sc->height = height >> 16;
  1736. if (display_matrix[0][0] == -65536 && display_matrix[1][1] == -65536) {
  1737. av_dict_set(&st->metadata, "rotate", "180", 0);
  1738. }
  1739. // transform the display width/height according to the matrix
  1740. // skip this if the display matrix is the default identity matrix
  1741. // or if it is rotating the picture, ex iPhone 3GS
  1742. // to keep the same scale, use [width height 1<<16]
  1743. if (width && height &&
  1744. ((display_matrix[0][0] != 65536 ||
  1745. display_matrix[1][1] != 65536) &&
  1746. !display_matrix[0][1] &&
  1747. !display_matrix[1][0] &&
  1748. !display_matrix[2][0] && !display_matrix[2][1])) {
  1749. for (i = 0; i < 2; i++)
  1750. disp_transform[i] =
  1751. (int64_t) width * display_matrix[0][i] +
  1752. (int64_t) height * display_matrix[1][i] +
  1753. ((int64_t) display_matrix[2][i] << 16);
  1754. //sample aspect ratio is new width/height divided by old width/height
  1755. st->sample_aspect_ratio = av_d2q(
  1756. ((double) disp_transform[0] * height) /
  1757. ((double) disp_transform[1] * width), INT_MAX);
  1758. }
  1759. return 0;
  1760. }
  1761. static int mov_read_tfhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1762. {
  1763. MOVFragment *frag = &c->fragment;
  1764. MOVTrackExt *trex = NULL;
  1765. int flags, track_id, i;
  1766. avio_r8(pb); /* version */
  1767. flags = avio_rb24(pb);
  1768. track_id = avio_rb32(pb);
  1769. if (!track_id)
  1770. return -1;
  1771. frag->track_id = track_id;
  1772. for (i = 0; i < c->trex_count; i++)
  1773. if (c->trex_data[i].track_id == frag->track_id) {
  1774. trex = &c->trex_data[i];
  1775. break;
  1776. }
  1777. if (!trex) {
  1778. av_log(c->fc, AV_LOG_ERROR, "could not find corresponding trex\n");
  1779. return -1;
  1780. }
  1781. if (flags & 0x01) frag->base_data_offset = avio_rb64(pb);
  1782. else frag->base_data_offset = frag->moof_offset;
  1783. if (flags & 0x02) frag->stsd_id = avio_rb32(pb);
  1784. else frag->stsd_id = trex->stsd_id;
  1785. frag->duration = flags & 0x08 ? avio_rb32(pb) : trex->duration;
  1786. frag->size = flags & 0x10 ? avio_rb32(pb) : trex->size;
  1787. frag->flags = flags & 0x20 ? avio_rb32(pb) : trex->flags;
  1788. av_dlog(c->fc, "frag flags 0x%x\n", frag->flags);
  1789. return 0;
  1790. }
  1791. static int mov_read_chap(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1792. {
  1793. c->chapter_track = avio_rb32(pb);
  1794. return 0;
  1795. }
  1796. static int mov_read_trex(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1797. {
  1798. MOVTrackExt *trex;
  1799. if ((uint64_t)c->trex_count+1 >= UINT_MAX / sizeof(*c->trex_data))
  1800. return -1;
  1801. trex = av_realloc(c->trex_data, (c->trex_count+1)*sizeof(*c->trex_data));
  1802. if (!trex)
  1803. return AVERROR(ENOMEM);
  1804. c->trex_data = trex;
  1805. trex = &c->trex_data[c->trex_count++];
  1806. avio_r8(pb); /* version */
  1807. avio_rb24(pb); /* flags */
  1808. trex->track_id = avio_rb32(pb);
  1809. trex->stsd_id = avio_rb32(pb);
  1810. trex->duration = avio_rb32(pb);
  1811. trex->size = avio_rb32(pb);
  1812. trex->flags = avio_rb32(pb);
  1813. return 0;
  1814. }
  1815. static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1816. {
  1817. MOVFragment *frag = &c->fragment;
  1818. AVStream *st = NULL;
  1819. MOVStreamContext *sc;
  1820. MOVStts *ctts_data;
  1821. uint64_t offset;
  1822. int64_t dts;
  1823. int data_offset = 0;
  1824. unsigned entries, first_sample_flags = frag->flags;
  1825. int flags, distance, i;
  1826. for (i = 0; i < c->fc->nb_streams; i++) {
  1827. if (c->fc->streams[i]->id == frag->track_id) {
  1828. st = c->fc->streams[i];
  1829. break;
  1830. }
  1831. }
  1832. if (!st) {
  1833. av_log(c->fc, AV_LOG_ERROR, "could not find corresponding track id %d\n", frag->track_id);
  1834. return -1;
  1835. }
  1836. sc = st->priv_data;
  1837. if (sc->pseudo_stream_id+1 != frag->stsd_id)
  1838. return 0;
  1839. avio_r8(pb); /* version */
  1840. flags = avio_rb24(pb);
  1841. entries = avio_rb32(pb);
  1842. av_dlog(c->fc, "flags 0x%x entries %d\n", flags, entries);
  1843. /* Always assume the presence of composition time offsets.
  1844. * Without this assumption, for instance, we cannot deal with a track in fragmented movies that meet the following.
  1845. * 1) in the initial movie, there are no samples.
  1846. * 2) in the first movie fragment, there is only one sample without composition time offset.
  1847. * 3) in the subsequent movie fragments, there are samples with composition time offset. */
  1848. if (!sc->ctts_count && sc->sample_count)
  1849. {
  1850. /* Complement ctts table if moov atom doesn't have ctts atom. */
  1851. ctts_data = av_malloc(sizeof(*sc->ctts_data));
  1852. if (!ctts_data)
  1853. return AVERROR(ENOMEM);
  1854. sc->ctts_data = ctts_data;
  1855. sc->ctts_data[sc->ctts_count].count = sc->sample_count;
  1856. sc->ctts_data[sc->ctts_count].duration = 0;
  1857. sc->ctts_count++;
  1858. }
  1859. if ((uint64_t)entries+sc->ctts_count >= UINT_MAX/sizeof(*sc->ctts_data))
  1860. return -1;
  1861. ctts_data = av_realloc(sc->ctts_data,
  1862. (entries+sc->ctts_count)*sizeof(*sc->ctts_data));
  1863. if (!ctts_data)
  1864. return AVERROR(ENOMEM);
  1865. sc->ctts_data = ctts_data;
  1866. if (flags & 0x001) data_offset = avio_rb32(pb);
  1867. if (flags & 0x004) first_sample_flags = avio_rb32(pb);
  1868. dts = st->duration - sc->time_offset;
  1869. offset = frag->base_data_offset + data_offset;
  1870. distance = 0;
  1871. av_dlog(c->fc, "first sample flags 0x%x\n", first_sample_flags);
  1872. for (i = 0; i < entries; i++) {
  1873. unsigned sample_size = frag->size;
  1874. int sample_flags = i ? frag->flags : first_sample_flags;
  1875. unsigned sample_duration = frag->duration;
  1876. int keyframe;
  1877. if (flags & 0x100) sample_duration = avio_rb32(pb);
  1878. if (flags & 0x200) sample_size = avio_rb32(pb);
  1879. if (flags & 0x400) sample_flags = avio_rb32(pb);
  1880. sc->ctts_data[sc->ctts_count].count = 1;
  1881. sc->ctts_data[sc->ctts_count].duration = (flags & 0x800) ? avio_rb32(pb) : 0;
  1882. sc->ctts_count++;
  1883. if ((keyframe = st->codec->codec_type == AVMEDIA_TYPE_AUDIO ||
  1884. (flags & 0x004 && !i && !sample_flags) || sample_flags & 0x2000000))
  1885. distance = 0;
  1886. av_add_index_entry(st, offset, dts, sample_size, distance,
  1887. keyframe ? AVINDEX_KEYFRAME : 0);
  1888. av_dlog(c->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
  1889. "size %d, distance %d, keyframe %d\n", st->index, sc->sample_count+i,
  1890. offset, dts, sample_size, distance, keyframe);
  1891. distance++;
  1892. dts += sample_duration;
  1893. offset += sample_size;
  1894. }
  1895. frag->moof_offset = offset;
  1896. st->duration = dts + sc->time_offset;
  1897. return 0;
  1898. }
  1899. /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
  1900. /* like the files created with Adobe Premiere 5.0, for samples see */
  1901. /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
  1902. static int mov_read_wide(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1903. {
  1904. int err;
  1905. if (atom.size < 8)
  1906. return 0; /* continue */
  1907. if (avio_rb32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
  1908. avio_skip(pb, atom.size - 4);
  1909. return 0;
  1910. }
  1911. atom.type = avio_rl32(pb);
  1912. atom.size -= 8;
  1913. if (atom.type != MKTAG('m','d','a','t')) {
  1914. avio_skip(pb, atom.size);
  1915. return 0;
  1916. }
  1917. err = mov_read_mdat(c, pb, atom);
  1918. return err;
  1919. }
  1920. static int mov_read_cmov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1921. {
  1922. #if CONFIG_ZLIB
  1923. AVIOContext ctx;
  1924. uint8_t *cmov_data;
  1925. uint8_t *moov_data; /* uncompressed data */
  1926. long cmov_len, moov_len;
  1927. int ret = -1;
  1928. avio_rb32(pb); /* dcom atom */
  1929. if (avio_rl32(pb) != MKTAG('d','c','o','m'))
  1930. return -1;
  1931. if (avio_rl32(pb) != MKTAG('z','l','i','b')) {
  1932. av_log(c->fc, AV_LOG_ERROR, "unknown compression for cmov atom !");
  1933. return -1;
  1934. }
  1935. avio_rb32(pb); /* cmvd atom */
  1936. if (avio_rl32(pb) != MKTAG('c','m','v','d'))
  1937. return -1;
  1938. moov_len = avio_rb32(pb); /* uncompressed size */
  1939. cmov_len = atom.size - 6 * 4;
  1940. cmov_data = av_malloc(cmov_len);
  1941. if (!cmov_data)
  1942. return AVERROR(ENOMEM);
  1943. moov_data = av_malloc(moov_len);
  1944. if (!moov_data) {
  1945. av_free(cmov_data);
  1946. return AVERROR(ENOMEM);
  1947. }
  1948. avio_read(pb, cmov_data, cmov_len);
  1949. if (uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
  1950. goto free_and_return;
  1951. if (ffio_init_context(&ctx, moov_data, moov_len, 0, NULL, NULL, NULL, NULL) != 0)
  1952. goto free_and_return;
  1953. atom.type = MKTAG('m','o','o','v');
  1954. atom.size = moov_len;
  1955. ret = mov_read_default(c, &ctx, atom);
  1956. free_and_return:
  1957. av_free(moov_data);
  1958. av_free(cmov_data);
  1959. return ret;
  1960. #else
  1961. av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n");
  1962. return -1;
  1963. #endif
  1964. }
  1965. /* edit list atom */
  1966. static int mov_read_elst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1967. {
  1968. MOVStreamContext *sc;
  1969. int i, edit_count, version;
  1970. if (c->fc->nb_streams < 1)
  1971. return 0;
  1972. sc = c->fc->streams[c->fc->nb_streams-1]->priv_data;
  1973. version = avio_r8(pb); /* version */
  1974. avio_rb24(pb); /* flags */
  1975. edit_count = avio_rb32(pb); /* entries */
  1976. if ((uint64_t)edit_count*12+8 > atom.size)
  1977. return -1;
  1978. for (i=0; i<edit_count; i++){
  1979. int64_t time;
  1980. int64_t duration;
  1981. if (version == 1) {
  1982. duration = avio_rb64(pb);
  1983. time = avio_rb64(pb);
  1984. } else {
  1985. duration = avio_rb32(pb); /* segment duration */
  1986. time = (int32_t)avio_rb32(pb); /* media time */
  1987. }
  1988. avio_rb32(pb); /* Media rate */
  1989. if (i == 0 && time >= -1) {
  1990. sc->time_offset = time != -1 ? time : -duration;
  1991. }
  1992. }
  1993. if (edit_count > 1)
  1994. av_log(c->fc, AV_LOG_WARNING, "multiple edit list entries, "
  1995. "a/v desync might occur, patch welcome\n");
  1996. av_dlog(c->fc, "track[%i].edit_count = %i\n", c->fc->nb_streams-1, edit_count);
  1997. return 0;
  1998. }
  1999. static int mov_read_chan(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2000. {
  2001. if (atom.size < 16)
  2002. return AVERROR_INVALIDDATA;
  2003. avio_skip(pb, 4);
  2004. ff_mov_read_chan(c->fc, atom.size - 4, c->fc->streams[0]->codec);
  2005. return 0;
  2006. }
  2007. static const MOVParseTableEntry mov_default_parse_table[] = {
  2008. { MKTAG('a','v','s','s'), mov_read_avss },
  2009. { MKTAG('c','h','p','l'), mov_read_chpl },
  2010. { MKTAG('c','o','6','4'), mov_read_stco },
  2011. { MKTAG('c','t','t','s'), mov_read_ctts }, /* composition time to sample */
  2012. { MKTAG('d','i','n','f'), mov_read_default },
  2013. { MKTAG('d','r','e','f'), mov_read_dref },
  2014. { MKTAG('e','d','t','s'), mov_read_default },
  2015. { MKTAG('e','l','s','t'), mov_read_elst },
  2016. { MKTAG('e','n','d','a'), mov_read_enda },
  2017. { MKTAG('f','i','e','l'), mov_read_fiel },
  2018. { MKTAG('f','t','y','p'), mov_read_ftyp },
  2019. { MKTAG('g','l','b','l'), mov_read_glbl },
  2020. { MKTAG('h','d','l','r'), mov_read_hdlr },
  2021. { MKTAG('i','l','s','t'), mov_read_ilst },
  2022. { MKTAG('j','p','2','h'), mov_read_jp2h },
  2023. { MKTAG('m','d','a','t'), mov_read_mdat },
  2024. { MKTAG('m','d','h','d'), mov_read_mdhd },
  2025. { MKTAG('m','d','i','a'), mov_read_default },
  2026. { MKTAG('m','e','t','a'), mov_read_meta },
  2027. { MKTAG('m','i','n','f'), mov_read_default },
  2028. { MKTAG('m','o','o','f'), mov_read_moof },
  2029. { MKTAG('m','o','o','v'), mov_read_moov },
  2030. { MKTAG('m','v','e','x'), mov_read_default },
  2031. { MKTAG('m','v','h','d'), mov_read_mvhd },
  2032. { MKTAG('S','M','I',' '), mov_read_smi }, /* Sorenson extension ??? */
  2033. { MKTAG('a','l','a','c'), mov_read_alac }, /* alac specific atom */
  2034. { MKTAG('a','v','c','C'), mov_read_glbl },
  2035. { MKTAG('p','a','s','p'), mov_read_pasp },
  2036. { MKTAG('s','t','b','l'), mov_read_default },
  2037. { MKTAG('s','t','c','o'), mov_read_stco },
  2038. { MKTAG('s','t','p','s'), mov_read_stps },
  2039. { MKTAG('s','t','r','f'), mov_read_strf },
  2040. { MKTAG('s','t','s','c'), mov_read_stsc },
  2041. { MKTAG('s','t','s','d'), mov_read_stsd }, /* sample description */
  2042. { MKTAG('s','t','s','s'), mov_read_stss }, /* sync sample */
  2043. { MKTAG('s','t','s','z'), mov_read_stsz }, /* sample size */
  2044. { MKTAG('s','t','t','s'), mov_read_stts },
  2045. { MKTAG('s','t','z','2'), mov_read_stsz }, /* compact sample size */
  2046. { MKTAG('t','k','h','d'), mov_read_tkhd }, /* track header */
  2047. { MKTAG('t','f','h','d'), mov_read_tfhd }, /* track fragment header */
  2048. { MKTAG('t','r','a','k'), mov_read_trak },
  2049. { MKTAG('t','r','a','f'), mov_read_default },
  2050. { MKTAG('t','r','e','f'), mov_read_default },
  2051. { MKTAG('c','h','a','p'), mov_read_chap },
  2052. { MKTAG('t','r','e','x'), mov_read_trex },
  2053. { MKTAG('t','r','u','n'), mov_read_trun },
  2054. { MKTAG('u','d','t','a'), mov_read_default },
  2055. { MKTAG('w','a','v','e'), mov_read_wave },
  2056. { MKTAG('e','s','d','s'), mov_read_esds },
  2057. { MKTAG('d','a','c','3'), mov_read_dac3 }, /* AC-3 info */
  2058. { MKTAG('w','i','d','e'), mov_read_wide }, /* place holder */
  2059. { MKTAG('w','f','e','x'), mov_read_wfex },
  2060. { MKTAG('c','m','o','v'), mov_read_cmov },
  2061. { MKTAG('c','h','a','n'), mov_read_chan },
  2062. { 0, NULL }
  2063. };
  2064. static int mov_probe(AVProbeData *p)
  2065. {
  2066. unsigned int offset;
  2067. uint32_t tag;
  2068. int score = 0;
  2069. /* check file header */
  2070. offset = 0;
  2071. for (;;) {
  2072. /* ignore invalid offset */
  2073. if ((offset + 8) > (unsigned int)p->buf_size)
  2074. return score;
  2075. tag = AV_RL32(p->buf + offset + 4);
  2076. switch(tag) {
  2077. /* check for obvious tags */
  2078. case MKTAG('j','P',' ',' '): /* jpeg 2000 signature */
  2079. case MKTAG('m','o','o','v'):
  2080. case MKTAG('m','d','a','t'):
  2081. case MKTAG('p','n','o','t'): /* detect movs with preview pics like ew.mov and april.mov */
  2082. case MKTAG('u','d','t','a'): /* Packet Video PVAuthor adds this and a lot of more junk */
  2083. case MKTAG('f','t','y','p'):
  2084. return AVPROBE_SCORE_MAX;
  2085. /* those are more common words, so rate then a bit less */
  2086. case MKTAG('e','d','i','w'): /* xdcam files have reverted first tags */
  2087. case MKTAG('w','i','d','e'):
  2088. case MKTAG('f','r','e','e'):
  2089. case MKTAG('j','u','n','k'):
  2090. case MKTAG('p','i','c','t'):
  2091. return AVPROBE_SCORE_MAX - 5;
  2092. case MKTAG(0x82,0x82,0x7f,0x7d):
  2093. case MKTAG('s','k','i','p'):
  2094. case MKTAG('u','u','i','d'):
  2095. case MKTAG('p','r','f','l'):
  2096. offset = AV_RB32(p->buf+offset) + offset;
  2097. /* if we only find those cause probedata is too small at least rate them */
  2098. score = AVPROBE_SCORE_MAX - 50;
  2099. break;
  2100. default:
  2101. /* unrecognized tag */
  2102. return score;
  2103. }
  2104. }
  2105. }
  2106. // must be done after parsing all trak because there's no order requirement
  2107. static void mov_read_chapters(AVFormatContext *s)
  2108. {
  2109. MOVContext *mov = s->priv_data;
  2110. AVStream *st = NULL;
  2111. MOVStreamContext *sc;
  2112. int64_t cur_pos;
  2113. int i;
  2114. for (i = 0; i < s->nb_streams; i++)
  2115. if (s->streams[i]->id == mov->chapter_track) {
  2116. st = s->streams[i];
  2117. break;
  2118. }
  2119. if (!st) {
  2120. av_log(s, AV_LOG_ERROR, "Referenced QT chapter track not found\n");
  2121. return;
  2122. }
  2123. st->discard = AVDISCARD_ALL;
  2124. sc = st->priv_data;
  2125. cur_pos = avio_tell(sc->pb);
  2126. for (i = 0; i < st->nb_index_entries; i++) {
  2127. AVIndexEntry *sample = &st->index_entries[i];
  2128. int64_t end = i+1 < st->nb_index_entries ? st->index_entries[i+1].timestamp : st->duration;
  2129. uint8_t *title;
  2130. uint16_t ch;
  2131. int len, title_len;
  2132. if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
  2133. av_log(s, AV_LOG_ERROR, "Chapter %d not found in file\n", i);
  2134. goto finish;
  2135. }
  2136. // the first two bytes are the length of the title
  2137. len = avio_rb16(sc->pb);
  2138. if (len > sample->size-2)
  2139. continue;
  2140. title_len = 2*len + 1;
  2141. if (!(title = av_mallocz(title_len)))
  2142. goto finish;
  2143. // The samples could theoretically be in any encoding if there's an encd
  2144. // atom following, but in practice are only utf-8 or utf-16, distinguished
  2145. // instead by the presence of a BOM
  2146. if (!len) {
  2147. title[0] = 0;
  2148. } else {
  2149. ch = avio_rb16(sc->pb);
  2150. if (ch == 0xfeff)
  2151. avio_get_str16be(sc->pb, len, title, title_len);
  2152. else if (ch == 0xfffe)
  2153. avio_get_str16le(sc->pb, len, title, title_len);
  2154. else {
  2155. AV_WB16(title, ch);
  2156. if (len == 1 || len == 2)
  2157. title[len] = 0;
  2158. else
  2159. get_strz(sc->pb, title + 2, len - 1);
  2160. }
  2161. }
  2162. ff_new_chapter(s, i, st->time_base, sample->timestamp, end, title);
  2163. av_freep(&title);
  2164. }
  2165. finish:
  2166. avio_seek(sc->pb, cur_pos, SEEK_SET);
  2167. }
  2168. static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap)
  2169. {
  2170. MOVContext *mov = s->priv_data;
  2171. AVIOContext *pb = s->pb;
  2172. int err;
  2173. MOVAtom atom = { AV_RL32("root") };
  2174. mov->fc = s;
  2175. /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
  2176. if (pb->seekable)
  2177. atom.size = avio_size(pb);
  2178. else
  2179. atom.size = INT64_MAX;
  2180. /* check MOV header */
  2181. if ((err = mov_read_default(mov, pb, atom)) < 0) {
  2182. av_log(s, AV_LOG_ERROR, "error reading header: %d\n", err);
  2183. return err;
  2184. }
  2185. if (!mov->found_moov) {
  2186. av_log(s, AV_LOG_ERROR, "moov atom not found\n");
  2187. return -1;
  2188. }
  2189. av_dlog(mov->fc, "on_parse_exit_offset=%"PRId64"\n", avio_tell(pb));
  2190. if (pb->seekable && mov->chapter_track > 0)
  2191. mov_read_chapters(s);
  2192. return 0;
  2193. }
  2194. static AVIndexEntry *mov_find_next_sample(AVFormatContext *s, AVStream **st)
  2195. {
  2196. AVIndexEntry *sample = NULL;
  2197. int64_t best_dts = INT64_MAX;
  2198. int i;
  2199. for (i = 0; i < s->nb_streams; i++) {
  2200. AVStream *avst = s->streams[i];
  2201. MOVStreamContext *msc = avst->priv_data;
  2202. if (msc->pb && msc->current_sample < avst->nb_index_entries) {
  2203. AVIndexEntry *current_sample = &avst->index_entries[msc->current_sample];
  2204. int64_t dts = av_rescale(current_sample->timestamp, AV_TIME_BASE, msc->time_scale);
  2205. av_dlog(s, "stream %d, sample %d, dts %"PRId64"\n", i, msc->current_sample, dts);
  2206. if (!sample || (!s->pb->seekable && current_sample->pos < sample->pos) ||
  2207. (s->pb->seekable &&
  2208. ((msc->pb != s->pb && dts < best_dts) || (msc->pb == s->pb &&
  2209. ((FFABS(best_dts - dts) <= AV_TIME_BASE && current_sample->pos < sample->pos) ||
  2210. (FFABS(best_dts - dts) > AV_TIME_BASE && dts < best_dts)))))) {
  2211. sample = current_sample;
  2212. best_dts = dts;
  2213. *st = avst;
  2214. }
  2215. }
  2216. }
  2217. return sample;
  2218. }
  2219. static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
  2220. {
  2221. MOVContext *mov = s->priv_data;
  2222. MOVStreamContext *sc;
  2223. AVIndexEntry *sample;
  2224. AVStream *st = NULL;
  2225. int ret;
  2226. retry:
  2227. sample = mov_find_next_sample(s, &st);
  2228. if (!sample) {
  2229. mov->found_mdat = 0;
  2230. if (s->pb->seekable||
  2231. mov_read_default(mov, s->pb, (MOVAtom){ AV_RL32("root"), INT64_MAX }) < 0 ||
  2232. url_feof(s->pb))
  2233. return AVERROR_EOF;
  2234. av_dlog(s, "read fragments, offset 0x%"PRIx64"\n", avio_tell(s->pb));
  2235. goto retry;
  2236. }
  2237. sc = st->priv_data;
  2238. /* must be done just before reading, to avoid infinite loop on sample */
  2239. sc->current_sample++;
  2240. if (st->discard != AVDISCARD_ALL) {
  2241. if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
  2242. av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n",
  2243. sc->ffindex, sample->pos);
  2244. return -1;
  2245. }
  2246. ret = av_get_packet(sc->pb, pkt, sample->size);
  2247. if (ret < 0)
  2248. return ret;
  2249. if (sc->has_palette) {
  2250. uint8_t *pal;
  2251. pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE);
  2252. if (!pal) {
  2253. av_log(mov->fc, AV_LOG_ERROR, "Cannot append palette to packet\n");
  2254. } else {
  2255. memcpy(pal, sc->palette, AVPALETTE_SIZE);
  2256. sc->has_palette = 0;
  2257. }
  2258. }
  2259. #if CONFIG_DV_DEMUXER
  2260. if (mov->dv_demux && sc->dv_audio_container) {
  2261. dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size, pkt->pos);
  2262. av_free(pkt->data);
  2263. pkt->size = 0;
  2264. ret = dv_get_packet(mov->dv_demux, pkt);
  2265. if (ret < 0)
  2266. return ret;
  2267. }
  2268. #endif
  2269. }
  2270. pkt->stream_index = sc->ffindex;
  2271. pkt->dts = sample->timestamp;
  2272. if (sc->ctts_data) {
  2273. pkt->pts = pkt->dts + sc->dts_shift + sc->ctts_data[sc->ctts_index].duration;
  2274. /* update ctts context */
  2275. sc->ctts_sample++;
  2276. if (sc->ctts_index < sc->ctts_count &&
  2277. sc->ctts_data[sc->ctts_index].count == sc->ctts_sample) {
  2278. sc->ctts_index++;
  2279. sc->ctts_sample = 0;
  2280. }
  2281. if (sc->wrong_dts)
  2282. pkt->dts = AV_NOPTS_VALUE;
  2283. } else {
  2284. int64_t next_dts = (sc->current_sample < st->nb_index_entries) ?
  2285. st->index_entries[sc->current_sample].timestamp : st->duration;
  2286. pkt->duration = next_dts - pkt->dts;
  2287. pkt->pts = pkt->dts;
  2288. }
  2289. if (st->discard == AVDISCARD_ALL)
  2290. goto retry;
  2291. pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? AV_PKT_FLAG_KEY : 0;
  2292. pkt->pos = sample->pos;
  2293. av_dlog(s, "stream %d, pts %"PRId64", dts %"PRId64", pos 0x%"PRIx64", duration %d\n",
  2294. pkt->stream_index, pkt->pts, pkt->dts, pkt->pos, pkt->duration);
  2295. return 0;
  2296. }
  2297. static int mov_seek_stream(AVFormatContext *s, AVStream *st, int64_t timestamp, int flags)
  2298. {
  2299. MOVStreamContext *sc = st->priv_data;
  2300. int sample, time_sample;
  2301. int i;
  2302. sample = av_index_search_timestamp(st, timestamp, flags);
  2303. av_dlog(s, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample);
  2304. if (sample < 0 && st->nb_index_entries && timestamp < st->index_entries[0].timestamp)
  2305. sample = 0;
  2306. if (sample < 0) /* not sure what to do */
  2307. return -1;
  2308. sc->current_sample = sample;
  2309. av_dlog(s, "stream %d, found sample %d\n", st->index, sc->current_sample);
  2310. /* adjust ctts index */
  2311. if (sc->ctts_data) {
  2312. time_sample = 0;
  2313. for (i = 0; i < sc->ctts_count; i++) {
  2314. int next = time_sample + sc->ctts_data[i].count;
  2315. if (next > sc->current_sample) {
  2316. sc->ctts_index = i;
  2317. sc->ctts_sample = sc->current_sample - time_sample;
  2318. break;
  2319. }
  2320. time_sample = next;
  2321. }
  2322. }
  2323. return sample;
  2324. }
  2325. static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
  2326. {
  2327. AVStream *st;
  2328. int64_t seek_timestamp, timestamp;
  2329. int sample;
  2330. int i;
  2331. if (stream_index >= s->nb_streams)
  2332. return -1;
  2333. if (sample_time < 0)
  2334. sample_time = 0;
  2335. st = s->streams[stream_index];
  2336. sample = mov_seek_stream(s, st, sample_time, flags);
  2337. if (sample < 0)
  2338. return -1;
  2339. /* adjust seek timestamp to found sample timestamp */
  2340. seek_timestamp = st->index_entries[sample].timestamp;
  2341. for (i = 0; i < s->nb_streams; i++) {
  2342. st = s->streams[i];
  2343. if (stream_index == i)
  2344. continue;
  2345. timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base);
  2346. mov_seek_stream(s, st, timestamp, flags);
  2347. }
  2348. return 0;
  2349. }
  2350. static int mov_read_close(AVFormatContext *s)
  2351. {
  2352. MOVContext *mov = s->priv_data;
  2353. int i, j;
  2354. for (i = 0; i < s->nb_streams; i++) {
  2355. AVStream *st = s->streams[i];
  2356. MOVStreamContext *sc = st->priv_data;
  2357. av_freep(&sc->ctts_data);
  2358. for (j = 0; j < sc->drefs_count; j++) {
  2359. av_freep(&sc->drefs[j].path);
  2360. av_freep(&sc->drefs[j].dir);
  2361. }
  2362. av_freep(&sc->drefs);
  2363. if (sc->pb && sc->pb != s->pb)
  2364. avio_close(sc->pb);
  2365. }
  2366. if (mov->dv_demux) {
  2367. for (i = 0; i < mov->dv_fctx->nb_streams; i++) {
  2368. av_freep(&mov->dv_fctx->streams[i]->codec);
  2369. av_freep(&mov->dv_fctx->streams[i]);
  2370. }
  2371. av_freep(&mov->dv_fctx);
  2372. av_freep(&mov->dv_demux);
  2373. }
  2374. av_freep(&mov->trex_data);
  2375. return 0;
  2376. }
  2377. AVInputFormat ff_mov_demuxer = {
  2378. .name = "mov,mp4,m4a,3gp,3g2,mj2",
  2379. .long_name = NULL_IF_CONFIG_SMALL("QuickTime/MPEG-4/Motion JPEG 2000 format"),
  2380. .priv_data_size = sizeof(MOVContext),
  2381. .read_probe = mov_probe,
  2382. .read_header = mov_read_header,
  2383. .read_packet = mov_read_packet,
  2384. .read_close = mov_read_close,
  2385. .read_seek = mov_read_seek,
  2386. };