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