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.

2365 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. if (st->codec->width != sc->width || st->codec->height != sc->height) {
  799. AVRational r = av_d2q(
  800. ((double)st->codec->height * sc->width) /
  801. ((double)st->codec->width * sc->height), INT_MAX);
  802. if (st->sample_aspect_ratio.num)
  803. st->sample_aspect_ratio = av_mul_q(st->sample_aspect_ratio, r);
  804. else
  805. st->sample_aspect_ratio = r;
  806. }
  807. get_be32(pb); /* horiz resolution */
  808. get_be32(pb); /* vert resolution */
  809. get_be32(pb); /* data size, always 0 */
  810. get_be16(pb); /* frames per samples */
  811. get_buffer(pb, codec_name, 32); /* codec name, pascal string */
  812. if (codec_name[0] <= 31) {
  813. int i;
  814. int pos = 0;
  815. for (i = 0; i < codec_name[0] && pos < sizeof(st->codec->codec_name) - 3; i++) {
  816. uint8_t tmp;
  817. PUT_UTF8(codec_name[i+1], tmp, st->codec->codec_name[pos++] = tmp;)
  818. }
  819. st->codec->codec_name[pos] = 0;
  820. }
  821. st->codec->bits_per_coded_sample = get_be16(pb); /* depth */
  822. st->codec->color_table_id = get_be16(pb); /* colortable id */
  823. dprintf(c->fc, "depth %d, ctab id %d\n",
  824. st->codec->bits_per_coded_sample, st->codec->color_table_id);
  825. /* figure out the palette situation */
  826. color_depth = st->codec->bits_per_coded_sample & 0x1F;
  827. color_greyscale = st->codec->bits_per_coded_sample & 0x20;
  828. /* if the depth is 2, 4, or 8 bpp, file is palettized */
  829. if ((color_depth == 2) || (color_depth == 4) ||
  830. (color_depth == 8)) {
  831. /* for palette traversal */
  832. unsigned int color_start, color_count, color_end;
  833. unsigned char r, g, b;
  834. st->codec->palctrl = av_malloc(sizeof(*st->codec->palctrl));
  835. if (color_greyscale) {
  836. int color_index, color_dec;
  837. /* compute the greyscale palette */
  838. st->codec->bits_per_coded_sample = color_depth;
  839. color_count = 1 << color_depth;
  840. color_index = 255;
  841. color_dec = 256 / (color_count - 1);
  842. for (j = 0; j < color_count; j++) {
  843. r = g = b = color_index;
  844. st->codec->palctrl->palette[j] =
  845. (r << 16) | (g << 8) | (b);
  846. color_index -= color_dec;
  847. if (color_index < 0)
  848. color_index = 0;
  849. }
  850. } else if (st->codec->color_table_id) {
  851. const uint8_t *color_table;
  852. /* if flag bit 3 is set, use the default palette */
  853. color_count = 1 << color_depth;
  854. if (color_depth == 2)
  855. color_table = ff_qt_default_palette_4;
  856. else if (color_depth == 4)
  857. color_table = ff_qt_default_palette_16;
  858. else
  859. color_table = ff_qt_default_palette_256;
  860. for (j = 0; j < color_count; j++) {
  861. r = color_table[j * 3 + 0];
  862. g = color_table[j * 3 + 1];
  863. b = color_table[j * 3 + 2];
  864. st->codec->palctrl->palette[j] =
  865. (r << 16) | (g << 8) | (b);
  866. }
  867. } else {
  868. /* load the palette from the file */
  869. color_start = get_be32(pb);
  870. color_count = get_be16(pb);
  871. color_end = get_be16(pb);
  872. if ((color_start <= 255) &&
  873. (color_end <= 255)) {
  874. for (j = color_start; j <= color_end; j++) {
  875. /* each R, G, or B component is 16 bits;
  876. * only use the top 8 bits; skip alpha bytes
  877. * up front */
  878. get_byte(pb);
  879. get_byte(pb);
  880. r = get_byte(pb);
  881. get_byte(pb);
  882. g = get_byte(pb);
  883. get_byte(pb);
  884. b = get_byte(pb);
  885. get_byte(pb);
  886. st->codec->palctrl->palette[j] =
  887. (r << 16) | (g << 8) | (b);
  888. }
  889. }
  890. }
  891. st->codec->palctrl->palette_changed = 1;
  892. }
  893. } else if(st->codec->codec_type==CODEC_TYPE_AUDIO) {
  894. int bits_per_sample, flags;
  895. uint16_t version = get_be16(pb);
  896. st->codec->codec_id = id;
  897. get_be16(pb); /* revision level */
  898. get_be32(pb); /* vendor */
  899. st->codec->channels = get_be16(pb); /* channel count */
  900. dprintf(c->fc, "audio channels %d\n", st->codec->channels);
  901. st->codec->bits_per_coded_sample = get_be16(pb); /* sample size */
  902. sc->audio_cid = get_be16(pb);
  903. get_be16(pb); /* packet size = 0 */
  904. st->codec->sample_rate = ((get_be32(pb) >> 16));
  905. //Read QT version 1 fields. In version 0 these do not exist.
  906. dprintf(c->fc, "version =%d, isom =%d\n",version,c->isom);
  907. if(!c->isom) {
  908. if(version==1) {
  909. sc->samples_per_frame = get_be32(pb);
  910. get_be32(pb); /* bytes per packet */
  911. sc->bytes_per_frame = get_be32(pb);
  912. get_be32(pb); /* bytes per sample */
  913. } else if(version==2) {
  914. get_be32(pb); /* sizeof struct only */
  915. st->codec->sample_rate = av_int2dbl(get_be64(pb)); /* float 64 */
  916. st->codec->channels = get_be32(pb);
  917. get_be32(pb); /* always 0x7F000000 */
  918. st->codec->bits_per_coded_sample = get_be32(pb); /* bits per channel if sound is uncompressed */
  919. flags = get_be32(pb); /* lpcm format specific flag */
  920. sc->bytes_per_frame = get_be32(pb); /* bytes per audio packet if constant */
  921. sc->samples_per_frame = get_be32(pb); /* lpcm frames per audio packet if constant */
  922. if (format == MKTAG('l','p','c','m'))
  923. st->codec->codec_id = ff_mov_get_lpcm_codec_id(st->codec->bits_per_coded_sample, flags);
  924. }
  925. }
  926. switch (st->codec->codec_id) {
  927. case CODEC_ID_PCM_S8:
  928. case CODEC_ID_PCM_U8:
  929. if (st->codec->bits_per_coded_sample == 16)
  930. st->codec->codec_id = CODEC_ID_PCM_S16BE;
  931. break;
  932. case CODEC_ID_PCM_S16LE:
  933. case CODEC_ID_PCM_S16BE:
  934. if (st->codec->bits_per_coded_sample == 8)
  935. st->codec->codec_id = CODEC_ID_PCM_S8;
  936. else if (st->codec->bits_per_coded_sample == 24)
  937. st->codec->codec_id =
  938. st->codec->codec_id == CODEC_ID_PCM_S16BE ?
  939. CODEC_ID_PCM_S24BE : CODEC_ID_PCM_S24LE;
  940. break;
  941. /* set values for old format before stsd version 1 appeared */
  942. case CODEC_ID_MACE3:
  943. sc->samples_per_frame = 6;
  944. sc->bytes_per_frame = 2*st->codec->channels;
  945. break;
  946. case CODEC_ID_MACE6:
  947. sc->samples_per_frame = 6;
  948. sc->bytes_per_frame = 1*st->codec->channels;
  949. break;
  950. case CODEC_ID_ADPCM_IMA_QT:
  951. sc->samples_per_frame = 64;
  952. sc->bytes_per_frame = 34*st->codec->channels;
  953. break;
  954. case CODEC_ID_GSM:
  955. sc->samples_per_frame = 160;
  956. sc->bytes_per_frame = 33;
  957. break;
  958. default:
  959. break;
  960. }
  961. bits_per_sample = av_get_bits_per_sample(st->codec->codec_id);
  962. if (bits_per_sample) {
  963. st->codec->bits_per_coded_sample = bits_per_sample;
  964. sc->sample_size = (bits_per_sample >> 3) * st->codec->channels;
  965. }
  966. } else if(st->codec->codec_type==CODEC_TYPE_SUBTITLE){
  967. // ttxt stsd contains display flags, justification, background
  968. // color, fonts, and default styles, so fake an atom to read it
  969. MOVAtom fake_atom = { .size = size - (url_ftell(pb) - start_pos) };
  970. if (format != AV_RL32("mp4s")) // mp4s contains a regular esds atom
  971. mov_read_glbl(c, pb, fake_atom);
  972. st->codec->codec_id= id;
  973. st->codec->width = sc->width;
  974. st->codec->height = sc->height;
  975. } else {
  976. /* other codec type, just skip (rtp, mp4s, tmcd ...) */
  977. url_fskip(pb, size - (url_ftell(pb) - start_pos));
  978. }
  979. /* this will read extra atoms at the end (wave, alac, damr, avcC, SMI ...) */
  980. a.size = size - (url_ftell(pb) - start_pos);
  981. if (a.size > 8) {
  982. if (mov_read_default(c, pb, a) < 0)
  983. return -1;
  984. } else if (a.size > 0)
  985. url_fskip(pb, a.size);
  986. }
  987. if(st->codec->codec_type==CODEC_TYPE_AUDIO && st->codec->sample_rate==0 && sc->time_scale>1)
  988. st->codec->sample_rate= sc->time_scale;
  989. /* special codec parameters handling */
  990. switch (st->codec->codec_id) {
  991. #if CONFIG_DV_DEMUXER
  992. case CODEC_ID_DVAUDIO:
  993. c->dv_fctx = avformat_alloc_context();
  994. c->dv_demux = dv_init_demux(c->dv_fctx);
  995. if (!c->dv_demux) {
  996. av_log(c->fc, AV_LOG_ERROR, "dv demux context init error\n");
  997. return -1;
  998. }
  999. sc->dv_audio_container = 1;
  1000. st->codec->codec_id = CODEC_ID_PCM_S16LE;
  1001. break;
  1002. #endif
  1003. /* no ifdef since parameters are always those */
  1004. case CODEC_ID_QCELP:
  1005. // force sample rate for qcelp when not stored in mov
  1006. if (st->codec->codec_tag != MKTAG('Q','c','l','p'))
  1007. st->codec->sample_rate = 8000;
  1008. st->codec->frame_size= 160;
  1009. st->codec->channels= 1; /* really needed */
  1010. break;
  1011. case CODEC_ID_AMR_NB:
  1012. case CODEC_ID_AMR_WB:
  1013. st->codec->frame_size= sc->samples_per_frame;
  1014. st->codec->channels= 1; /* really needed */
  1015. /* force sample rate for amr, stsd in 3gp does not store sample rate */
  1016. if (st->codec->codec_id == CODEC_ID_AMR_NB)
  1017. st->codec->sample_rate = 8000;
  1018. else if (st->codec->codec_id == CODEC_ID_AMR_WB)
  1019. st->codec->sample_rate = 16000;
  1020. break;
  1021. case CODEC_ID_MP2:
  1022. case CODEC_ID_MP3:
  1023. st->codec->codec_type = CODEC_TYPE_AUDIO; /* force type after stsd for m1a hdlr */
  1024. st->need_parsing = AVSTREAM_PARSE_FULL;
  1025. break;
  1026. case CODEC_ID_GSM:
  1027. case CODEC_ID_ADPCM_MS:
  1028. case CODEC_ID_ADPCM_IMA_WAV:
  1029. st->codec->block_align = sc->bytes_per_frame;
  1030. break;
  1031. case CODEC_ID_ALAC:
  1032. if (st->codec->extradata_size == 36) {
  1033. st->codec->frame_size = AV_RB32(st->codec->extradata+12);
  1034. st->codec->channels = AV_RB8 (st->codec->extradata+21);
  1035. }
  1036. break;
  1037. default:
  1038. break;
  1039. }
  1040. return 0;
  1041. }
  1042. static int mov_read_stsc(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1043. {
  1044. AVStream *st;
  1045. MOVStreamContext *sc;
  1046. unsigned int i, entries;
  1047. if (c->fc->nb_streams < 1)
  1048. return 0;
  1049. st = c->fc->streams[c->fc->nb_streams-1];
  1050. sc = st->priv_data;
  1051. get_byte(pb); /* version */
  1052. get_be24(pb); /* flags */
  1053. entries = get_be32(pb);
  1054. dprintf(c->fc, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
  1055. if(entries >= UINT_MAX / sizeof(*sc->stsc_data))
  1056. return -1;
  1057. sc->stsc_data = av_malloc(entries * sizeof(*sc->stsc_data));
  1058. if (!sc->stsc_data)
  1059. return AVERROR(ENOMEM);
  1060. sc->stsc_count = entries;
  1061. for(i=0; i<entries; i++) {
  1062. sc->stsc_data[i].first = get_be32(pb);
  1063. sc->stsc_data[i].count = get_be32(pb);
  1064. sc->stsc_data[i].id = get_be32(pb);
  1065. }
  1066. return 0;
  1067. }
  1068. static int mov_read_stps(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1069. {
  1070. AVStream *st;
  1071. MOVStreamContext *sc;
  1072. unsigned i, entries;
  1073. if (c->fc->nb_streams < 1)
  1074. return 0;
  1075. st = c->fc->streams[c->fc->nb_streams-1];
  1076. sc = st->priv_data;
  1077. get_be32(pb); // version + flags
  1078. entries = get_be32(pb);
  1079. if (entries >= UINT_MAX / sizeof(*sc->stps_data))
  1080. return -1;
  1081. sc->stps_data = av_malloc(entries * sizeof(*sc->stps_data));
  1082. if (!sc->stps_data)
  1083. return AVERROR(ENOMEM);
  1084. sc->stps_count = entries;
  1085. for (i = 0; i < entries; i++) {
  1086. sc->stps_data[i] = get_be32(pb);
  1087. //dprintf(c->fc, "stps %d\n", sc->stps_data[i]);
  1088. }
  1089. return 0;
  1090. }
  1091. static int mov_read_stss(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1092. {
  1093. AVStream *st;
  1094. MOVStreamContext *sc;
  1095. unsigned int i, entries;
  1096. if (c->fc->nb_streams < 1)
  1097. return 0;
  1098. st = c->fc->streams[c->fc->nb_streams-1];
  1099. sc = st->priv_data;
  1100. get_byte(pb); /* version */
  1101. get_be24(pb); /* flags */
  1102. entries = get_be32(pb);
  1103. dprintf(c->fc, "keyframe_count = %d\n", entries);
  1104. if(entries >= UINT_MAX / sizeof(int))
  1105. return -1;
  1106. sc->keyframes = av_malloc(entries * sizeof(int));
  1107. if (!sc->keyframes)
  1108. return AVERROR(ENOMEM);
  1109. sc->keyframe_count = entries;
  1110. for(i=0; i<entries; i++) {
  1111. sc->keyframes[i] = get_be32(pb);
  1112. //dprintf(c->fc, "keyframes[]=%d\n", sc->keyframes[i]);
  1113. }
  1114. return 0;
  1115. }
  1116. static int mov_read_stsz(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1117. {
  1118. AVStream *st;
  1119. MOVStreamContext *sc;
  1120. unsigned int i, entries, sample_size, field_size, num_bytes;
  1121. GetBitContext gb;
  1122. unsigned char* buf;
  1123. if (c->fc->nb_streams < 1)
  1124. return 0;
  1125. st = c->fc->streams[c->fc->nb_streams-1];
  1126. sc = st->priv_data;
  1127. get_byte(pb); /* version */
  1128. get_be24(pb); /* flags */
  1129. if (atom.type == MKTAG('s','t','s','z')) {
  1130. sample_size = get_be32(pb);
  1131. if (!sc->sample_size) /* do not overwrite value computed in stsd */
  1132. sc->sample_size = sample_size;
  1133. field_size = 32;
  1134. } else {
  1135. sample_size = 0;
  1136. get_be24(pb); /* reserved */
  1137. field_size = get_byte(pb);
  1138. }
  1139. entries = get_be32(pb);
  1140. dprintf(c->fc, "sample_size = %d sample_count = %d\n", sc->sample_size, entries);
  1141. sc->sample_count = entries;
  1142. if (sample_size)
  1143. return 0;
  1144. if (field_size != 4 && field_size != 8 && field_size != 16 && field_size != 32) {
  1145. av_log(c->fc, AV_LOG_ERROR, "Invalid sample field size %d\n", field_size);
  1146. return -1;
  1147. }
  1148. if (entries >= UINT_MAX / sizeof(int) || entries >= (UINT_MAX - 4) / field_size)
  1149. return -1;
  1150. sc->sample_sizes = av_malloc(entries * sizeof(int));
  1151. if (!sc->sample_sizes)
  1152. return AVERROR(ENOMEM);
  1153. num_bytes = (entries*field_size+4)>>3;
  1154. buf = av_malloc(num_bytes+FF_INPUT_BUFFER_PADDING_SIZE);
  1155. if (!buf) {
  1156. av_freep(&sc->sample_sizes);
  1157. return AVERROR(ENOMEM);
  1158. }
  1159. if (get_buffer(pb, buf, num_bytes) < num_bytes) {
  1160. av_freep(&sc->sample_sizes);
  1161. av_free(buf);
  1162. return -1;
  1163. }
  1164. init_get_bits(&gb, buf, 8*num_bytes);
  1165. for(i=0; i<entries; i++)
  1166. sc->sample_sizes[i] = get_bits_long(&gb, field_size);
  1167. av_free(buf);
  1168. return 0;
  1169. }
  1170. static int mov_read_stts(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1171. {
  1172. AVStream *st;
  1173. MOVStreamContext *sc;
  1174. unsigned int i, entries;
  1175. int64_t duration=0;
  1176. int64_t total_sample_count=0;
  1177. if (c->fc->nb_streams < 1)
  1178. return 0;
  1179. st = c->fc->streams[c->fc->nb_streams-1];
  1180. sc = st->priv_data;
  1181. get_byte(pb); /* version */
  1182. get_be24(pb); /* flags */
  1183. entries = get_be32(pb);
  1184. dprintf(c->fc, "track[%i].stts.entries = %i\n", c->fc->nb_streams-1, entries);
  1185. if(entries >= UINT_MAX / sizeof(*sc->stts_data))
  1186. return -1;
  1187. sc->stts_data = av_malloc(entries * sizeof(*sc->stts_data));
  1188. if (!sc->stts_data)
  1189. return AVERROR(ENOMEM);
  1190. sc->stts_count = entries;
  1191. for(i=0; i<entries; i++) {
  1192. int sample_duration;
  1193. int sample_count;
  1194. sample_count=get_be32(pb);
  1195. sample_duration = get_be32(pb);
  1196. sc->stts_data[i].count= sample_count;
  1197. sc->stts_data[i].duration= sample_duration;
  1198. dprintf(c->fc, "sample_count=%d, sample_duration=%d\n",sample_count,sample_duration);
  1199. duration+=(int64_t)sample_duration*sample_count;
  1200. total_sample_count+=sample_count;
  1201. }
  1202. st->nb_frames= total_sample_count;
  1203. if(duration)
  1204. st->duration= duration;
  1205. return 0;
  1206. }
  1207. static int mov_read_ctts(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1208. {
  1209. AVStream *st;
  1210. MOVStreamContext *sc;
  1211. unsigned int i, entries;
  1212. if (c->fc->nb_streams < 1)
  1213. return 0;
  1214. st = c->fc->streams[c->fc->nb_streams-1];
  1215. sc = st->priv_data;
  1216. get_byte(pb); /* version */
  1217. get_be24(pb); /* flags */
  1218. entries = get_be32(pb);
  1219. dprintf(c->fc, "track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries);
  1220. if(entries >= UINT_MAX / sizeof(*sc->ctts_data))
  1221. return -1;
  1222. sc->ctts_data = av_malloc(entries * sizeof(*sc->ctts_data));
  1223. if (!sc->ctts_data)
  1224. return AVERROR(ENOMEM);
  1225. sc->ctts_count = entries;
  1226. for(i=0; i<entries; i++) {
  1227. int count =get_be32(pb);
  1228. int duration =get_be32(pb);
  1229. sc->ctts_data[i].count = count;
  1230. sc->ctts_data[i].duration= duration;
  1231. if (duration < 0)
  1232. sc->dts_shift = FFMAX(sc->dts_shift, -duration);
  1233. }
  1234. dprintf(c->fc, "dts shift %d\n", sc->dts_shift);
  1235. return 0;
  1236. }
  1237. static void mov_build_index(MOVContext *mov, AVStream *st)
  1238. {
  1239. MOVStreamContext *sc = st->priv_data;
  1240. int64_t current_offset;
  1241. int64_t current_dts = 0;
  1242. unsigned int stts_index = 0;
  1243. unsigned int stsc_index = 0;
  1244. unsigned int stss_index = 0;
  1245. unsigned int stps_index = 0;
  1246. unsigned int i, j;
  1247. uint64_t stream_size = 0;
  1248. /* adjust first dts according to edit list */
  1249. if (sc->time_offset) {
  1250. int rescaled = sc->time_offset < 0 ? av_rescale(sc->time_offset, sc->time_scale, mov->time_scale) : sc->time_offset;
  1251. current_dts = -rescaled;
  1252. if (sc->ctts_data && sc->ctts_data[0].duration / sc->stts_data[0].duration > 16) {
  1253. /* more than 16 frames delay, dts are likely wrong
  1254. this happens with files created by iMovie */
  1255. sc->wrong_dts = 1;
  1256. st->codec->has_b_frames = 1;
  1257. }
  1258. }
  1259. /* only use old uncompressed audio chunk demuxing when stts specifies it */
  1260. if (!(st->codec->codec_type == CODEC_TYPE_AUDIO &&
  1261. sc->stts_count == 1 && sc->stts_data[0].duration == 1)) {
  1262. unsigned int current_sample = 0;
  1263. unsigned int stts_sample = 0;
  1264. unsigned int sample_size;
  1265. unsigned int distance = 0;
  1266. int key_off = sc->keyframes && sc->keyframes[0] == 1;
  1267. current_dts -= sc->dts_shift;
  1268. for (i = 0; i < sc->chunk_count; i++) {
  1269. current_offset = sc->chunk_offsets[i];
  1270. if (stsc_index + 1 < sc->stsc_count &&
  1271. i + 1 == sc->stsc_data[stsc_index + 1].first)
  1272. stsc_index++;
  1273. for (j = 0; j < sc->stsc_data[stsc_index].count; j++) {
  1274. int keyframe = 0;
  1275. if (current_sample >= sc->sample_count) {
  1276. av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n");
  1277. return;
  1278. }
  1279. if (!sc->keyframe_count || current_sample+key_off == sc->keyframes[stss_index]) {
  1280. keyframe = 1;
  1281. if (stss_index + 1 < sc->keyframe_count)
  1282. stss_index++;
  1283. } else if (sc->stps_count && current_sample+key_off == sc->stps_data[stps_index]) {
  1284. keyframe = 1;
  1285. if (stps_index + 1 < sc->stps_count)
  1286. stps_index++;
  1287. }
  1288. if (keyframe)
  1289. distance = 0;
  1290. sample_size = sc->sample_size > 0 ? sc->sample_size : sc->sample_sizes[current_sample];
  1291. if(sc->pseudo_stream_id == -1 ||
  1292. sc->stsc_data[stsc_index].id - 1 == sc->pseudo_stream_id) {
  1293. av_add_index_entry(st, current_offset, current_dts, sample_size, distance,
  1294. keyframe ? AVINDEX_KEYFRAME : 0);
  1295. dprintf(mov->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
  1296. "size %d, distance %d, keyframe %d\n", st->index, current_sample,
  1297. current_offset, current_dts, sample_size, distance, keyframe);
  1298. }
  1299. current_offset += sample_size;
  1300. stream_size += sample_size;
  1301. current_dts += sc->stts_data[stts_index].duration;
  1302. distance++;
  1303. stts_sample++;
  1304. current_sample++;
  1305. if (stts_index + 1 < sc->stts_count && stts_sample == sc->stts_data[stts_index].count) {
  1306. stts_sample = 0;
  1307. stts_index++;
  1308. }
  1309. }
  1310. }
  1311. if (st->duration > 0)
  1312. st->codec->bit_rate = stream_size*8*sc->time_scale/st->duration;
  1313. } else {
  1314. for (i = 0; i < sc->chunk_count; i++) {
  1315. unsigned chunk_samples;
  1316. current_offset = sc->chunk_offsets[i];
  1317. if (stsc_index + 1 < sc->stsc_count &&
  1318. i + 1 == sc->stsc_data[stsc_index + 1].first)
  1319. stsc_index++;
  1320. chunk_samples = sc->stsc_data[stsc_index].count;
  1321. if (sc->samples_per_frame && chunk_samples % sc->samples_per_frame) {
  1322. av_log(mov->fc, AV_LOG_ERROR, "error unaligned chunk\n");
  1323. return;
  1324. }
  1325. while (chunk_samples > 0) {
  1326. unsigned size, samples;
  1327. if (sc->samples_per_frame >= 160) { // gsm
  1328. samples = sc->samples_per_frame;
  1329. size = sc->bytes_per_frame;
  1330. } else {
  1331. if (sc->samples_per_frame > 1) {
  1332. samples = FFMIN((1024 / sc->samples_per_frame)*
  1333. sc->samples_per_frame, chunk_samples);
  1334. size = (samples / sc->samples_per_frame) * sc->bytes_per_frame;
  1335. } else {
  1336. samples = FFMIN(1024, chunk_samples);
  1337. size = samples * sc->sample_size;
  1338. }
  1339. }
  1340. av_add_index_entry(st, current_offset, current_dts, size, 0, AVINDEX_KEYFRAME);
  1341. dprintf(mov->fc, "AVIndex stream %d, chunk %d, offset %"PRIx64", dts %"PRId64", "
  1342. "size %d, duration %d\n", st->index, i, current_offset, current_dts,
  1343. size, samples);
  1344. current_offset += size;
  1345. current_dts += samples;
  1346. chunk_samples -= samples;
  1347. }
  1348. }
  1349. }
  1350. }
  1351. static int mov_open_dref(ByteIOContext **pb, char *src, MOVDref *ref)
  1352. {
  1353. /* try absolute path */
  1354. if (!url_fopen(pb, ref->path, URL_RDONLY))
  1355. return 0;
  1356. /* try relative path */
  1357. if (ref->nlvl_to > 0 && ref->nlvl_from > 0) {
  1358. char filename[1024];
  1359. char *src_path;
  1360. int i, l;
  1361. /* find a source dir */
  1362. src_path = strrchr(src, '/');
  1363. if (src_path)
  1364. src_path++;
  1365. else
  1366. src_path = src;
  1367. /* find a next level down to target */
  1368. for (i = 0, l = strlen(ref->path) - 1; l >= 0; l--)
  1369. if (ref->path[l] == '/') {
  1370. if (i == ref->nlvl_to - 1)
  1371. break;
  1372. else
  1373. i++;
  1374. }
  1375. /* compose filename if next level down to target was found */
  1376. if (i == ref->nlvl_to - 1) {
  1377. memcpy(filename, src, src_path - src);
  1378. filename[src_path - src] = 0;
  1379. for (i = 1; i < ref->nlvl_from; i++)
  1380. av_strlcat(filename, "../", 1024);
  1381. av_strlcat(filename, ref->path + l + 1, 1024);
  1382. if (!url_fopen(pb, filename, URL_RDONLY))
  1383. return 0;
  1384. }
  1385. }
  1386. return AVERROR(ENOENT);
  1387. };
  1388. static int mov_read_trak(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1389. {
  1390. AVStream *st;
  1391. MOVStreamContext *sc;
  1392. int ret;
  1393. st = av_new_stream(c->fc, c->fc->nb_streams);
  1394. if (!st) return AVERROR(ENOMEM);
  1395. sc = av_mallocz(sizeof(MOVStreamContext));
  1396. if (!sc) return AVERROR(ENOMEM);
  1397. st->priv_data = sc;
  1398. st->codec->codec_type = CODEC_TYPE_DATA;
  1399. sc->ffindex = st->index;
  1400. if ((ret = mov_read_default(c, pb, atom)) < 0)
  1401. return ret;
  1402. /* sanity checks */
  1403. if (sc->chunk_count && (!sc->stts_count || !sc->stsc_count ||
  1404. (!sc->sample_size && !sc->sample_count))) {
  1405. av_log(c->fc, AV_LOG_ERROR, "stream %d, missing mandatory atoms, broken header\n",
  1406. st->index);
  1407. return 0;
  1408. }
  1409. if (!sc->time_scale) {
  1410. av_log(c->fc, AV_LOG_WARNING, "stream %d, timescale not set\n", st->index);
  1411. sc->time_scale = c->time_scale;
  1412. if (!sc->time_scale)
  1413. sc->time_scale = 1;
  1414. }
  1415. av_set_pts_info(st, 64, 1, sc->time_scale);
  1416. if (st->codec->codec_type == CODEC_TYPE_AUDIO &&
  1417. !st->codec->frame_size && sc->stts_count == 1) {
  1418. st->codec->frame_size = av_rescale(sc->stts_data[0].duration,
  1419. st->codec->sample_rate, sc->time_scale);
  1420. dprintf(c->fc, "frame size %d\n", st->codec->frame_size);
  1421. }
  1422. mov_build_index(c, st);
  1423. if (sc->dref_id-1 < sc->drefs_count && sc->drefs[sc->dref_id-1].path) {
  1424. MOVDref *dref = &sc->drefs[sc->dref_id - 1];
  1425. if (mov_open_dref(&sc->pb, c->fc->filename, dref) < 0)
  1426. av_log(c->fc, AV_LOG_ERROR,
  1427. "stream %d, error opening alias: path='%s', dir='%s', "
  1428. "filename='%s', volume='%s', nlvl_from=%d, nlvl_to=%d\n",
  1429. st->index, dref->path, dref->dir, dref->filename,
  1430. dref->volume, dref->nlvl_from, dref->nlvl_to);
  1431. } else
  1432. sc->pb = c->fc->pb;
  1433. switch (st->codec->codec_id) {
  1434. #if CONFIG_H261_DECODER
  1435. case CODEC_ID_H261:
  1436. #endif
  1437. #if CONFIG_H263_DECODER
  1438. case CODEC_ID_H263:
  1439. #endif
  1440. #if CONFIG_H264_DECODER
  1441. case CODEC_ID_H264:
  1442. #endif
  1443. #if CONFIG_MPEG4_DECODER
  1444. case CODEC_ID_MPEG4:
  1445. #endif
  1446. st->codec->width = 0; /* let decoder init width/height */
  1447. st->codec->height= 0;
  1448. break;
  1449. }
  1450. /* Do not need those anymore. */
  1451. av_freep(&sc->chunk_offsets);
  1452. av_freep(&sc->stsc_data);
  1453. av_freep(&sc->sample_sizes);
  1454. av_freep(&sc->keyframes);
  1455. av_freep(&sc->stts_data);
  1456. av_freep(&sc->stps_data);
  1457. return 0;
  1458. }
  1459. static int mov_read_ilst(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1460. {
  1461. int ret;
  1462. c->itunes_metadata = 1;
  1463. ret = mov_read_default(c, pb, atom);
  1464. c->itunes_metadata = 0;
  1465. return ret;
  1466. }
  1467. static int mov_read_meta(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1468. {
  1469. while (atom.size > 8) {
  1470. uint32_t tag = get_le32(pb);
  1471. atom.size -= 4;
  1472. if (tag == MKTAG('h','d','l','r')) {
  1473. url_fseek(pb, -8, SEEK_CUR);
  1474. atom.size += 8;
  1475. return mov_read_default(c, pb, atom);
  1476. }
  1477. }
  1478. return 0;
  1479. }
  1480. static int mov_read_tkhd(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1481. {
  1482. int i;
  1483. int width;
  1484. int height;
  1485. int64_t disp_transform[2];
  1486. int display_matrix[3][2];
  1487. AVStream *st;
  1488. MOVStreamContext *sc;
  1489. int version;
  1490. if (c->fc->nb_streams < 1)
  1491. return 0;
  1492. st = c->fc->streams[c->fc->nb_streams-1];
  1493. sc = st->priv_data;
  1494. version = get_byte(pb);
  1495. get_be24(pb); /* flags */
  1496. /*
  1497. MOV_TRACK_ENABLED 0x0001
  1498. MOV_TRACK_IN_MOVIE 0x0002
  1499. MOV_TRACK_IN_PREVIEW 0x0004
  1500. MOV_TRACK_IN_POSTER 0x0008
  1501. */
  1502. if (version == 1) {
  1503. get_be64(pb);
  1504. get_be64(pb);
  1505. } else {
  1506. get_be32(pb); /* creation time */
  1507. get_be32(pb); /* modification time */
  1508. }
  1509. st->id = (int)get_be32(pb); /* track id (NOT 0 !)*/
  1510. get_be32(pb); /* reserved */
  1511. /* highlevel (considering edits) duration in movie timebase */
  1512. (version == 1) ? get_be64(pb) : get_be32(pb);
  1513. get_be32(pb); /* reserved */
  1514. get_be32(pb); /* reserved */
  1515. get_be16(pb); /* layer */
  1516. get_be16(pb); /* alternate group */
  1517. get_be16(pb); /* volume */
  1518. get_be16(pb); /* reserved */
  1519. //read in the display matrix (outlined in ISO 14496-12, Section 6.2.2)
  1520. // they're kept in fixed point format through all calculations
  1521. // ignore u,v,z b/c we don't need the scale factor to calc aspect ratio
  1522. for (i = 0; i < 3; i++) {
  1523. display_matrix[i][0] = get_be32(pb); // 16.16 fixed point
  1524. display_matrix[i][1] = get_be32(pb); // 16.16 fixed point
  1525. get_be32(pb); // 2.30 fixed point (not used)
  1526. }
  1527. width = get_be32(pb); // 16.16 fixed point track width
  1528. height = get_be32(pb); // 16.16 fixed point track height
  1529. sc->width = width >> 16;
  1530. sc->height = height >> 16;
  1531. // transform the display width/height according to the matrix
  1532. // skip this if the display matrix is the default identity matrix
  1533. // or if it is rotating the picture, ex iPhone 3GS
  1534. // to keep the same scale, use [width height 1<<16]
  1535. if (width && height &&
  1536. ((display_matrix[0][0] != 65536 ||
  1537. display_matrix[1][1] != 65536) &&
  1538. !display_matrix[0][1] &&
  1539. !display_matrix[1][0] &&
  1540. !display_matrix[2][0] && !display_matrix[2][1])) {
  1541. for (i = 0; i < 2; i++)
  1542. disp_transform[i] =
  1543. (int64_t) width * display_matrix[0][i] +
  1544. (int64_t) height * display_matrix[1][i] +
  1545. ((int64_t) display_matrix[2][i] << 16);
  1546. //sample aspect ratio is new width/height divided by old width/height
  1547. st->sample_aspect_ratio = av_d2q(
  1548. ((double) disp_transform[0] * height) /
  1549. ((double) disp_transform[1] * width), INT_MAX);
  1550. }
  1551. return 0;
  1552. }
  1553. static int mov_read_tfhd(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1554. {
  1555. MOVFragment *frag = &c->fragment;
  1556. MOVTrackExt *trex = NULL;
  1557. int flags, track_id, i;
  1558. get_byte(pb); /* version */
  1559. flags = get_be24(pb);
  1560. track_id = get_be32(pb);
  1561. if (!track_id)
  1562. return -1;
  1563. frag->track_id = track_id;
  1564. for (i = 0; i < c->trex_count; i++)
  1565. if (c->trex_data[i].track_id == frag->track_id) {
  1566. trex = &c->trex_data[i];
  1567. break;
  1568. }
  1569. if (!trex) {
  1570. av_log(c->fc, AV_LOG_ERROR, "could not find corresponding trex\n");
  1571. return -1;
  1572. }
  1573. if (flags & 0x01) frag->base_data_offset = get_be64(pb);
  1574. else frag->base_data_offset = frag->moof_offset;
  1575. if (flags & 0x02) frag->stsd_id = get_be32(pb);
  1576. else frag->stsd_id = trex->stsd_id;
  1577. frag->duration = flags & 0x08 ? get_be32(pb) : trex->duration;
  1578. frag->size = flags & 0x10 ? get_be32(pb) : trex->size;
  1579. frag->flags = flags & 0x20 ? get_be32(pb) : trex->flags;
  1580. dprintf(c->fc, "frag flags 0x%x\n", frag->flags);
  1581. return 0;
  1582. }
  1583. static int mov_read_trex(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1584. {
  1585. MOVTrackExt *trex;
  1586. if ((uint64_t)c->trex_count+1 >= UINT_MAX / sizeof(*c->trex_data))
  1587. return -1;
  1588. trex = av_realloc(c->trex_data, (c->trex_count+1)*sizeof(*c->trex_data));
  1589. if (!trex)
  1590. return AVERROR(ENOMEM);
  1591. c->trex_data = trex;
  1592. trex = &c->trex_data[c->trex_count++];
  1593. get_byte(pb); /* version */
  1594. get_be24(pb); /* flags */
  1595. trex->track_id = get_be32(pb);
  1596. trex->stsd_id = get_be32(pb);
  1597. trex->duration = get_be32(pb);
  1598. trex->size = get_be32(pb);
  1599. trex->flags = get_be32(pb);
  1600. return 0;
  1601. }
  1602. static int mov_read_trun(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1603. {
  1604. MOVFragment *frag = &c->fragment;
  1605. AVStream *st = NULL;
  1606. MOVStreamContext *sc;
  1607. uint64_t offset;
  1608. int64_t dts;
  1609. int data_offset = 0;
  1610. unsigned entries, first_sample_flags = frag->flags;
  1611. int flags, distance, i;
  1612. for (i = 0; i < c->fc->nb_streams; i++) {
  1613. if (c->fc->streams[i]->id == frag->track_id) {
  1614. st = c->fc->streams[i];
  1615. break;
  1616. }
  1617. }
  1618. if (!st) {
  1619. av_log(c->fc, AV_LOG_ERROR, "could not find corresponding track id %d\n", frag->track_id);
  1620. return -1;
  1621. }
  1622. sc = st->priv_data;
  1623. if (sc->pseudo_stream_id+1 != frag->stsd_id)
  1624. return 0;
  1625. get_byte(pb); /* version */
  1626. flags = get_be24(pb);
  1627. entries = get_be32(pb);
  1628. dprintf(c->fc, "flags 0x%x entries %d\n", flags, entries);
  1629. if (flags & 0x001) data_offset = get_be32(pb);
  1630. if (flags & 0x004) first_sample_flags = get_be32(pb);
  1631. if (flags & 0x800) {
  1632. MOVStts *ctts_data;
  1633. if ((uint64_t)entries+sc->ctts_count >= UINT_MAX/sizeof(*sc->ctts_data))
  1634. return -1;
  1635. ctts_data = av_realloc(sc->ctts_data,
  1636. (entries+sc->ctts_count)*sizeof(*sc->ctts_data));
  1637. if (!ctts_data)
  1638. return AVERROR(ENOMEM);
  1639. sc->ctts_data = ctts_data;
  1640. }
  1641. dts = st->duration;
  1642. offset = frag->base_data_offset + data_offset;
  1643. distance = 0;
  1644. dprintf(c->fc, "first sample flags 0x%x\n", first_sample_flags);
  1645. for (i = 0; i < entries; i++) {
  1646. unsigned sample_size = frag->size;
  1647. int sample_flags = i ? frag->flags : first_sample_flags;
  1648. unsigned sample_duration = frag->duration;
  1649. int keyframe;
  1650. if (flags & 0x100) sample_duration = get_be32(pb);
  1651. if (flags & 0x200) sample_size = get_be32(pb);
  1652. if (flags & 0x400) sample_flags = get_be32(pb);
  1653. if (flags & 0x800) {
  1654. sc->ctts_data[sc->ctts_count].count = 1;
  1655. sc->ctts_data[sc->ctts_count].duration = get_be32(pb);
  1656. sc->ctts_count++;
  1657. }
  1658. if ((keyframe = st->codec->codec_type == CODEC_TYPE_AUDIO ||
  1659. (flags & 0x004 && !i && !sample_flags) || sample_flags & 0x2000000))
  1660. distance = 0;
  1661. av_add_index_entry(st, offset, dts, sample_size, distance,
  1662. keyframe ? AVINDEX_KEYFRAME : 0);
  1663. dprintf(c->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
  1664. "size %d, distance %d, keyframe %d\n", st->index, sc->sample_count+i,
  1665. offset, dts, sample_size, distance, keyframe);
  1666. distance++;
  1667. dts += sample_duration;
  1668. offset += sample_size;
  1669. }
  1670. frag->moof_offset = offset;
  1671. st->duration = dts;
  1672. return 0;
  1673. }
  1674. /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
  1675. /* like the files created with Adobe Premiere 5.0, for samples see */
  1676. /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
  1677. static int mov_read_wide(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1678. {
  1679. int err;
  1680. if (atom.size < 8)
  1681. return 0; /* continue */
  1682. if (get_be32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
  1683. url_fskip(pb, atom.size - 4);
  1684. return 0;
  1685. }
  1686. atom.type = get_le32(pb);
  1687. atom.size -= 8;
  1688. if (atom.type != MKTAG('m','d','a','t')) {
  1689. url_fskip(pb, atom.size);
  1690. return 0;
  1691. }
  1692. err = mov_read_mdat(c, pb, atom);
  1693. return err;
  1694. }
  1695. static int mov_read_cmov(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1696. {
  1697. #if CONFIG_ZLIB
  1698. ByteIOContext ctx;
  1699. uint8_t *cmov_data;
  1700. uint8_t *moov_data; /* uncompressed data */
  1701. long cmov_len, moov_len;
  1702. int ret = -1;
  1703. get_be32(pb); /* dcom atom */
  1704. if (get_le32(pb) != MKTAG('d','c','o','m'))
  1705. return -1;
  1706. if (get_le32(pb) != MKTAG('z','l','i','b')) {
  1707. av_log(c->fc, AV_LOG_ERROR, "unknown compression for cmov atom !");
  1708. return -1;
  1709. }
  1710. get_be32(pb); /* cmvd atom */
  1711. if (get_le32(pb) != MKTAG('c','m','v','d'))
  1712. return -1;
  1713. moov_len = get_be32(pb); /* uncompressed size */
  1714. cmov_len = atom.size - 6 * 4;
  1715. cmov_data = av_malloc(cmov_len);
  1716. if (!cmov_data)
  1717. return AVERROR(ENOMEM);
  1718. moov_data = av_malloc(moov_len);
  1719. if (!moov_data) {
  1720. av_free(cmov_data);
  1721. return AVERROR(ENOMEM);
  1722. }
  1723. get_buffer(pb, cmov_data, cmov_len);
  1724. if(uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
  1725. goto free_and_return;
  1726. if(init_put_byte(&ctx, moov_data, moov_len, 0, NULL, NULL, NULL, NULL) != 0)
  1727. goto free_and_return;
  1728. atom.type = MKTAG('m','o','o','v');
  1729. atom.size = moov_len;
  1730. #ifdef DEBUG
  1731. // { int fd = open("/tmp/uncompheader.mov", O_WRONLY | O_CREAT); write(fd, moov_data, moov_len); close(fd); }
  1732. #endif
  1733. ret = mov_read_default(c, &ctx, atom);
  1734. free_and_return:
  1735. av_free(moov_data);
  1736. av_free(cmov_data);
  1737. return ret;
  1738. #else
  1739. av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n");
  1740. return -1;
  1741. #endif
  1742. }
  1743. /* edit list atom */
  1744. static int mov_read_elst(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1745. {
  1746. MOVStreamContext *sc;
  1747. int i, edit_count;
  1748. if (c->fc->nb_streams < 1)
  1749. return 0;
  1750. sc = c->fc->streams[c->fc->nb_streams-1]->priv_data;
  1751. get_byte(pb); /* version */
  1752. get_be24(pb); /* flags */
  1753. edit_count = get_be32(pb); /* entries */
  1754. if((uint64_t)edit_count*12+8 > atom.size)
  1755. return -1;
  1756. for(i=0; i<edit_count; i++){
  1757. int time;
  1758. int duration = get_be32(pb); /* Track duration */
  1759. time = get_be32(pb); /* Media time */
  1760. get_be32(pb); /* Media rate */
  1761. if (i == 0 && time >= -1) {
  1762. sc->time_offset = time != -1 ? time : -duration;
  1763. }
  1764. }
  1765. if(edit_count > 1)
  1766. av_log(c->fc, AV_LOG_WARNING, "multiple edit list entries, "
  1767. "a/v desync might occur, patch welcome\n");
  1768. dprintf(c->fc, "track[%i].edit_count = %i\n", c->fc->nb_streams-1, edit_count);
  1769. return 0;
  1770. }
  1771. static const MOVParseTableEntry mov_default_parse_table[] = {
  1772. { MKTAG('a','v','s','s'), mov_read_extradata },
  1773. { MKTAG('c','o','6','4'), mov_read_stco },
  1774. { MKTAG('c','t','t','s'), mov_read_ctts }, /* composition time to sample */
  1775. { MKTAG('d','i','n','f'), mov_read_default },
  1776. { MKTAG('d','r','e','f'), mov_read_dref },
  1777. { MKTAG('e','d','t','s'), mov_read_default },
  1778. { MKTAG('e','l','s','t'), mov_read_elst },
  1779. { MKTAG('e','n','d','a'), mov_read_enda },
  1780. { MKTAG('f','i','e','l'), mov_read_extradata },
  1781. { MKTAG('f','t','y','p'), mov_read_ftyp },
  1782. { MKTAG('g','l','b','l'), mov_read_glbl },
  1783. { MKTAG('h','d','l','r'), mov_read_hdlr },
  1784. { MKTAG('i','l','s','t'), mov_read_ilst },
  1785. { MKTAG('j','p','2','h'), mov_read_extradata },
  1786. { MKTAG('m','d','a','t'), mov_read_mdat },
  1787. { MKTAG('m','d','h','d'), mov_read_mdhd },
  1788. { MKTAG('m','d','i','a'), mov_read_default },
  1789. { MKTAG('m','e','t','a'), mov_read_meta },
  1790. { MKTAG('m','i','n','f'), mov_read_default },
  1791. { MKTAG('m','o','o','f'), mov_read_moof },
  1792. { MKTAG('m','o','o','v'), mov_read_moov },
  1793. { MKTAG('m','v','e','x'), mov_read_default },
  1794. { MKTAG('m','v','h','d'), mov_read_mvhd },
  1795. { MKTAG('S','M','I',' '), mov_read_smi }, /* Sorenson extension ??? */
  1796. { MKTAG('a','l','a','c'), mov_read_extradata }, /* alac specific atom */
  1797. { MKTAG('a','v','c','C'), mov_read_glbl },
  1798. { MKTAG('p','a','s','p'), mov_read_pasp },
  1799. { MKTAG('s','t','b','l'), mov_read_default },
  1800. { MKTAG('s','t','c','o'), mov_read_stco },
  1801. { MKTAG('s','t','p','s'), mov_read_stps },
  1802. { MKTAG('s','t','s','c'), mov_read_stsc },
  1803. { MKTAG('s','t','s','d'), mov_read_stsd }, /* sample description */
  1804. { MKTAG('s','t','s','s'), mov_read_stss }, /* sync sample */
  1805. { MKTAG('s','t','s','z'), mov_read_stsz }, /* sample size */
  1806. { MKTAG('s','t','t','s'), mov_read_stts },
  1807. { MKTAG('s','t','z','2'), mov_read_stsz }, /* compact sample size */
  1808. { MKTAG('t','k','h','d'), mov_read_tkhd }, /* track header */
  1809. { MKTAG('t','f','h','d'), mov_read_tfhd }, /* track fragment header */
  1810. { MKTAG('t','r','a','k'), mov_read_trak },
  1811. { MKTAG('t','r','a','f'), mov_read_default },
  1812. { MKTAG('t','r','e','x'), mov_read_trex },
  1813. { MKTAG('t','r','u','n'), mov_read_trun },
  1814. { MKTAG('u','d','t','a'), mov_read_default },
  1815. { MKTAG('w','a','v','e'), mov_read_wave },
  1816. { MKTAG('e','s','d','s'), mov_read_esds },
  1817. { MKTAG('w','i','d','e'), mov_read_wide }, /* place holder */
  1818. { MKTAG('c','m','o','v'), mov_read_cmov },
  1819. { 0, NULL }
  1820. };
  1821. static int mov_probe(AVProbeData *p)
  1822. {
  1823. unsigned int offset;
  1824. uint32_t tag;
  1825. int score = 0;
  1826. /* check file header */
  1827. offset = 0;
  1828. for(;;) {
  1829. /* ignore invalid offset */
  1830. if ((offset + 8) > (unsigned int)p->buf_size)
  1831. return score;
  1832. tag = AV_RL32(p->buf + offset + 4);
  1833. switch(tag) {
  1834. /* check for obvious tags */
  1835. case MKTAG('j','P',' ',' '): /* jpeg 2000 signature */
  1836. case MKTAG('m','o','o','v'):
  1837. case MKTAG('m','d','a','t'):
  1838. case MKTAG('p','n','o','t'): /* detect movs with preview pics like ew.mov and april.mov */
  1839. case MKTAG('u','d','t','a'): /* Packet Video PVAuthor adds this and a lot of more junk */
  1840. case MKTAG('f','t','y','p'):
  1841. return AVPROBE_SCORE_MAX;
  1842. /* those are more common words, so rate then a bit less */
  1843. case MKTAG('e','d','i','w'): /* xdcam files have reverted first tags */
  1844. case MKTAG('w','i','d','e'):
  1845. case MKTAG('f','r','e','e'):
  1846. case MKTAG('j','u','n','k'):
  1847. case MKTAG('p','i','c','t'):
  1848. return AVPROBE_SCORE_MAX - 5;
  1849. case MKTAG(0x82,0x82,0x7f,0x7d):
  1850. case MKTAG('s','k','i','p'):
  1851. case MKTAG('u','u','i','d'):
  1852. case MKTAG('p','r','f','l'):
  1853. offset = AV_RB32(p->buf+offset) + offset;
  1854. /* if we only find those cause probedata is too small at least rate them */
  1855. score = AVPROBE_SCORE_MAX - 50;
  1856. break;
  1857. default:
  1858. /* unrecognized tag */
  1859. return score;
  1860. }
  1861. }
  1862. return score;
  1863. }
  1864. static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap)
  1865. {
  1866. MOVContext *mov = s->priv_data;
  1867. ByteIOContext *pb = s->pb;
  1868. int err;
  1869. MOVAtom atom = { 0 };
  1870. mov->fc = s;
  1871. /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
  1872. if(!url_is_streamed(pb))
  1873. atom.size = url_fsize(pb);
  1874. else
  1875. atom.size = INT64_MAX;
  1876. /* check MOV header */
  1877. if ((err = mov_read_default(mov, pb, atom)) < 0) {
  1878. av_log(s, AV_LOG_ERROR, "error reading header: %d\n", err);
  1879. return err;
  1880. }
  1881. if (!mov->found_moov) {
  1882. av_log(s, AV_LOG_ERROR, "moov atom not found\n");
  1883. return -1;
  1884. }
  1885. dprintf(mov->fc, "on_parse_exit_offset=%lld\n", url_ftell(pb));
  1886. return 0;
  1887. }
  1888. static AVIndexEntry *mov_find_next_sample(AVFormatContext *s, AVStream **st)
  1889. {
  1890. AVIndexEntry *sample = NULL;
  1891. int64_t best_dts = INT64_MAX;
  1892. int i;
  1893. for (i = 0; i < s->nb_streams; i++) {
  1894. AVStream *avst = s->streams[i];
  1895. MOVStreamContext *msc = avst->priv_data;
  1896. if (msc->pb && msc->current_sample < avst->nb_index_entries) {
  1897. AVIndexEntry *current_sample = &avst->index_entries[msc->current_sample];
  1898. int64_t dts = av_rescale(current_sample->timestamp, AV_TIME_BASE, msc->time_scale);
  1899. dprintf(s, "stream %d, sample %d, dts %"PRId64"\n", i, msc->current_sample, dts);
  1900. if (!sample || (url_is_streamed(s->pb) && current_sample->pos < sample->pos) ||
  1901. (!url_is_streamed(s->pb) &&
  1902. ((msc->pb != s->pb && dts < best_dts) || (msc->pb == s->pb &&
  1903. ((FFABS(best_dts - dts) <= AV_TIME_BASE && current_sample->pos < sample->pos) ||
  1904. (FFABS(best_dts - dts) > AV_TIME_BASE && dts < best_dts)))))) {
  1905. sample = current_sample;
  1906. best_dts = dts;
  1907. *st = avst;
  1908. }
  1909. }
  1910. }
  1911. return sample;
  1912. }
  1913. static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
  1914. {
  1915. MOVContext *mov = s->priv_data;
  1916. MOVStreamContext *sc;
  1917. AVIndexEntry *sample;
  1918. AVStream *st = NULL;
  1919. int ret;
  1920. retry:
  1921. sample = mov_find_next_sample(s, &st);
  1922. if (!sample) {
  1923. mov->found_mdat = 0;
  1924. if (!url_is_streamed(s->pb) ||
  1925. mov_read_default(mov, s->pb, (MOVAtom){ 0, INT64_MAX }) < 0 ||
  1926. url_feof(s->pb))
  1927. return AVERROR_EOF;
  1928. dprintf(s, "read fragments, offset 0x%llx\n", url_ftell(s->pb));
  1929. goto retry;
  1930. }
  1931. sc = st->priv_data;
  1932. /* must be done just before reading, to avoid infinite loop on sample */
  1933. sc->current_sample++;
  1934. if (st->discard != AVDISCARD_ALL) {
  1935. if (url_fseek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
  1936. av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n",
  1937. sc->ffindex, sample->pos);
  1938. return -1;
  1939. }
  1940. ret = av_get_packet(sc->pb, pkt, sample->size);
  1941. if (ret < 0)
  1942. return ret;
  1943. #if CONFIG_DV_DEMUXER
  1944. if (mov->dv_demux && sc->dv_audio_container) {
  1945. dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size);
  1946. av_free(pkt->data);
  1947. pkt->size = 0;
  1948. ret = dv_get_packet(mov->dv_demux, pkt);
  1949. if (ret < 0)
  1950. return ret;
  1951. }
  1952. #endif
  1953. }
  1954. pkt->stream_index = sc->ffindex;
  1955. pkt->dts = sample->timestamp;
  1956. if (sc->ctts_data) {
  1957. pkt->pts = pkt->dts + sc->dts_shift + sc->ctts_data[sc->ctts_index].duration;
  1958. /* update ctts context */
  1959. sc->ctts_sample++;
  1960. if (sc->ctts_index < sc->ctts_count &&
  1961. sc->ctts_data[sc->ctts_index].count == sc->ctts_sample) {
  1962. sc->ctts_index++;
  1963. sc->ctts_sample = 0;
  1964. }
  1965. if (sc->wrong_dts)
  1966. pkt->dts = AV_NOPTS_VALUE;
  1967. } else {
  1968. int64_t next_dts = (sc->current_sample < st->nb_index_entries) ?
  1969. st->index_entries[sc->current_sample].timestamp : st->duration;
  1970. pkt->duration = next_dts - pkt->dts;
  1971. pkt->pts = pkt->dts;
  1972. }
  1973. if (st->discard == AVDISCARD_ALL)
  1974. goto retry;
  1975. pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? PKT_FLAG_KEY : 0;
  1976. pkt->pos = sample->pos;
  1977. dprintf(s, "stream %d, pts %"PRId64", dts %"PRId64", pos 0x%"PRIx64", duration %d\n",
  1978. pkt->stream_index, pkt->pts, pkt->dts, pkt->pos, pkt->duration);
  1979. return 0;
  1980. }
  1981. static int mov_seek_stream(AVFormatContext *s, AVStream *st, int64_t timestamp, int flags)
  1982. {
  1983. MOVStreamContext *sc = st->priv_data;
  1984. int sample, time_sample;
  1985. int i;
  1986. sample = av_index_search_timestamp(st, timestamp, flags);
  1987. dprintf(s, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample);
  1988. if (sample < 0) /* not sure what to do */
  1989. return -1;
  1990. sc->current_sample = sample;
  1991. dprintf(s, "stream %d, found sample %d\n", st->index, sc->current_sample);
  1992. /* adjust ctts index */
  1993. if (sc->ctts_data) {
  1994. time_sample = 0;
  1995. for (i = 0; i < sc->ctts_count; i++) {
  1996. int next = time_sample + sc->ctts_data[i].count;
  1997. if (next > sc->current_sample) {
  1998. sc->ctts_index = i;
  1999. sc->ctts_sample = sc->current_sample - time_sample;
  2000. break;
  2001. }
  2002. time_sample = next;
  2003. }
  2004. }
  2005. return sample;
  2006. }
  2007. static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
  2008. {
  2009. AVStream *st;
  2010. int64_t seek_timestamp, timestamp;
  2011. int sample;
  2012. int i;
  2013. if (stream_index >= s->nb_streams)
  2014. return -1;
  2015. if (sample_time < 0)
  2016. sample_time = 0;
  2017. st = s->streams[stream_index];
  2018. sample = mov_seek_stream(s, st, sample_time, flags);
  2019. if (sample < 0)
  2020. return -1;
  2021. /* adjust seek timestamp to found sample timestamp */
  2022. seek_timestamp = st->index_entries[sample].timestamp;
  2023. for (i = 0; i < s->nb_streams; i++) {
  2024. st = s->streams[i];
  2025. if (stream_index == i)
  2026. continue;
  2027. timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base);
  2028. mov_seek_stream(s, st, timestamp, flags);
  2029. }
  2030. return 0;
  2031. }
  2032. static int mov_read_close(AVFormatContext *s)
  2033. {
  2034. MOVContext *mov = s->priv_data;
  2035. int i, j;
  2036. for (i = 0; i < s->nb_streams; i++) {
  2037. AVStream *st = s->streams[i];
  2038. MOVStreamContext *sc = st->priv_data;
  2039. av_freep(&sc->ctts_data);
  2040. for (j = 0; j < sc->drefs_count; j++) {
  2041. av_freep(&sc->drefs[j].path);
  2042. av_freep(&sc->drefs[j].dir);
  2043. }
  2044. av_freep(&sc->drefs);
  2045. if (sc->pb && sc->pb != s->pb)
  2046. url_fclose(sc->pb);
  2047. av_freep(&st->codec->palctrl);
  2048. }
  2049. if (mov->dv_demux) {
  2050. for(i = 0; i < mov->dv_fctx->nb_streams; i++) {
  2051. av_freep(&mov->dv_fctx->streams[i]->codec);
  2052. av_freep(&mov->dv_fctx->streams[i]);
  2053. }
  2054. av_freep(&mov->dv_fctx);
  2055. av_freep(&mov->dv_demux);
  2056. }
  2057. av_freep(&mov->trex_data);
  2058. return 0;
  2059. }
  2060. AVInputFormat mov_demuxer = {
  2061. "mov,mp4,m4a,3gp,3g2,mj2",
  2062. NULL_IF_CONFIG_SMALL("QuickTime/MPEG-4/Motion JPEG 2000 format"),
  2063. sizeof(MOVContext),
  2064. mov_probe,
  2065. mov_read_header,
  2066. mov_read_packet,
  2067. mov_read_close,
  2068. mov_read_seek,
  2069. };