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