00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <limits.h>
00024
00025
00026
00027
00028
00029 #include "libavutil/intreadwrite.h"
00030 #include "libavutil/avstring.h"
00031 #include "avformat.h"
00032 #include "riff.h"
00033 #include "isom.h"
00034 #include "libavcodec/mpeg4audio.h"
00035 #include "libavcodec/mpegaudiodata.h"
00036 #include "libavcodec/get_bits.h"
00037
00038 #if CONFIG_ZLIB
00039 #include <zlib.h>
00040 #endif
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062 #include "qtpalette.h"
00063
00064
00065 #undef NDEBUG
00066 #include <assert.h>
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076 typedef struct MOVParseTableEntry {
00077 uint32_t type;
00078 int (*parse)(MOVContext *ctx, ByteIOContext *pb, MOVAtom atom);
00079 } MOVParseTableEntry;
00080
00081 static const MOVParseTableEntry mov_default_parse_table[];
00082
00083 static int mov_metadata_trkn(MOVContext *c, ByteIOContext *pb, unsigned len)
00084 {
00085 char buf[16];
00086
00087 get_be16(pb);
00088 snprintf(buf, sizeof(buf), "%d", get_be16(pb));
00089 av_metadata_set2(&c->fc->metadata, "track", buf, 0);
00090
00091 get_be16(pb);
00092
00093 return 0;
00094 }
00095
00096 static const uint32_t mac_to_unicode[128] = {
00097 0x00C4,0x00C5,0x00C7,0x00C9,0x00D1,0x00D6,0x00DC,0x00E1,
00098 0x00E0,0x00E2,0x00E4,0x00E3,0x00E5,0x00E7,0x00E9,0x00E8,
00099 0x00EA,0x00EB,0x00ED,0x00EC,0x00EE,0x00EF,0x00F1,0x00F3,
00100 0x00F2,0x00F4,0x00F6,0x00F5,0x00FA,0x00F9,0x00FB,0x00FC,
00101 0x2020,0x00B0,0x00A2,0x00A3,0x00A7,0x2022,0x00B6,0x00DF,
00102 0x00AE,0x00A9,0x2122,0x00B4,0x00A8,0x2260,0x00C6,0x00D8,
00103 0x221E,0x00B1,0x2264,0x2265,0x00A5,0x00B5,0x2202,0x2211,
00104 0x220F,0x03C0,0x222B,0x00AA,0x00BA,0x03A9,0x00E6,0x00F8,
00105 0x00BF,0x00A1,0x00AC,0x221A,0x0192,0x2248,0x2206,0x00AB,
00106 0x00BB,0x2026,0x00A0,0x00C0,0x00C3,0x00D5,0x0152,0x0153,
00107 0x2013,0x2014,0x201C,0x201D,0x2018,0x2019,0x00F7,0x25CA,
00108 0x00FF,0x0178,0x2044,0x20AC,0x2039,0x203A,0xFB01,0xFB02,
00109 0x2021,0x00B7,0x201A,0x201E,0x2030,0x00C2,0x00CA,0x00C1,
00110 0x00CB,0x00C8,0x00CD,0x00CE,0x00CF,0x00CC,0x00D3,0x00D4,
00111 0xF8FF,0x00D2,0x00DA,0x00DB,0x00D9,0x0131,0x02C6,0x02DC,
00112 0x00AF,0x02D8,0x02D9,0x02DA,0x00B8,0x02DD,0x02DB,0x02C7,
00113 };
00114
00115 static int mov_read_mac_string(MOVContext *c, ByteIOContext *pb, int len,
00116 char *dst, int dstlen)
00117 {
00118 char *p = dst;
00119 char *end = dst+dstlen-1;
00120 int i;
00121
00122 for (i = 0; i < len; i++) {
00123 uint8_t t, c = get_byte(pb);
00124 if (c < 0x80 && p < end)
00125 *p++ = c;
00126 else
00127 PUT_UTF8(mac_to_unicode[c-0x80], t, if (p < end) *p++ = t;);
00128 }
00129 *p = 0;
00130 return p - dst;
00131 }
00132
00133 static int mov_read_udta_string(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
00134 {
00135 #ifdef MOV_EXPORT_ALL_METADATA
00136 char tmp_key[5];
00137 #endif
00138 char str[1024], key2[16], language[4] = {0};
00139 const char *key = NULL;
00140 uint16_t str_size, langcode = 0;
00141 uint32_t data_type = 0;
00142 int (*parse)(MOVContext*, ByteIOContext*, unsigned) = NULL;
00143
00144 switch (atom.type) {
00145 case MKTAG(0xa9,'n','a','m'): key = "title"; break;
00146 case MKTAG(0xa9,'a','u','t'):
00147 case MKTAG(0xa9,'A','R','T'): key = "artist"; break;
00148 case MKTAG(0xa9,'w','r','t'): key = "composer"; break;
00149 case MKTAG( 'c','p','r','t'):
00150 case MKTAG(0xa9,'c','p','y'): key = "copyright"; break;
00151 case MKTAG(0xa9,'c','m','t'):
00152 case MKTAG(0xa9,'i','n','f'): key = "comment"; break;
00153 case MKTAG(0xa9,'a','l','b'): key = "album"; break;
00154 case MKTAG(0xa9,'d','a','y'): key = "date"; break;
00155 case MKTAG(0xa9,'g','e','n'): key = "genre"; break;
00156 case MKTAG(0xa9,'t','o','o'):
00157 case MKTAG(0xa9,'e','n','c'): key = "encoder"; break;
00158 case MKTAG( 'd','e','s','c'): key = "description";break;
00159 case MKTAG( 'l','d','e','s'): key = "synopsis"; break;
00160 case MKTAG( 't','v','s','h'): key = "show"; break;
00161 case MKTAG( 't','v','e','n'): key = "episode_id";break;
00162 case MKTAG( 't','v','n','n'): key = "network"; break;
00163 case MKTAG( 't','r','k','n'): key = "track";
00164 parse = mov_metadata_trkn; break;
00165 }
00166
00167 if (c->itunes_metadata && atom.size > 8) {
00168 int data_size = get_be32(pb);
00169 int tag = get_le32(pb);
00170 if (tag == MKTAG('d','a','t','a')) {
00171 data_type = get_be32(pb);
00172 get_be32(pb);
00173 str_size = data_size - 16;
00174 atom.size -= 16;
00175 } else return 0;
00176 } else if (atom.size > 4 && key && !c->itunes_metadata) {
00177 str_size = get_be16(pb);
00178 langcode = get_be16(pb);
00179 ff_mov_lang_to_iso639(langcode, language);
00180 atom.size -= 4;
00181 } else
00182 str_size = atom.size;
00183
00184 #ifdef MOV_EXPORT_ALL_METADATA
00185 if (!key) {
00186 snprintf(tmp_key, 5, "%.4s", (char*)&atom.type);
00187 key = tmp_key;
00188 }
00189 #endif
00190
00191 if (!key)
00192 return 0;
00193 if (atom.size < 0)
00194 return -1;
00195
00196 str_size = FFMIN3(sizeof(str)-1, str_size, atom.size);
00197
00198 if (parse)
00199 parse(c, pb, str_size);
00200 else {
00201 if (data_type == 3 || (data_type == 0 && langcode < 0x800)) {
00202 mov_read_mac_string(c, pb, str_size, str, sizeof(str));
00203 } else {
00204 get_buffer(pb, str, str_size);
00205 str[str_size] = 0;
00206 }
00207 av_metadata_set2(&c->fc->metadata, key, str, 0);
00208 if (*language && strcmp(language, "und")) {
00209 snprintf(key2, sizeof(key2), "%s-%s", key, language);
00210 av_metadata_set2(&c->fc->metadata, key2, str, 0);
00211 }
00212 }
00213 #ifdef DEBUG_METADATA
00214 av_log(c->fc, AV_LOG_DEBUG, "lang \"%3s\" ", language);
00215 av_log(c->fc, AV_LOG_DEBUG, "tag \"%s\" value \"%s\" atom \"%.4s\" %d %lld\n",
00216 key, str, (char*)&atom.type, str_size, atom.size);
00217 #endif
00218
00219 return 0;
00220 }
00221
00222 static int mov_read_chpl(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
00223 {
00224 int64_t start;
00225 int i, nb_chapters, str_len;
00226 char str[256+1];
00227
00228 if ((atom.size -= 5) < 0)
00229 return 0;
00230
00231 get_be32(pb);
00232 get_be32(pb);
00233 nb_chapters = get_byte(pb);
00234
00235 for (i = 0; i < nb_chapters; i++) {
00236 if (atom.size < 9)
00237 return 0;
00238
00239 start = get_be64(pb);
00240 str_len = get_byte(pb);
00241
00242 if ((atom.size -= 9+str_len) < 0)
00243 return 0;
00244
00245 get_buffer(pb, str, str_len);
00246 str[str_len] = 0;
00247 ff_new_chapter(c->fc, i, (AVRational){1,10000000}, start, AV_NOPTS_VALUE, str);
00248 }
00249 return 0;
00250 }
00251
00252 static int mov_read_default(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
00253 {
00254 int64_t total_size = 0;
00255 MOVAtom a;
00256 int i;
00257
00258 if (atom.size < 0)
00259 atom.size = INT64_MAX;
00260 while (total_size + 8 < atom.size && !url_feof(pb)) {
00261 int (*parse)(MOVContext*, ByteIOContext*, MOVAtom) = NULL;
00262 a.size = atom.size;
00263 a.type=0;
00264 if(atom.size >= 8) {
00265 a.size = get_be32(pb);
00266 a.type = get_le32(pb);
00267 }
00268 total_size += 8;
00269 dprintf(c->fc, "type: %08x %.4s sz: %"PRIx64" %"PRIx64" %"PRIx64"\n",
00270 a.type, (char*)&a.type, a.size, atom.size, total_size);
00271 if (a.size == 1) {
00272 a.size = get_be64(pb) - 8;
00273 total_size += 8;
00274 }
00275 if (a.size == 0) {
00276 a.size = atom.size - total_size;
00277 if (a.size <= 8)
00278 break;
00279 }
00280 a.size -= 8;
00281 if(a.size < 0)
00282 break;
00283 a.size = FFMIN(a.size, atom.size - total_size);
00284
00285 for (i = 0; mov_default_parse_table[i].type; i++)
00286 if (mov_default_parse_table[i].type == a.type) {
00287 parse = mov_default_parse_table[i].parse;
00288 break;
00289 }
00290
00291
00292 if (!parse && (atom.type == MKTAG('u','d','t','a') ||
00293 atom.type == MKTAG('i','l','s','t')))
00294 parse = mov_read_udta_string;
00295
00296 if (!parse) {
00297 url_fskip(pb, a.size);
00298 } else {
00299 int64_t start_pos = url_ftell(pb);
00300 int64_t left;
00301 int err = parse(c, pb, a);
00302 if (err < 0)
00303 return err;
00304 if (c->found_moov && c->found_mdat &&
00305 (url_is_streamed(pb) || start_pos + a.size == url_fsize(pb)))
00306 return 0;
00307 left = a.size - url_ftell(pb) + start_pos;
00308 if (left > 0)
00309 url_fskip(pb, left);
00310 }
00311
00312 total_size += a.size;
00313 }
00314
00315 if (total_size < atom.size && atom.size < 0x7ffff)
00316 url_fskip(pb, atom.size - total_size);
00317
00318 return 0;
00319 }
00320
00321 static int mov_read_dref(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
00322 {
00323 AVStream *st;
00324 MOVStreamContext *sc;
00325 int entries, i, j;
00326
00327 if (c->fc->nb_streams < 1)
00328 return 0;
00329 st = c->fc->streams[c->fc->nb_streams-1];
00330 sc = st->priv_data;
00331
00332 get_be32(pb);
00333 entries = get_be32(pb);
00334 if (entries >= UINT_MAX / sizeof(*sc->drefs))
00335 return -1;
00336 sc->drefs = av_mallocz(entries * sizeof(*sc->drefs));
00337 if (!sc->drefs)
00338 return AVERROR(ENOMEM);
00339 sc->drefs_count = entries;
00340
00341 for (i = 0; i < sc->drefs_count; i++) {
00342 MOVDref *dref = &sc->drefs[i];
00343 uint32_t size = get_be32(pb);
00344 int64_t next = url_ftell(pb) + size - 4;
00345
00346 dref->type = get_le32(pb);
00347 get_be32(pb);
00348 dprintf(c->fc, "type %.4s size %d\n", (char*)&dref->type, size);
00349
00350 if (dref->type == MKTAG('a','l','i','s') && size > 150) {
00351
00352 uint16_t volume_len, len;
00353 int16_t type;
00354
00355 url_fskip(pb, 10);
00356
00357 volume_len = get_byte(pb);
00358 volume_len = FFMIN(volume_len, 27);
00359 get_buffer(pb, dref->volume, 27);
00360 dref->volume[volume_len] = 0;
00361 av_log(c->fc, AV_LOG_DEBUG, "volume %s, len %d\n", dref->volume, volume_len);
00362
00363 url_fskip(pb, 12);
00364
00365 len = get_byte(pb);
00366 len = FFMIN(len, 63);
00367 get_buffer(pb, dref->filename, 63);
00368 dref->filename[len] = 0;
00369 av_log(c->fc, AV_LOG_DEBUG, "filename %s, len %d\n", dref->filename, len);
00370
00371 url_fskip(pb, 16);
00372
00373
00374 dref->nlvl_from = get_be16(pb);
00375 dref->nlvl_to = get_be16(pb);
00376 av_log(c->fc, AV_LOG_DEBUG, "nlvl from %d, nlvl to %d\n",
00377 dref->nlvl_from, dref->nlvl_to);
00378
00379 url_fskip(pb, 16);
00380
00381 for (type = 0; type != -1 && url_ftell(pb) < next; ) {
00382 type = get_be16(pb);
00383 len = get_be16(pb);
00384 av_log(c->fc, AV_LOG_DEBUG, "type %d, len %d\n", type, len);
00385 if (len&1)
00386 len += 1;
00387 if (type == 2) {
00388 av_free(dref->path);
00389 dref->path = av_mallocz(len+1);
00390 if (!dref->path)
00391 return AVERROR(ENOMEM);
00392 get_buffer(pb, dref->path, len);
00393 if (len > volume_len && !strncmp(dref->path, dref->volume, volume_len)) {
00394 len -= volume_len;
00395 memmove(dref->path, dref->path+volume_len, len);
00396 dref->path[len] = 0;
00397 }
00398 for (j = 0; j < len; j++)
00399 if (dref->path[j] == ':')
00400 dref->path[j] = '/';
00401 av_log(c->fc, AV_LOG_DEBUG, "path %s\n", dref->path);
00402 } else if (type == 0) {
00403 av_free(dref->dir);
00404 dref->dir = av_malloc(len+1);
00405 if (!dref->dir)
00406 return AVERROR(ENOMEM);
00407 get_buffer(pb, dref->dir, len);
00408 dref->dir[len] = 0;
00409 for (j = 0; j < len; j++)
00410 if (dref->dir[j] == ':')
00411 dref->dir[j] = '/';
00412 av_log(c->fc, AV_LOG_DEBUG, "dir %s\n", dref->dir);
00413 } else
00414 url_fskip(pb, len);
00415 }
00416 }
00417 url_fseek(pb, next, SEEK_SET);
00418 }
00419 return 0;
00420 }
00421
00422 static int mov_read_hdlr(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
00423 {
00424 AVStream *st;
00425 uint32_t type;
00426 uint32_t ctype;
00427
00428 if (c->fc->nb_streams < 1)
00429 return 0;
00430
00431 st = c->fc->streams[c->fc->nb_streams-1];
00432
00433 get_byte(pb);
00434 get_be24(pb);
00435
00436
00437 ctype = get_le32(pb);
00438 type = get_le32(pb);
00439
00440 dprintf(c->fc, "ctype= %.4s (0x%08x)\n", (char*)&ctype, ctype);
00441 dprintf(c->fc, "stype= %.4s\n", (char*)&type);
00442
00443 if (type == MKTAG('v','i','d','e'))
00444 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00445 else if(type == MKTAG('s','o','u','n'))
00446 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00447 else if(type == MKTAG('m','1','a',' '))
00448 st->codec->codec_id = CODEC_ID_MP2;
00449 else if(type == MKTAG('s','u','b','p'))
00450 st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
00451
00452 get_be32(pb);
00453 get_be32(pb);
00454 get_be32(pb);
00455
00456 return 0;
00457 }
00458
00459 int ff_mp4_read_descr_len(ByteIOContext *pb)
00460 {
00461 int len = 0;
00462 int count = 4;
00463 while (count--) {
00464 int c = get_byte(pb);
00465 len = (len << 7) | (c & 0x7f);
00466 if (!(c & 0x80))
00467 break;
00468 }
00469 return len;
00470 }
00471
00472 static int mp4_read_descr(AVFormatContext *fc, ByteIOContext *pb, int *tag)
00473 {
00474 int len;
00475 *tag = get_byte(pb);
00476 len = ff_mp4_read_descr_len(pb);
00477 dprintf(fc, "MPEG4 description: tag=0x%02x len=%d\n", *tag, len);
00478 return len;
00479 }
00480
00481 #define MP4ESDescrTag 0x03
00482 #define MP4DecConfigDescrTag 0x04
00483 #define MP4DecSpecificDescrTag 0x05
00484
00485 static const AVCodecTag mp4_audio_types[] = {
00486 { CODEC_ID_MP3ON4, AOT_PS },
00487 { CODEC_ID_MP3ON4, AOT_L1 },
00488 { CODEC_ID_MP3ON4, AOT_L2 },
00489 { CODEC_ID_MP3ON4, AOT_L3 },
00490 { CODEC_ID_MP4ALS, AOT_ALS },
00491 { CODEC_ID_NONE, AOT_NULL },
00492 };
00493
00494 int ff_mov_read_esds(AVFormatContext *fc, ByteIOContext *pb, MOVAtom atom)
00495 {
00496 AVStream *st;
00497 int tag, len;
00498
00499 if (fc->nb_streams < 1)
00500 return 0;
00501 st = fc->streams[fc->nb_streams-1];
00502
00503 get_be32(pb);
00504 len = mp4_read_descr(fc, pb, &tag);
00505 if (tag == MP4ESDescrTag) {
00506 get_be16(pb);
00507 get_byte(pb);
00508 } else
00509 get_be16(pb);
00510
00511 len = mp4_read_descr(fc, pb, &tag);
00512 if (tag == MP4DecConfigDescrTag) {
00513 int object_type_id = get_byte(pb);
00514 get_byte(pb);
00515 get_be24(pb);
00516 get_be32(pb);
00517 get_be32(pb);
00518
00519 st->codec->codec_id= ff_codec_get_id(ff_mp4_obj_type, object_type_id);
00520 dprintf(fc, "esds object type id 0x%02x\n", object_type_id);
00521 len = mp4_read_descr(fc, pb, &tag);
00522 if (tag == MP4DecSpecificDescrTag) {
00523 dprintf(fc, "Specific MPEG4 header len=%d\n", len);
00524 if((uint64_t)len > (1<<30))
00525 return -1;
00526 st->codec->extradata = av_mallocz(len + FF_INPUT_BUFFER_PADDING_SIZE);
00527 if (!st->codec->extradata)
00528 return AVERROR(ENOMEM);
00529 get_buffer(pb, st->codec->extradata, len);
00530 st->codec->extradata_size = len;
00531 if (st->codec->codec_id == CODEC_ID_AAC) {
00532 MPEG4AudioConfig cfg;
00533 ff_mpeg4audio_get_config(&cfg, st->codec->extradata,
00534 st->codec->extradata_size);
00535 st->codec->channels = cfg.channels;
00536 if (cfg.object_type == 29 && cfg.sampling_index < 3)
00537 st->codec->sample_rate = ff_mpa_freq_tab[cfg.sampling_index];
00538 else
00539 st->codec->sample_rate = cfg.sample_rate;
00540 dprintf(fc, "mp4a config channels %d obj %d ext obj %d "
00541 "sample rate %d ext sample rate %d\n", st->codec->channels,
00542 cfg.object_type, cfg.ext_object_type,
00543 cfg.sample_rate, cfg.ext_sample_rate);
00544 if (!(st->codec->codec_id = ff_codec_get_id(mp4_audio_types,
00545 cfg.object_type)))
00546 st->codec->codec_id = CODEC_ID_AAC;
00547 }
00548 }
00549 }
00550 return 0;
00551 }
00552
00553 static int mov_read_esds(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
00554 {
00555 return ff_mov_read_esds(c->fc, pb, atom);
00556 }
00557
00558 static int mov_read_pasp(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
00559 {
00560 const int num = get_be32(pb);
00561 const int den = get_be32(pb);
00562 AVStream *st;
00563
00564 if (c->fc->nb_streams < 1)
00565 return 0;
00566 st = c->fc->streams[c->fc->nb_streams-1];
00567
00568 if (den != 0) {
00569 if ((st->sample_aspect_ratio.den != 1 || st->sample_aspect_ratio.num) &&
00570 (den != st->sample_aspect_ratio.den || num != st->sample_aspect_ratio.num))
00571 av_log(c->fc, AV_LOG_WARNING,
00572 "sample aspect ratio already set to %d:%d, overriding by 'pasp' atom\n",
00573 st->sample_aspect_ratio.num, st->sample_aspect_ratio.den);
00574 st->sample_aspect_ratio.num = num;
00575 st->sample_aspect_ratio.den = den;
00576 }
00577 return 0;
00578 }
00579
00580
00581 static int mov_read_mdat(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
00582 {
00583 if(atom.size == 0)
00584 return 0;
00585 c->found_mdat=1;
00586 return 0;
00587 }
00588
00589
00590 static int mov_read_ftyp(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
00591 {
00592 uint32_t minor_ver;
00593 int comp_brand_size;
00594 char minor_ver_str[11];
00595 char* comp_brands_str;
00596 uint8_t type[5] = {0};
00597
00598 get_buffer(pb, type, 4);
00599 if (strcmp(type, "qt "))
00600 c->isom = 1;
00601 av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type);
00602 av_metadata_set2(&c->fc->metadata, "major_brand", type, 0);
00603 minor_ver = get_be32(pb);
00604 snprintf(minor_ver_str, sizeof(minor_ver_str), "%d", minor_ver);
00605 av_metadata_set2(&c->fc->metadata, "minor_version", minor_ver_str, 0);
00606
00607 comp_brand_size = atom.size - 8;
00608 if (comp_brand_size < 0)
00609 return -1;
00610 comp_brands_str = av_malloc(comp_brand_size + 1);
00611 if (!comp_brands_str)
00612 return AVERROR(ENOMEM);
00613 get_buffer(pb, comp_brands_str, comp_brand_size);
00614 comp_brands_str[comp_brand_size] = 0;
00615 av_metadata_set2(&c->fc->metadata, "compatible_brands", comp_brands_str, 0);
00616 av_freep(&comp_brands_str);
00617
00618 return 0;
00619 }
00620
00621
00622 static int mov_read_moov(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
00623 {
00624 if (mov_read_default(c, pb, atom) < 0)
00625 return -1;
00626
00627
00628 c->found_moov=1;
00629 return 0;
00630 }
00631
00632 static int mov_read_moof(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
00633 {
00634 c->fragment.moof_offset = url_ftell(pb) - 8;
00635 dprintf(c->fc, "moof offset %llx\n", c->fragment.moof_offset);
00636 return mov_read_default(c, pb, atom);
00637 }
00638
00639 static int mov_read_mdhd(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
00640 {
00641 AVStream *st;
00642 MOVStreamContext *sc;
00643 int version;
00644 char language[4] = {0};
00645 unsigned lang;
00646
00647 if (c->fc->nb_streams < 1)
00648 return 0;
00649 st = c->fc->streams[c->fc->nb_streams-1];
00650 sc = st->priv_data;
00651
00652 version = get_byte(pb);
00653 if (version > 1)
00654 return -1;
00655
00656 get_be24(pb);
00657 if (version == 1) {
00658 get_be64(pb);
00659 get_be64(pb);
00660 } else {
00661 get_be32(pb);
00662 get_be32(pb);
00663 }
00664
00665 sc->time_scale = get_be32(pb);
00666 st->duration = (version == 1) ? get_be64(pb) : get_be32(pb);
00667
00668 lang = get_be16(pb);
00669 if (ff_mov_lang_to_iso639(lang, language))
00670 av_metadata_set2(&st->metadata, "language", language, 0);
00671 get_be16(pb);
00672
00673 return 0;
00674 }
00675
00676 static int mov_read_mvhd(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
00677 {
00678 int version = get_byte(pb);
00679 get_be24(pb);
00680
00681 if (version == 1) {
00682 get_be64(pb);
00683 get_be64(pb);
00684 } else {
00685 get_be32(pb);
00686 get_be32(pb);
00687 }
00688 c->time_scale = get_be32(pb);
00689
00690 dprintf(c->fc, "time scale = %i\n", c->time_scale);
00691
00692 c->duration = (version == 1) ? get_be64(pb) : get_be32(pb);
00693 get_be32(pb);
00694
00695 get_be16(pb);
00696
00697 url_fskip(pb, 10);
00698
00699 url_fskip(pb, 36);
00700
00701 get_be32(pb);
00702 get_be32(pb);
00703 get_be32(pb);
00704 get_be32(pb);
00705 get_be32(pb);
00706 get_be32(pb);
00707 get_be32(pb);
00708
00709 return 0;
00710 }
00711
00712 static int mov_read_smi(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
00713 {
00714 AVStream *st;
00715
00716 if (c->fc->nb_streams < 1)
00717 return 0;
00718 st = c->fc->streams[c->fc->nb_streams-1];
00719
00720 if((uint64_t)atom.size > (1<<30))
00721 return -1;
00722
00723
00724
00725 av_free(st->codec->extradata);
00726 st->codec->extradata = av_mallocz(atom.size + 0x5a + FF_INPUT_BUFFER_PADDING_SIZE);
00727 if (!st->codec->extradata)
00728 return AVERROR(ENOMEM);
00729 st->codec->extradata_size = 0x5a + atom.size;
00730 memcpy(st->codec->extradata, "SVQ3", 4);
00731 get_buffer(pb, st->codec->extradata + 0x5a, atom.size);
00732 dprintf(c->fc, "Reading SMI %"PRId64" %s\n", atom.size, st->codec->extradata + 0x5a);
00733 return 0;
00734 }
00735
00736 static int mov_read_enda(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
00737 {
00738 AVStream *st;
00739 int little_endian;
00740
00741 if (c->fc->nb_streams < 1)
00742 return 0;
00743 st = c->fc->streams[c->fc->nb_streams-1];
00744
00745 little_endian = get_be16(pb);
00746 dprintf(c->fc, "enda %d\n", little_endian);
00747 if (little_endian == 1) {
00748 switch (st->codec->codec_id) {
00749 case CODEC_ID_PCM_S24BE:
00750 st->codec->codec_id = CODEC_ID_PCM_S24LE;
00751 break;
00752 case CODEC_ID_PCM_S32BE:
00753 st->codec->codec_id = CODEC_ID_PCM_S32LE;
00754 break;
00755 case CODEC_ID_PCM_F32BE:
00756 st->codec->codec_id = CODEC_ID_PCM_F32LE;
00757 break;
00758 case CODEC_ID_PCM_F64BE:
00759 st->codec->codec_id = CODEC_ID_PCM_F64LE;
00760 break;
00761 default:
00762 break;
00763 }
00764 }
00765 return 0;
00766 }
00767
00768
00769 static int mov_read_extradata(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
00770 {
00771 AVStream *st;
00772 uint64_t size;
00773 uint8_t *buf;
00774
00775 if (c->fc->nb_streams < 1)
00776 return 0;
00777 st= c->fc->streams[c->fc->nb_streams-1];
00778 size= (uint64_t)st->codec->extradata_size + atom.size + 8 + FF_INPUT_BUFFER_PADDING_SIZE;
00779 if(size > INT_MAX || (uint64_t)atom.size > INT_MAX)
00780 return -1;
00781 buf= av_realloc(st->codec->extradata, size);
00782 if(!buf)
00783 return -1;
00784 st->codec->extradata= buf;
00785 buf+= st->codec->extradata_size;
00786 st->codec->extradata_size= size - FF_INPUT_BUFFER_PADDING_SIZE;
00787 AV_WB32( buf , atom.size + 8);
00788 AV_WL32( buf + 4, atom.type);
00789 get_buffer(pb, buf + 8, atom.size);
00790 return 0;
00791 }
00792
00793 static int mov_read_wave(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
00794 {
00795 AVStream *st;
00796
00797 if (c->fc->nb_streams < 1)
00798 return 0;
00799 st = c->fc->streams[c->fc->nb_streams-1];
00800
00801 if((uint64_t)atom.size > (1<<30))
00802 return -1;
00803
00804 if (st->codec->codec_id == CODEC_ID_QDM2) {
00805
00806 av_free(st->codec->extradata);
00807 st->codec->extradata = av_mallocz(atom.size + FF_INPUT_BUFFER_PADDING_SIZE);
00808 if (!st->codec->extradata)
00809 return AVERROR(ENOMEM);
00810 st->codec->extradata_size = atom.size;
00811 get_buffer(pb, st->codec->extradata, atom.size);
00812 } else if (atom.size > 8) {
00813 if (mov_read_default(c, pb, atom) < 0)
00814 return -1;
00815 } else
00816 url_fskip(pb, atom.size);
00817 return 0;
00818 }
00819
00824 static int mov_read_glbl(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
00825 {
00826 AVStream *st;
00827
00828 if (c->fc->nb_streams < 1)
00829 return 0;
00830 st = c->fc->streams[c->fc->nb_streams-1];
00831
00832 if((uint64_t)atom.size > (1<<30))
00833 return -1;
00834
00835 av_free(st->codec->extradata);
00836 st->codec->extradata = av_mallocz(atom.size + FF_INPUT_BUFFER_PADDING_SIZE);
00837 if (!st->codec->extradata)
00838 return AVERROR(ENOMEM);
00839 st->codec->extradata_size = atom.size;
00840 get_buffer(pb, st->codec->extradata, atom.size);
00841 return 0;
00842 }
00843
00849 static int mov_read_strf(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
00850 {
00851 AVStream *st;
00852
00853 if (c->fc->nb_streams < 1)
00854 return 0;
00855 if (atom.size <= 40)
00856 return 0;
00857 st = c->fc->streams[c->fc->nb_streams-1];
00858
00859 if((uint64_t)atom.size > (1<<30))
00860 return -1;
00861
00862 av_free(st->codec->extradata);
00863 st->codec->extradata = av_mallocz(atom.size - 40 + FF_INPUT_BUFFER_PADDING_SIZE);
00864 if (!st->codec->extradata)
00865 return AVERROR(ENOMEM);
00866 st->codec->extradata_size = atom.size - 40;
00867 url_fskip(pb, 40);
00868 get_buffer(pb, st->codec->extradata, atom.size - 40);
00869 return 0;
00870 }
00871
00872 static int mov_read_stco(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
00873 {
00874 AVStream *st;
00875 MOVStreamContext *sc;
00876 unsigned int i, entries;
00877
00878 if (c->fc->nb_streams < 1)
00879 return 0;
00880 st = c->fc->streams[c->fc->nb_streams-1];
00881 sc = st->priv_data;
00882
00883 get_byte(pb);
00884 get_be24(pb);
00885
00886 entries = get_be32(pb);
00887
00888 if(entries >= UINT_MAX/sizeof(int64_t))
00889 return -1;
00890
00891 sc->chunk_offsets = av_malloc(entries * sizeof(int64_t));
00892 if (!sc->chunk_offsets)
00893 return AVERROR(ENOMEM);
00894 sc->chunk_count = entries;
00895
00896 if (atom.type == MKTAG('s','t','c','o'))
00897 for(i=0; i<entries; i++)
00898 sc->chunk_offsets[i] = get_be32(pb);
00899 else if (atom.type == MKTAG('c','o','6','4'))
00900 for(i=0; i<entries; i++)
00901 sc->chunk_offsets[i] = get_be64(pb);
00902 else
00903 return -1;
00904
00905 return 0;
00906 }
00907
00912 enum CodecID ff_mov_get_lpcm_codec_id(int bps, int flags)
00913 {
00914 if (flags & 1) {
00915 if (flags & 2) {
00916 if (bps == 32) return CODEC_ID_PCM_F32BE;
00917 else if (bps == 64) return CODEC_ID_PCM_F64BE;
00918 } else {
00919 if (bps == 32) return CODEC_ID_PCM_F32LE;
00920 else if (bps == 64) return CODEC_ID_PCM_F64LE;
00921 }
00922 } else {
00923 if (flags & 2) {
00924 if (bps == 8)
00925
00926 if (flags & 4) return CODEC_ID_PCM_S8;
00927 else return CODEC_ID_PCM_U8;
00928 else if (bps == 16) return CODEC_ID_PCM_S16BE;
00929 else if (bps == 24) return CODEC_ID_PCM_S24BE;
00930 else if (bps == 32) return CODEC_ID_PCM_S32BE;
00931 } else {
00932 if (bps == 8)
00933 if (flags & 4) return CODEC_ID_PCM_S8;
00934 else return CODEC_ID_PCM_U8;
00935 else if (bps == 16) return CODEC_ID_PCM_S16LE;
00936 else if (bps == 24) return CODEC_ID_PCM_S24LE;
00937 else if (bps == 32) return CODEC_ID_PCM_S32LE;
00938 }
00939 }
00940 return CODEC_ID_NONE;
00941 }
00942
00943 static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
00944 {
00945 AVStream *st;
00946 MOVStreamContext *sc;
00947 int j, entries, pseudo_stream_id;
00948
00949 if (c->fc->nb_streams < 1)
00950 return 0;
00951 st = c->fc->streams[c->fc->nb_streams-1];
00952 sc = st->priv_data;
00953
00954 get_byte(pb);
00955 get_be24(pb);
00956
00957 entries = get_be32(pb);
00958
00959 for(pseudo_stream_id=0; pseudo_stream_id<entries; pseudo_stream_id++) {
00960
00961 enum CodecID id;
00962 int dref_id = 1;
00963 MOVAtom a = { 0 };
00964 int64_t start_pos = url_ftell(pb);
00965 int size = get_be32(pb);
00966 uint32_t format = get_le32(pb);
00967
00968 if (size >= 16) {
00969 get_be32(pb);
00970 get_be16(pb);
00971 dref_id = get_be16(pb);
00972 }
00973
00974 if (st->codec->codec_tag &&
00975 st->codec->codec_tag != format &&
00976 (c->fc->video_codec_id ? ff_codec_get_id(codec_movvideo_tags, format) != c->fc->video_codec_id
00977 : st->codec->codec_tag != MKTAG('j','p','e','g'))
00978 ){
00979
00980
00981
00982 av_log(c->fc, AV_LOG_WARNING, "multiple fourcc not supported\n");
00983 url_fskip(pb, size - (url_ftell(pb) - start_pos));
00984 continue;
00985 }
00986 sc->pseudo_stream_id = st->codec->codec_tag ? -1 : pseudo_stream_id;
00987 sc->dref_id= dref_id;
00988
00989 st->codec->codec_tag = format;
00990 id = ff_codec_get_id(codec_movaudio_tags, format);
00991 if (id<=0 && ((format&0xFFFF) == 'm'+('s'<<8) || (format&0xFFFF) == 'T'+('S'<<8)))
00992 id = ff_codec_get_id(ff_codec_wav_tags, bswap_32(format)&0xFFFF);
00993
00994 if (st->codec->codec_type != AVMEDIA_TYPE_VIDEO && id > 0) {
00995 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00996 } else if (st->codec->codec_type != AVMEDIA_TYPE_AUDIO &&
00997 format && format != MKTAG('m','p','4','s')) {
00998 id = ff_codec_get_id(codec_movvideo_tags, format);
00999 if (id <= 0)
01000 id = ff_codec_get_id(ff_codec_bmp_tags, format);
01001 if (id > 0)
01002 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
01003 else if(st->codec->codec_type == AVMEDIA_TYPE_DATA){
01004 id = ff_codec_get_id(ff_codec_movsubtitle_tags, format);
01005 if(id > 0)
01006 st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
01007 }
01008 }
01009
01010 dprintf(c->fc, "size=%d 4CC= %c%c%c%c codec_type=%d\n", size,
01011 (format >> 0) & 0xff, (format >> 8) & 0xff, (format >> 16) & 0xff,
01012 (format >> 24) & 0xff, st->codec->codec_type);
01013
01014 if(st->codec->codec_type==AVMEDIA_TYPE_VIDEO) {
01015 unsigned int color_depth, len;
01016 int color_greyscale;
01017
01018 st->codec->codec_id = id;
01019 get_be16(pb);
01020 get_be16(pb);
01021 get_be32(pb);
01022 get_be32(pb);
01023 get_be32(pb);
01024
01025 st->codec->width = get_be16(pb);
01026 st->codec->height = get_be16(pb);
01027
01028 get_be32(pb);
01029 get_be32(pb);
01030 get_be32(pb);
01031 get_be16(pb);
01032
01033 len = get_byte(pb);
01034 if (len > 31)
01035 len = 31;
01036 mov_read_mac_string(c, pb, len, st->codec->codec_name, 32);
01037 if (len < 31)
01038 url_fskip(pb, 31 - len);
01039
01040 if (!memcmp(st->codec->codec_name, "Planar Y'CbCr 8-bit 4:2:0", 25))
01041 st->codec->codec_tag=MKTAG('I', '4', '2', '0');
01042
01043 st->codec->bits_per_coded_sample = get_be16(pb);
01044 st->codec->color_table_id = get_be16(pb);
01045 dprintf(c->fc, "depth %d, ctab id %d\n",
01046 st->codec->bits_per_coded_sample, st->codec->color_table_id);
01047
01048 color_depth = st->codec->bits_per_coded_sample & 0x1F;
01049 color_greyscale = st->codec->bits_per_coded_sample & 0x20;
01050
01051
01052 if ((color_depth == 2) || (color_depth == 4) ||
01053 (color_depth == 8)) {
01054
01055 unsigned int color_start, color_count, color_end;
01056 unsigned char r, g, b;
01057
01058 st->codec->palctrl = av_malloc(sizeof(*st->codec->palctrl));
01059 if (color_greyscale) {
01060 int color_index, color_dec;
01061
01062 st->codec->bits_per_coded_sample = color_depth;
01063 color_count = 1 << color_depth;
01064 color_index = 255;
01065 color_dec = 256 / (color_count - 1);
01066 for (j = 0; j < color_count; j++) {
01067 r = g = b = color_index;
01068 st->codec->palctrl->palette[j] =
01069 (r << 16) | (g << 8) | (b);
01070 color_index -= color_dec;
01071 if (color_index < 0)
01072 color_index = 0;
01073 }
01074 } else if (st->codec->color_table_id) {
01075 const uint8_t *color_table;
01076
01077 color_count = 1 << color_depth;
01078 if (color_depth == 2)
01079 color_table = ff_qt_default_palette_4;
01080 else if (color_depth == 4)
01081 color_table = ff_qt_default_palette_16;
01082 else
01083 color_table = ff_qt_default_palette_256;
01084
01085 for (j = 0; j < color_count; j++) {
01086 r = color_table[j * 3 + 0];
01087 g = color_table[j * 3 + 1];
01088 b = color_table[j * 3 + 2];
01089 st->codec->palctrl->palette[j] =
01090 (r << 16) | (g << 8) | (b);
01091 }
01092 } else {
01093
01094 color_start = get_be32(pb);
01095 color_count = get_be16(pb);
01096 color_end = get_be16(pb);
01097 if ((color_start <= 255) &&
01098 (color_end <= 255)) {
01099 for (j = color_start; j <= color_end; j++) {
01100
01101
01102
01103 get_byte(pb);
01104 get_byte(pb);
01105 r = get_byte(pb);
01106 get_byte(pb);
01107 g = get_byte(pb);
01108 get_byte(pb);
01109 b = get_byte(pb);
01110 get_byte(pb);
01111 st->codec->palctrl->palette[j] =
01112 (r << 16) | (g << 8) | (b);
01113 }
01114 }
01115 }
01116 st->codec->palctrl->palette_changed = 1;
01117 }
01118 } else if(st->codec->codec_type==AVMEDIA_TYPE_AUDIO) {
01119 int bits_per_sample, flags;
01120 uint16_t version = get_be16(pb);
01121
01122 st->codec->codec_id = id;
01123 get_be16(pb);
01124 get_be32(pb);
01125
01126 st->codec->channels = get_be16(pb);
01127 dprintf(c->fc, "audio channels %d\n", st->codec->channels);
01128 st->codec->bits_per_coded_sample = get_be16(pb);
01129
01130 sc->audio_cid = get_be16(pb);
01131 get_be16(pb);
01132
01133 st->codec->sample_rate = ((get_be32(pb) >> 16));
01134
01135
01136 dprintf(c->fc, "version =%d, isom =%d\n",version,c->isom);
01137 if(!c->isom) {
01138 if(version==1) {
01139 sc->samples_per_frame = get_be32(pb);
01140 get_be32(pb);
01141 sc->bytes_per_frame = get_be32(pb);
01142 get_be32(pb);
01143 } else if(version==2) {
01144 get_be32(pb);
01145 st->codec->sample_rate = av_int2dbl(get_be64(pb));
01146 st->codec->channels = get_be32(pb);
01147 get_be32(pb);
01148 st->codec->bits_per_coded_sample = get_be32(pb);
01149 flags = get_be32(pb);
01150 sc->bytes_per_frame = get_be32(pb);
01151 sc->samples_per_frame = get_be32(pb);
01152 if (format == MKTAG('l','p','c','m'))
01153 st->codec->codec_id = ff_mov_get_lpcm_codec_id(st->codec->bits_per_coded_sample, flags);
01154 }
01155 }
01156
01157 switch (st->codec->codec_id) {
01158 case CODEC_ID_PCM_S8:
01159 case CODEC_ID_PCM_U8:
01160 if (st->codec->bits_per_coded_sample == 16)
01161 st->codec->codec_id = CODEC_ID_PCM_S16BE;
01162 break;
01163 case CODEC_ID_PCM_S16LE:
01164 case CODEC_ID_PCM_S16BE:
01165 if (st->codec->bits_per_coded_sample == 8)
01166 st->codec->codec_id = CODEC_ID_PCM_S8;
01167 else if (st->codec->bits_per_coded_sample == 24)
01168 st->codec->codec_id =
01169 st->codec->codec_id == CODEC_ID_PCM_S16BE ?
01170 CODEC_ID_PCM_S24BE : CODEC_ID_PCM_S24LE;
01171 break;
01172
01173 case CODEC_ID_MACE3:
01174 sc->samples_per_frame = 6;
01175 sc->bytes_per_frame = 2*st->codec->channels;
01176 break;
01177 case CODEC_ID_MACE6:
01178 sc->samples_per_frame = 6;
01179 sc->bytes_per_frame = 1*st->codec->channels;
01180 break;
01181 case CODEC_ID_ADPCM_IMA_QT:
01182 sc->samples_per_frame = 64;
01183 sc->bytes_per_frame = 34*st->codec->channels;
01184 break;
01185 case CODEC_ID_GSM:
01186 sc->samples_per_frame = 160;
01187 sc->bytes_per_frame = 33;
01188 break;
01189 default:
01190 break;
01191 }
01192
01193 bits_per_sample = av_get_bits_per_sample(st->codec->codec_id);
01194 if (bits_per_sample) {
01195 st->codec->bits_per_coded_sample = bits_per_sample;
01196 sc->sample_size = (bits_per_sample >> 3) * st->codec->channels;
01197 }
01198 } else if(st->codec->codec_type==AVMEDIA_TYPE_SUBTITLE){
01199
01200
01201 MOVAtom fake_atom = { .size = size - (url_ftell(pb) - start_pos) };
01202 if (format != AV_RL32("mp4s"))
01203 mov_read_glbl(c, pb, fake_atom);
01204 st->codec->codec_id= id;
01205 st->codec->width = sc->width;
01206 st->codec->height = sc->height;
01207 } else {
01208
01209 url_fskip(pb, size - (url_ftell(pb) - start_pos));
01210 }
01211
01212 a.size = size - (url_ftell(pb) - start_pos);
01213 if (a.size > 8) {
01214 if (mov_read_default(c, pb, a) < 0)
01215 return -1;
01216 } else if (a.size > 0)
01217 url_fskip(pb, a.size);
01218 }
01219
01220 if(st->codec->codec_type==AVMEDIA_TYPE_AUDIO && st->codec->sample_rate==0 && sc->time_scale>1)
01221 st->codec->sample_rate= sc->time_scale;
01222
01223
01224 switch (st->codec->codec_id) {
01225 #if CONFIG_DV_DEMUXER
01226 case CODEC_ID_DVAUDIO:
01227 c->dv_fctx = avformat_alloc_context();
01228 c->dv_demux = dv_init_demux(c->dv_fctx);
01229 if (!c->dv_demux) {
01230 av_log(c->fc, AV_LOG_ERROR, "dv demux context init error\n");
01231 return -1;
01232 }
01233 sc->dv_audio_container = 1;
01234 st->codec->codec_id = CODEC_ID_PCM_S16LE;
01235 break;
01236 #endif
01237
01238 case CODEC_ID_QCELP:
01239
01240 if (st->codec->codec_tag != MKTAG('Q','c','l','p'))
01241 st->codec->sample_rate = 8000;
01242 st->codec->frame_size= 160;
01243 st->codec->channels= 1;
01244 break;
01245 case CODEC_ID_AMR_NB:
01246 case CODEC_ID_AMR_WB:
01247 st->codec->frame_size= sc->samples_per_frame;
01248 st->codec->channels= 1;
01249
01250 if (st->codec->codec_id == CODEC_ID_AMR_NB)
01251 st->codec->sample_rate = 8000;
01252 else if (st->codec->codec_id == CODEC_ID_AMR_WB)
01253 st->codec->sample_rate = 16000;
01254 break;
01255 case CODEC_ID_MP2:
01256 case CODEC_ID_MP3:
01257 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
01258 st->need_parsing = AVSTREAM_PARSE_FULL;
01259 break;
01260 case CODEC_ID_GSM:
01261 case CODEC_ID_ADPCM_MS:
01262 case CODEC_ID_ADPCM_IMA_WAV:
01263 st->codec->block_align = sc->bytes_per_frame;
01264 break;
01265 case CODEC_ID_ALAC:
01266 if (st->codec->extradata_size == 36) {
01267 st->codec->frame_size = AV_RB32(st->codec->extradata+12);
01268 st->codec->channels = AV_RB8 (st->codec->extradata+21);
01269 }
01270 break;
01271 default:
01272 break;
01273 }
01274
01275 return 0;
01276 }
01277
01278 static int mov_read_stsc(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
01279 {
01280 AVStream *st;
01281 MOVStreamContext *sc;
01282 unsigned int i, entries;
01283
01284 if (c->fc->nb_streams < 1)
01285 return 0;
01286 st = c->fc->streams[c->fc->nb_streams-1];
01287 sc = st->priv_data;
01288
01289 get_byte(pb);
01290 get_be24(pb);
01291
01292 entries = get_be32(pb);
01293
01294 dprintf(c->fc, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
01295
01296 if(entries >= UINT_MAX / sizeof(*sc->stsc_data))
01297 return -1;
01298 sc->stsc_data = av_malloc(entries * sizeof(*sc->stsc_data));
01299 if (!sc->stsc_data)
01300 return AVERROR(ENOMEM);
01301 sc->stsc_count = entries;
01302
01303 for(i=0; i<entries; i++) {
01304 sc->stsc_data[i].first = get_be32(pb);
01305 sc->stsc_data[i].count = get_be32(pb);
01306 sc->stsc_data[i].id = get_be32(pb);
01307 }
01308 return 0;
01309 }
01310
01311 static int mov_read_stps(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
01312 {
01313 AVStream *st;
01314 MOVStreamContext *sc;
01315 unsigned i, entries;
01316
01317 if (c->fc->nb_streams < 1)
01318 return 0;
01319 st = c->fc->streams[c->fc->nb_streams-1];
01320 sc = st->priv_data;
01321
01322 get_be32(pb);
01323
01324 entries = get_be32(pb);
01325 if (entries >= UINT_MAX / sizeof(*sc->stps_data))
01326 return -1;
01327 sc->stps_data = av_malloc(entries * sizeof(*sc->stps_data));
01328 if (!sc->stps_data)
01329 return AVERROR(ENOMEM);
01330 sc->stps_count = entries;
01331
01332 for (i = 0; i < entries; i++) {
01333 sc->stps_data[i] = get_be32(pb);
01334
01335 }
01336
01337 return 0;
01338 }
01339
01340 static int mov_read_stss(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
01341 {
01342 AVStream *st;
01343 MOVStreamContext *sc;
01344 unsigned int i, entries;
01345
01346 if (c->fc->nb_streams < 1)
01347 return 0;
01348 st = c->fc->streams[c->fc->nb_streams-1];
01349 sc = st->priv_data;
01350
01351 get_byte(pb);
01352 get_be24(pb);
01353
01354 entries = get_be32(pb);
01355
01356 dprintf(c->fc, "keyframe_count = %d\n", entries);
01357
01358 if(entries >= UINT_MAX / sizeof(int))
01359 return -1;
01360 sc->keyframes = av_malloc(entries * sizeof(int));
01361 if (!sc->keyframes)
01362 return AVERROR(ENOMEM);
01363 sc->keyframe_count = entries;
01364
01365 for(i=0; i<entries; i++) {
01366 sc->keyframes[i] = get_be32(pb);
01367
01368 }
01369 return 0;
01370 }
01371
01372 static int mov_read_stsz(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
01373 {
01374 AVStream *st;
01375 MOVStreamContext *sc;
01376 unsigned int i, entries, sample_size, field_size, num_bytes;
01377 GetBitContext gb;
01378 unsigned char* buf;
01379
01380 if (c->fc->nb_streams < 1)
01381 return 0;
01382 st = c->fc->streams[c->fc->nb_streams-1];
01383 sc = st->priv_data;
01384
01385 get_byte(pb);
01386 get_be24(pb);
01387
01388 if (atom.type == MKTAG('s','t','s','z')) {
01389 sample_size = get_be32(pb);
01390 if (!sc->sample_size)
01391 sc->sample_size = sample_size;
01392 field_size = 32;
01393 } else {
01394 sample_size = 0;
01395 get_be24(pb);
01396 field_size = get_byte(pb);
01397 }
01398 entries = get_be32(pb);
01399
01400 dprintf(c->fc, "sample_size = %d sample_count = %d\n", sc->sample_size, entries);
01401
01402 sc->sample_count = entries;
01403 if (sample_size)
01404 return 0;
01405
01406 if (field_size != 4 && field_size != 8 && field_size != 16 && field_size != 32) {
01407 av_log(c->fc, AV_LOG_ERROR, "Invalid sample field size %d\n", field_size);
01408 return -1;
01409 }
01410
01411 if (entries >= UINT_MAX / sizeof(int) || entries >= (UINT_MAX - 4) / field_size)
01412 return -1;
01413 sc->sample_sizes = av_malloc(entries * sizeof(int));
01414 if (!sc->sample_sizes)
01415 return AVERROR(ENOMEM);
01416
01417 num_bytes = (entries*field_size+4)>>3;
01418
01419 buf = av_malloc(num_bytes+FF_INPUT_BUFFER_PADDING_SIZE);
01420 if (!buf) {
01421 av_freep(&sc->sample_sizes);
01422 return AVERROR(ENOMEM);
01423 }
01424
01425 if (get_buffer(pb, buf, num_bytes) < num_bytes) {
01426 av_freep(&sc->sample_sizes);
01427 av_free(buf);
01428 return -1;
01429 }
01430
01431 init_get_bits(&gb, buf, 8*num_bytes);
01432
01433 for(i=0; i<entries; i++)
01434 sc->sample_sizes[i] = get_bits_long(&gb, field_size);
01435
01436 av_free(buf);
01437 return 0;
01438 }
01439
01440 static int mov_read_stts(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
01441 {
01442 AVStream *st;
01443 MOVStreamContext *sc;
01444 unsigned int i, entries;
01445 int64_t duration=0;
01446 int64_t total_sample_count=0;
01447
01448 if (c->fc->nb_streams < 1)
01449 return 0;
01450 st = c->fc->streams[c->fc->nb_streams-1];
01451 sc = st->priv_data;
01452
01453 get_byte(pb);
01454 get_be24(pb);
01455 entries = get_be32(pb);
01456
01457 dprintf(c->fc, "track[%i].stts.entries = %i\n", c->fc->nb_streams-1, entries);
01458
01459 if(entries >= UINT_MAX / sizeof(*sc->stts_data))
01460 return -1;
01461 sc->stts_data = av_malloc(entries * sizeof(*sc->stts_data));
01462 if (!sc->stts_data)
01463 return AVERROR(ENOMEM);
01464 sc->stts_count = entries;
01465
01466 for(i=0; i<entries; i++) {
01467 int sample_duration;
01468 int sample_count;
01469
01470 sample_count=get_be32(pb);
01471 sample_duration = get_be32(pb);
01472 sc->stts_data[i].count= sample_count;
01473 sc->stts_data[i].duration= sample_duration;
01474
01475 dprintf(c->fc, "sample_count=%d, sample_duration=%d\n",sample_count,sample_duration);
01476
01477 duration+=(int64_t)sample_duration*sample_count;
01478 total_sample_count+=sample_count;
01479 }
01480
01481 st->nb_frames= total_sample_count;
01482 if(duration)
01483 st->duration= duration;
01484 return 0;
01485 }
01486
01487 static int mov_read_ctts(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
01488 {
01489 AVStream *st;
01490 MOVStreamContext *sc;
01491 unsigned int i, entries;
01492
01493 if (c->fc->nb_streams < 1)
01494 return 0;
01495 st = c->fc->streams[c->fc->nb_streams-1];
01496 sc = st->priv_data;
01497
01498 get_byte(pb);
01499 get_be24(pb);
01500 entries = get_be32(pb);
01501
01502 dprintf(c->fc, "track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries);
01503
01504 if(entries >= UINT_MAX / sizeof(*sc->ctts_data))
01505 return -1;
01506 sc->ctts_data = av_malloc(entries * sizeof(*sc->ctts_data));
01507 if (!sc->ctts_data)
01508 return AVERROR(ENOMEM);
01509 sc->ctts_count = entries;
01510
01511 for(i=0; i<entries; i++) {
01512 int count =get_be32(pb);
01513 int duration =get_be32(pb);
01514
01515 sc->ctts_data[i].count = count;
01516 sc->ctts_data[i].duration= duration;
01517 if (duration < 0)
01518 sc->dts_shift = FFMAX(sc->dts_shift, -duration);
01519 }
01520
01521 dprintf(c->fc, "dts shift %d\n", sc->dts_shift);
01522
01523 return 0;
01524 }
01525
01526 static void mov_build_index(MOVContext *mov, AVStream *st)
01527 {
01528 MOVStreamContext *sc = st->priv_data;
01529 int64_t current_offset;
01530 int64_t current_dts = 0;
01531 unsigned int stts_index = 0;
01532 unsigned int stsc_index = 0;
01533 unsigned int stss_index = 0;
01534 unsigned int stps_index = 0;
01535 unsigned int i, j;
01536 uint64_t stream_size = 0;
01537
01538
01539 if (sc->time_offset) {
01540 int rescaled = sc->time_offset < 0 ? av_rescale(sc->time_offset, sc->time_scale, mov->time_scale) : sc->time_offset;
01541 current_dts = -rescaled;
01542 if (sc->ctts_data && sc->ctts_data[0].duration / sc->stts_data[0].duration > 16) {
01543
01544
01545 sc->wrong_dts = 1;
01546 st->codec->has_b_frames = 1;
01547 }
01548 }
01549
01550
01551 if (!(st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
01552 sc->stts_count == 1 && sc->stts_data[0].duration == 1)) {
01553 unsigned int current_sample = 0;
01554 unsigned int stts_sample = 0;
01555 unsigned int sample_size;
01556 unsigned int distance = 0;
01557 int key_off = sc->keyframes && sc->keyframes[0] == 1;
01558
01559 current_dts -= sc->dts_shift;
01560
01561 if (sc->sample_count >= UINT_MAX / sizeof(*st->index_entries))
01562 return;
01563 st->index_entries = av_malloc(sc->sample_count*sizeof(*st->index_entries));
01564 if (!st->index_entries)
01565 return;
01566 st->index_entries_allocated_size = sc->sample_count*sizeof(*st->index_entries);
01567
01568 for (i = 0; i < sc->chunk_count; i++) {
01569 current_offset = sc->chunk_offsets[i];
01570 if (stsc_index + 1 < sc->stsc_count &&
01571 i + 1 == sc->stsc_data[stsc_index + 1].first)
01572 stsc_index++;
01573 for (j = 0; j < sc->stsc_data[stsc_index].count; j++) {
01574 int keyframe = 0;
01575 if (current_sample >= sc->sample_count) {
01576 av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n");
01577 return;
01578 }
01579
01580 if (!sc->keyframe_count || current_sample+key_off == sc->keyframes[stss_index]) {
01581 keyframe = 1;
01582 if (stss_index + 1 < sc->keyframe_count)
01583 stss_index++;
01584 } else if (sc->stps_count && current_sample+key_off == sc->stps_data[stps_index]) {
01585 keyframe = 1;
01586 if (stps_index + 1 < sc->stps_count)
01587 stps_index++;
01588 }
01589 if (keyframe)
01590 distance = 0;
01591 sample_size = sc->sample_size > 0 ? sc->sample_size : sc->sample_sizes[current_sample];
01592 if(sc->pseudo_stream_id == -1 ||
01593 sc->stsc_data[stsc_index].id - 1 == sc->pseudo_stream_id) {
01594 AVIndexEntry *e = &st->index_entries[st->nb_index_entries++];
01595 e->pos = current_offset;
01596 e->timestamp = current_dts;
01597 e->size = sample_size;
01598 e->min_distance = distance;
01599 e->flags = keyframe ? AVINDEX_KEYFRAME : 0;
01600 dprintf(mov->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
01601 "size %d, distance %d, keyframe %d\n", st->index, current_sample,
01602 current_offset, current_dts, sample_size, distance, keyframe);
01603 }
01604
01605 current_offset += sample_size;
01606 stream_size += sample_size;
01607 current_dts += sc->stts_data[stts_index].duration;
01608 distance++;
01609 stts_sample++;
01610 current_sample++;
01611 if (stts_index + 1 < sc->stts_count && stts_sample == sc->stts_data[stts_index].count) {
01612 stts_sample = 0;
01613 stts_index++;
01614 }
01615 }
01616 }
01617 if (st->duration > 0)
01618 st->codec->bit_rate = stream_size*8*sc->time_scale/st->duration;
01619 } else {
01620 unsigned chunk_samples, total = 0;
01621
01622
01623 for (i = 0; i < sc->stsc_count; i++) {
01624 unsigned count, chunk_count;
01625
01626 chunk_samples = sc->stsc_data[i].count;
01627 if (sc->samples_per_frame && chunk_samples % sc->samples_per_frame) {
01628 av_log(mov->fc, AV_LOG_ERROR, "error unaligned chunk\n");
01629 return;
01630 }
01631
01632 if (sc->samples_per_frame >= 160) {
01633 count = chunk_samples / sc->samples_per_frame;
01634 } else if (sc->samples_per_frame > 1) {
01635 unsigned samples = (1024/sc->samples_per_frame)*sc->samples_per_frame;
01636 count = (chunk_samples+samples-1) / samples;
01637 } else {
01638 count = (chunk_samples+1023) / 1024;
01639 }
01640
01641 if (i < sc->stsc_count - 1)
01642 chunk_count = sc->stsc_data[i+1].first - sc->stsc_data[i].first;
01643 else
01644 chunk_count = sc->chunk_count - (sc->stsc_data[i].first - 1);
01645 total += chunk_count * count;
01646 }
01647
01648 dprintf(mov->fc, "chunk count %d\n", total);
01649 if (total >= UINT_MAX / sizeof(*st->index_entries))
01650 return;
01651 st->index_entries = av_malloc(total*sizeof(*st->index_entries));
01652 if (!st->index_entries)
01653 return;
01654 st->index_entries_allocated_size = total*sizeof(*st->index_entries);
01655
01656
01657 for (i = 0; i < sc->chunk_count; i++) {
01658 current_offset = sc->chunk_offsets[i];
01659 if (stsc_index + 1 < sc->stsc_count &&
01660 i + 1 == sc->stsc_data[stsc_index + 1].first)
01661 stsc_index++;
01662 chunk_samples = sc->stsc_data[stsc_index].count;
01663
01664 while (chunk_samples > 0) {
01665 AVIndexEntry *e;
01666 unsigned size, samples;
01667
01668 if (sc->samples_per_frame >= 160) {
01669 samples = sc->samples_per_frame;
01670 size = sc->bytes_per_frame;
01671 } else {
01672 if (sc->samples_per_frame > 1) {
01673 samples = FFMIN((1024 / sc->samples_per_frame)*
01674 sc->samples_per_frame, chunk_samples);
01675 size = (samples / sc->samples_per_frame) * sc->bytes_per_frame;
01676 } else {
01677 samples = FFMIN(1024, chunk_samples);
01678 size = samples * sc->sample_size;
01679 }
01680 }
01681
01682 if (st->nb_index_entries >= total) {
01683 av_log(mov->fc, AV_LOG_ERROR, "wrong chunk count %d\n", total);
01684 return;
01685 }
01686 e = &st->index_entries[st->nb_index_entries++];
01687 e->pos = current_offset;
01688 e->timestamp = current_dts;
01689 e->size = size;
01690 e->min_distance = 0;
01691 e->flags = AVINDEX_KEYFRAME;
01692 dprintf(mov->fc, "AVIndex stream %d, chunk %d, offset %"PRIx64", dts %"PRId64", "
01693 "size %d, duration %d\n", st->index, i, current_offset, current_dts,
01694 size, samples);
01695
01696 current_offset += size;
01697 current_dts += samples;
01698 chunk_samples -= samples;
01699 }
01700 }
01701 }
01702 }
01703
01704 static int mov_open_dref(ByteIOContext **pb, char *src, MOVDref *ref)
01705 {
01706
01707
01708 if (ref->nlvl_to > 0 && ref->nlvl_from > 0) {
01709 char filename[1024];
01710 char *src_path;
01711 int i, l;
01712
01713
01714 src_path = strrchr(src, '/');
01715 if (src_path)
01716 src_path++;
01717 else
01718 src_path = src;
01719
01720
01721 for (i = 0, l = strlen(ref->path) - 1; l >= 0; l--)
01722 if (ref->path[l] == '/') {
01723 if (i == ref->nlvl_to - 1)
01724 break;
01725 else
01726 i++;
01727 }
01728
01729
01730 if (i == ref->nlvl_to - 1 && src_path - src < sizeof(filename)) {
01731 memcpy(filename, src, src_path - src);
01732 filename[src_path - src] = 0;
01733
01734 for (i = 1; i < ref->nlvl_from; i++)
01735 av_strlcat(filename, "../", 1024);
01736
01737 av_strlcat(filename, ref->path + l + 1, 1024);
01738
01739 if (!url_fopen(pb, filename, URL_RDONLY))
01740 return 0;
01741 }
01742 }
01743
01744 return AVERROR(ENOENT);
01745 };
01746
01747 static int mov_read_trak(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
01748 {
01749 AVStream *st;
01750 MOVStreamContext *sc;
01751 int ret;
01752
01753 st = av_new_stream(c->fc, c->fc->nb_streams);
01754 if (!st) return AVERROR(ENOMEM);
01755 sc = av_mallocz(sizeof(MOVStreamContext));
01756 if (!sc) return AVERROR(ENOMEM);
01757
01758 st->priv_data = sc;
01759 st->codec->codec_type = AVMEDIA_TYPE_DATA;
01760 sc->ffindex = st->index;
01761
01762 if ((ret = mov_read_default(c, pb, atom)) < 0)
01763 return ret;
01764
01765
01766 if (sc->chunk_count && (!sc->stts_count || !sc->stsc_count ||
01767 (!sc->sample_size && !sc->sample_count))) {
01768 av_log(c->fc, AV_LOG_ERROR, "stream %d, missing mandatory atoms, broken header\n",
01769 st->index);
01770 return 0;
01771 }
01772
01773 if (!sc->time_scale) {
01774 av_log(c->fc, AV_LOG_WARNING, "stream %d, timescale not set\n", st->index);
01775 sc->time_scale = c->time_scale;
01776 if (!sc->time_scale)
01777 sc->time_scale = 1;
01778 }
01779
01780 av_set_pts_info(st, 64, 1, sc->time_scale);
01781
01782 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
01783 !st->codec->frame_size && sc->stts_count == 1) {
01784 st->codec->frame_size = av_rescale(sc->stts_data[0].duration,
01785 st->codec->sample_rate, sc->time_scale);
01786 dprintf(c->fc, "frame size %d\n", st->codec->frame_size);
01787 }
01788
01789 mov_build_index(c, st);
01790
01791 if (sc->dref_id-1 < sc->drefs_count && sc->drefs[sc->dref_id-1].path) {
01792 MOVDref *dref = &sc->drefs[sc->dref_id - 1];
01793 if (mov_open_dref(&sc->pb, c->fc->filename, dref) < 0)
01794 av_log(c->fc, AV_LOG_ERROR,
01795 "stream %d, error opening alias: path='%s', dir='%s', "
01796 "filename='%s', volume='%s', nlvl_from=%d, nlvl_to=%d\n",
01797 st->index, dref->path, dref->dir, dref->filename,
01798 dref->volume, dref->nlvl_from, dref->nlvl_to);
01799 } else
01800 sc->pb = c->fc->pb;
01801
01802 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
01803 if (st->codec->width != sc->width || st->codec->height != sc->height) {
01804 AVRational r = av_d2q(((double)st->codec->height * sc->width) /
01805 ((double)st->codec->width * sc->height), INT_MAX);
01806 if (st->sample_aspect_ratio.num)
01807 st->sample_aspect_ratio = av_mul_q(st->sample_aspect_ratio, r);
01808 else
01809 st->sample_aspect_ratio = r;
01810 }
01811
01812 av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
01813 sc->time_scale*st->nb_frames, st->duration, INT_MAX);
01814 }
01815
01816 switch (st->codec->codec_id) {
01817 #if CONFIG_H261_DECODER
01818 case CODEC_ID_H261:
01819 #endif
01820 #if CONFIG_H263_DECODER
01821 case CODEC_ID_H263:
01822 #endif
01823 #if CONFIG_MPEG4_DECODER
01824 case CODEC_ID_MPEG4:
01825 #endif
01826 st->codec->width = 0;
01827 st->codec->height= 0;
01828 break;
01829 }
01830
01831
01832 av_freep(&sc->chunk_offsets);
01833 av_freep(&sc->stsc_data);
01834 av_freep(&sc->sample_sizes);
01835 av_freep(&sc->keyframes);
01836 av_freep(&sc->stts_data);
01837 av_freep(&sc->stps_data);
01838
01839 return 0;
01840 }
01841
01842 static int mov_read_ilst(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
01843 {
01844 int ret;
01845 c->itunes_metadata = 1;
01846 ret = mov_read_default(c, pb, atom);
01847 c->itunes_metadata = 0;
01848 return ret;
01849 }
01850
01851 static int mov_read_meta(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
01852 {
01853 while (atom.size > 8) {
01854 uint32_t tag = get_le32(pb);
01855 atom.size -= 4;
01856 if (tag == MKTAG('h','d','l','r')) {
01857 url_fseek(pb, -8, SEEK_CUR);
01858 atom.size += 8;
01859 return mov_read_default(c, pb, atom);
01860 }
01861 }
01862 return 0;
01863 }
01864
01865 static int mov_read_tkhd(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
01866 {
01867 int i;
01868 int width;
01869 int height;
01870 int64_t disp_transform[2];
01871 int display_matrix[3][2];
01872 AVStream *st;
01873 MOVStreamContext *sc;
01874 int version;
01875
01876 if (c->fc->nb_streams < 1)
01877 return 0;
01878 st = c->fc->streams[c->fc->nb_streams-1];
01879 sc = st->priv_data;
01880
01881 version = get_byte(pb);
01882 get_be24(pb);
01883
01884
01885
01886
01887
01888
01889
01890 if (version == 1) {
01891 get_be64(pb);
01892 get_be64(pb);
01893 } else {
01894 get_be32(pb);
01895 get_be32(pb);
01896 }
01897 st->id = (int)get_be32(pb);
01898 get_be32(pb);
01899
01900
01901 (version == 1) ? get_be64(pb) : get_be32(pb);
01902 get_be32(pb);
01903 get_be32(pb);
01904
01905 get_be16(pb);
01906 get_be16(pb);
01907 get_be16(pb);
01908 get_be16(pb);
01909
01910
01911
01912
01913 for (i = 0; i < 3; i++) {
01914 display_matrix[i][0] = get_be32(pb);
01915 display_matrix[i][1] = get_be32(pb);
01916 get_be32(pb);
01917 }
01918
01919 width = get_be32(pb);
01920 height = get_be32(pb);
01921 sc->width = width >> 16;
01922 sc->height = height >> 16;
01923
01924
01925
01926
01927
01928 if (width && height &&
01929 ((display_matrix[0][0] != 65536 ||
01930 display_matrix[1][1] != 65536) &&
01931 !display_matrix[0][1] &&
01932 !display_matrix[1][0] &&
01933 !display_matrix[2][0] && !display_matrix[2][1])) {
01934 for (i = 0; i < 2; i++)
01935 disp_transform[i] =
01936 (int64_t) width * display_matrix[0][i] +
01937 (int64_t) height * display_matrix[1][i] +
01938 ((int64_t) display_matrix[2][i] << 16);
01939
01940
01941 st->sample_aspect_ratio = av_d2q(
01942 ((double) disp_transform[0] * height) /
01943 ((double) disp_transform[1] * width), INT_MAX);
01944 }
01945 return 0;
01946 }
01947
01948 static int mov_read_tfhd(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
01949 {
01950 MOVFragment *frag = &c->fragment;
01951 MOVTrackExt *trex = NULL;
01952 int flags, track_id, i;
01953
01954 get_byte(pb);
01955 flags = get_be24(pb);
01956
01957 track_id = get_be32(pb);
01958 if (!track_id)
01959 return -1;
01960 frag->track_id = track_id;
01961 for (i = 0; i < c->trex_count; i++)
01962 if (c->trex_data[i].track_id == frag->track_id) {
01963 trex = &c->trex_data[i];
01964 break;
01965 }
01966 if (!trex) {
01967 av_log(c->fc, AV_LOG_ERROR, "could not find corresponding trex\n");
01968 return -1;
01969 }
01970
01971 if (flags & 0x01) frag->base_data_offset = get_be64(pb);
01972 else frag->base_data_offset = frag->moof_offset;
01973 if (flags & 0x02) frag->stsd_id = get_be32(pb);
01974 else frag->stsd_id = trex->stsd_id;
01975
01976 frag->duration = flags & 0x08 ? get_be32(pb) : trex->duration;
01977 frag->size = flags & 0x10 ? get_be32(pb) : trex->size;
01978 frag->flags = flags & 0x20 ? get_be32(pb) : trex->flags;
01979 dprintf(c->fc, "frag flags 0x%x\n", frag->flags);
01980 return 0;
01981 }
01982
01983 static int mov_read_chap(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
01984 {
01985 c->chapter_track = get_be32(pb);
01986 return 0;
01987 }
01988
01989 static int mov_read_trex(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
01990 {
01991 MOVTrackExt *trex;
01992
01993 if ((uint64_t)c->trex_count+1 >= UINT_MAX / sizeof(*c->trex_data))
01994 return -1;
01995 trex = av_realloc(c->trex_data, (c->trex_count+1)*sizeof(*c->trex_data));
01996 if (!trex)
01997 return AVERROR(ENOMEM);
01998 c->trex_data = trex;
01999 trex = &c->trex_data[c->trex_count++];
02000 get_byte(pb);
02001 get_be24(pb);
02002 trex->track_id = get_be32(pb);
02003 trex->stsd_id = get_be32(pb);
02004 trex->duration = get_be32(pb);
02005 trex->size = get_be32(pb);
02006 trex->flags = get_be32(pb);
02007 return 0;
02008 }
02009
02010 static int mov_read_trun(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
02011 {
02012 MOVFragment *frag = &c->fragment;
02013 AVStream *st = NULL;
02014 MOVStreamContext *sc;
02015 uint64_t offset;
02016 int64_t dts;
02017 int data_offset = 0;
02018 unsigned entries, first_sample_flags = frag->flags;
02019 int flags, distance, i;
02020
02021 for (i = 0; i < c->fc->nb_streams; i++) {
02022 if (c->fc->streams[i]->id == frag->track_id) {
02023 st = c->fc->streams[i];
02024 break;
02025 }
02026 }
02027 if (!st) {
02028 av_log(c->fc, AV_LOG_ERROR, "could not find corresponding track id %d\n", frag->track_id);
02029 return -1;
02030 }
02031 sc = st->priv_data;
02032 if (sc->pseudo_stream_id+1 != frag->stsd_id)
02033 return 0;
02034 get_byte(pb);
02035 flags = get_be24(pb);
02036 entries = get_be32(pb);
02037 dprintf(c->fc, "flags 0x%x entries %d\n", flags, entries);
02038 if (flags & 0x001) data_offset = get_be32(pb);
02039 if (flags & 0x004) first_sample_flags = get_be32(pb);
02040 if (flags & 0x800) {
02041 MOVStts *ctts_data;
02042 if ((uint64_t)entries+sc->ctts_count >= UINT_MAX/sizeof(*sc->ctts_data))
02043 return -1;
02044 ctts_data = av_realloc(sc->ctts_data,
02045 (entries+sc->ctts_count)*sizeof(*sc->ctts_data));
02046 if (!ctts_data)
02047 return AVERROR(ENOMEM);
02048 sc->ctts_data = ctts_data;
02049 }
02050 dts = st->duration;
02051 offset = frag->base_data_offset + data_offset;
02052 distance = 0;
02053 dprintf(c->fc, "first sample flags 0x%x\n", first_sample_flags);
02054 for (i = 0; i < entries; i++) {
02055 unsigned sample_size = frag->size;
02056 int sample_flags = i ? frag->flags : first_sample_flags;
02057 unsigned sample_duration = frag->duration;
02058 int keyframe;
02059
02060 if (flags & 0x100) sample_duration = get_be32(pb);
02061 if (flags & 0x200) sample_size = get_be32(pb);
02062 if (flags & 0x400) sample_flags = get_be32(pb);
02063 if (flags & 0x800) {
02064 sc->ctts_data[sc->ctts_count].count = 1;
02065 sc->ctts_data[sc->ctts_count].duration = get_be32(pb);
02066 sc->ctts_count++;
02067 }
02068 if ((keyframe = st->codec->codec_type == AVMEDIA_TYPE_AUDIO ||
02069 (flags & 0x004 && !i && !sample_flags) || sample_flags & 0x2000000))
02070 distance = 0;
02071 av_add_index_entry(st, offset, dts, sample_size, distance,
02072 keyframe ? AVINDEX_KEYFRAME : 0);
02073 dprintf(c->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
02074 "size %d, distance %d, keyframe %d\n", st->index, sc->sample_count+i,
02075 offset, dts, sample_size, distance, keyframe);
02076 distance++;
02077 dts += sample_duration;
02078 offset += sample_size;
02079 }
02080 frag->moof_offset = offset;
02081 st->duration = dts;
02082 return 0;
02083 }
02084
02085
02086
02087
02088 static int mov_read_wide(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
02089 {
02090 int err;
02091
02092 if (atom.size < 8)
02093 return 0;
02094 if (get_be32(pb) != 0) {
02095 url_fskip(pb, atom.size - 4);
02096 return 0;
02097 }
02098 atom.type = get_le32(pb);
02099 atom.size -= 8;
02100 if (atom.type != MKTAG('m','d','a','t')) {
02101 url_fskip(pb, atom.size);
02102 return 0;
02103 }
02104 err = mov_read_mdat(c, pb, atom);
02105 return err;
02106 }
02107
02108 static int mov_read_cmov(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
02109 {
02110 #if CONFIG_ZLIB
02111 ByteIOContext ctx;
02112 uint8_t *cmov_data;
02113 uint8_t *moov_data;
02114 long cmov_len, moov_len;
02115 int ret = -1;
02116
02117 get_be32(pb);
02118 if (get_le32(pb) != MKTAG('d','c','o','m'))
02119 return -1;
02120 if (get_le32(pb) != MKTAG('z','l','i','b')) {
02121 av_log(c->fc, AV_LOG_ERROR, "unknown compression for cmov atom !");
02122 return -1;
02123 }
02124 get_be32(pb);
02125 if (get_le32(pb) != MKTAG('c','m','v','d'))
02126 return -1;
02127 moov_len = get_be32(pb);
02128 cmov_len = atom.size - 6 * 4;
02129
02130 cmov_data = av_malloc(cmov_len);
02131 if (!cmov_data)
02132 return AVERROR(ENOMEM);
02133 moov_data = av_malloc(moov_len);
02134 if (!moov_data) {
02135 av_free(cmov_data);
02136 return AVERROR(ENOMEM);
02137 }
02138 get_buffer(pb, cmov_data, cmov_len);
02139 if(uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
02140 goto free_and_return;
02141 if(init_put_byte(&ctx, moov_data, moov_len, 0, NULL, NULL, NULL, NULL) != 0)
02142 goto free_and_return;
02143 atom.type = MKTAG('m','o','o','v');
02144 atom.size = moov_len;
02145 #ifdef DEBUG
02146
02147 #endif
02148 ret = mov_read_default(c, &ctx, atom);
02149 free_and_return:
02150 av_free(moov_data);
02151 av_free(cmov_data);
02152 return ret;
02153 #else
02154 av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n");
02155 return -1;
02156 #endif
02157 }
02158
02159
02160 static int mov_read_elst(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
02161 {
02162 MOVStreamContext *sc;
02163 int i, edit_count;
02164
02165 if (c->fc->nb_streams < 1)
02166 return 0;
02167 sc = c->fc->streams[c->fc->nb_streams-1]->priv_data;
02168
02169 get_byte(pb);
02170 get_be24(pb);
02171 edit_count = get_be32(pb);
02172
02173 if((uint64_t)edit_count*12+8 > atom.size)
02174 return -1;
02175
02176 for(i=0; i<edit_count; i++){
02177 int time;
02178 int duration = get_be32(pb);
02179 time = get_be32(pb);
02180 get_be32(pb);
02181 if (i == 0 && time >= -1) {
02182 sc->time_offset = time != -1 ? time : -duration;
02183 }
02184 }
02185
02186 if(edit_count > 1)
02187 av_log(c->fc, AV_LOG_WARNING, "multiple edit list entries, "
02188 "a/v desync might occur, patch welcome\n");
02189
02190 dprintf(c->fc, "track[%i].edit_count = %i\n", c->fc->nb_streams-1, edit_count);
02191 return 0;
02192 }
02193
02194 static const MOVParseTableEntry mov_default_parse_table[] = {
02195 { MKTAG('a','v','s','s'), mov_read_extradata },
02196 { MKTAG('c','h','p','l'), mov_read_chpl },
02197 { MKTAG('c','o','6','4'), mov_read_stco },
02198 { MKTAG('c','t','t','s'), mov_read_ctts },
02199 { MKTAG('d','i','n','f'), mov_read_default },
02200 { MKTAG('d','r','e','f'), mov_read_dref },
02201 { MKTAG('e','d','t','s'), mov_read_default },
02202 { MKTAG('e','l','s','t'), mov_read_elst },
02203 { MKTAG('e','n','d','a'), mov_read_enda },
02204 { MKTAG('f','i','e','l'), mov_read_extradata },
02205 { MKTAG('f','t','y','p'), mov_read_ftyp },
02206 { MKTAG('g','l','b','l'), mov_read_glbl },
02207 { MKTAG('h','d','l','r'), mov_read_hdlr },
02208 { MKTAG('i','l','s','t'), mov_read_ilst },
02209 { MKTAG('j','p','2','h'), mov_read_extradata },
02210 { MKTAG('m','d','a','t'), mov_read_mdat },
02211 { MKTAG('m','d','h','d'), mov_read_mdhd },
02212 { MKTAG('m','d','i','a'), mov_read_default },
02213 { MKTAG('m','e','t','a'), mov_read_meta },
02214 { MKTAG('m','i','n','f'), mov_read_default },
02215 { MKTAG('m','o','o','f'), mov_read_moof },
02216 { MKTAG('m','o','o','v'), mov_read_moov },
02217 { MKTAG('m','v','e','x'), mov_read_default },
02218 { MKTAG('m','v','h','d'), mov_read_mvhd },
02219 { MKTAG('S','M','I',' '), mov_read_smi },
02220 { MKTAG('a','l','a','c'), mov_read_extradata },
02221 { MKTAG('a','v','c','C'), mov_read_glbl },
02222 { MKTAG('p','a','s','p'), mov_read_pasp },
02223 { MKTAG('s','t','b','l'), mov_read_default },
02224 { MKTAG('s','t','c','o'), mov_read_stco },
02225 { MKTAG('s','t','p','s'), mov_read_stps },
02226 { MKTAG('s','t','r','f'), mov_read_strf },
02227 { MKTAG('s','t','s','c'), mov_read_stsc },
02228 { MKTAG('s','t','s','d'), mov_read_stsd },
02229 { MKTAG('s','t','s','s'), mov_read_stss },
02230 { MKTAG('s','t','s','z'), mov_read_stsz },
02231 { MKTAG('s','t','t','s'), mov_read_stts },
02232 { MKTAG('s','t','z','2'), mov_read_stsz },
02233 { MKTAG('t','k','h','d'), mov_read_tkhd },
02234 { MKTAG('t','f','h','d'), mov_read_tfhd },
02235 { MKTAG('t','r','a','k'), mov_read_trak },
02236 { MKTAG('t','r','a','f'), mov_read_default },
02237 { MKTAG('t','r','e','f'), mov_read_default },
02238 { MKTAG('c','h','a','p'), mov_read_chap },
02239 { MKTAG('t','r','e','x'), mov_read_trex },
02240 { MKTAG('t','r','u','n'), mov_read_trun },
02241 { MKTAG('u','d','t','a'), mov_read_default },
02242 { MKTAG('w','a','v','e'), mov_read_wave },
02243 { MKTAG('e','s','d','s'), mov_read_esds },
02244 { MKTAG('w','i','d','e'), mov_read_wide },
02245 { MKTAG('c','m','o','v'), mov_read_cmov },
02246 { 0, NULL }
02247 };
02248
02249 static int mov_probe(AVProbeData *p)
02250 {
02251 unsigned int offset;
02252 uint32_t tag;
02253 int score = 0;
02254
02255
02256 offset = 0;
02257 for(;;) {
02258
02259 if ((offset + 8) > (unsigned int)p->buf_size)
02260 return score;
02261 tag = AV_RL32(p->buf + offset + 4);
02262 switch(tag) {
02263
02264 case MKTAG('j','P',' ',' '):
02265 case MKTAG('m','o','o','v'):
02266 case MKTAG('m','d','a','t'):
02267 case MKTAG('p','n','o','t'):
02268 case MKTAG('u','d','t','a'):
02269 case MKTAG('f','t','y','p'):
02270 return AVPROBE_SCORE_MAX;
02271
02272 case MKTAG('e','d','i','w'):
02273 case MKTAG('w','i','d','e'):
02274 case MKTAG('f','r','e','e'):
02275 case MKTAG('j','u','n','k'):
02276 case MKTAG('p','i','c','t'):
02277 return AVPROBE_SCORE_MAX - 5;
02278 case MKTAG(0x82,0x82,0x7f,0x7d):
02279 case MKTAG('s','k','i','p'):
02280 case MKTAG('u','u','i','d'):
02281 case MKTAG('p','r','f','l'):
02282 offset = AV_RB32(p->buf+offset) + offset;
02283
02284 score = AVPROBE_SCORE_MAX - 50;
02285 break;
02286 default:
02287
02288 return score;
02289 }
02290 }
02291 return score;
02292 }
02293
02294
02295 static void mov_read_chapters(AVFormatContext *s)
02296 {
02297 MOVContext *mov = s->priv_data;
02298 AVStream *st = NULL;
02299 MOVStreamContext *sc;
02300 int64_t cur_pos;
02301 uint8_t *title = NULL;
02302 int i, len, i8, i16;
02303
02304 for (i = 0; i < s->nb_streams; i++)
02305 if (s->streams[i]->id == mov->chapter_track) {
02306 st = s->streams[i];
02307 break;
02308 }
02309 if (!st) {
02310 av_log(s, AV_LOG_ERROR, "Referenced QT chapter track not found\n");
02311 return;
02312 }
02313
02314 st->discard = AVDISCARD_ALL;
02315 sc = st->priv_data;
02316 cur_pos = url_ftell(sc->pb);
02317
02318 for (i = 0; i < st->nb_index_entries; i++) {
02319 AVIndexEntry *sample = &st->index_entries[i];
02320 int64_t end = i+1 < st->nb_index_entries ? st->index_entries[i+1].timestamp : st->duration;
02321
02322 if (url_fseek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
02323 av_log(s, AV_LOG_ERROR, "Chapter %d not found in file\n", i);
02324 goto finish;
02325 }
02326
02327 title = av_malloc(sample->size+2);
02328 get_buffer(sc->pb, title, sample->size);
02329
02330
02331 len = AV_RB16(title);
02332 if (len > sample->size-2)
02333 continue;
02334
02335
02336
02337
02338 if (AV_RB16(title+2) == 0xfeff) {
02339 uint8_t *utf8 = av_malloc(2*len+3);
02340
02341 i8 = i16 = 0;
02342 while (i16 < len) {
02343 uint32_t ch;
02344 uint8_t tmp;
02345 GET_UTF16(ch, i16 < len ? AV_RB16(title + (i16+=2)) : 0, break;)
02346 PUT_UTF8(ch, tmp, if (i8 < 2*len) utf8[2+i8++] = tmp;)
02347 }
02348 utf8[2+i8] = 0;
02349 av_freep(&title);
02350 title = utf8;
02351 }
02352
02353 ff_new_chapter(s, i, st->time_base, sample->timestamp, end, title+2);
02354 av_freep(&title);
02355 }
02356 finish:
02357 av_free(title);
02358 url_fseek(sc->pb, cur_pos, SEEK_SET);
02359 }
02360
02361 static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap)
02362 {
02363 MOVContext *mov = s->priv_data;
02364 ByteIOContext *pb = s->pb;
02365 int err;
02366 MOVAtom atom = { 0 };
02367
02368 mov->fc = s;
02369
02370 if(!url_is_streamed(pb))
02371 atom.size = url_fsize(pb);
02372 else
02373 atom.size = INT64_MAX;
02374
02375
02376 if ((err = mov_read_default(mov, pb, atom)) < 0) {
02377 av_log(s, AV_LOG_ERROR, "error reading header: %d\n", err);
02378 return err;
02379 }
02380 if (!mov->found_moov) {
02381 av_log(s, AV_LOG_ERROR, "moov atom not found\n");
02382 return -1;
02383 }
02384 dprintf(mov->fc, "on_parse_exit_offset=%lld\n", url_ftell(pb));
02385
02386 if (!url_is_streamed(pb) && mov->chapter_track > 0)
02387 mov_read_chapters(s);
02388
02389 return 0;
02390 }
02391
02392 static AVIndexEntry *mov_find_next_sample(AVFormatContext *s, AVStream **st)
02393 {
02394 AVIndexEntry *sample = NULL;
02395 int64_t best_dts = INT64_MAX;
02396 int i;
02397 for (i = 0; i < s->nb_streams; i++) {
02398 AVStream *avst = s->streams[i];
02399 MOVStreamContext *msc = avst->priv_data;
02400 if (msc->pb && msc->current_sample < avst->nb_index_entries) {
02401 AVIndexEntry *current_sample = &avst->index_entries[msc->current_sample];
02402 int64_t dts = av_rescale(current_sample->timestamp, AV_TIME_BASE, msc->time_scale);
02403 dprintf(s, "stream %d, sample %d, dts %"PRId64"\n", i, msc->current_sample, dts);
02404 if (!sample || (url_is_streamed(s->pb) && current_sample->pos < sample->pos) ||
02405 (!url_is_streamed(s->pb) &&
02406 ((msc->pb != s->pb && dts < best_dts) || (msc->pb == s->pb &&
02407 ((FFABS(best_dts - dts) <= AV_TIME_BASE && current_sample->pos < sample->pos) ||
02408 (FFABS(best_dts - dts) > AV_TIME_BASE && dts < best_dts)))))) {
02409 sample = current_sample;
02410 best_dts = dts;
02411 *st = avst;
02412 }
02413 }
02414 }
02415 return sample;
02416 }
02417
02418 static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
02419 {
02420 MOVContext *mov = s->priv_data;
02421 MOVStreamContext *sc;
02422 AVIndexEntry *sample;
02423 AVStream *st = NULL;
02424 int ret;
02425 retry:
02426 sample = mov_find_next_sample(s, &st);
02427 if (!sample) {
02428 mov->found_mdat = 0;
02429 if (!url_is_streamed(s->pb) ||
02430 mov_read_default(mov, s->pb, (MOVAtom){ 0, INT64_MAX }) < 0 ||
02431 url_feof(s->pb))
02432 return AVERROR_EOF;
02433 dprintf(s, "read fragments, offset 0x%llx\n", url_ftell(s->pb));
02434 goto retry;
02435 }
02436 sc = st->priv_data;
02437
02438 sc->current_sample++;
02439
02440 if (st->discard != AVDISCARD_ALL) {
02441 if (url_fseek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
02442 av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n",
02443 sc->ffindex, sample->pos);
02444 return -1;
02445 }
02446 ret = av_get_packet(sc->pb, pkt, sample->size);
02447 if (ret < 0)
02448 return ret;
02449 #if CONFIG_DV_DEMUXER
02450 if (mov->dv_demux && sc->dv_audio_container) {
02451 dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size);
02452 av_free(pkt->data);
02453 pkt->size = 0;
02454 ret = dv_get_packet(mov->dv_demux, pkt);
02455 if (ret < 0)
02456 return ret;
02457 }
02458 #endif
02459 }
02460
02461 pkt->stream_index = sc->ffindex;
02462 pkt->dts = sample->timestamp;
02463 if (sc->ctts_data) {
02464 pkt->pts = pkt->dts + sc->dts_shift + sc->ctts_data[sc->ctts_index].duration;
02465
02466 sc->ctts_sample++;
02467 if (sc->ctts_index < sc->ctts_count &&
02468 sc->ctts_data[sc->ctts_index].count == sc->ctts_sample) {
02469 sc->ctts_index++;
02470 sc->ctts_sample = 0;
02471 }
02472 if (sc->wrong_dts)
02473 pkt->dts = AV_NOPTS_VALUE;
02474 } else {
02475 int64_t next_dts = (sc->current_sample < st->nb_index_entries) ?
02476 st->index_entries[sc->current_sample].timestamp : st->duration;
02477 pkt->duration = next_dts - pkt->dts;
02478 pkt->pts = pkt->dts;
02479 }
02480 if (st->discard == AVDISCARD_ALL)
02481 goto retry;
02482 pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? AV_PKT_FLAG_KEY : 0;
02483 pkt->pos = sample->pos;
02484 dprintf(s, "stream %d, pts %"PRId64", dts %"PRId64", pos 0x%"PRIx64", duration %d\n",
02485 pkt->stream_index, pkt->pts, pkt->dts, pkt->pos, pkt->duration);
02486 return 0;
02487 }
02488
02489 static int mov_seek_stream(AVFormatContext *s, AVStream *st, int64_t timestamp, int flags)
02490 {
02491 MOVStreamContext *sc = st->priv_data;
02492 int sample, time_sample;
02493 int i;
02494
02495 sample = av_index_search_timestamp(st, timestamp, flags);
02496 dprintf(s, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample);
02497 if (sample < 0)
02498 return -1;
02499 sc->current_sample = sample;
02500 dprintf(s, "stream %d, found sample %d\n", st->index, sc->current_sample);
02501
02502 if (sc->ctts_data) {
02503 time_sample = 0;
02504 for (i = 0; i < sc->ctts_count; i++) {
02505 int next = time_sample + sc->ctts_data[i].count;
02506 if (next > sc->current_sample) {
02507 sc->ctts_index = i;
02508 sc->ctts_sample = sc->current_sample - time_sample;
02509 break;
02510 }
02511 time_sample = next;
02512 }
02513 }
02514 return sample;
02515 }
02516
02517 static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
02518 {
02519 AVStream *st;
02520 int64_t seek_timestamp, timestamp;
02521 int sample;
02522 int i;
02523
02524 if (stream_index >= s->nb_streams)
02525 return -1;
02526 if (sample_time < 0)
02527 sample_time = 0;
02528
02529 st = s->streams[stream_index];
02530 sample = mov_seek_stream(s, st, sample_time, flags);
02531 if (sample < 0)
02532 return -1;
02533
02534
02535 seek_timestamp = st->index_entries[sample].timestamp;
02536
02537 for (i = 0; i < s->nb_streams; i++) {
02538 st = s->streams[i];
02539 if (stream_index == i)
02540 continue;
02541
02542 timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base);
02543 mov_seek_stream(s, st, timestamp, flags);
02544 }
02545 return 0;
02546 }
02547
02548 static int mov_read_close(AVFormatContext *s)
02549 {
02550 MOVContext *mov = s->priv_data;
02551 int i, j;
02552
02553 for (i = 0; i < s->nb_streams; i++) {
02554 AVStream *st = s->streams[i];
02555 MOVStreamContext *sc = st->priv_data;
02556
02557 av_freep(&sc->ctts_data);
02558 for (j = 0; j < sc->drefs_count; j++) {
02559 av_freep(&sc->drefs[j].path);
02560 av_freep(&sc->drefs[j].dir);
02561 }
02562 av_freep(&sc->drefs);
02563 if (sc->pb && sc->pb != s->pb)
02564 url_fclose(sc->pb);
02565
02566 av_freep(&st->codec->palctrl);
02567 }
02568
02569 if (mov->dv_demux) {
02570 for(i = 0; i < mov->dv_fctx->nb_streams; i++) {
02571 av_freep(&mov->dv_fctx->streams[i]->codec);
02572 av_freep(&mov->dv_fctx->streams[i]);
02573 }
02574 av_freep(&mov->dv_fctx);
02575 av_freep(&mov->dv_demux);
02576 }
02577
02578 av_freep(&mov->trex_data);
02579
02580 return 0;
02581 }
02582
02583 AVInputFormat mov_demuxer = {
02584 "mov,mp4,m4a,3gp,3g2,mj2",
02585 NULL_IF_CONFIG_SMALL("QuickTime/MPEG-4/Motion JPEG 2000 format"),
02586 sizeof(MOVContext),
02587 mov_probe,
02588 mov_read_header,
02589 mov_read_packet,
02590 mov_read_close,
02591 mov_read_seek,
02592 };