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