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