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