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.

2369 lines
79KB

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