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.

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