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