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.

2978 lines
99KB

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