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 "avformat.h"
00027 #include "riff.h"
00028 #include "isom.h"
00029 #include "dv.h"
00030
00031 #ifdef CONFIG_ZLIB
00032 #include <zlib.h>
00033 #endif
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 #include "qtpalette.h"
00056
00057
00058 #undef NDEBUG
00059 #include <assert.h>
00060
00061
00062
00063
00064
00065
00066 typedef struct {
00067 int first;
00068 int count;
00069 int id;
00070 } MOV_stsc_t;
00071
00072 typedef struct {
00073 uint32_t type;
00074 int64_t offset;
00075 int64_t size;
00076 } MOV_atom_t;
00077
00078 typedef struct {
00079 offset_t offset;
00080 int64_t size;
00081 } MOV_mdat_t;
00082
00083 struct MOVParseTableEntry;
00084
00085 typedef struct MOVStreamContext {
00086 int ffindex;
00087 int next_chunk;
00088 unsigned int chunk_count;
00089 int64_t *chunk_offsets;
00090 unsigned int stts_count;
00091 MOV_stts_t *stts_data;
00092 unsigned int ctts_count;
00093 MOV_stts_t *ctts_data;
00094 unsigned int edit_count;
00095 unsigned int sample_to_chunk_sz;
00096 MOV_stsc_t *sample_to_chunk;
00097 int sample_to_ctime_index;
00098 int sample_to_ctime_sample;
00099 unsigned int sample_size;
00100 unsigned int sample_count;
00101 int *sample_sizes;
00102 unsigned int keyframe_count;
00103 int *keyframes;
00104 int time_scale;
00105 int time_rate;
00106 int current_sample;
00107 unsigned int bytes_per_frame;
00108 unsigned int samples_per_frame;
00109 int dv_audio_container;
00110 int pseudo_stream_id;
00111 } MOVStreamContext;
00112
00113 typedef struct MOVContext {
00114 AVFormatContext *fc;
00115 int time_scale;
00116 int64_t duration;
00117 int found_moov;
00118 int found_mdat;
00119 AVPaletteControl palette_control;
00120 MOV_mdat_t *mdat_list;
00121 int mdat_count;
00122 DVDemuxContext *dv_demux;
00123 AVFormatContext *dv_fctx;
00124 int isom;
00125 } MOVContext;
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137 typedef struct MOVParseTableEntry {
00138 uint32_t type;
00139 int (*parse)(MOVContext *ctx, ByteIOContext *pb, MOV_atom_t atom);
00140 } MOVParseTableEntry;
00141
00142 static const MOVParseTableEntry mov_default_parse_table[];
00143
00144 static int mov_read_default(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
00145 {
00146 int64_t total_size = 0;
00147 MOV_atom_t a;
00148 int i;
00149 int err = 0;
00150
00151 a.offset = atom.offset;
00152
00153 if (atom.size < 0)
00154 atom.size = INT64_MAX;
00155 while(((total_size + 8) < atom.size) && !url_feof(pb) && !err) {
00156 a.size = atom.size;
00157 a.type=0;
00158 if(atom.size >= 8) {
00159 a.size = get_be32(pb);
00160 a.type = get_le32(pb);
00161 }
00162 total_size += 8;
00163 a.offset += 8;
00164 dprintf(c->fc, "type: %08x %.4s sz: %"PRIx64" %"PRIx64" %"PRIx64"\n",
00165 a.type, (char*)&a.type, a.size, atom.size, total_size);
00166 if (a.size == 1) {
00167 a.size = get_be64(pb) - 8;
00168 a.offset += 8;
00169 total_size += 8;
00170 }
00171 if (a.size == 0) {
00172 a.size = atom.size - total_size;
00173 if (a.size <= 8)
00174 break;
00175 }
00176 a.size -= 8;
00177 if(a.size < 0)
00178 break;
00179 a.size = FFMIN(a.size, atom.size - total_size);
00180
00181 for (i = 0; mov_default_parse_table[i].type != 0
00182 && mov_default_parse_table[i].type != a.type; i++)
00183 ;
00184
00185 if (mov_default_parse_table[i].type == 0) {
00186 url_fskip(pb, a.size);
00187 } else {
00188 offset_t start_pos = url_ftell(pb);
00189 int64_t left;
00190 err = mov_default_parse_table[i].parse(c, pb, a);
00191 if (c->found_moov && c->found_mdat)
00192 break;
00193 left = a.size - url_ftell(pb) + start_pos;
00194 if (left > 0)
00195 url_fskip(pb, left);
00196 }
00197
00198 a.offset += a.size;
00199 total_size += a.size;
00200 }
00201
00202 if (!err && total_size < atom.size && atom.size < 0x7ffff) {
00203 url_fskip(pb, atom.size - total_size);
00204 }
00205
00206 return err;
00207 }
00208
00209 static int mov_read_hdlr(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
00210 {
00211 AVStream *st;
00212 uint32_t type;
00213 uint32_t ctype;
00214
00215 if (c->fc->nb_streams < 1)
00216 return 0;
00217 st = c->fc->streams[c->fc->nb_streams-1];
00218
00219 get_byte(pb);
00220 get_byte(pb); get_byte(pb); get_byte(pb);
00221
00222
00223 ctype = get_le32(pb);
00224 type = get_le32(pb);
00225
00226 dprintf(c->fc, "ctype= %c%c%c%c (0x%08x)\n", *((char *)&ctype), ((char *)&ctype)[1],
00227 ((char *)&ctype)[2], ((char *)&ctype)[3], (int) ctype);
00228 dprintf(c->fc, "stype= %c%c%c%c\n",
00229 *((char *)&type), ((char *)&type)[1], ((char *)&type)[2], ((char *)&type)[3]);
00230 if(!ctype)
00231 c->isom = 1;
00232 if(type == MKTAG('v', 'i', 'd', 'e'))
00233 st->codec->codec_type = CODEC_TYPE_VIDEO;
00234 else if(type == MKTAG('s', 'o', 'u', 'n'))
00235 st->codec->codec_type = CODEC_TYPE_AUDIO;
00236 else if(type == MKTAG('m', '1', 'a', ' '))
00237 st->codec->codec_id = CODEC_ID_MP2;
00238 else if(type == MKTAG('s', 'u', 'b', 'p')) {
00239 st->codec->codec_type = CODEC_TYPE_SUBTITLE;
00240 }
00241 get_be32(pb);
00242 get_be32(pb);
00243 get_be32(pb);
00244
00245 if(atom.size <= 24)
00246 return 0;
00247
00248 url_fskip(pb, atom.size - (url_ftell(pb) - atom.offset));
00249 return 0;
00250 }
00251
00252 static int mp4_read_descr_len(ByteIOContext *pb)
00253 {
00254 int len = 0;
00255 int count = 4;
00256 while (count--) {
00257 int c = get_byte(pb);
00258 len = (len << 7) | (c & 0x7f);
00259 if (!(c & 0x80))
00260 break;
00261 }
00262 return len;
00263 }
00264
00265 static int mp4_read_descr(MOVContext *c, ByteIOContext *pb, int *tag)
00266 {
00267 int len;
00268 *tag = get_byte(pb);
00269 len = mp4_read_descr_len(pb);
00270 dprintf(c->fc, "MPEG4 description: tag=0x%02x len=%d\n", *tag, len);
00271 return len;
00272 }
00273
00274 #define MP4ESDescrTag 0x03
00275 #define MP4DecConfigDescrTag 0x04
00276 #define MP4DecSpecificDescrTag 0x05
00277
00278 static int mov_read_esds(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
00279 {
00280 AVStream *st;
00281 int tag, len;
00282
00283 if (c->fc->nb_streams < 1)
00284 return 0;
00285 st = c->fc->streams[c->fc->nb_streams-1];
00286
00287 get_be32(pb);
00288 len = mp4_read_descr(c, pb, &tag);
00289 if (tag == MP4ESDescrTag) {
00290 get_be16(pb);
00291 get_byte(pb);
00292 } else
00293 get_be16(pb);
00294
00295 len = mp4_read_descr(c, pb, &tag);
00296 if (tag == MP4DecConfigDescrTag) {
00297 int object_type_id = get_byte(pb);
00298 get_byte(pb);
00299 get_be24(pb);
00300 get_be32(pb);
00301 get_be32(pb);
00302
00303 st->codec->codec_id= codec_get_id(ff_mp4_obj_type, object_type_id);
00304 dprintf(c->fc, "esds object type id %d\n", object_type_id);
00305 len = mp4_read_descr(c, pb, &tag);
00306 if (tag == MP4DecSpecificDescrTag) {
00307 dprintf(c->fc, "Specific MPEG4 header len=%d\n", len);
00308 st->codec->extradata = av_mallocz(len + FF_INPUT_BUFFER_PADDING_SIZE);
00309 if (st->codec->extradata) {
00310 get_buffer(pb, st->codec->extradata, len);
00311 st->codec->extradata_size = len;
00312
00313 if ((*st->codec->extradata >> 3) == 29) {
00314 st->codec->codec_id = CODEC_ID_MP3ON4;
00315 }
00316 }
00317 }
00318 }
00319 return 0;
00320 }
00321
00322
00323 static int mov_read_mdat(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
00324 {
00325 if(atom.size == 0)
00326 return 0;
00327 c->mdat_list = av_realloc(c->mdat_list, (c->mdat_count + 1) * sizeof(*c->mdat_list));
00328 c->mdat_list[c->mdat_count].offset = atom.offset;
00329 c->mdat_list[c->mdat_count].size = atom.size;
00330 c->mdat_count++;
00331 c->found_mdat=1;
00332 if(c->found_moov)
00333 return 1;
00334 url_fskip(pb, atom.size);
00335 return 0;
00336 }
00337
00338 static int mov_read_ftyp(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
00339 {
00340 uint32_t type = get_le32(pb);
00341
00342 if (type != MKTAG('q','t',' ',' '))
00343 c->isom = 1;
00344 av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type);
00345 get_be32(pb);
00346 url_fskip(pb, atom.size - 8);
00347 return 0;
00348 }
00349
00350
00351 static int mov_read_moov(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
00352 {
00353 if (mov_read_default(c, pb, atom) < 0)
00354 return -1;
00355
00356
00357 c->found_moov=1;
00358 if(c->found_mdat)
00359 return 1;
00360 return 0;
00361 }
00362
00363
00364 static int mov_read_mdhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
00365 {
00366 AVStream *st;
00367 MOVStreamContext *sc;
00368 int version;
00369 int lang;
00370
00371 if (c->fc->nb_streams < 1)
00372 return 0;
00373 st = c->fc->streams[c->fc->nb_streams-1];
00374 sc = st->priv_data;
00375
00376 version = get_byte(pb);
00377 if (version > 1)
00378 return 1;
00379
00380 get_byte(pb); get_byte(pb);
00381 get_byte(pb);
00382
00383 if (version == 1) {
00384 get_be64(pb);
00385 get_be64(pb);
00386 } else {
00387 get_be32(pb);
00388 get_be32(pb);
00389 }
00390
00391 sc->time_scale = get_be32(pb);
00392 st->duration = (version == 1) ? get_be64(pb) : get_be32(pb);
00393
00394 lang = get_be16(pb);
00395 ff_mov_lang_to_iso639(lang, st->language);
00396 get_be16(pb);
00397
00398 return 0;
00399 }
00400
00401 static int mov_read_mvhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
00402 {
00403 int version = get_byte(pb);
00404 get_byte(pb); get_byte(pb); get_byte(pb);
00405
00406 if (version == 1) {
00407 get_be64(pb);
00408 get_be64(pb);
00409 } else {
00410 get_be32(pb);
00411 get_be32(pb);
00412 }
00413 c->time_scale = get_be32(pb);
00414
00415 dprintf(c->fc, "time scale = %i\n", c->time_scale);
00416
00417 c->duration = (version == 1) ? get_be64(pb) : get_be32(pb);
00418 get_be32(pb);
00419
00420 get_be16(pb);
00421
00422 url_fskip(pb, 10);
00423
00424 url_fskip(pb, 36);
00425
00426 get_be32(pb);
00427 get_be32(pb);
00428 get_be32(pb);
00429 get_be32(pb);
00430 get_be32(pb);
00431 get_be32(pb);
00432 get_be32(pb);
00433
00434 return 0;
00435 }
00436
00437 static int mov_read_smi(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
00438 {
00439 AVStream *st;
00440
00441 if (c->fc->nb_streams < 1)
00442 return 0;
00443 st = c->fc->streams[c->fc->nb_streams-1];
00444
00445 if((uint64_t)atom.size > (1<<30))
00446 return -1;
00447
00448
00449
00450 av_free(st->codec->extradata);
00451 st->codec->extradata_size = 0x5a + atom.size;
00452 st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
00453
00454 if (st->codec->extradata) {
00455 memcpy(st->codec->extradata, "SVQ3", 4);
00456 get_buffer(pb, st->codec->extradata + 0x5a, atom.size);
00457 dprintf(c->fc, "Reading SMI %"PRId64" %s\n", atom.size, st->codec->extradata + 0x5a);
00458 } else
00459 url_fskip(pb, atom.size);
00460
00461 return 0;
00462 }
00463
00464 static int mov_read_enda(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
00465 {
00466 AVStream *st;
00467 int little_endian;
00468
00469 if (c->fc->nb_streams < 1)
00470 return 0;
00471 st = c->fc->streams[c->fc->nb_streams-1];
00472
00473 little_endian = get_be16(pb);
00474 if (little_endian) {
00475 switch (st->codec->codec_id) {
00476 case CODEC_ID_PCM_S24BE:
00477 st->codec->codec_id = CODEC_ID_PCM_S24LE;
00478 break;
00479 case CODEC_ID_PCM_S32BE:
00480 st->codec->codec_id = CODEC_ID_PCM_S32LE;
00481 break;
00482 default:
00483 break;
00484 }
00485 }
00486 return 0;
00487 }
00488
00489
00490 static int mov_read_extradata(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
00491 {
00492 AVStream *st;
00493 uint64_t size;
00494 uint8_t *buf;
00495
00496 if (c->fc->nb_streams < 1)
00497 return 0;
00498 st = c->fc->streams[c->fc->nb_streams-1];
00499 size = (uint64_t)st->codec->extradata_size + atom.size + 8 + FF_INPUT_BUFFER_PADDING_SIZE;
00500
00501 if(size > INT_MAX || (uint64_t)atom.size > INT_MAX)
00502 return -1;
00503 buf= av_realloc(st->codec->extradata, size);
00504 if(!buf)
00505 return -1;
00506 st->codec->extradata= buf;
00507 buf+= st->codec->extradata_size;
00508 st->codec->extradata_size= size - FF_INPUT_BUFFER_PADDING_SIZE;
00509 AV_WB32( buf , atom.size + 8);
00510 AV_WL32( buf + 4, atom.type);
00511 get_buffer(pb, buf + 8, atom.size);
00512 return 0;
00513 }
00514
00515 static int mov_read_wave(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
00516 {
00517 AVStream *st;
00518
00519 if (c->fc->nb_streams < 1)
00520 return 0;
00521 st = c->fc->streams[c->fc->nb_streams-1];
00522
00523 if((uint64_t)atom.size > (1<<30))
00524 return -1;
00525
00526 if (st->codec->codec_id == CODEC_ID_QDM2) {
00527
00528 av_free(st->codec->extradata);
00529 st->codec->extradata_size = atom.size;
00530 st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
00531
00532 if (st->codec->extradata) {
00533 get_buffer(pb, st->codec->extradata, atom.size);
00534 } else
00535 url_fskip(pb, atom.size);
00536 } else if (atom.size > 8) {
00537 if (mov_read_default(c, pb, atom) < 0)
00538 return -1;
00539 } else
00540 url_fskip(pb, atom.size);
00541 return 0;
00542 }
00543
00548 static int mov_read_glbl(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
00549 {
00550 AVStream *st;
00551
00552 if (c->fc->nb_streams < 1)
00553 return 0;
00554 st = c->fc->streams[c->fc->nb_streams-1];
00555
00556 if((uint64_t)atom.size > (1<<30))
00557 return -1;
00558
00559 av_free(st->codec->extradata);
00560
00561 st->codec->extradata_size = atom.size;
00562 st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
00563
00564 if (st->codec->extradata) {
00565 get_buffer(pb, st->codec->extradata, atom.size);
00566 } else
00567 url_fskip(pb, atom.size);
00568
00569 return 0;
00570 }
00571
00572 static int mov_read_stco(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
00573 {
00574 AVStream *st;
00575 MOVStreamContext *sc;
00576 unsigned int i, entries;
00577
00578 if (c->fc->nb_streams < 1)
00579 return 0;
00580 st = c->fc->streams[c->fc->nb_streams-1];
00581 sc = st->priv_data;
00582
00583 get_byte(pb);
00584 get_byte(pb); get_byte(pb); get_byte(pb);
00585
00586 entries = get_be32(pb);
00587
00588 if(entries >= UINT_MAX/sizeof(int64_t))
00589 return -1;
00590
00591 sc->chunk_count = entries;
00592 sc->chunk_offsets = av_malloc(entries * sizeof(int64_t));
00593 if (!sc->chunk_offsets)
00594 return -1;
00595 if (atom.type == MKTAG('s', 't', 'c', 'o')) {
00596 for(i=0; i<entries; i++) {
00597 sc->chunk_offsets[i] = get_be32(pb);
00598 }
00599 } else if (atom.type == MKTAG('c', 'o', '6', '4')) {
00600 for(i=0; i<entries; i++) {
00601 sc->chunk_offsets[i] = get_be64(pb);
00602 }
00603 } else
00604 return -1;
00605
00606 return 0;
00607 }
00608
00609 static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
00610 {
00611 AVStream *st;
00612 MOVStreamContext *sc;
00613 int entries, frames_per_sample;
00614 uint32_t format;
00615 uint8_t codec_name[32];
00616
00617
00618 unsigned int color_depth;
00619 unsigned int color_start;
00620 unsigned int color_count;
00621 unsigned int color_end;
00622 int color_index;
00623 int color_dec;
00624 int color_greyscale;
00625 const uint8_t *color_table;
00626 int j, pseudo_stream_id;
00627 unsigned char r, g, b;
00628
00629 if (c->fc->nb_streams < 1)
00630 return 0;
00631 st = c->fc->streams[c->fc->nb_streams-1];
00632 sc = st->priv_data;
00633
00634 get_byte(pb);
00635 get_byte(pb); get_byte(pb); get_byte(pb);
00636
00637 entries = get_be32(pb);
00638
00639 for(pseudo_stream_id=0; pseudo_stream_id<entries; pseudo_stream_id++) {
00640 enum CodecID id;
00641 MOV_atom_t a = { 0, 0, 0 };
00642 offset_t start_pos = url_ftell(pb);
00643 int size = get_be32(pb);
00644 format = get_le32(pb);
00645
00646 get_be32(pb);
00647 get_be16(pb);
00648 get_be16(pb);
00649
00650 if (st->codec->codec_tag && st->codec->codec_tag != MKTAG('j', 'p', 'e', 'g')) {
00651
00652
00653
00654 url_fskip(pb, size - (url_ftell(pb) - start_pos));
00655 continue;
00656 }
00657 sc->pseudo_stream_id= pseudo_stream_id;
00658
00659 st->codec->codec_tag = format;
00660 id = codec_get_id(codec_movaudio_tags, format);
00661 if (id<=0 && (format&0xFFFF) == 'm' + ('s'<<8))
00662 id = codec_get_id(codec_wav_tags, bswap_32(format)&0xFFFF);
00663
00664 if (st->codec->codec_type != CODEC_TYPE_VIDEO && id > 0) {
00665 st->codec->codec_type = CODEC_TYPE_AUDIO;
00666 } else if (st->codec->codec_type != CODEC_TYPE_AUDIO &&
00667 format && format != MKTAG('m', 'p', '4', 's')) {
00668 id = codec_get_id(codec_movvideo_tags, format);
00669 if (id <= 0)
00670 id = codec_get_id(codec_bmp_tags, format);
00671 if (id > 0)
00672 st->codec->codec_type = CODEC_TYPE_VIDEO;
00673 else if(st->codec->codec_type == CODEC_TYPE_DATA){
00674 id = codec_get_id(ff_codec_movsubtitle_tags, format);
00675 if(id > 0)
00676 st->codec->codec_type = CODEC_TYPE_SUBTITLE;
00677 }
00678 }
00679
00680 dprintf(c->fc, "size=%d 4CC= %c%c%c%c codec_type=%d\n", size,
00681 (format >> 0) & 0xff, (format >> 8) & 0xff, (format >> 16) & 0xff,
00682 (format >> 24) & 0xff, st->codec->codec_type);
00683
00684 if(st->codec->codec_type==CODEC_TYPE_VIDEO) {
00685 st->codec->codec_id = id;
00686 get_be16(pb);
00687 get_be16(pb);
00688 get_be32(pb);
00689 get_be32(pb);
00690 get_be32(pb);
00691
00692 st->codec->width = get_be16(pb);
00693 st->codec->height = get_be16(pb);
00694
00695 get_be32(pb);
00696 get_be32(pb);
00697 get_be32(pb);
00698 frames_per_sample = get_be16(pb);
00699
00700 dprintf(c->fc, "frames/samples = %d\n", frames_per_sample);
00701
00702 get_buffer(pb, codec_name, 32);
00703 if (codec_name[0] <= 31) {
00704 memcpy(st->codec->codec_name, &codec_name[1],codec_name[0]);
00705 st->codec->codec_name[codec_name[0]] = 0;
00706 }
00707
00708 st->codec->bits_per_sample = get_be16(pb);
00709 st->codec->color_table_id = get_be16(pb);
00710
00711
00712 color_depth = st->codec->bits_per_sample & 0x1F;
00713 color_greyscale = st->codec->bits_per_sample & 0x20;
00714
00715
00716 if ((color_depth == 2) || (color_depth == 4) ||
00717 (color_depth == 8)) {
00718 if (color_greyscale) {
00719
00720 color_count = 1 << color_depth;
00721 color_index = 255;
00722 color_dec = 256 / (color_count - 1);
00723 for (j = 0; j < color_count; j++) {
00724 r = g = b = color_index;
00725 c->palette_control.palette[j] =
00726 (r << 16) | (g << 8) | (b);
00727 color_index -= color_dec;
00728 if (color_index < 0)
00729 color_index = 0;
00730 }
00731 } else if (st->codec->color_table_id & 0x08) {
00732
00733 color_count = 1 << color_depth;
00734 if (color_depth == 2)
00735 color_table = ff_qt_default_palette_4;
00736 else if (color_depth == 4)
00737 color_table = ff_qt_default_palette_16;
00738 else
00739 color_table = ff_qt_default_palette_256;
00740
00741 for (j = 0; j < color_count; j++) {
00742 r = color_table[j * 4 + 0];
00743 g = color_table[j * 4 + 1];
00744 b = color_table[j * 4 + 2];
00745 c->palette_control.palette[j] =
00746 (r << 16) | (g << 8) | (b);
00747 }
00748 } else {
00749
00750 color_start = get_be32(pb);
00751 color_count = get_be16(pb);
00752 color_end = get_be16(pb);
00753 if ((color_start <= 255) &&
00754 (color_end <= 255)) {
00755 for (j = color_start; j <= color_end; j++) {
00756
00757
00758
00759 get_byte(pb);
00760 get_byte(pb);
00761 r = get_byte(pb);
00762 get_byte(pb);
00763 g = get_byte(pb);
00764 get_byte(pb);
00765 b = get_byte(pb);
00766 get_byte(pb);
00767 c->palette_control.palette[j] =
00768 (r << 16) | (g << 8) | (b);
00769 }
00770 }
00771 }
00772 st->codec->palctrl = &c->palette_control;
00773 st->codec->palctrl->palette_changed = 1;
00774 } else
00775 st->codec->palctrl = NULL;
00776 } else if(st->codec->codec_type==CODEC_TYPE_AUDIO) {
00777 int bits_per_sample;
00778 uint16_t version = get_be16(pb);
00779
00780 st->codec->codec_id = id;
00781 get_be16(pb);
00782 get_be32(pb);
00783
00784 st->codec->channels = get_be16(pb);
00785 dprintf(c->fc, "audio channels %d\n", st->codec->channels);
00786 st->codec->bits_per_sample = get_be16(pb);
00787
00788
00789
00790 get_be16(pb);
00791 get_be16(pb);
00792
00793 st->codec->sample_rate = ((get_be32(pb) >> 16));
00794
00795 switch (st->codec->codec_id) {
00796 case CODEC_ID_PCM_S8:
00797 case CODEC_ID_PCM_U8:
00798 if (st->codec->bits_per_sample == 16)
00799 st->codec->codec_id = CODEC_ID_PCM_S16BE;
00800 break;
00801 case CODEC_ID_PCM_S16LE:
00802 case CODEC_ID_PCM_S16BE:
00803 if (st->codec->bits_per_sample == 8)
00804 st->codec->codec_id = CODEC_ID_PCM_S8;
00805 else if (st->codec->bits_per_sample == 24)
00806 st->codec->codec_id = CODEC_ID_PCM_S24BE;
00807 break;
00808 default:
00809 break;
00810 }
00811
00812
00813 dprintf(c->fc, "version =%d, isom =%d\n",version,c->isom);
00814 if(!c->isom) {
00815 if(version==1) {
00816 sc->samples_per_frame = get_be32(pb);
00817 get_be32(pb);
00818 sc->bytes_per_frame = get_be32(pb);
00819 get_be32(pb);
00820 } else if(version==2) {
00821 get_be32(pb);
00822 st->codec->sample_rate = av_int2dbl(get_be64(pb));
00823 st->codec->channels = get_be32(pb);
00824 get_be32(pb);
00825 get_be32(pb);
00826 get_be32(pb);
00827 get_be32(pb);
00828 get_be32(pb);
00829 }
00830 }
00831
00832 bits_per_sample = av_get_bits_per_sample(st->codec->codec_id);
00833 if (bits_per_sample) {
00834 st->codec->bits_per_sample = bits_per_sample;
00835 sc->sample_size = (bits_per_sample >> 3) * st->codec->channels;
00836 }
00837 } else if(st->codec->codec_type==CODEC_TYPE_SUBTITLE){
00838 st->codec->codec_id= id;
00839 } else {
00840
00841 url_fskip(pb, size - (url_ftell(pb) - start_pos));
00842 }
00843
00844 a.size = size - (url_ftell(pb) - start_pos);
00845 if (a.size > 8) {
00846 if (mov_read_default(c, pb, a) < 0)
00847 return -1;
00848 } else if (a.size > 0)
00849 url_fskip(pb, a.size);
00850 }
00851
00852 if(st->codec->codec_type==CODEC_TYPE_AUDIO && st->codec->sample_rate==0 && sc->time_scale>1) {
00853 st->codec->sample_rate= sc->time_scale;
00854 }
00855
00856
00857 switch (st->codec->codec_id) {
00858 #ifdef CONFIG_H261_DECODER
00859 case CODEC_ID_H261:
00860 #endif
00861 #ifdef CONFIG_H263_DECODER
00862 case CODEC_ID_H263:
00863 #endif
00864 #ifdef CONFIG_MPEG4_DECODER
00865 case CODEC_ID_MPEG4:
00866 #endif
00867 st->codec->width= 0;
00868 st->codec->height= 0;
00869 break;
00870 #ifdef CONFIG_LIBFAAD
00871 case CODEC_ID_AAC:
00872 #endif
00873 #ifdef CONFIG_VORBIS_DECODER
00874 case CODEC_ID_VORBIS:
00875 #endif
00876 case CODEC_ID_MP3ON4:
00877 st->codec->sample_rate= 0;
00878 break;
00879 #ifdef CONFIG_DV_DEMUXER
00880 case CODEC_ID_DVAUDIO:
00881 c->dv_fctx = av_alloc_format_context();
00882 c->dv_demux = dv_init_demux(c->dv_fctx);
00883 if (!c->dv_demux) {
00884 av_log(c->fc, AV_LOG_ERROR, "dv demux context init error\n");
00885 return -1;
00886 }
00887 sc->dv_audio_container = 1;
00888 st->codec->codec_id = CODEC_ID_PCM_S16LE;
00889 break;
00890 #endif
00891
00892 case CODEC_ID_AMR_WB:
00893 st->codec->sample_rate= 16000;
00894 st->codec->channels= 1;
00895 break;
00896 case CODEC_ID_AMR_NB:
00897 st->codec->sample_rate= 8000;
00898 st->codec->channels= 1;
00899 break;
00900 case CODEC_ID_MP2:
00901 case CODEC_ID_MP3:
00902 st->codec->codec_type = CODEC_TYPE_AUDIO;
00903 st->need_parsing = AVSTREAM_PARSE_FULL;
00904 break;
00905 case CODEC_ID_ADPCM_MS:
00906 case CODEC_ID_ADPCM_IMA_WAV:
00907 st->codec->block_align = sc->bytes_per_frame;
00908 break;
00909 default:
00910 break;
00911 }
00912
00913 return 0;
00914 }
00915
00916 static int mov_read_stsc(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
00917 {
00918 AVStream *st;
00919 MOVStreamContext *sc;
00920 unsigned int i, entries;
00921
00922 if (c->fc->nb_streams < 1)
00923 return 0;
00924 st = c->fc->streams[c->fc->nb_streams-1];
00925 sc = st->priv_data;
00926
00927 get_byte(pb);
00928 get_byte(pb); get_byte(pb); get_byte(pb);
00929
00930 entries = get_be32(pb);
00931
00932 if(entries >= UINT_MAX / sizeof(MOV_stsc_t))
00933 return -1;
00934
00935 dprintf(c->fc, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
00936
00937 sc->sample_to_chunk_sz = entries;
00938 sc->sample_to_chunk = av_malloc(entries * sizeof(MOV_stsc_t));
00939 if (!sc->sample_to_chunk)
00940 return -1;
00941 for(i=0; i<entries; i++) {
00942 sc->sample_to_chunk[i].first = get_be32(pb);
00943 sc->sample_to_chunk[i].count = get_be32(pb);
00944 sc->sample_to_chunk[i].id = get_be32(pb);
00945 }
00946 return 0;
00947 }
00948
00949 static int mov_read_stss(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
00950 {
00951 AVStream *st;
00952 MOVStreamContext *sc;
00953 unsigned int i, entries;
00954
00955 if (c->fc->nb_streams < 1)
00956 return 0;
00957 st = c->fc->streams[c->fc->nb_streams-1];
00958 sc = st->priv_data;
00959
00960 get_byte(pb);
00961 get_byte(pb); get_byte(pb); get_byte(pb);
00962
00963 entries = get_be32(pb);
00964
00965 if(entries >= UINT_MAX / sizeof(int))
00966 return -1;
00967
00968 sc->keyframe_count = entries;
00969
00970 dprintf(c->fc, "keyframe_count = %d\n", sc->keyframe_count);
00971
00972 sc->keyframes = av_malloc(entries * sizeof(int));
00973 if (!sc->keyframes)
00974 return -1;
00975 for(i=0; i<entries; i++) {
00976 sc->keyframes[i] = get_be32(pb);
00977
00978 }
00979 return 0;
00980 }
00981
00982 static int mov_read_stsz(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
00983 {
00984 AVStream *st;
00985 MOVStreamContext *sc;
00986 unsigned int i, entries, sample_size;
00987
00988 if (c->fc->nb_streams < 1)
00989 return 0;
00990 st = c->fc->streams[c->fc->nb_streams-1];
00991 sc = st->priv_data;
00992
00993 get_byte(pb);
00994 get_byte(pb); get_byte(pb); get_byte(pb);
00995
00996 sample_size = get_be32(pb);
00997 if (!sc->sample_size)
00998 sc->sample_size = sample_size;
00999 entries = get_be32(pb);
01000 if(entries >= UINT_MAX / sizeof(int))
01001 return -1;
01002
01003 sc->sample_count = entries;
01004 if (sample_size)
01005 return 0;
01006
01007 dprintf(c->fc, "sample_size = %d sample_count = %d\n", sc->sample_size, sc->sample_count);
01008
01009 sc->sample_sizes = av_malloc(entries * sizeof(int));
01010 if (!sc->sample_sizes)
01011 return -1;
01012 for(i=0; i<entries; i++) {
01013 sc->sample_sizes[i] = get_be32(pb);
01014 dprintf(c->fc, "sample_sizes[]=%d\n", sc->sample_sizes[i]);
01015 }
01016 return 0;
01017 }
01018
01019 static int mov_read_stts(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
01020 {
01021 AVStream *st;
01022 MOVStreamContext *sc;
01023 unsigned int i, entries;
01024 int64_t duration=0;
01025 int64_t total_sample_count=0;
01026
01027 if (c->fc->nb_streams < 1)
01028 return 0;
01029 st = c->fc->streams[c->fc->nb_streams-1];
01030 sc = st->priv_data;
01031
01032 get_byte(pb);
01033 get_byte(pb); get_byte(pb); get_byte(pb);
01034 entries = get_be32(pb);
01035 if(entries >= UINT_MAX / sizeof(MOV_stts_t))
01036 return -1;
01037
01038 sc->stts_count = entries;
01039 sc->stts_data = av_malloc(entries * sizeof(MOV_stts_t));
01040 if (!sc->stts_data)
01041 return -1;
01042 dprintf(c->fc, "track[%i].stts.entries = %i\n", c->fc->nb_streams-1, entries);
01043
01044 sc->time_rate=0;
01045
01046 for(i=0; i<entries; i++) {
01047 int sample_duration;
01048 int sample_count;
01049
01050 sample_count=get_be32(pb);
01051 sample_duration = get_be32(pb);
01052 sc->stts_data[i].count= sample_count;
01053 sc->stts_data[i].duration= sample_duration;
01054
01055 sc->time_rate= ff_gcd(sc->time_rate, sample_duration);
01056
01057 dprintf(c->fc, "sample_count=%d, sample_duration=%d\n",sample_count,sample_duration);
01058
01059 duration+=(int64_t)sample_duration*sample_count;
01060 total_sample_count+=sample_count;
01061 }
01062
01063 st->nb_frames= total_sample_count;
01064 if(duration)
01065 st->duration= duration;
01066 return 0;
01067 }
01068
01069 static int mov_read_ctts(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
01070 {
01071 AVStream *st;
01072 MOVStreamContext *sc;
01073 unsigned int i, entries;
01074
01075 if (c->fc->nb_streams < 1)
01076 return 0;
01077 st = c->fc->streams[c->fc->nb_streams-1];
01078 sc = st->priv_data;
01079
01080 get_byte(pb);
01081 get_byte(pb); get_byte(pb); get_byte(pb);
01082 entries = get_be32(pb);
01083 if(entries >= UINT_MAX / sizeof(MOV_stts_t))
01084 return -1;
01085
01086 sc->ctts_count = entries;
01087 sc->ctts_data = av_malloc(entries * sizeof(MOV_stts_t));
01088 if (!sc->ctts_data)
01089 return -1;
01090 dprintf(c->fc, "track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries);
01091
01092 for(i=0; i<entries; i++) {
01093 int count =get_be32(pb);
01094 int duration =get_be32(pb);
01095
01096 if (duration < 0) {
01097 av_log(c->fc, AV_LOG_ERROR, "negative ctts, ignoring\n");
01098 sc->ctts_count = 0;
01099 url_fskip(pb, 8 * (entries - i - 1));
01100 break;
01101 }
01102 sc->ctts_data[i].count = count;
01103 sc->ctts_data[i].duration= duration;
01104
01105 sc->time_rate= ff_gcd(sc->time_rate, duration);
01106 }
01107 return 0;
01108 }
01109
01110 static int mov_read_trak(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
01111 {
01112 AVStream *st;
01113 MOVStreamContext *sc;
01114
01115 st = av_new_stream(c->fc, c->fc->nb_streams);
01116 if (!st) return -2;
01117 sc = av_mallocz(sizeof(MOVStreamContext));
01118 if (!sc) {
01119 av_free(st);
01120 return -1;
01121 }
01122
01123 st->priv_data = sc;
01124 st->codec->codec_type = CODEC_TYPE_DATA;
01125 st->start_time = 0;
01126
01127 return mov_read_default(c, pb, atom);
01128 }
01129
01130 static void mov_parse_udta_string(ByteIOContext *pb, char *str, int size)
01131 {
01132 uint16_t str_size = get_be16(pb); ;
01133
01134 get_be16(pb);
01135 get_buffer(pb, str, FFMIN(size, str_size));
01136 }
01137
01138 static int mov_read_udta(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
01139 {
01140 uint64_t end = url_ftell(pb) + atom.size;
01141
01142 while (url_ftell(pb) + 8 < end) {
01143 uint32_t tag_size = get_be32(pb);
01144 uint32_t tag = get_le32(pb);
01145 uint64_t next = url_ftell(pb) + tag_size - 8;
01146
01147 if (next > end)
01148 break;
01149
01150 switch (tag) {
01151 case MKTAG(0xa9,'n','a','m'):
01152 mov_parse_udta_string(pb, c->fc->title, sizeof(c->fc->title));
01153 break;
01154 case MKTAG(0xa9,'w','r','t'):
01155 mov_parse_udta_string(pb, c->fc->author, sizeof(c->fc->author));
01156 break;
01157 case MKTAG(0xa9,'c','p','y'):
01158 mov_parse_udta_string(pb, c->fc->copyright, sizeof(c->fc->copyright));
01159 break;
01160 case MKTAG(0xa9,'i','n','f'):
01161 mov_parse_udta_string(pb, c->fc->comment, sizeof(c->fc->comment));
01162 break;
01163 default:
01164 break;
01165 }
01166
01167 url_fseek(pb, next, SEEK_SET);
01168 }
01169
01170 return 0;
01171 }
01172
01173 static int mov_read_tkhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
01174 {
01175 AVStream *st;
01176 int version = get_byte(pb);
01177
01178 if (c->fc->nb_streams < 1)
01179 return 0;
01180 st = c->fc->streams[c->fc->nb_streams-1];
01181
01182 get_byte(pb); get_byte(pb);
01183 get_byte(pb);
01184
01185
01186
01187
01188
01189
01190
01191 if (version == 1) {
01192 get_be64(pb);
01193 get_be64(pb);
01194 } else {
01195 get_be32(pb);
01196 get_be32(pb);
01197 }
01198 st->id = (int)get_be32(pb);
01199 get_be32(pb);
01200 st->start_time = 0;
01201 (version == 1) ? get_be64(pb) : get_be32(pb);
01202 get_be32(pb);
01203 get_be32(pb);
01204
01205 get_be16(pb);
01206 get_be16(pb);
01207 get_be16(pb);
01208 get_be16(pb);
01209
01210 url_fskip(pb, 36);
01211
01212
01213 get_be32(pb);
01214 get_be32(pb);
01215
01216 return 0;
01217 }
01218
01219
01220
01221
01222 static int mov_read_wide(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
01223 {
01224 int err;
01225
01226 if (atom.size < 8)
01227 return 0;
01228 if (get_be32(pb) != 0) {
01229 url_fskip(pb, atom.size - 4);
01230 return 0;
01231 }
01232 atom.type = get_le32(pb);
01233 atom.offset += 8;
01234 atom.size -= 8;
01235 if (atom.type != MKTAG('m', 'd', 'a', 't')) {
01236 url_fskip(pb, atom.size);
01237 return 0;
01238 }
01239 err = mov_read_mdat(c, pb, atom);
01240 return err;
01241 }
01242
01243 static int mov_read_cmov(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
01244 {
01245 #ifdef CONFIG_ZLIB
01246 ByteIOContext ctx;
01247 uint8_t *cmov_data;
01248 uint8_t *moov_data;
01249 long cmov_len, moov_len;
01250 int ret;
01251
01252 get_be32(pb);
01253 if (get_le32(pb) != MKTAG( 'd', 'c', 'o', 'm' ))
01254 return -1;
01255 if (get_le32(pb) != MKTAG( 'z', 'l', 'i', 'b' )) {
01256 av_log(NULL, AV_LOG_ERROR, "unknown compression for cmov atom !");
01257 return -1;
01258 }
01259 get_be32(pb);
01260 if (get_le32(pb) != MKTAG( 'c', 'm', 'v', 'd' ))
01261 return -1;
01262 moov_len = get_be32(pb);
01263 cmov_len = atom.size - 6 * 4;
01264
01265 cmov_data = av_malloc(cmov_len);
01266 if (!cmov_data)
01267 return -1;
01268 moov_data = av_malloc(moov_len);
01269 if (!moov_data) {
01270 av_free(cmov_data);
01271 return -1;
01272 }
01273 get_buffer(pb, cmov_data, cmov_len);
01274 if(uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
01275 return -1;
01276 if(init_put_byte(&ctx, moov_data, moov_len, 0, NULL, NULL, NULL, NULL) != 0)
01277 return -1;
01278 atom.type = MKTAG( 'm', 'o', 'o', 'v' );
01279 atom.offset = 0;
01280 atom.size = moov_len;
01281 #ifdef DEBUG
01282
01283 #endif
01284 ret = mov_read_default(c, &ctx, atom);
01285 av_free(moov_data);
01286 av_free(cmov_data);
01287 return ret;
01288 #else
01289 av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n");
01290 return -1;
01291 #endif
01292 }
01293
01294
01295 static int mov_read_elst(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
01296 {
01297 MOVStreamContext *sc;
01298 int i, edit_count;
01299
01300 if (c->fc->nb_streams < 1)
01301 return 0;
01302 sc = c->fc->streams[c->fc->nb_streams-1]->priv_data;
01303
01304 get_byte(pb);
01305 get_byte(pb); get_byte(pb); get_byte(pb);
01306 edit_count= sc->edit_count = get_be32(pb);
01307
01308 for(i=0; i<edit_count; i++){
01309 get_be32(pb);
01310 get_be32(pb);
01311 get_be32(pb);
01312 }
01313 dprintf(c->fc, "track[%i].edit_count = %i\n", c->fc->nb_streams-1, sc->edit_count);
01314 return 0;
01315 }
01316
01317 static const MOVParseTableEntry mov_default_parse_table[] = {
01318
01319 { MKTAG( 'c', 'o', '6', '4' ), mov_read_stco },
01320 { MKTAG( 'c', 't', 't', 's' ), mov_read_ctts },
01321 { MKTAG( 'e', 'd', 't', 's' ), mov_read_default },
01322 { MKTAG( 'e', 'l', 's', 't' ), mov_read_elst },
01323 { MKTAG( 'e', 'n', 'd', 'a' ), mov_read_enda },
01324 { MKTAG( 'f', 'i', 'e', 'l' ), mov_read_extradata },
01325 { MKTAG( 'f', 't', 'y', 'p' ), mov_read_ftyp },
01326 { MKTAG( 'g', 'l', 'b', 'l' ), mov_read_glbl },
01327 { MKTAG( 'h', 'd', 'l', 'r' ), mov_read_hdlr },
01328 { MKTAG( 'j', 'p', '2', 'h' ), mov_read_extradata },
01329 { MKTAG( 'm', 'd', 'a', 't' ), mov_read_mdat },
01330 { MKTAG( 'm', 'd', 'h', 'd' ), mov_read_mdhd },
01331 { MKTAG( 'm', 'd', 'i', 'a' ), mov_read_default },
01332 { MKTAG( 'm', 'i', 'n', 'f' ), mov_read_default },
01333 { MKTAG( 'm', 'o', 'o', 'v' ), mov_read_moov },
01334 { MKTAG( 'm', 'v', 'h', 'd' ), mov_read_mvhd },
01335 { MKTAG( 'S', 'M', 'I', ' ' ), mov_read_smi },
01336 { MKTAG( 'a', 'l', 'a', 'c' ), mov_read_extradata },
01337 { MKTAG( 'a', 'v', 'c', 'C' ), mov_read_glbl },
01338 { MKTAG( 's', 't', 'b', 'l' ), mov_read_default },
01339 { MKTAG( 's', 't', 'c', 'o' ), mov_read_stco },
01340 { MKTAG( 's', 't', 's', 'c' ), mov_read_stsc },
01341 { MKTAG( 's', 't', 's', 'd' ), mov_read_stsd },
01342 { MKTAG( 's', 't', 's', 's' ), mov_read_stss },
01343 { MKTAG( 's', 't', 's', 'z' ), mov_read_stsz },
01344 { MKTAG( 's', 't', 't', 's' ), mov_read_stts },
01345 { MKTAG( 't', 'k', 'h', 'd' ), mov_read_tkhd },
01346 { MKTAG( 't', 'r', 'a', 'k' ), mov_read_trak },
01347 { MKTAG( 'u', 'd', 't', 'a' ), mov_read_udta },
01348 { MKTAG( 'w', 'a', 'v', 'e' ), mov_read_wave },
01349 { MKTAG( 'e', 's', 'd', 's' ), mov_read_esds },
01350 { MKTAG( 'w', 'i', 'd', 'e' ), mov_read_wide },
01351 { MKTAG( 'c', 'm', 'o', 'v' ), mov_read_cmov },
01352 { 0, NULL }
01353 };
01354
01355
01356 static int mov_probe(AVProbeData *p)
01357 {
01358 unsigned int offset;
01359 uint32_t tag;
01360 int score = 0;
01361
01362
01363 offset = 0;
01364 for(;;) {
01365
01366 if ((offset + 8) > (unsigned int)p->buf_size)
01367 return score;
01368 tag = AV_RL32(p->buf + offset + 4);
01369 switch(tag) {
01370
01371 case MKTAG( 'j', 'P', ' ', ' ' ):
01372 case MKTAG( 'm', 'o', 'o', 'v' ):
01373 case MKTAG( 'm', 'd', 'a', 't' ):
01374 case MKTAG( 'p', 'n', 'o', 't' ):
01375 case MKTAG( 'u', 'd', 't', 'a' ):
01376 return AVPROBE_SCORE_MAX;
01377
01378 case MKTAG( 'e', 'd', 'i', 'w' ):
01379 case MKTAG( 'w', 'i', 'd', 'e' ):
01380 case MKTAG( 'f', 'r', 'e', 'e' ):
01381 case MKTAG( 'j', 'u', 'n', 'k' ):
01382 case MKTAG( 'p', 'i', 'c', 't' ):
01383 return AVPROBE_SCORE_MAX - 5;
01384 case MKTAG( 'f', 't', 'y', 'p' ):
01385 case MKTAG( 's', 'k', 'i', 'p' ):
01386 case MKTAG( 'u', 'u', 'i', 'd' ):
01387 offset = AV_RB32(p->buf+offset) + offset;
01388
01389 score = AVPROBE_SCORE_MAX - 50;
01390 break;
01391 default:
01392
01393 return score;
01394 }
01395 }
01396 return score;
01397 }
01398
01399 static void mov_build_index(MOVContext *mov, AVStream *st)
01400 {
01401 MOVStreamContext *sc = st->priv_data;
01402 offset_t current_offset;
01403 int64_t current_dts = 0;
01404 unsigned int stts_index = 0;
01405 unsigned int stsc_index = 0;
01406 unsigned int stss_index = 0;
01407 unsigned int i, j, k;
01408
01409 if (sc->sample_sizes || st->codec->codec_type == CODEC_TYPE_VIDEO || sc->dv_audio_container) {
01410 unsigned int current_sample = 0;
01411 unsigned int stts_sample = 0;
01412 unsigned int keyframe, sample_size;
01413 unsigned int distance = 0;
01414
01415 st->nb_frames = sc->sample_count;
01416 for (i = 0; i < sc->chunk_count; i++) {
01417 current_offset = sc->chunk_offsets[i];
01418 if (stsc_index + 1 < sc->sample_to_chunk_sz &&
01419 i + 1 == sc->sample_to_chunk[stsc_index + 1].first)
01420 stsc_index++;
01421 for (j = 0; j < sc->sample_to_chunk[stsc_index].count; j++) {
01422 if (current_sample >= sc->sample_count) {
01423 av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n");
01424 goto out;
01425 }
01426 keyframe = !sc->keyframe_count || current_sample + 1 == sc->keyframes[stss_index];
01427 if (keyframe) {
01428 distance = 0;
01429 if (stss_index + 1 < sc->keyframe_count)
01430 stss_index++;
01431 }
01432 sample_size = sc->sample_size > 0 ? sc->sample_size : sc->sample_sizes[current_sample];
01433 dprintf(mov->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
01434 "size %d, distance %d, keyframe %d\n", st->index, current_sample,
01435 current_offset, current_dts, sample_size, distance, keyframe);
01436 if(sc->sample_to_chunk[stsc_index].id - 1 == sc->pseudo_stream_id)
01437 av_add_index_entry(st, current_offset, current_dts, sample_size, distance,
01438 keyframe ? AVINDEX_KEYFRAME : 0);
01439 current_offset += sample_size;
01440 assert(sc->stts_data[stts_index].duration % sc->time_rate == 0);
01441 current_dts += sc->stts_data[stts_index].duration / sc->time_rate;
01442 distance++;
01443 stts_sample++;
01444 current_sample++;
01445 if (stts_index + 1 < sc->stts_count && stts_sample == sc->stts_data[stts_index].count) {
01446 stts_sample = 0;
01447 stts_index++;
01448 }
01449 }
01450 }
01451 } else {
01452 unsigned int chunk_samples, chunk_size, chunk_duration;
01453 for (i = 0; i < sc->chunk_count; i++) {
01454 current_offset = sc->chunk_offsets[i];
01455 if (stsc_index + 1 < sc->sample_to_chunk_sz &&
01456 i + 1 == sc->sample_to_chunk[stsc_index + 1].first)
01457 stsc_index++;
01458 chunk_samples = sc->sample_to_chunk[stsc_index].count;
01459
01460 if (sc->sample_size > 1 ||
01461 st->codec->codec_id == CODEC_ID_PCM_U8 || st->codec->codec_id == CODEC_ID_PCM_S8)
01462 chunk_size = chunk_samples * sc->sample_size;
01463 else if (sc->samples_per_frame > 0 &&
01464 (chunk_samples * sc->bytes_per_frame % sc->samples_per_frame == 0))
01465 chunk_size = chunk_samples * sc->bytes_per_frame / sc->samples_per_frame;
01466 else {
01467 chunk_size = INT_MAX;
01468 for (j = 0; j < mov->fc->nb_streams; j++) {
01469 MOVStreamContext *msc = mov->fc->streams[j]->priv_data;
01470 for (k = msc->next_chunk; k < msc->chunk_count; k++) {
01471 if (msc->chunk_offsets[k] > current_offset &&
01472 msc->chunk_offsets[k] - current_offset < chunk_size) {
01473 chunk_size = msc->chunk_offsets[k] - current_offset;
01474 msc->next_chunk = k;
01475 break;
01476 }
01477 }
01478 }
01479
01480 if (chunk_size == INT_MAX)
01481 for (j = 0; j < mov->mdat_count; j++) {
01482 dprintf(mov->fc, "mdat %d, offset %"PRIx64", size %"PRId64", current offset %"PRIx64"\n",
01483 j, mov->mdat_list[j].offset, mov->mdat_list[j].size, current_offset);
01484 if (mov->mdat_list[j].offset <= current_offset &&
01485 mov->mdat_list[j].offset + mov->mdat_list[j].size > current_offset)
01486 chunk_size = mov->mdat_list[j].offset + mov->mdat_list[j].size - current_offset;
01487 }
01488 assert(chunk_size != INT_MAX);
01489 for (j = 0; j < mov->fc->nb_streams; j++) {
01490 MOVStreamContext *msc = mov->fc->streams[j]->priv_data;
01491 msc->next_chunk = 0;
01492 }
01493 }
01494 av_add_index_entry(st, current_offset, current_dts, chunk_size, 0, AVINDEX_KEYFRAME);
01495
01496 chunk_duration = 0;
01497 while (chunk_samples > 0) {
01498 if (chunk_samples < sc->stts_data[stts_index].count) {
01499 chunk_duration += sc->stts_data[stts_index].duration * chunk_samples;
01500 sc->stts_data[stts_index].count -= chunk_samples;
01501 break;
01502 } else {
01503 chunk_duration += sc->stts_data[stts_index].duration * chunk_samples;
01504 chunk_samples -= sc->stts_data[stts_index].count;
01505 if (stts_index + 1 < sc->stts_count) {
01506 stts_index++;
01507 }
01508 }
01509 }
01510 dprintf(mov->fc, "AVIndex stream %d, chunk %d, offset %"PRIx64", dts %"PRId64", size %d, "
01511 "duration %d\n", st->index, i, current_offset, current_dts, chunk_size, chunk_duration);
01512 assert(chunk_duration % sc->time_rate == 0);
01513 current_dts += chunk_duration / sc->time_rate;
01514 }
01515 }
01516 out:
01517
01518 sc->sample_count = st->nb_index_entries;
01519 }
01520
01521 static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap)
01522 {
01523 MOVContext *mov = s->priv_data;
01524 ByteIOContext *pb = s->pb;
01525 int i, err;
01526 MOV_atom_t atom = { 0, 0, 0 };
01527
01528 mov->fc = s;
01529
01530 if(!url_is_streamed(pb))
01531 atom.size = url_fsize(pb);
01532 else
01533 atom.size = INT64_MAX;
01534
01535
01536 err = mov_read_default(mov, pb, atom);
01537 if (err<0 || (!mov->found_moov && !mov->found_mdat)) {
01538 av_log(s, AV_LOG_ERROR, "mov: header not found !!! (err:%d, moov:%d, mdat:%d) pos:%"PRId64"\n",
01539 err, mov->found_moov, mov->found_mdat, url_ftell(pb));
01540 return -1;
01541 }
01542 dprintf(mov->fc, "on_parse_exit_offset=%d\n", (int) url_ftell(pb));
01543
01544 for(i=0; i<s->nb_streams; i++) {
01545 AVStream *st = s->streams[i];
01546 MOVStreamContext *sc = st->priv_data;
01547
01548 if(!sc->stts_count || !sc->chunk_count || !sc->sample_to_chunk_sz ||
01549 (!sc->sample_size && !sc->sample_count)){
01550 av_log(s, AV_LOG_ERROR, "missing mandatory atoms, broken header\n");
01551 sc->sample_count = 0;
01552 continue;
01553 }
01554 if(!sc->time_rate)
01555 sc->time_rate=1;
01556 if(!sc->time_scale)
01557 sc->time_scale= mov->time_scale;
01558 av_set_pts_info(st, 64, sc->time_rate, sc->time_scale);
01559
01560 if (st->codec->codec_type == CODEC_TYPE_AUDIO && sc->stts_count == 1)
01561 st->codec->frame_size = sc->stts_data[0].duration;
01562
01563 if(st->duration != AV_NOPTS_VALUE){
01564 assert(st->duration % sc->time_rate == 0);
01565 st->duration /= sc->time_rate;
01566 }
01567 sc->ffindex = i;
01568 mov_build_index(mov, st);
01569 }
01570
01571 for(i=0; i<s->nb_streams; i++) {
01572 MOVStreamContext *sc = s->streams[i]->priv_data;
01573
01574 av_freep(&sc->chunk_offsets);
01575 av_freep(&sc->sample_to_chunk);
01576 av_freep(&sc->sample_sizes);
01577 av_freep(&sc->keyframes);
01578 av_freep(&sc->stts_data);
01579 }
01580 av_freep(&mov->mdat_list);
01581 return 0;
01582 }
01583
01584 static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
01585 {
01586 MOVContext *mov = s->priv_data;
01587 MOVStreamContext *sc = 0;
01588 AVIndexEntry *sample = 0;
01589 int64_t best_dts = INT64_MAX;
01590 int i;
01591
01592 for (i = 0; i < s->nb_streams; i++) {
01593 AVStream *st = s->streams[i];
01594 MOVStreamContext *msc = st->priv_data;
01595 if (st->discard != AVDISCARD_ALL && msc->current_sample < msc->sample_count) {
01596 AVIndexEntry *current_sample = &st->index_entries[msc->current_sample];
01597 int64_t dts = av_rescale(current_sample->timestamp * (int64_t)msc->time_rate,
01598 AV_TIME_BASE, msc->time_scale);
01599 dprintf(s, "stream %d, sample %d, dts %"PRId64"\n", i, msc->current_sample, dts);
01600 if (!sample || (url_is_streamed(s->pb) && current_sample->pos < sample->pos) ||
01601 (!url_is_streamed(s->pb) &&
01602 ((FFABS(best_dts - dts) <= AV_TIME_BASE && current_sample->pos < sample->pos) ||
01603 (FFABS(best_dts - dts) > AV_TIME_BASE && dts < best_dts)))) {
01604 sample = current_sample;
01605 best_dts = dts;
01606 sc = msc;
01607 }
01608 }
01609 }
01610 if (!sample)
01611 return -1;
01612
01613 sc->current_sample++;
01614 if (url_fseek(s->pb, sample->pos, SEEK_SET) != sample->pos) {
01615 av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n",
01616 sc->ffindex, sample->pos);
01617 return -1;
01618 }
01619 #ifdef CONFIG_DV_DEMUXER
01620 if (sc->dv_audio_container) {
01621 dv_get_packet(mov->dv_demux, pkt);
01622 dprintf(s, "dv audio pkt size %d\n", pkt->size);
01623 } else {
01624 #endif
01625 av_get_packet(s->pb, pkt, sample->size);
01626 #ifdef CONFIG_DV_DEMUXER
01627 if (mov->dv_demux) {
01628 void *pkt_destruct_func = pkt->destruct;
01629 dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size);
01630 pkt->destruct = pkt_destruct_func;
01631 }
01632 }
01633 #endif
01634 pkt->stream_index = sc->ffindex;
01635 pkt->dts = sample->timestamp;
01636 if (sc->ctts_data) {
01637 assert(sc->ctts_data[sc->sample_to_ctime_index].duration % sc->time_rate == 0);
01638 pkt->pts = pkt->dts + sc->ctts_data[sc->sample_to_ctime_index].duration / sc->time_rate;
01639
01640 sc->sample_to_ctime_sample++;
01641 if (sc->sample_to_ctime_index < sc->ctts_count &&
01642 sc->ctts_data[sc->sample_to_ctime_index].count == sc->sample_to_ctime_sample) {
01643 sc->sample_to_ctime_index++;
01644 sc->sample_to_ctime_sample = 0;
01645 }
01646 } else {
01647 pkt->pts = pkt->dts;
01648 }
01649 pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? PKT_FLAG_KEY : 0;
01650 pkt->pos = sample->pos;
01651 dprintf(s, "stream %d, pts %"PRId64", dts %"PRId64", pos 0x%"PRIx64", duration %d\n",
01652 pkt->stream_index, pkt->pts, pkt->dts, pkt->pos, pkt->duration);
01653 return 0;
01654 }
01655
01656 static int mov_seek_stream(AVStream *st, int64_t timestamp, int flags)
01657 {
01658 MOVStreamContext *sc = st->priv_data;
01659 int sample, time_sample;
01660 int i;
01661
01662 sample = av_index_search_timestamp(st, timestamp, flags);
01663 dprintf(st->codec, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample);
01664 if (sample < 0)
01665 return -1;
01666 sc->current_sample = sample;
01667 dprintf(st->codec, "stream %d, found sample %d\n", st->index, sc->current_sample);
01668
01669 if (sc->ctts_data) {
01670 time_sample = 0;
01671 for (i = 0; i < sc->ctts_count; i++) {
01672 int next = time_sample + sc->ctts_data[i].count;
01673 if (next > sc->current_sample) {
01674 sc->sample_to_ctime_index = i;
01675 sc->sample_to_ctime_sample = sc->current_sample - time_sample;
01676 break;
01677 }
01678 time_sample = next;
01679 }
01680 }
01681 return sample;
01682 }
01683
01684 static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
01685 {
01686 AVStream *st;
01687 int64_t seek_timestamp, timestamp;
01688 int sample;
01689 int i;
01690
01691 if (stream_index >= s->nb_streams)
01692 return -1;
01693
01694 st = s->streams[stream_index];
01695 sample = mov_seek_stream(st, sample_time, flags);
01696 if (sample < 0)
01697 return -1;
01698
01699
01700 seek_timestamp = st->index_entries[sample].timestamp;
01701
01702 for (i = 0; i < s->nb_streams; i++) {
01703 st = s->streams[i];
01704 if (stream_index == i || st->discard == AVDISCARD_ALL)
01705 continue;
01706
01707 timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base);
01708 mov_seek_stream(st, timestamp, flags);
01709 }
01710 return 0;
01711 }
01712
01713 static int mov_read_close(AVFormatContext *s)
01714 {
01715 int i;
01716 MOVContext *mov = s->priv_data;
01717 for(i=0; i<s->nb_streams; i++) {
01718 MOVStreamContext *sc = s->streams[i]->priv_data;
01719 av_freep(&sc->ctts_data);
01720 }
01721 if(mov->dv_demux){
01722 for(i=0; i<mov->dv_fctx->nb_streams; i++){
01723 av_freep(&mov->dv_fctx->streams[i]->codec);
01724 av_freep(&mov->dv_fctx->streams[i]);
01725 }
01726 av_freep(&mov->dv_fctx);
01727 av_freep(&mov->dv_demux);
01728 }
01729 return 0;
01730 }
01731
01732 AVInputFormat mov_demuxer = {
01733 "mov,mp4,m4a,3gp,3g2,mj2",
01734 "QuickTime/MPEG4/Motion JPEG 2000 format",
01735 sizeof(MOVContext),
01736 mov_probe,
01737 mov_read_header,
01738 mov_read_packet,
01739 mov_read_close,
01740 mov_read_seek,
01741 };