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