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.

3526 lines
118KB

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