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.

2687 lines
90KB

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