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 if (color_greyscale) {
01073 int color_index, color_dec;
01074
01075 st->codec->bits_per_coded_sample = color_depth;
01076 color_count = 1 << color_depth;
01077 color_index = 255;
01078 color_dec = 256 / (color_count - 1);
01079 for (j = 0; j < color_count; j++) {
01080 r = g = b = color_index;
01081 sc->palette[j] =
01082 (r << 16) | (g << 8) | (b);
01083 color_index -= color_dec;
01084 if (color_index < 0)
01085 color_index = 0;
01086 }
01087 } else if (st->codec->color_table_id) {
01088 const uint8_t *color_table;
01089
01090 color_count = 1 << color_depth;
01091 if (color_depth == 2)
01092 color_table = ff_qt_default_palette_4;
01093 else if (color_depth == 4)
01094 color_table = ff_qt_default_palette_16;
01095 else
01096 color_table = ff_qt_default_palette_256;
01097
01098 for (j = 0; j < color_count; j++) {
01099 r = color_table[j * 3 + 0];
01100 g = color_table[j * 3 + 1];
01101 b = color_table[j * 3 + 2];
01102 sc->palette[j] =
01103 (r << 16) | (g << 8) | (b);
01104 }
01105 } else {
01106
01107 color_start = avio_rb32(pb);
01108 color_count = avio_rb16(pb);
01109 color_end = avio_rb16(pb);
01110 if ((color_start <= 255) &&
01111 (color_end <= 255)) {
01112 for (j = color_start; j <= color_end; j++) {
01113
01114
01115
01116 avio_r8(pb);
01117 avio_r8(pb);
01118 r = avio_r8(pb);
01119 avio_r8(pb);
01120 g = avio_r8(pb);
01121 avio_r8(pb);
01122 b = avio_r8(pb);
01123 avio_r8(pb);
01124 sc->palette[j] =
01125 (r << 16) | (g << 8) | (b);
01126 }
01127 }
01128 }
01129 sc->has_palette = 1;
01130 }
01131 } else if(st->codec->codec_type==AVMEDIA_TYPE_AUDIO) {
01132 int bits_per_sample, flags;
01133 uint16_t version = avio_rb16(pb);
01134
01135 st->codec->codec_id = id;
01136 avio_rb16(pb);
01137 avio_rb32(pb);
01138
01139 st->codec->channels = avio_rb16(pb);
01140 av_dlog(c->fc, "audio channels %d\n", st->codec->channels);
01141 st->codec->bits_per_coded_sample = avio_rb16(pb);
01142
01143 sc->audio_cid = avio_rb16(pb);
01144 avio_rb16(pb);
01145
01146 st->codec->sample_rate = ((avio_rb32(pb) >> 16));
01147
01148
01149 av_dlog(c->fc, "version =%d, isom =%d\n",version,c->isom);
01150 if(!c->isom) {
01151 if(version==1) {
01152 sc->samples_per_frame = avio_rb32(pb);
01153 avio_rb32(pb);
01154 sc->bytes_per_frame = avio_rb32(pb);
01155 avio_rb32(pb);
01156 } else if(version==2) {
01157 avio_rb32(pb);
01158 st->codec->sample_rate = av_int2dbl(avio_rb64(pb));
01159 st->codec->channels = avio_rb32(pb);
01160 avio_rb32(pb);
01161 st->codec->bits_per_coded_sample = avio_rb32(pb);
01162 flags = avio_rb32(pb);
01163 sc->bytes_per_frame = avio_rb32(pb);
01164 sc->samples_per_frame = avio_rb32(pb);
01165 if (format == MKTAG('l','p','c','m'))
01166 st->codec->codec_id = ff_mov_get_lpcm_codec_id(st->codec->bits_per_coded_sample, flags);
01167 }
01168 }
01169
01170 switch (st->codec->codec_id) {
01171 case CODEC_ID_PCM_S8:
01172 case CODEC_ID_PCM_U8:
01173 if (st->codec->bits_per_coded_sample == 16)
01174 st->codec->codec_id = CODEC_ID_PCM_S16BE;
01175 break;
01176 case CODEC_ID_PCM_S16LE:
01177 case CODEC_ID_PCM_S16BE:
01178 if (st->codec->bits_per_coded_sample == 8)
01179 st->codec->codec_id = CODEC_ID_PCM_S8;
01180 else if (st->codec->bits_per_coded_sample == 24)
01181 st->codec->codec_id =
01182 st->codec->codec_id == CODEC_ID_PCM_S16BE ?
01183 CODEC_ID_PCM_S24BE : CODEC_ID_PCM_S24LE;
01184 break;
01185
01186 case CODEC_ID_MACE3:
01187 sc->samples_per_frame = 6;
01188 sc->bytes_per_frame = 2*st->codec->channels;
01189 break;
01190 case CODEC_ID_MACE6:
01191 sc->samples_per_frame = 6;
01192 sc->bytes_per_frame = 1*st->codec->channels;
01193 break;
01194 case CODEC_ID_ADPCM_IMA_QT:
01195 sc->samples_per_frame = 64;
01196 sc->bytes_per_frame = 34*st->codec->channels;
01197 break;
01198 case CODEC_ID_GSM:
01199 sc->samples_per_frame = 160;
01200 sc->bytes_per_frame = 33;
01201 break;
01202 default:
01203 break;
01204 }
01205
01206 bits_per_sample = av_get_bits_per_sample(st->codec->codec_id);
01207 if (bits_per_sample) {
01208 st->codec->bits_per_coded_sample = bits_per_sample;
01209 sc->sample_size = (bits_per_sample >> 3) * st->codec->channels;
01210 }
01211 } else if(st->codec->codec_type==AVMEDIA_TYPE_SUBTITLE){
01212
01213
01214 MOVAtom fake_atom = { .size = size - (avio_tell(pb) - start_pos) };
01215 if (format != AV_RL32("mp4s"))
01216 mov_read_glbl(c, pb, fake_atom);
01217 st->codec->codec_id= id;
01218 st->codec->width = sc->width;
01219 st->codec->height = sc->height;
01220 } else {
01221
01222 avio_skip(pb, size - (avio_tell(pb) - start_pos));
01223 }
01224
01225 a.size = size - (avio_tell(pb) - start_pos);
01226 if (a.size > 8) {
01227 if (mov_read_default(c, pb, a) < 0)
01228 return -1;
01229 } else if (a.size > 0)
01230 avio_skip(pb, a.size);
01231 }
01232
01233 if(st->codec->codec_type==AVMEDIA_TYPE_AUDIO && st->codec->sample_rate==0 && sc->time_scale>1)
01234 st->codec->sample_rate= sc->time_scale;
01235
01236
01237 switch (st->codec->codec_id) {
01238 #if CONFIG_DV_DEMUXER
01239 case CODEC_ID_DVAUDIO:
01240 c->dv_fctx = avformat_alloc_context();
01241 c->dv_demux = dv_init_demux(c->dv_fctx);
01242 if (!c->dv_demux) {
01243 av_log(c->fc, AV_LOG_ERROR, "dv demux context init error\n");
01244 return -1;
01245 }
01246 sc->dv_audio_container = 1;
01247 st->codec->codec_id = CODEC_ID_PCM_S16LE;
01248 break;
01249 #endif
01250
01251 case CODEC_ID_QCELP:
01252
01253 if (st->codec->codec_tag != MKTAG('Q','c','l','p'))
01254 st->codec->sample_rate = 8000;
01255 st->codec->frame_size= 160;
01256 st->codec->channels= 1;
01257 break;
01258 case CODEC_ID_AMR_NB:
01259 case CODEC_ID_AMR_WB:
01260 st->codec->frame_size= sc->samples_per_frame;
01261 st->codec->channels= 1;
01262
01263 if (st->codec->codec_id == CODEC_ID_AMR_NB)
01264 st->codec->sample_rate = 8000;
01265 else if (st->codec->codec_id == CODEC_ID_AMR_WB)
01266 st->codec->sample_rate = 16000;
01267 break;
01268 case CODEC_ID_MP2:
01269 case CODEC_ID_MP3:
01270 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
01271 st->need_parsing = AVSTREAM_PARSE_FULL;
01272 break;
01273 case CODEC_ID_GSM:
01274 case CODEC_ID_ADPCM_MS:
01275 case CODEC_ID_ADPCM_IMA_WAV:
01276 st->codec->frame_size = sc->samples_per_frame;
01277 st->codec->block_align = sc->bytes_per_frame;
01278 break;
01279 case CODEC_ID_ALAC:
01280 if (st->codec->extradata_size == 36) {
01281 st->codec->frame_size = AV_RB32(st->codec->extradata+12);
01282 st->codec->channels = AV_RB8 (st->codec->extradata+21);
01283 st->codec->sample_rate = AV_RB32(st->codec->extradata+32);
01284 }
01285 break;
01286 default:
01287 break;
01288 }
01289
01290 return 0;
01291 }
01292
01293 static int mov_read_stsd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
01294 {
01295 int entries;
01296
01297 avio_r8(pb);
01298 avio_rb24(pb);
01299 entries = avio_rb32(pb);
01300
01301 return ff_mov_read_stsd_entries(c, pb, entries);
01302 }
01303
01304 static int mov_read_stsc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
01305 {
01306 AVStream *st;
01307 MOVStreamContext *sc;
01308 unsigned int i, entries;
01309
01310 if (c->fc->nb_streams < 1)
01311 return 0;
01312 st = c->fc->streams[c->fc->nb_streams-1];
01313 sc = st->priv_data;
01314
01315 avio_r8(pb);
01316 avio_rb24(pb);
01317
01318 entries = avio_rb32(pb);
01319
01320 av_dlog(c->fc, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
01321
01322 if(entries >= UINT_MAX / sizeof(*sc->stsc_data))
01323 return -1;
01324 sc->stsc_data = av_malloc(entries * sizeof(*sc->stsc_data));
01325 if (!sc->stsc_data)
01326 return AVERROR(ENOMEM);
01327 sc->stsc_count = entries;
01328
01329 for(i=0; i<entries; i++) {
01330 sc->stsc_data[i].first = avio_rb32(pb);
01331 sc->stsc_data[i].count = avio_rb32(pb);
01332 sc->stsc_data[i].id = avio_rb32(pb);
01333 }
01334 return 0;
01335 }
01336
01337 static int mov_read_stps(MOVContext *c, AVIOContext *pb, MOVAtom atom)
01338 {
01339 AVStream *st;
01340 MOVStreamContext *sc;
01341 unsigned i, entries;
01342
01343 if (c->fc->nb_streams < 1)
01344 return 0;
01345 st = c->fc->streams[c->fc->nb_streams-1];
01346 sc = st->priv_data;
01347
01348 avio_rb32(pb);
01349
01350 entries = avio_rb32(pb);
01351 if (entries >= UINT_MAX / sizeof(*sc->stps_data))
01352 return -1;
01353 sc->stps_data = av_malloc(entries * sizeof(*sc->stps_data));
01354 if (!sc->stps_data)
01355 return AVERROR(ENOMEM);
01356 sc->stps_count = entries;
01357
01358 for (i = 0; i < entries; i++) {
01359 sc->stps_data[i] = avio_rb32(pb);
01360
01361 }
01362
01363 return 0;
01364 }
01365
01366 static int mov_read_stss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
01367 {
01368 AVStream *st;
01369 MOVStreamContext *sc;
01370 unsigned int i, entries;
01371
01372 if (c->fc->nb_streams < 1)
01373 return 0;
01374 st = c->fc->streams[c->fc->nb_streams-1];
01375 sc = st->priv_data;
01376
01377 avio_r8(pb);
01378 avio_rb24(pb);
01379
01380 entries = avio_rb32(pb);
01381
01382 av_dlog(c->fc, "keyframe_count = %d\n", entries);
01383
01384 if(entries >= UINT_MAX / sizeof(int))
01385 return -1;
01386 sc->keyframes = av_malloc(entries * sizeof(int));
01387 if (!sc->keyframes)
01388 return AVERROR(ENOMEM);
01389 sc->keyframe_count = entries;
01390
01391 for(i=0; i<entries; i++) {
01392 sc->keyframes[i] = avio_rb32(pb);
01393
01394 }
01395 return 0;
01396 }
01397
01398 static int mov_read_stsz(MOVContext *c, AVIOContext *pb, MOVAtom atom)
01399 {
01400 AVStream *st;
01401 MOVStreamContext *sc;
01402 unsigned int i, entries, sample_size, field_size, num_bytes;
01403 GetBitContext gb;
01404 unsigned char* buf;
01405
01406 if (c->fc->nb_streams < 1)
01407 return 0;
01408 st = c->fc->streams[c->fc->nb_streams-1];
01409 sc = st->priv_data;
01410
01411 avio_r8(pb);
01412 avio_rb24(pb);
01413
01414 if (atom.type == MKTAG('s','t','s','z')) {
01415 sample_size = avio_rb32(pb);
01416 if (!sc->sample_size)
01417 sc->sample_size = sample_size;
01418 field_size = 32;
01419 } else {
01420 sample_size = 0;
01421 avio_rb24(pb);
01422 field_size = avio_r8(pb);
01423 }
01424 entries = avio_rb32(pb);
01425
01426 av_dlog(c->fc, "sample_size = %d sample_count = %d\n", sc->sample_size, entries);
01427
01428 sc->sample_count = entries;
01429 if (sample_size)
01430 return 0;
01431
01432 if (field_size != 4 && field_size != 8 && field_size != 16 && field_size != 32) {
01433 av_log(c->fc, AV_LOG_ERROR, "Invalid sample field size %d\n", field_size);
01434 return -1;
01435 }
01436
01437 if (entries >= UINT_MAX / sizeof(int) || entries >= (UINT_MAX - 4) / field_size)
01438 return -1;
01439 sc->sample_sizes = av_malloc(entries * sizeof(int));
01440 if (!sc->sample_sizes)
01441 return AVERROR(ENOMEM);
01442
01443 num_bytes = (entries*field_size+4)>>3;
01444
01445 buf = av_malloc(num_bytes+FF_INPUT_BUFFER_PADDING_SIZE);
01446 if (!buf) {
01447 av_freep(&sc->sample_sizes);
01448 return AVERROR(ENOMEM);
01449 }
01450
01451 if (avio_read(pb, buf, num_bytes) < num_bytes) {
01452 av_freep(&sc->sample_sizes);
01453 av_free(buf);
01454 return -1;
01455 }
01456
01457 init_get_bits(&gb, buf, 8*num_bytes);
01458
01459 for(i=0; i<entries; i++)
01460 sc->sample_sizes[i] = get_bits_long(&gb, field_size);
01461
01462 av_free(buf);
01463 return 0;
01464 }
01465
01466 static int mov_read_stts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
01467 {
01468 AVStream *st;
01469 MOVStreamContext *sc;
01470 unsigned int i, entries;
01471 int64_t duration=0;
01472 int64_t total_sample_count=0;
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].stts.entries = %i\n", c->fc->nb_streams-1, entries);
01484
01485 if(entries >= UINT_MAX / sizeof(*sc->stts_data))
01486 return -1;
01487 sc->stts_data = av_malloc(entries * sizeof(*sc->stts_data));
01488 if (!sc->stts_data)
01489 return AVERROR(ENOMEM);
01490 sc->stts_count = entries;
01491
01492 for(i=0; i<entries; i++) {
01493 int sample_duration;
01494 int sample_count;
01495
01496 sample_count=avio_rb32(pb);
01497 sample_duration = avio_rb32(pb);
01498 sc->stts_data[i].count= sample_count;
01499 sc->stts_data[i].duration= sample_duration;
01500
01501 av_dlog(c->fc, "sample_count=%d, sample_duration=%d\n",sample_count,sample_duration);
01502
01503 duration+=(int64_t)sample_duration*sample_count;
01504 total_sample_count+=sample_count;
01505 }
01506
01507 st->nb_frames= total_sample_count;
01508 if(duration)
01509 st->duration= duration;
01510 return 0;
01511 }
01512
01513 static int mov_read_ctts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
01514 {
01515 AVStream *st;
01516 MOVStreamContext *sc;
01517 unsigned int i, entries;
01518
01519 if (c->fc->nb_streams < 1)
01520 return 0;
01521 st = c->fc->streams[c->fc->nb_streams-1];
01522 sc = st->priv_data;
01523
01524 avio_r8(pb);
01525 avio_rb24(pb);
01526 entries = avio_rb32(pb);
01527
01528 av_dlog(c->fc, "track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries);
01529
01530 if(entries >= UINT_MAX / sizeof(*sc->ctts_data))
01531 return -1;
01532 sc->ctts_data = av_malloc(entries * sizeof(*sc->ctts_data));
01533 if (!sc->ctts_data)
01534 return AVERROR(ENOMEM);
01535 sc->ctts_count = entries;
01536
01537 for(i=0; i<entries; i++) {
01538 int count =avio_rb32(pb);
01539 int duration =avio_rb32(pb);
01540
01541 sc->ctts_data[i].count = count;
01542 sc->ctts_data[i].duration= duration;
01543 if (duration < 0 && i+1<entries)
01544 sc->dts_shift = FFMAX(sc->dts_shift, -duration);
01545 }
01546
01547 av_dlog(c->fc, "dts shift %d\n", sc->dts_shift);
01548
01549 return 0;
01550 }
01551
01552 static void mov_build_index(MOVContext *mov, AVStream *st)
01553 {
01554 MOVStreamContext *sc = st->priv_data;
01555 int64_t current_offset;
01556 int64_t current_dts = 0;
01557 unsigned int stts_index = 0;
01558 unsigned int stsc_index = 0;
01559 unsigned int stss_index = 0;
01560 unsigned int stps_index = 0;
01561 unsigned int i, j;
01562 uint64_t stream_size = 0;
01563
01564
01565 if (sc->time_offset && mov->time_scale > 0) {
01566 if (sc->time_offset < 0)
01567 sc->time_offset = av_rescale(sc->time_offset, sc->time_scale, mov->time_scale);
01568 current_dts = -sc->time_offset;
01569 if (sc->ctts_data && sc->stts_data &&
01570 sc->ctts_data[0].duration / FFMAX(sc->stts_data[0].duration, 1) > 16) {
01571
01572
01573 sc->wrong_dts = 1;
01574 st->codec->has_b_frames = 1;
01575 }
01576 }
01577
01578
01579 if (!(st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
01580 sc->stts_count == 1 && sc->stts_data[0].duration == 1)) {
01581 unsigned int current_sample = 0;
01582 unsigned int stts_sample = 0;
01583 unsigned int sample_size;
01584 unsigned int distance = 0;
01585 int key_off = sc->keyframes && sc->keyframes[0] == 1;
01586
01587 current_dts -= sc->dts_shift;
01588
01589 if (sc->sample_count >= UINT_MAX / sizeof(*st->index_entries))
01590 return;
01591 st->index_entries = av_malloc(sc->sample_count*sizeof(*st->index_entries));
01592 if (!st->index_entries)
01593 return;
01594 st->index_entries_allocated_size = sc->sample_count*sizeof(*st->index_entries);
01595
01596 for (i = 0; i < sc->chunk_count; i++) {
01597 current_offset = sc->chunk_offsets[i];
01598 while (stsc_index + 1 < sc->stsc_count &&
01599 i + 1 == sc->stsc_data[stsc_index + 1].first)
01600 stsc_index++;
01601 for (j = 0; j < sc->stsc_data[stsc_index].count; j++) {
01602 int keyframe = 0;
01603 if (current_sample >= sc->sample_count) {
01604 av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n");
01605 return;
01606 }
01607
01608 if (!sc->keyframe_count || current_sample+key_off == sc->keyframes[stss_index]) {
01609 keyframe = 1;
01610 if (stss_index + 1 < sc->keyframe_count)
01611 stss_index++;
01612 } else if (sc->stps_count && current_sample+key_off == sc->stps_data[stps_index]) {
01613 keyframe = 1;
01614 if (stps_index + 1 < sc->stps_count)
01615 stps_index++;
01616 }
01617 if (keyframe)
01618 distance = 0;
01619 sample_size = sc->sample_size > 0 ? sc->sample_size : sc->sample_sizes[current_sample];
01620 if(sc->pseudo_stream_id == -1 ||
01621 sc->stsc_data[stsc_index].id - 1 == sc->pseudo_stream_id) {
01622 AVIndexEntry *e = &st->index_entries[st->nb_index_entries++];
01623 e->pos = current_offset;
01624 e->timestamp = current_dts;
01625 e->size = sample_size;
01626 e->min_distance = distance;
01627 e->flags = keyframe ? AVINDEX_KEYFRAME : 0;
01628 av_dlog(mov->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
01629 "size %d, distance %d, keyframe %d\n", st->index, current_sample,
01630 current_offset, current_dts, sample_size, distance, keyframe);
01631 }
01632
01633 current_offset += sample_size;
01634 stream_size += sample_size;
01635 current_dts += sc->stts_data[stts_index].duration;
01636 distance++;
01637 stts_sample++;
01638 current_sample++;
01639 if (stts_index + 1 < sc->stts_count && stts_sample == sc->stts_data[stts_index].count) {
01640 stts_sample = 0;
01641 stts_index++;
01642 }
01643 }
01644 }
01645 if (st->duration > 0)
01646 st->codec->bit_rate = stream_size*8*sc->time_scale/st->duration;
01647 } else {
01648 unsigned chunk_samples, total = 0;
01649
01650
01651 for (i = 0; i < sc->stsc_count; i++) {
01652 unsigned count, chunk_count;
01653
01654 chunk_samples = sc->stsc_data[i].count;
01655 if (sc->samples_per_frame && chunk_samples % sc->samples_per_frame) {
01656 av_log(mov->fc, AV_LOG_ERROR, "error unaligned chunk\n");
01657 return;
01658 }
01659
01660 if (sc->samples_per_frame >= 160) {
01661 count = chunk_samples / sc->samples_per_frame;
01662 } else if (sc->samples_per_frame > 1) {
01663 unsigned samples = (1024/sc->samples_per_frame)*sc->samples_per_frame;
01664 count = (chunk_samples+samples-1) / samples;
01665 } else {
01666 count = (chunk_samples+1023) / 1024;
01667 }
01668
01669 if (i < sc->stsc_count - 1)
01670 chunk_count = sc->stsc_data[i+1].first - sc->stsc_data[i].first;
01671 else
01672 chunk_count = sc->chunk_count - (sc->stsc_data[i].first - 1);
01673 total += chunk_count * count;
01674 }
01675
01676 av_dlog(mov->fc, "chunk count %d\n", total);
01677 if (total >= UINT_MAX / sizeof(*st->index_entries))
01678 return;
01679 st->index_entries = av_malloc(total*sizeof(*st->index_entries));
01680 if (!st->index_entries)
01681 return;
01682 st->index_entries_allocated_size = total*sizeof(*st->index_entries);
01683
01684
01685 for (i = 0; i < sc->chunk_count; i++) {
01686 current_offset = sc->chunk_offsets[i];
01687 if (stsc_index + 1 < sc->stsc_count &&
01688 i + 1 == sc->stsc_data[stsc_index + 1].first)
01689 stsc_index++;
01690 chunk_samples = sc->stsc_data[stsc_index].count;
01691
01692 while (chunk_samples > 0) {
01693 AVIndexEntry *e;
01694 unsigned size, samples;
01695
01696 if (sc->samples_per_frame >= 160) {
01697 samples = sc->samples_per_frame;
01698 size = sc->bytes_per_frame;
01699 } else {
01700 if (sc->samples_per_frame > 1) {
01701 samples = FFMIN((1024 / sc->samples_per_frame)*
01702 sc->samples_per_frame, chunk_samples);
01703 size = (samples / sc->samples_per_frame) * sc->bytes_per_frame;
01704 } else {
01705 samples = FFMIN(1024, chunk_samples);
01706 size = samples * sc->sample_size;
01707 }
01708 }
01709
01710 if (st->nb_index_entries >= total) {
01711 av_log(mov->fc, AV_LOG_ERROR, "wrong chunk count %d\n", total);
01712 return;
01713 }
01714 e = &st->index_entries[st->nb_index_entries++];
01715 e->pos = current_offset;
01716 e->timestamp = current_dts;
01717 e->size = size;
01718 e->min_distance = 0;
01719 e->flags = AVINDEX_KEYFRAME;
01720 av_dlog(mov->fc, "AVIndex stream %d, chunk %d, offset %"PRIx64", dts %"PRId64", "
01721 "size %d, duration %d\n", st->index, i, current_offset, current_dts,
01722 size, samples);
01723
01724 current_offset += size;
01725 current_dts += samples;
01726 chunk_samples -= samples;
01727 }
01728 }
01729 }
01730 }
01731
01732 static int mov_open_dref(AVIOContext **pb, const char *src, MOVDref *ref)
01733 {
01734
01735
01736 if (ref->nlvl_to > 0 && ref->nlvl_from > 0) {
01737 char filename[1024];
01738 const char *src_path;
01739 int i, l;
01740
01741
01742 src_path = strrchr(src, '/');
01743 if (src_path)
01744 src_path++;
01745 else
01746 src_path = src;
01747
01748
01749 for (i = 0, l = strlen(ref->path) - 1; l >= 0; l--)
01750 if (ref->path[l] == '/') {
01751 if (i == ref->nlvl_to - 1)
01752 break;
01753 else
01754 i++;
01755 }
01756
01757
01758 if (i == ref->nlvl_to - 1 && src_path - src < sizeof(filename)) {
01759 memcpy(filename, src, src_path - src);
01760 filename[src_path - src] = 0;
01761
01762 for (i = 1; i < ref->nlvl_from; i++)
01763 av_strlcat(filename, "../", 1024);
01764
01765 av_strlcat(filename, ref->path + l + 1, 1024);
01766
01767 if (!avio_open(pb, filename, AVIO_FLAG_READ))
01768 return 0;
01769 }
01770 }
01771
01772 return AVERROR(ENOENT);
01773 }
01774
01775 static int mov_read_trak(MOVContext *c, AVIOContext *pb, MOVAtom atom)
01776 {
01777 AVStream *st;
01778 MOVStreamContext *sc;
01779 int ret;
01780
01781 st = av_new_stream(c->fc, c->fc->nb_streams);
01782 if (!st) return AVERROR(ENOMEM);
01783 sc = av_mallocz(sizeof(MOVStreamContext));
01784 if (!sc) return AVERROR(ENOMEM);
01785
01786 st->priv_data = sc;
01787 st->codec->codec_type = AVMEDIA_TYPE_DATA;
01788 sc->ffindex = st->index;
01789
01790 if ((ret = mov_read_default(c, pb, atom)) < 0)
01791 return ret;
01792
01793
01794 if (sc->chunk_count && (!sc->stts_count || !sc->stsc_count ||
01795 (!sc->sample_size && !sc->sample_count))) {
01796 av_log(c->fc, AV_LOG_ERROR, "stream %d, missing mandatory atoms, broken header\n",
01797 st->index);
01798 return 0;
01799 }
01800
01801 if (sc->time_scale <= 0) {
01802 av_log(c->fc, AV_LOG_WARNING, "stream %d, timescale not set\n", st->index);
01803 sc->time_scale = c->time_scale;
01804 if (sc->time_scale <= 0)
01805 sc->time_scale = 1;
01806 }
01807
01808 av_set_pts_info(st, 64, 1, sc->time_scale);
01809
01810 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
01811 !st->codec->frame_size && sc->stts_count == 1) {
01812 st->codec->frame_size = av_rescale(sc->stts_data[0].duration,
01813 st->codec->sample_rate, sc->time_scale);
01814 av_dlog(c->fc, "frame size %d\n", st->codec->frame_size);
01815 }
01816
01817 mov_build_index(c, st);
01818
01819 if (sc->dref_id-1 < sc->drefs_count && sc->drefs[sc->dref_id-1].path) {
01820 MOVDref *dref = &sc->drefs[sc->dref_id - 1];
01821 if (mov_open_dref(&sc->pb, c->fc->filename, dref) < 0)
01822 av_log(c->fc, AV_LOG_ERROR,
01823 "stream %d, error opening alias: path='%s', dir='%s', "
01824 "filename='%s', volume='%s', nlvl_from=%d, nlvl_to=%d\n",
01825 st->index, dref->path, dref->dir, dref->filename,
01826 dref->volume, dref->nlvl_from, dref->nlvl_to);
01827 } else
01828 sc->pb = c->fc->pb;
01829
01830 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
01831 if (!st->sample_aspect_ratio.num &&
01832 (st->codec->width != sc->width || st->codec->height != sc->height)) {
01833 st->sample_aspect_ratio = av_d2q(((double)st->codec->height * sc->width) /
01834 ((double)st->codec->width * sc->height), INT_MAX);
01835 }
01836
01837 av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
01838 sc->time_scale*st->nb_frames, st->duration, INT_MAX);
01839
01840 if (sc->stts_count == 1 || (sc->stts_count == 2 && sc->stts_data[1].count == 1))
01841 av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den,
01842 sc->time_scale, sc->stts_data[0].duration, INT_MAX);
01843 }
01844
01845 switch (st->codec->codec_id) {
01846 #if CONFIG_H261_DECODER
01847 case CODEC_ID_H261:
01848 #endif
01849 #if CONFIG_H263_DECODER
01850 case CODEC_ID_H263:
01851 #endif
01852 #if CONFIG_H264_DECODER
01853 case CODEC_ID_H264:
01854 #endif
01855 #if CONFIG_MPEG4_DECODER
01856 case CODEC_ID_MPEG4:
01857 #endif
01858 st->codec->width = 0;
01859 st->codec->height= 0;
01860 break;
01861 }
01862
01863
01864 av_freep(&sc->chunk_offsets);
01865 av_freep(&sc->stsc_data);
01866 av_freep(&sc->sample_sizes);
01867 av_freep(&sc->keyframes);
01868 av_freep(&sc->stts_data);
01869 av_freep(&sc->stps_data);
01870
01871 return 0;
01872 }
01873
01874 static int mov_read_ilst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
01875 {
01876 int ret;
01877 c->itunes_metadata = 1;
01878 ret = mov_read_default(c, pb, atom);
01879 c->itunes_metadata = 0;
01880 return ret;
01881 }
01882
01883 static int mov_read_meta(MOVContext *c, AVIOContext *pb, MOVAtom atom)
01884 {
01885 while (atom.size > 8) {
01886 uint32_t tag = avio_rl32(pb);
01887 atom.size -= 4;
01888 if (tag == MKTAG('h','d','l','r')) {
01889 avio_seek(pb, -8, SEEK_CUR);
01890 atom.size += 8;
01891 return mov_read_default(c, pb, atom);
01892 }
01893 }
01894 return 0;
01895 }
01896
01897 static int mov_read_tkhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
01898 {
01899 int i;
01900 int width;
01901 int height;
01902 int64_t disp_transform[2];
01903 int display_matrix[3][2];
01904 AVStream *st;
01905 MOVStreamContext *sc;
01906 int version;
01907
01908 if (c->fc->nb_streams < 1)
01909 return 0;
01910 st = c->fc->streams[c->fc->nb_streams-1];
01911 sc = st->priv_data;
01912
01913 version = avio_r8(pb);
01914 avio_rb24(pb);
01915
01916
01917
01918
01919
01920
01921
01922 if (version == 1) {
01923 avio_rb64(pb);
01924 avio_rb64(pb);
01925 } else {
01926 avio_rb32(pb);
01927 avio_rb32(pb);
01928 }
01929 st->id = (int)avio_rb32(pb);
01930 avio_rb32(pb);
01931
01932
01933 (version == 1) ? avio_rb64(pb) : avio_rb32(pb);
01934 avio_rb32(pb);
01935 avio_rb32(pb);
01936
01937 avio_rb16(pb);
01938 avio_rb16(pb);
01939 avio_rb16(pb);
01940 avio_rb16(pb);
01941
01942
01943
01944
01945 for (i = 0; i < 3; i++) {
01946 display_matrix[i][0] = avio_rb32(pb);
01947 display_matrix[i][1] = avio_rb32(pb);
01948 avio_rb32(pb);
01949 }
01950
01951 width = avio_rb32(pb);
01952 height = avio_rb32(pb);
01953 sc->width = width >> 16;
01954 sc->height = height >> 16;
01955
01956 if (display_matrix[0][0] == -65536 && display_matrix[1][1] == -65536) {
01957 av_dict_set(&st->metadata, "rotate", "180", 0);
01958 }
01959
01960
01961
01962
01963
01964 if (width && height &&
01965 ((display_matrix[0][0] != 65536 ||
01966 display_matrix[1][1] != 65536) &&
01967 !display_matrix[0][1] &&
01968 !display_matrix[1][0] &&
01969 !display_matrix[2][0] && !display_matrix[2][1])) {
01970 for (i = 0; i < 2; i++)
01971 disp_transform[i] =
01972 (int64_t) width * display_matrix[0][i] +
01973 (int64_t) height * display_matrix[1][i] +
01974 ((int64_t) display_matrix[2][i] << 16);
01975
01976
01977 st->sample_aspect_ratio = av_d2q(
01978 ((double) disp_transform[0] * height) /
01979 ((double) disp_transform[1] * width), INT_MAX);
01980 }
01981 return 0;
01982 }
01983
01984 static int mov_read_tfhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
01985 {
01986 MOVFragment *frag = &c->fragment;
01987 MOVTrackExt *trex = NULL;
01988 int flags, track_id, i;
01989
01990 avio_r8(pb);
01991 flags = avio_rb24(pb);
01992
01993 track_id = avio_rb32(pb);
01994 if (!track_id)
01995 return -1;
01996 frag->track_id = track_id;
01997 for (i = 0; i < c->trex_count; i++)
01998 if (c->trex_data[i].track_id == frag->track_id) {
01999 trex = &c->trex_data[i];
02000 break;
02001 }
02002 if (!trex) {
02003 av_log(c->fc, AV_LOG_ERROR, "could not find corresponding trex\n");
02004 return -1;
02005 }
02006
02007 if (flags & 0x01) frag->base_data_offset = avio_rb64(pb);
02008 else frag->base_data_offset = frag->moof_offset;
02009 if (flags & 0x02) frag->stsd_id = avio_rb32(pb);
02010 else frag->stsd_id = trex->stsd_id;
02011
02012 frag->duration = flags & 0x08 ? avio_rb32(pb) : trex->duration;
02013 frag->size = flags & 0x10 ? avio_rb32(pb) : trex->size;
02014 frag->flags = flags & 0x20 ? avio_rb32(pb) : trex->flags;
02015 av_dlog(c->fc, "frag flags 0x%x\n", frag->flags);
02016 return 0;
02017 }
02018
02019 static int mov_read_chap(MOVContext *c, AVIOContext *pb, MOVAtom atom)
02020 {
02021 c->chapter_track = avio_rb32(pb);
02022 return 0;
02023 }
02024
02025 static int mov_read_trex(MOVContext *c, AVIOContext *pb, MOVAtom atom)
02026 {
02027 MOVTrackExt *trex;
02028
02029 if ((uint64_t)c->trex_count+1 >= UINT_MAX / sizeof(*c->trex_data))
02030 return -1;
02031 trex = av_realloc(c->trex_data, (c->trex_count+1)*sizeof(*c->trex_data));
02032 if (!trex)
02033 return AVERROR(ENOMEM);
02034 c->trex_data = trex;
02035 trex = &c->trex_data[c->trex_count++];
02036 avio_r8(pb);
02037 avio_rb24(pb);
02038 trex->track_id = avio_rb32(pb);
02039 trex->stsd_id = avio_rb32(pb);
02040 trex->duration = avio_rb32(pb);
02041 trex->size = avio_rb32(pb);
02042 trex->flags = avio_rb32(pb);
02043 return 0;
02044 }
02045
02046 static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom)
02047 {
02048 MOVFragment *frag = &c->fragment;
02049 AVStream *st = NULL;
02050 MOVStreamContext *sc;
02051 MOVStts *ctts_data;
02052 uint64_t offset;
02053 int64_t dts;
02054 int data_offset = 0;
02055 unsigned entries, first_sample_flags = frag->flags;
02056 int flags, distance, i;
02057
02058 for (i = 0; i < c->fc->nb_streams; i++) {
02059 if (c->fc->streams[i]->id == frag->track_id) {
02060 st = c->fc->streams[i];
02061 break;
02062 }
02063 }
02064 if (!st) {
02065 av_log(c->fc, AV_LOG_ERROR, "could not find corresponding track id %d\n", frag->track_id);
02066 return -1;
02067 }
02068 sc = st->priv_data;
02069 if (sc->pseudo_stream_id+1 != frag->stsd_id)
02070 return 0;
02071 avio_r8(pb);
02072 flags = avio_rb24(pb);
02073 entries = avio_rb32(pb);
02074 av_dlog(c->fc, "flags 0x%x entries %d\n", flags, entries);
02075
02076
02077
02078
02079
02080
02081 if (!sc->ctts_count && sc->sample_count)
02082 {
02083
02084 ctts_data = av_malloc(sizeof(*sc->ctts_data));
02085 if (!ctts_data)
02086 return AVERROR(ENOMEM);
02087 sc->ctts_data = ctts_data;
02088 sc->ctts_data[sc->ctts_count].count = sc->sample_count;
02089 sc->ctts_data[sc->ctts_count].duration = 0;
02090 sc->ctts_count++;
02091 }
02092 if ((uint64_t)entries+sc->ctts_count >= UINT_MAX/sizeof(*sc->ctts_data))
02093 return -1;
02094 ctts_data = av_realloc(sc->ctts_data,
02095 (entries+sc->ctts_count)*sizeof(*sc->ctts_data));
02096 if (!ctts_data)
02097 return AVERROR(ENOMEM);
02098 sc->ctts_data = ctts_data;
02099
02100 if (flags & 0x001) data_offset = avio_rb32(pb);
02101 if (flags & 0x004) first_sample_flags = avio_rb32(pb);
02102 dts = st->duration - sc->time_offset;
02103 offset = frag->base_data_offset + data_offset;
02104 distance = 0;
02105 av_dlog(c->fc, "first sample flags 0x%x\n", first_sample_flags);
02106 for (i = 0; i < entries; i++) {
02107 unsigned sample_size = frag->size;
02108 int sample_flags = i ? frag->flags : first_sample_flags;
02109 unsigned sample_duration = frag->duration;
02110 int keyframe;
02111
02112 if (flags & 0x100) sample_duration = avio_rb32(pb);
02113 if (flags & 0x200) sample_size = avio_rb32(pb);
02114 if (flags & 0x400) sample_flags = avio_rb32(pb);
02115 sc->ctts_data[sc->ctts_count].count = 1;
02116 sc->ctts_data[sc->ctts_count].duration = (flags & 0x800) ? avio_rb32(pb) : 0;
02117 sc->ctts_count++;
02118 if ((keyframe = st->codec->codec_type == AVMEDIA_TYPE_AUDIO ||
02119 (flags & 0x004 && !i && !sample_flags) || sample_flags & 0x2000000))
02120 distance = 0;
02121 av_add_index_entry(st, offset, dts, sample_size, distance,
02122 keyframe ? AVINDEX_KEYFRAME : 0);
02123 av_dlog(c->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
02124 "size %d, distance %d, keyframe %d\n", st->index, sc->sample_count+i,
02125 offset, dts, sample_size, distance, keyframe);
02126 distance++;
02127 dts += sample_duration;
02128 offset += sample_size;
02129 }
02130 frag->moof_offset = offset;
02131 st->duration = dts + sc->time_offset;
02132 return 0;
02133 }
02134
02135
02136
02137
02138 static int mov_read_wide(MOVContext *c, AVIOContext *pb, MOVAtom atom)
02139 {
02140 int err;
02141
02142 if (atom.size < 8)
02143 return 0;
02144 if (avio_rb32(pb) != 0) {
02145 avio_skip(pb, atom.size - 4);
02146 return 0;
02147 }
02148 atom.type = avio_rl32(pb);
02149 atom.size -= 8;
02150 if (atom.type != MKTAG('m','d','a','t')) {
02151 avio_skip(pb, atom.size);
02152 return 0;
02153 }
02154 err = mov_read_mdat(c, pb, atom);
02155 return err;
02156 }
02157
02158 static int mov_read_cmov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
02159 {
02160 #if CONFIG_ZLIB
02161 AVIOContext ctx;
02162 uint8_t *cmov_data;
02163 uint8_t *moov_data;
02164 long cmov_len, moov_len;
02165 int ret = -1;
02166
02167 avio_rb32(pb);
02168 if (avio_rl32(pb) != MKTAG('d','c','o','m'))
02169 return -1;
02170 if (avio_rl32(pb) != MKTAG('z','l','i','b')) {
02171 av_log(c->fc, AV_LOG_ERROR, "unknown compression for cmov atom !");
02172 return -1;
02173 }
02174 avio_rb32(pb);
02175 if (avio_rl32(pb) != MKTAG('c','m','v','d'))
02176 return -1;
02177 moov_len = avio_rb32(pb);
02178 cmov_len = atom.size - 6 * 4;
02179
02180 cmov_data = av_malloc(cmov_len);
02181 if (!cmov_data)
02182 return AVERROR(ENOMEM);
02183 moov_data = av_malloc(moov_len);
02184 if (!moov_data) {
02185 av_free(cmov_data);
02186 return AVERROR(ENOMEM);
02187 }
02188 avio_read(pb, cmov_data, cmov_len);
02189 if(uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
02190 goto free_and_return;
02191 if(ffio_init_context(&ctx, moov_data, moov_len, 0, NULL, NULL, NULL, NULL) != 0)
02192 goto free_and_return;
02193 atom.type = MKTAG('m','o','o','v');
02194 atom.size = moov_len;
02195 ret = mov_read_default(c, &ctx, atom);
02196 free_and_return:
02197 av_free(moov_data);
02198 av_free(cmov_data);
02199 return ret;
02200 #else
02201 av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n");
02202 return -1;
02203 #endif
02204 }
02205
02206
02207 static int mov_read_elst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
02208 {
02209 MOVStreamContext *sc;
02210 int i, edit_count, version;
02211
02212 if (c->fc->nb_streams < 1)
02213 return 0;
02214 sc = c->fc->streams[c->fc->nb_streams-1]->priv_data;
02215
02216 version = avio_r8(pb);
02217 avio_rb24(pb);
02218 edit_count = avio_rb32(pb);
02219
02220 if((uint64_t)edit_count*12+8 > atom.size)
02221 return -1;
02222
02223 for(i=0; i<edit_count; i++){
02224 int64_t time;
02225 int64_t duration;
02226 if (version == 1) {
02227 duration = avio_rb64(pb);
02228 time = avio_rb64(pb);
02229 } else {
02230 duration = avio_rb32(pb);
02231 time = (int32_t)avio_rb32(pb);
02232 }
02233 avio_rb32(pb);
02234 if (i == 0 && time >= -1) {
02235 sc->time_offset = time != -1 ? time : -duration;
02236 }
02237 }
02238
02239 if(edit_count > 1)
02240 av_log(c->fc, AV_LOG_WARNING, "multiple edit list entries, "
02241 "a/v desync might occur, patch welcome\n");
02242
02243 av_dlog(c->fc, "track[%i].edit_count = %i\n", c->fc->nb_streams-1, edit_count);
02244 return 0;
02245 }
02246
02247 static int mov_read_chan(MOVContext *c, AVIOContext *pb, MOVAtom atom)
02248 {
02249 if (atom.size < 16)
02250 return AVERROR_INVALIDDATA;
02251 avio_skip(pb, 4);
02252 ff_mov_read_chan(c->fc, atom.size - 4, c->fc->streams[0]->codec);
02253 return 0;
02254 }
02255
02256 static const MOVParseTableEntry mov_default_parse_table[] = {
02257 { MKTAG('a','v','s','s'), mov_read_avss },
02258 { MKTAG('c','h','p','l'), mov_read_chpl },
02259 { MKTAG('c','o','6','4'), mov_read_stco },
02260 { MKTAG('c','t','t','s'), mov_read_ctts },
02261 { MKTAG('d','i','n','f'), mov_read_default },
02262 { MKTAG('d','r','e','f'), mov_read_dref },
02263 { MKTAG('e','d','t','s'), mov_read_default },
02264 { MKTAG('e','l','s','t'), mov_read_elst },
02265 { MKTAG('e','n','d','a'), mov_read_enda },
02266 { MKTAG('f','i','e','l'), mov_read_fiel },
02267 { MKTAG('f','t','y','p'), mov_read_ftyp },
02268 { MKTAG('g','l','b','l'), mov_read_glbl },
02269 { MKTAG('h','d','l','r'), mov_read_hdlr },
02270 { MKTAG('i','l','s','t'), mov_read_ilst },
02271 { MKTAG('j','p','2','h'), mov_read_jp2h },
02272 { MKTAG('m','d','a','t'), mov_read_mdat },
02273 { MKTAG('m','d','h','d'), mov_read_mdhd },
02274 { MKTAG('m','d','i','a'), mov_read_default },
02275 { MKTAG('m','e','t','a'), mov_read_meta },
02276 { MKTAG('m','i','n','f'), mov_read_default },
02277 { MKTAG('m','o','o','f'), mov_read_moof },
02278 { MKTAG('m','o','o','v'), mov_read_moov },
02279 { MKTAG('m','v','e','x'), mov_read_default },
02280 { MKTAG('m','v','h','d'), mov_read_mvhd },
02281 { MKTAG('S','M','I',' '), mov_read_smi },
02282 { MKTAG('a','l','a','c'), mov_read_alac },
02283 { MKTAG('a','v','c','C'), mov_read_glbl },
02284 { MKTAG('p','a','s','p'), mov_read_pasp },
02285 { MKTAG('s','t','b','l'), mov_read_default },
02286 { MKTAG('s','t','c','o'), mov_read_stco },
02287 { MKTAG('s','t','p','s'), mov_read_stps },
02288 { MKTAG('s','t','r','f'), mov_read_strf },
02289 { MKTAG('s','t','s','c'), mov_read_stsc },
02290 { MKTAG('s','t','s','d'), mov_read_stsd },
02291 { MKTAG('s','t','s','s'), mov_read_stss },
02292 { MKTAG('s','t','s','z'), mov_read_stsz },
02293 { MKTAG('s','t','t','s'), mov_read_stts },
02294 { MKTAG('s','t','z','2'), mov_read_stsz },
02295 { MKTAG('t','k','h','d'), mov_read_tkhd },
02296 { MKTAG('t','f','h','d'), mov_read_tfhd },
02297 { MKTAG('t','r','a','k'), mov_read_trak },
02298 { MKTAG('t','r','a','f'), mov_read_default },
02299 { MKTAG('t','r','e','f'), mov_read_default },
02300 { MKTAG('c','h','a','p'), mov_read_chap },
02301 { MKTAG('t','r','e','x'), mov_read_trex },
02302 { MKTAG('t','r','u','n'), mov_read_trun },
02303 { MKTAG('u','d','t','a'), mov_read_default },
02304 { MKTAG('w','a','v','e'), mov_read_wave },
02305 { MKTAG('e','s','d','s'), mov_read_esds },
02306 { MKTAG('d','a','c','3'), mov_read_dac3 },
02307 { MKTAG('w','i','d','e'), mov_read_wide },
02308 { MKTAG('w','f','e','x'), mov_read_wfex },
02309 { MKTAG('c','m','o','v'), mov_read_cmov },
02310 { MKTAG('c','h','a','n'), mov_read_chan },
02311 { 0, NULL }
02312 };
02313
02314 static int mov_probe(AVProbeData *p)
02315 {
02316 unsigned int offset;
02317 uint32_t tag;
02318 int score = 0;
02319
02320
02321 offset = 0;
02322 for(;;) {
02323
02324 if ((offset + 8) > (unsigned int)p->buf_size)
02325 return score;
02326 tag = AV_RL32(p->buf + offset + 4);
02327 switch(tag) {
02328
02329 case MKTAG('j','P',' ',' '):
02330 case MKTAG('m','o','o','v'):
02331 case MKTAG('m','d','a','t'):
02332 case MKTAG('p','n','o','t'):
02333 case MKTAG('u','d','t','a'):
02334 case MKTAG('f','t','y','p'):
02335 return AVPROBE_SCORE_MAX;
02336
02337 case MKTAG('e','d','i','w'):
02338 case MKTAG('w','i','d','e'):
02339 case MKTAG('f','r','e','e'):
02340 case MKTAG('j','u','n','k'):
02341 case MKTAG('p','i','c','t'):
02342 return AVPROBE_SCORE_MAX - 5;
02343 case MKTAG(0x82,0x82,0x7f,0x7d):
02344 case MKTAG('s','k','i','p'):
02345 case MKTAG('u','u','i','d'):
02346 case MKTAG('p','r','f','l'):
02347 offset = AV_RB32(p->buf+offset) + offset;
02348
02349 score = AVPROBE_SCORE_MAX - 50;
02350 break;
02351 default:
02352
02353 return score;
02354 }
02355 }
02356 return score;
02357 }
02358
02359
02360 static void mov_read_chapters(AVFormatContext *s)
02361 {
02362 MOVContext *mov = s->priv_data;
02363 AVStream *st = NULL;
02364 MOVStreamContext *sc;
02365 int64_t cur_pos;
02366 int i;
02367
02368 for (i = 0; i < s->nb_streams; i++)
02369 if (s->streams[i]->id == mov->chapter_track) {
02370 st = s->streams[i];
02371 break;
02372 }
02373 if (!st) {
02374 av_log(s, AV_LOG_ERROR, "Referenced QT chapter track not found\n");
02375 return;
02376 }
02377
02378 st->discard = AVDISCARD_ALL;
02379 sc = st->priv_data;
02380 cur_pos = avio_tell(sc->pb);
02381
02382 for (i = 0; i < st->nb_index_entries; i++) {
02383 AVIndexEntry *sample = &st->index_entries[i];
02384 int64_t end = i+1 < st->nb_index_entries ? st->index_entries[i+1].timestamp : st->duration;
02385 uint8_t *title;
02386 uint16_t ch;
02387 int len, title_len;
02388
02389 if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
02390 av_log(s, AV_LOG_ERROR, "Chapter %d not found in file\n", i);
02391 goto finish;
02392 }
02393
02394
02395 len = avio_rb16(sc->pb);
02396 if (len > sample->size-2)
02397 continue;
02398 title_len = 2*len + 1;
02399 if (!(title = av_mallocz(title_len)))
02400 goto finish;
02401
02402
02403
02404
02405 if (!len) {
02406 title[0] = 0;
02407 } else {
02408 ch = avio_rb16(sc->pb);
02409 if (ch == 0xfeff)
02410 avio_get_str16be(sc->pb, len, title, title_len);
02411 else if (ch == 0xfffe)
02412 avio_get_str16le(sc->pb, len, title, title_len);
02413 else {
02414 AV_WB16(title, ch);
02415 if (len == 1 || len == 2)
02416 title[len] = 0;
02417 else
02418 get_strz(sc->pb, title + 2, len - 1);
02419 }
02420 }
02421
02422 ff_new_chapter(s, i, st->time_base, sample->timestamp, end, title);
02423 av_freep(&title);
02424 }
02425 finish:
02426 avio_seek(sc->pb, cur_pos, SEEK_SET);
02427 }
02428
02429 static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap)
02430 {
02431 MOVContext *mov = s->priv_data;
02432 AVIOContext *pb = s->pb;
02433 int err;
02434 MOVAtom atom = { AV_RL32("root") };
02435
02436 mov->fc = s;
02437
02438 if(pb->seekable)
02439 atom.size = avio_size(pb);
02440 else
02441 atom.size = INT64_MAX;
02442
02443
02444 if ((err = mov_read_default(mov, pb, atom)) < 0) {
02445 av_log(s, AV_LOG_ERROR, "error reading header: %d\n", err);
02446 return err;
02447 }
02448 if (!mov->found_moov) {
02449 av_log(s, AV_LOG_ERROR, "moov atom not found\n");
02450 return -1;
02451 }
02452 av_dlog(mov->fc, "on_parse_exit_offset=%"PRId64"\n", avio_tell(pb));
02453
02454 if (pb->seekable && mov->chapter_track > 0)
02455 mov_read_chapters(s);
02456
02457 return 0;
02458 }
02459
02460 static AVIndexEntry *mov_find_next_sample(AVFormatContext *s, AVStream **st)
02461 {
02462 AVIndexEntry *sample = NULL;
02463 int64_t best_dts = INT64_MAX;
02464 int i;
02465 for (i = 0; i < s->nb_streams; i++) {
02466 AVStream *avst = s->streams[i];
02467 MOVStreamContext *msc = avst->priv_data;
02468 if (msc->pb && msc->current_sample < avst->nb_index_entries) {
02469 AVIndexEntry *current_sample = &avst->index_entries[msc->current_sample];
02470 int64_t dts = av_rescale(current_sample->timestamp, AV_TIME_BASE, msc->time_scale);
02471 av_dlog(s, "stream %d, sample %d, dts %"PRId64"\n", i, msc->current_sample, dts);
02472 if (!sample || (!s->pb->seekable && current_sample->pos < sample->pos) ||
02473 (s->pb->seekable &&
02474 ((msc->pb != s->pb && dts < best_dts) || (msc->pb == s->pb &&
02475 ((FFABS(best_dts - dts) <= AV_TIME_BASE && current_sample->pos < sample->pos) ||
02476 (FFABS(best_dts - dts) > AV_TIME_BASE && dts < best_dts)))))) {
02477 sample = current_sample;
02478 best_dts = dts;
02479 *st = avst;
02480 }
02481 }
02482 }
02483 return sample;
02484 }
02485
02486 static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
02487 {
02488 MOVContext *mov = s->priv_data;
02489 MOVStreamContext *sc;
02490 AVIndexEntry *sample;
02491 AVStream *st = NULL;
02492 int ret;
02493 retry:
02494 sample = mov_find_next_sample(s, &st);
02495 if (!sample) {
02496 mov->found_mdat = 0;
02497 if (s->pb->seekable||
02498 mov_read_default(mov, s->pb, (MOVAtom){ AV_RL32("root"), INT64_MAX }) < 0 ||
02499 url_feof(s->pb))
02500 return AVERROR_EOF;
02501 av_dlog(s, "read fragments, offset 0x%"PRIx64"\n", avio_tell(s->pb));
02502 goto retry;
02503 }
02504 sc = st->priv_data;
02505
02506 sc->current_sample++;
02507
02508 if (st->discard != AVDISCARD_ALL) {
02509 if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
02510 av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n",
02511 sc->ffindex, sample->pos);
02512 return -1;
02513 }
02514 ret = av_get_packet(sc->pb, pkt, sample->size);
02515 if (ret < 0)
02516 return ret;
02517 if (sc->has_palette) {
02518 uint8_t *pal;
02519
02520 pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE);
02521 if (!pal) {
02522 av_log(mov->fc, AV_LOG_ERROR, "Cannot append palette to packet\n");
02523 } else {
02524 memcpy(pal, sc->palette, AVPALETTE_SIZE);
02525 sc->has_palette = 0;
02526 }
02527 }
02528 #if CONFIG_DV_DEMUXER
02529 if (mov->dv_demux && sc->dv_audio_container) {
02530 dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size, pkt->pos);
02531 av_free(pkt->data);
02532 pkt->size = 0;
02533 ret = dv_get_packet(mov->dv_demux, pkt);
02534 if (ret < 0)
02535 return ret;
02536 }
02537 #endif
02538 }
02539
02540 pkt->stream_index = sc->ffindex;
02541 pkt->dts = sample->timestamp;
02542 if (sc->ctts_data) {
02543 pkt->pts = pkt->dts + sc->dts_shift + sc->ctts_data[sc->ctts_index].duration;
02544
02545 sc->ctts_sample++;
02546 if (sc->ctts_index < sc->ctts_count &&
02547 sc->ctts_data[sc->ctts_index].count == sc->ctts_sample) {
02548 sc->ctts_index++;
02549 sc->ctts_sample = 0;
02550 }
02551 if (sc->wrong_dts)
02552 pkt->dts = AV_NOPTS_VALUE;
02553 } else {
02554 int64_t next_dts = (sc->current_sample < st->nb_index_entries) ?
02555 st->index_entries[sc->current_sample].timestamp : st->duration;
02556 pkt->duration = next_dts - pkt->dts;
02557 pkt->pts = pkt->dts;
02558 }
02559 if (st->discard == AVDISCARD_ALL)
02560 goto retry;
02561 pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? AV_PKT_FLAG_KEY : 0;
02562 pkt->pos = sample->pos;
02563 av_dlog(s, "stream %d, pts %"PRId64", dts %"PRId64", pos 0x%"PRIx64", duration %d\n",
02564 pkt->stream_index, pkt->pts, pkt->dts, pkt->pos, pkt->duration);
02565 return 0;
02566 }
02567
02568 static int mov_seek_stream(AVFormatContext *s, AVStream *st, int64_t timestamp, int flags)
02569 {
02570 MOVStreamContext *sc = st->priv_data;
02571 int sample, time_sample;
02572 int i;
02573
02574 sample = av_index_search_timestamp(st, timestamp, flags);
02575 av_dlog(s, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample);
02576 if (sample < 0 && st->nb_index_entries && timestamp < st->index_entries[0].timestamp)
02577 sample = 0;
02578 if (sample < 0)
02579 return -1;
02580 sc->current_sample = sample;
02581 av_dlog(s, "stream %d, found sample %d\n", st->index, sc->current_sample);
02582
02583 if (sc->ctts_data) {
02584 time_sample = 0;
02585 for (i = 0; i < sc->ctts_count; i++) {
02586 int next = time_sample + sc->ctts_data[i].count;
02587 if (next > sc->current_sample) {
02588 sc->ctts_index = i;
02589 sc->ctts_sample = sc->current_sample - time_sample;
02590 break;
02591 }
02592 time_sample = next;
02593 }
02594 }
02595 return sample;
02596 }
02597
02598 static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
02599 {
02600 AVStream *st;
02601 int64_t seek_timestamp, timestamp;
02602 int sample;
02603 int i;
02604
02605 if (stream_index >= s->nb_streams)
02606 return -1;
02607 if (sample_time < 0)
02608 sample_time = 0;
02609
02610 st = s->streams[stream_index];
02611 sample = mov_seek_stream(s, st, sample_time, flags);
02612 if (sample < 0)
02613 return -1;
02614
02615
02616 seek_timestamp = st->index_entries[sample].timestamp;
02617
02618 for (i = 0; i < s->nb_streams; i++) {
02619 st = s->streams[i];
02620 if (stream_index == i)
02621 continue;
02622
02623 timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base);
02624 mov_seek_stream(s, st, timestamp, flags);
02625 }
02626 return 0;
02627 }
02628
02629 static int mov_read_close(AVFormatContext *s)
02630 {
02631 MOVContext *mov = s->priv_data;
02632 int i, j;
02633
02634 for (i = 0; i < s->nb_streams; i++) {
02635 AVStream *st = s->streams[i];
02636 MOVStreamContext *sc = st->priv_data;
02637
02638 av_freep(&sc->ctts_data);
02639 for (j = 0; j < sc->drefs_count; j++) {
02640 av_freep(&sc->drefs[j].path);
02641 av_freep(&sc->drefs[j].dir);
02642 }
02643 av_freep(&sc->drefs);
02644 if (sc->pb && sc->pb != s->pb)
02645 avio_close(sc->pb);
02646
02647 av_freep(&st->codec->palctrl);
02648 }
02649
02650 if (mov->dv_demux) {
02651 for(i = 0; i < mov->dv_fctx->nb_streams; i++) {
02652 av_freep(&mov->dv_fctx->streams[i]->codec);
02653 av_freep(&mov->dv_fctx->streams[i]);
02654 }
02655 av_freep(&mov->dv_fctx);
02656 av_freep(&mov->dv_demux);
02657 }
02658
02659 av_freep(&mov->trex_data);
02660
02661 return 0;
02662 }
02663
02664 AVInputFormat ff_mov_demuxer = {
02665 "mov,mp4,m4a,3gp,3g2,mj2",
02666 NULL_IF_CONFIG_SMALL("QuickTime/MPEG-4/Motion JPEG 2000 format"),
02667 sizeof(MOVContext),
02668 mov_probe,
02669 mov_read_header,
02670 mov_read_packet,
02671 mov_read_close,
02672 mov_read_seek,
02673 };