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