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.

2622 lines
88KB

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