00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "avformat.h"
00022 #include "avi.h"
00023 #include "dv.h"
00024 #include "riff.h"
00025
00026 #undef NDEBUG
00027 #include <assert.h>
00028
00029
00030
00031
00032 typedef struct AVIStream {
00033 int64_t frame_offset;
00034
00035 int remaining;
00036 int packet_size;
00037
00038 int scale;
00039 int rate;
00040 int sample_size;
00041
00042 int64_t cum_len;
00043
00044 int prefix;
00045 int prefix_count;
00046 } AVIStream;
00047
00048 typedef struct {
00049 int64_t riff_end;
00050 int64_t movi_end;
00051 int64_t fsize;
00052 offset_t movi_list;
00053 int index_loaded;
00054 int is_odml;
00055 int non_interleaved;
00056 int stream_index;
00057 DVDemuxContext* dv_demux;
00058 } AVIContext;
00059
00060 static const char avi_headers[][8] = {
00061 { 'R', 'I', 'F', 'F', 'A', 'V', 'I', ' ' },
00062 { 'R', 'I', 'F', 'F', 'A', 'V', 'I', 'X' },
00063 { 'R', 'I', 'F', 'F', 'A', 'V', 'I', 0x19},
00064 { 'O', 'N', '2', ' ', 'O', 'N', '2', 'f' },
00065 { 'R', 'I', 'F', 'F', 'A', 'M', 'V', ' ' },
00066 { 0 }
00067 };
00068
00069 static int avi_load_index(AVFormatContext *s);
00070 static int guess_ni_flag(AVFormatContext *s);
00071
00072 #ifdef DEBUG
00073 static void print_tag(const char *str, unsigned int tag, int size)
00074 {
00075 printf("%s: tag=%c%c%c%c size=0x%x\n",
00076 str, tag & 0xff,
00077 (tag >> 8) & 0xff,
00078 (tag >> 16) & 0xff,
00079 (tag >> 24) & 0xff,
00080 size);
00081 }
00082 #endif
00083
00084 static int get_riff(AVIContext *avi, ByteIOContext *pb)
00085 {
00086 char header[8];
00087 int i;
00088
00089
00090 get_buffer(pb, header, 4);
00091 avi->riff_end = get_le32(pb);
00092 avi->riff_end += url_ftell(pb);
00093 get_buffer(pb, header+4, 4);
00094
00095 for(i=0; avi_headers[i][0]; i++)
00096 if(!memcmp(header, avi_headers[i], 8))
00097 break;
00098 if(!avi_headers[i][0])
00099 return -1;
00100
00101 if(header[7] == 0x19)
00102 av_log(NULL, AV_LOG_INFO, "file has been generated with a totally broken muxer\n");
00103
00104 return 0;
00105 }
00106
00107 static int read_braindead_odml_indx(AVFormatContext *s, int frame_num){
00108 AVIContext *avi = s->priv_data;
00109 ByteIOContext *pb = s->pb;
00110 int longs_pre_entry= get_le16(pb);
00111 int index_sub_type = get_byte(pb);
00112 int index_type = get_byte(pb);
00113 int entries_in_use = get_le32(pb);
00114 int chunk_id = get_le32(pb);
00115 int64_t base = get_le64(pb);
00116 int stream_id= 10*((chunk_id&0xFF) - '0') + (((chunk_id>>8)&0xFF) - '0');
00117 AVStream *st;
00118 AVIStream *ast;
00119 int i;
00120 int64_t last_pos= -1;
00121 int64_t filesize= url_fsize(s->pb);
00122
00123 #ifdef DEBUG_SEEK
00124 av_log(s, AV_LOG_ERROR, "longs_pre_entry:%d index_type:%d entries_in_use:%d chunk_id:%X base:%16"PRIX64"\n",
00125 longs_pre_entry,index_type, entries_in_use, chunk_id, base);
00126 #endif
00127
00128 if(stream_id > s->nb_streams || stream_id < 0)
00129 return -1;
00130 st= s->streams[stream_id];
00131 ast = st->priv_data;
00132
00133 if(index_sub_type)
00134 return -1;
00135
00136 get_le32(pb);
00137
00138 if(index_type && longs_pre_entry != 2)
00139 return -1;
00140 if(index_type>1)
00141 return -1;
00142
00143 if(filesize > 0 && base >= filesize){
00144 av_log(s, AV_LOG_ERROR, "ODML index invalid\n");
00145 if(base>>32 == (base & 0xFFFFFFFF) && (base & 0xFFFFFFFF) < filesize && filesize <= 0xFFFFFFFF)
00146 base &= 0xFFFFFFFF;
00147 else
00148 return -1;
00149 }
00150
00151 for(i=0; i<entries_in_use; i++){
00152 if(index_type){
00153 int64_t pos= get_le32(pb) + base - 8;
00154 int len = get_le32(pb);
00155 int key= len >= 0;
00156 len &= 0x7FFFFFFF;
00157
00158 #ifdef DEBUG_SEEK
00159 av_log(s, AV_LOG_ERROR, "pos:%"PRId64", len:%X\n", pos, len);
00160 #endif
00161 if(last_pos == pos || pos == base - 8)
00162 avi->non_interleaved= 1;
00163 else
00164 av_add_index_entry(st, pos, ast->cum_len / FFMAX(1, ast->sample_size), len, 0, key ? AVINDEX_KEYFRAME : 0);
00165
00166 if(ast->sample_size)
00167 ast->cum_len += len;
00168 else
00169 ast->cum_len ++;
00170 last_pos= pos;
00171 }else{
00172 int64_t offset, pos;
00173 int duration;
00174 offset = get_le64(pb);
00175 get_le32(pb);
00176 duration = get_le32(pb);
00177 pos = url_ftell(pb);
00178
00179 url_fseek(pb, offset+8, SEEK_SET);
00180 read_braindead_odml_indx(s, frame_num);
00181 frame_num += duration;
00182
00183 url_fseek(pb, pos, SEEK_SET);
00184 }
00185 }
00186 avi->index_loaded=1;
00187 return 0;
00188 }
00189
00190 static void clean_index(AVFormatContext *s){
00191 int i;
00192 int64_t j;
00193
00194 for(i=0; i<s->nb_streams; i++){
00195 AVStream *st = s->streams[i];
00196 AVIStream *ast = st->priv_data;
00197 int n= st->nb_index_entries;
00198 int max= ast->sample_size;
00199 int64_t pos, size, ts;
00200
00201 if(n != 1 || ast->sample_size==0)
00202 continue;
00203
00204 while(max < 1024) max+=max;
00205
00206 pos= st->index_entries[0].pos;
00207 size= st->index_entries[0].size;
00208 ts= st->index_entries[0].timestamp;
00209
00210 for(j=0; j<size; j+=max){
00211 av_add_index_entry(st, pos+j, ts + j/ast->sample_size, FFMIN(max, size-j), 0, AVINDEX_KEYFRAME);
00212 }
00213 }
00214 }
00215
00216 static int avi_read_tag(ByteIOContext *pb, char *buf, int maxlen, unsigned int size)
00217 {
00218 offset_t i = url_ftell(pb);
00219 size += (size & 1);
00220 get_strz(pb, buf, maxlen);
00221 url_fseek(pb, i+size, SEEK_SET);
00222 return 0;
00223 }
00224
00225 static int avi_read_header(AVFormatContext *s, AVFormatParameters *ap)
00226 {
00227 AVIContext *avi = s->priv_data;
00228 ByteIOContext *pb = s->pb;
00229 uint32_t tag, tag1, handler;
00230 int codec_type, stream_index, frame_period, bit_rate;
00231 unsigned int size, nb_frames;
00232 int i;
00233 AVStream *st;
00234 AVIStream *ast = NULL;
00235 char str_track[4];
00236 int avih_width=0, avih_height=0;
00237 int amv_file_format=0;
00238
00239 avi->stream_index= -1;
00240
00241 if (get_riff(avi, pb) < 0)
00242 return -1;
00243
00244 avi->fsize = url_fsize(pb);
00245 if(avi->fsize<=0)
00246 avi->fsize= avi->riff_end;
00247
00248
00249 stream_index = -1;
00250 codec_type = -1;
00251 frame_period = 0;
00252 for(;;) {
00253 if (url_feof(pb))
00254 goto fail;
00255 tag = get_le32(pb);
00256 size = get_le32(pb);
00257 #ifdef DEBUG
00258 print_tag("tag", tag, size);
00259 #endif
00260
00261 switch(tag) {
00262 case MKTAG('L', 'I', 'S', 'T'):
00263
00264 tag1 = get_le32(pb);
00265 #ifdef DEBUG
00266 print_tag("list", tag1, 0);
00267 #endif
00268 if (tag1 == MKTAG('m', 'o', 'v', 'i')) {
00269 avi->movi_list = url_ftell(pb) - 4;
00270 if(size) avi->movi_end = avi->movi_list + size + (size & 1);
00271 else avi->movi_end = url_fsize(pb);
00272 #ifdef DEBUG
00273 printf("movi end=%"PRIx64"\n", avi->movi_end);
00274 #endif
00275 goto end_of_header;
00276 }
00277 break;
00278 case MKTAG('d', 'm', 'l', 'h'):
00279 avi->is_odml = 1;
00280 url_fskip(pb, size + (size & 1));
00281 break;
00282 case MKTAG('a', 'm', 'v', 'h'):
00283 amv_file_format=1;
00284 case MKTAG('a', 'v', 'i', 'h'):
00285
00286
00287 frame_period = get_le32(pb);
00288 bit_rate = get_le32(pb) * 8;
00289 get_le32(pb);
00290 avi->non_interleaved |= get_le32(pb) & AVIF_MUSTUSEINDEX;
00291
00292 url_fskip(pb, 2 * 4);
00293 get_le32(pb);
00294 get_le32(pb);
00295 avih_width=get_le32(pb);
00296 avih_height=get_le32(pb);
00297
00298 url_fskip(pb, size - 10 * 4);
00299 break;
00300 case MKTAG('s', 't', 'r', 'h'):
00301
00302
00303 tag1 = get_le32(pb);
00304 handler = get_le32(pb);
00305
00306 if(tag1 == MKTAG('p', 'a', 'd', 's')){
00307 url_fskip(pb, size - 8);
00308 break;
00309 }else{
00310 stream_index++;
00311 st = av_new_stream(s, stream_index);
00312 if (!st)
00313 goto fail;
00314
00315 ast = av_mallocz(sizeof(AVIStream));
00316 if (!ast)
00317 goto fail;
00318 st->priv_data = ast;
00319 }
00320 if(amv_file_format)
00321 tag1 = stream_index ? MKTAG('a','u','d','s') : MKTAG('v','i','d','s');
00322
00323 #ifdef DEBUG
00324 print_tag("strh", tag1, -1);
00325 #endif
00326 if(tag1 == MKTAG('i', 'a', 'v', 's') || tag1 == MKTAG('i', 'v', 'a', 's')){
00327 int64_t dv_dur;
00328
00329
00330
00331
00332
00333 if (s->nb_streams != 1)
00334 goto fail;
00335
00336 if (handler != MKTAG('d', 'v', 's', 'd') &&
00337 handler != MKTAG('d', 'v', 'h', 'd') &&
00338 handler != MKTAG('d', 'v', 's', 'l'))
00339 goto fail;
00340
00341 ast = s->streams[0]->priv_data;
00342 av_freep(&s->streams[0]->codec->extradata);
00343 av_freep(&s->streams[0]);
00344 s->nb_streams = 0;
00345 if (ENABLE_DV_DEMUXER) {
00346 avi->dv_demux = dv_init_demux(s);
00347 if (!avi->dv_demux)
00348 goto fail;
00349 }
00350 s->streams[0]->priv_data = ast;
00351 url_fskip(pb, 3 * 4);
00352 ast->scale = get_le32(pb);
00353 ast->rate = get_le32(pb);
00354 url_fskip(pb, 4);
00355
00356 dv_dur = get_le32(pb);
00357 if (ast->scale > 0 && ast->rate > 0 && dv_dur > 0) {
00358 dv_dur *= AV_TIME_BASE;
00359 s->duration = av_rescale(dv_dur, ast->scale, ast->rate);
00360 }
00361
00362
00363
00364
00365
00366 stream_index = s->nb_streams - 1;
00367 url_fskip(pb, size - 9*4);
00368 break;
00369 }
00370
00371 assert(stream_index < s->nb_streams);
00372 st->codec->stream_codec_tag= handler;
00373
00374 get_le32(pb);
00375 get_le16(pb);
00376 get_le16(pb);
00377 get_le32(pb);
00378 ast->scale = get_le32(pb);
00379 ast->rate = get_le32(pb);
00380 if(ast->scale && ast->rate){
00381 }else if(frame_period){
00382 ast->rate = 1000000;
00383 ast->scale = frame_period;
00384 }else{
00385 ast->rate = 25;
00386 ast->scale = 1;
00387 }
00388 av_set_pts_info(st, 64, ast->scale, ast->rate);
00389
00390 ast->cum_len=get_le32(pb);
00391 nb_frames = get_le32(pb);
00392
00393 st->start_time = 0;
00394 st->duration = nb_frames;
00395 get_le32(pb);
00396 get_le32(pb);
00397 ast->sample_size = get_le32(pb);
00398 ast->cum_len *= FFMAX(1, ast->sample_size);
00399
00400
00401 switch(tag1) {
00402 case MKTAG('v', 'i', 'd', 's'):
00403 codec_type = CODEC_TYPE_VIDEO;
00404
00405 ast->sample_size = 0;
00406 break;
00407 case MKTAG('a', 'u', 'd', 's'):
00408 codec_type = CODEC_TYPE_AUDIO;
00409 break;
00410 case MKTAG('t', 'x', 't', 's'):
00411
00412 codec_type = CODEC_TYPE_DATA;
00413 break;
00414 default:
00415 av_log(s, AV_LOG_ERROR, "unknown stream type %X\n", tag1);
00416 goto fail;
00417 }
00418 ast->frame_offset= ast->cum_len;
00419 url_fskip(pb, size - 12 * 4);
00420 break;
00421 case MKTAG('s', 't', 'r', 'f'):
00422
00423 if (stream_index >= (unsigned)s->nb_streams || avi->dv_demux) {
00424 url_fskip(pb, size);
00425 } else {
00426 st = s->streams[stream_index];
00427 switch(codec_type) {
00428 case CODEC_TYPE_VIDEO:
00429 if(amv_file_format){
00430 st->codec->width=avih_width;
00431 st->codec->height=avih_height;
00432 st->codec->codec_type = CODEC_TYPE_VIDEO;
00433 st->codec->codec_id = CODEC_ID_AMV;
00434 url_fskip(pb, size);
00435 break;
00436 }
00437 get_le32(pb);
00438 st->codec->width = get_le32(pb);
00439 st->codec->height = get_le32(pb);
00440 get_le16(pb);
00441 st->codec->bits_per_sample= get_le16(pb);
00442 tag1 = get_le32(pb);
00443 get_le32(pb);
00444 get_le32(pb);
00445 get_le32(pb);
00446 get_le32(pb);
00447 get_le32(pb);
00448
00449 if (tag1 == MKTAG('D', 'X', 'S', 'B')) {
00450 st->codec->codec_type = CODEC_TYPE_SUBTITLE;
00451 st->codec->codec_tag = tag1;
00452 st->codec->codec_id = CODEC_ID_XSUB;
00453 break;
00454 }
00455
00456 if(size > 10*4 && size<(1<<30)){
00457 st->codec->extradata_size= size - 10*4;
00458 st->codec->extradata= av_malloc(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
00459 if (!st->codec->extradata) {
00460 st->codec->extradata_size= 0;
00461 return AVERROR(ENOMEM);
00462 }
00463 get_buffer(pb, st->codec->extradata, st->codec->extradata_size);
00464 }
00465
00466 if(st->codec->extradata_size & 1)
00467 get_byte(pb);
00468
00469
00470
00471
00472 if (st->codec->extradata_size && (st->codec->bits_per_sample <= 8)) {
00473 st->codec->palctrl = av_mallocz(sizeof(AVPaletteControl));
00474 #ifdef WORDS_BIGENDIAN
00475 for (i = 0; i < FFMIN(st->codec->extradata_size, AVPALETTE_SIZE)/4; i++)
00476 st->codec->palctrl->palette[i] = bswap_32(((uint32_t*)st->codec->extradata)[i]);
00477 #else
00478 memcpy(st->codec->palctrl->palette, st->codec->extradata,
00479 FFMIN(st->codec->extradata_size, AVPALETTE_SIZE));
00480 #endif
00481 st->codec->palctrl->palette_changed = 1;
00482 }
00483
00484 #ifdef DEBUG
00485 print_tag("video", tag1, 0);
00486 #endif
00487 st->codec->codec_type = CODEC_TYPE_VIDEO;
00488 st->codec->codec_tag = tag1;
00489 st->codec->codec_id = codec_get_id(codec_bmp_tags, tag1);
00490 st->need_parsing = AVSTREAM_PARSE_HEADERS;
00491
00492 break;
00493 case CODEC_TYPE_AUDIO:
00494 get_wav_header(pb, st->codec, size);
00495 if(ast->sample_size && st->codec->block_align && ast->sample_size % st->codec->block_align)
00496 av_log(s, AV_LOG_DEBUG, "invalid sample size or block align detected\n");
00497 if (size%2)
00498 url_fskip(pb, 1);
00499
00500
00501 st->need_parsing = AVSTREAM_PARSE_TIMESTAMPS;
00502
00503 if (st->codec->codec_id == CODEC_ID_AAC && st->codec->extradata_size)
00504 st->need_parsing = AVSTREAM_PARSE_NONE;
00505
00506
00507 if (st->codec->stream_codec_tag == ff_get_fourcc("Axan")){
00508 st->codec->codec_id = CODEC_ID_XAN_DPCM;
00509 st->codec->codec_tag = 0;
00510 }
00511 if (amv_file_format)
00512 st->codec->codec_id = CODEC_ID_ADPCM_IMA_AMV;
00513 break;
00514 default:
00515 st->codec->codec_type = CODEC_TYPE_DATA;
00516 st->codec->codec_id= CODEC_ID_NONE;
00517 st->codec->codec_tag= 0;
00518 url_fskip(pb, size);
00519 break;
00520 }
00521 }
00522 break;
00523 case MKTAG('i', 'n', 'd', 'x'):
00524 i= url_ftell(pb);
00525 if(!url_is_streamed(pb) && !(s->flags & AVFMT_FLAG_IGNIDX)){
00526 read_braindead_odml_indx(s, 0);
00527 }
00528 url_fseek(pb, i+size, SEEK_SET);
00529 break;
00530 case MKTAG('v', 'p', 'r', 'p'):
00531 if(stream_index < (unsigned)s->nb_streams && size > 9*4){
00532 AVRational active, active_aspect;
00533
00534 st = s->streams[stream_index];
00535 get_le32(pb);
00536 get_le32(pb);
00537 get_le32(pb);
00538 get_le32(pb);
00539 get_le32(pb);
00540
00541 active_aspect.num= get_le16(pb);
00542 active_aspect.den= get_le16(pb);
00543 active.num = get_le32(pb);
00544 active.den = get_le32(pb);
00545 get_le32(pb);
00546
00547 if(active_aspect.num && active_aspect.den && active.num && active.den){
00548 st->codec->sample_aspect_ratio= av_div_q(active_aspect, active);
00549
00550 }
00551 size -= 9*4;
00552 }
00553 url_fseek(pb, size, SEEK_CUR);
00554 break;
00555 case MKTAG('I', 'N', 'A', 'M'):
00556 avi_read_tag(pb, s->title, sizeof(s->title), size);
00557 break;
00558 case MKTAG('I', 'A', 'R', 'T'):
00559 avi_read_tag(pb, s->author, sizeof(s->author), size);
00560 break;
00561 case MKTAG('I', 'C', 'O', 'P'):
00562 avi_read_tag(pb, s->copyright, sizeof(s->copyright), size);
00563 break;
00564 case MKTAG('I', 'C', 'M', 'T'):
00565 avi_read_tag(pb, s->comment, sizeof(s->comment), size);
00566 break;
00567 case MKTAG('I', 'G', 'N', 'R'):
00568 avi_read_tag(pb, s->genre, sizeof(s->genre), size);
00569 break;
00570 case MKTAG('I', 'P', 'R', 'D'):
00571 avi_read_tag(pb, s->album, sizeof(s->album), size);
00572 break;
00573 case MKTAG('I', 'P', 'R', 'T'):
00574 avi_read_tag(pb, str_track, sizeof(str_track), size);
00575 sscanf(str_track, "%d", &s->track);
00576 break;
00577 default:
00578 if(size > 1000000){
00579 av_log(s, AV_LOG_ERROR, "well something went wrong during header parsing, "
00580 "ill ignore it and try to continue anyway\n");
00581 avi->movi_list = url_ftell(pb) - 4;
00582 avi->movi_end = url_fsize(pb);
00583 goto end_of_header;
00584 }
00585
00586 size += (size & 1);
00587 url_fskip(pb, size);
00588 break;
00589 }
00590 }
00591 end_of_header:
00592
00593 if (stream_index != s->nb_streams - 1) {
00594 fail:
00595 for(i=0;i<s->nb_streams;i++) {
00596 av_freep(&s->streams[i]->codec->extradata);
00597 av_freep(&s->streams[i]);
00598 }
00599 return -1;
00600 }
00601
00602 if(!avi->index_loaded && !url_is_streamed(pb))
00603 avi_load_index(s);
00604 avi->index_loaded = 1;
00605 avi->non_interleaved |= guess_ni_flag(s);
00606 if(avi->non_interleaved)
00607 clean_index(s);
00608
00609 return 0;
00610 }
00611
00612 static int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
00613 {
00614 AVIContext *avi = s->priv_data;
00615 ByteIOContext *pb = s->pb;
00616 int n, d[8], size;
00617 offset_t i, sync;
00618 void* dstr;
00619
00620 if (ENABLE_DV_DEMUXER && avi->dv_demux) {
00621 size = dv_get_packet(avi->dv_demux, pkt);
00622 if (size >= 0)
00623 return size;
00624 }
00625
00626 if(avi->non_interleaved){
00627 int best_stream_index = 0;
00628 AVStream *best_st= NULL;
00629 AVIStream *best_ast;
00630 int64_t best_ts= INT64_MAX;
00631 int i;
00632
00633 for(i=0; i<s->nb_streams; i++){
00634 AVStream *st = s->streams[i];
00635 AVIStream *ast = st->priv_data;
00636 int64_t ts= ast->frame_offset;
00637
00638 if(ast->sample_size)
00639 ts /= ast->sample_size;
00640 ts= av_rescale(ts, AV_TIME_BASE * (int64_t)st->time_base.num, st->time_base.den);
00641
00642
00643 if(ts < best_ts){
00644 best_ts= ts;
00645 best_st= st;
00646 best_stream_index= i;
00647 }
00648 }
00649 best_ast = best_st->priv_data;
00650 best_ts= av_rescale(best_ts, best_st->time_base.den, AV_TIME_BASE * (int64_t)best_st->time_base.num);
00651 if(best_ast->remaining)
00652 i= av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY | AVSEEK_FLAG_BACKWARD);
00653 else
00654 i= av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY);
00655
00656
00657 if(i>=0){
00658 int64_t pos= best_st->index_entries[i].pos;
00659 pos += best_ast->packet_size - best_ast->remaining;
00660 url_fseek(s->pb, pos + 8, SEEK_SET);
00661
00662
00663 assert(best_ast->remaining <= best_ast->packet_size);
00664
00665 avi->stream_index= best_stream_index;
00666 if(!best_ast->remaining)
00667 best_ast->packet_size=
00668 best_ast->remaining= best_st->index_entries[i].size;
00669 }
00670 }
00671
00672 resync:
00673 if(avi->stream_index >= 0){
00674 AVStream *st= s->streams[ avi->stream_index ];
00675 AVIStream *ast= st->priv_data;
00676 int size;
00677
00678 if(ast->sample_size <= 1)
00679 size= INT_MAX;
00680 else if(ast->sample_size < 32)
00681 size= 64*ast->sample_size;
00682 else
00683 size= ast->sample_size;
00684
00685 if(size > ast->remaining)
00686 size= ast->remaining;
00687 av_get_packet(pb, pkt, size);
00688
00689 if (ENABLE_DV_DEMUXER && avi->dv_demux) {
00690 dstr = pkt->destruct;
00691 size = dv_produce_packet(avi->dv_demux, pkt,
00692 pkt->data, pkt->size);
00693 pkt->destruct = dstr;
00694 pkt->flags |= PKT_FLAG_KEY;
00695 } else {
00696
00697 pkt->dts = ast->frame_offset;
00698
00699 if(ast->sample_size)
00700 pkt->dts /= ast->sample_size;
00701
00702 pkt->stream_index = avi->stream_index;
00703
00704 if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
00705 AVIndexEntry *e;
00706 int index;
00707 assert(st->index_entries);
00708
00709 index= av_index_search_timestamp(st, pkt->dts, 0);
00710 e= &st->index_entries[index];
00711
00712 if(index >= 0 && e->timestamp == ast->frame_offset){
00713 if (e->flags & AVINDEX_KEYFRAME)
00714 pkt->flags |= PKT_FLAG_KEY;
00715 }
00716 } else {
00717 pkt->flags |= PKT_FLAG_KEY;
00718 }
00719 if(ast->sample_size)
00720 ast->frame_offset += pkt->size;
00721 else
00722 ast->frame_offset++;
00723 }
00724 ast->remaining -= size;
00725 if(!ast->remaining){
00726 avi->stream_index= -1;
00727 ast->packet_size= 0;
00728 }
00729
00730 return size;
00731 }
00732
00733 memset(d, -1, sizeof(int)*8);
00734 for(i=sync=url_ftell(pb); !url_feof(pb); i++) {
00735 int j;
00736
00737 for(j=0; j<7; j++)
00738 d[j]= d[j+1];
00739 d[7]= get_byte(pb);
00740
00741 size= d[4] + (d[5]<<8) + (d[6]<<16) + (d[7]<<24);
00742
00743 if( d[2] >= '0' && d[2] <= '9'
00744 && d[3] >= '0' && d[3] <= '9'){
00745 n= (d[2] - '0') * 10 + (d[3] - '0');
00746 }else{
00747 n= 100;
00748 }
00749
00750 if(i + size > avi->fsize || d[0]<0)
00751 continue;
00752
00753
00754 if( (d[0] == 'i' && d[1] == 'x' && n < s->nb_streams)
00755
00756 ||(d[0] == 'J' && d[1] == 'U' && d[2] == 'N' && d[3] == 'K')
00757 ||(d[0] == 'i' && d[1] == 'd' && d[2] == 'x' && d[3] == '1')){
00758 url_fskip(pb, size);
00759
00760 goto resync;
00761 }
00762
00763 if( d[0] >= '0' && d[0] <= '9'
00764 && d[1] >= '0' && d[1] <= '9'){
00765 n= (d[0] - '0') * 10 + (d[1] - '0');
00766 }else{
00767 n= 100;
00768 }
00769
00770
00771 if(n < s->nb_streams){
00772 AVStream *st;
00773 AVIStream *ast;
00774 st = s->streams[n];
00775 ast = st->priv_data;
00776
00777 if( (st->discard >= AVDISCARD_DEFAULT && size==0)
00778
00779 || st->discard >= AVDISCARD_ALL){
00780 if(ast->sample_size) ast->frame_offset += pkt->size;
00781 else ast->frame_offset++;
00782 url_fskip(pb, size);
00783 goto resync;
00784 }
00785
00786 if( ((ast->prefix_count<5 || sync+9 > i) && d[2]<128 && d[3]<128) ||
00787 d[2]*256+d[3] == ast->prefix
00788
00789 ) {
00790
00791
00792 if(d[2]*256+d[3] == ast->prefix)
00793 ast->prefix_count++;
00794 else{
00795 ast->prefix= d[2]*256+d[3];
00796 ast->prefix_count= 0;
00797 }
00798
00799 avi->stream_index= n;
00800 ast->packet_size= size + 8;
00801 ast->remaining= size;
00802
00803 {
00804 uint64_t pos= url_ftell(pb) - 8;
00805 if(!st->index_entries || !st->nb_index_entries || st->index_entries[st->nb_index_entries - 1].pos < pos){
00806 av_add_index_entry(st, pos, ast->frame_offset / FFMAX(1, ast->sample_size), size, 0, AVINDEX_KEYFRAME);
00807 }
00808 }
00809 goto resync;
00810 }
00811 }
00812
00813 if ( d[0] >= '0' && d[0] <= '9'
00814 && d[1] >= '0' && d[1] <= '9'
00815 && ((d[2] == 'p' && d[3] == 'c'))
00816 && n < s->nb_streams && i + size <= avi->fsize) {
00817
00818 AVStream *st;
00819 int first, clr, flags, k, p;
00820
00821 st = s->streams[n];
00822
00823 first = get_byte(pb);
00824 clr = get_byte(pb);
00825 if(!clr)
00826 clr = 256;
00827 flags = get_le16(pb);
00828 p = 4;
00829 for (k = first; k < clr + first; k++) {
00830 int r, g, b;
00831 r = get_byte(pb);
00832 g = get_byte(pb);
00833 b = get_byte(pb);
00834 get_byte(pb);
00835 st->codec->palctrl->palette[k] = b + (g << 8) + (r << 16);
00836 }
00837 st->codec->palctrl->palette_changed = 1;
00838 goto resync;
00839 }
00840
00841 }
00842
00843 return -1;
00844 }
00845
00846
00847
00848 static int avi_read_idx1(AVFormatContext *s, int size)
00849 {
00850 AVIContext *avi = s->priv_data;
00851 ByteIOContext *pb = s->pb;
00852 int nb_index_entries, i;
00853 AVStream *st;
00854 AVIStream *ast;
00855 unsigned int index, tag, flags, pos, len;
00856 unsigned last_pos= -1;
00857
00858 nb_index_entries = size / 16;
00859 if (nb_index_entries <= 0)
00860 return -1;
00861
00862
00863 for(i = 0; i < nb_index_entries; i++) {
00864 tag = get_le32(pb);
00865 flags = get_le32(pb);
00866 pos = get_le32(pb);
00867 len = get_le32(pb);
00868 #if defined(DEBUG_SEEK)
00869 av_log(NULL, AV_LOG_DEBUG, "%d: tag=0x%x flags=0x%x pos=0x%x len=%d/",
00870 i, tag, flags, pos, len);
00871 #endif
00872 if(i==0 && pos > avi->movi_list)
00873 avi->movi_list= 0;
00874 pos += avi->movi_list;
00875
00876 index = ((tag & 0xff) - '0') * 10;
00877 index += ((tag >> 8) & 0xff) - '0';
00878 if (index >= s->nb_streams)
00879 continue;
00880 st = s->streams[index];
00881 ast = st->priv_data;
00882
00883 #if defined(DEBUG_SEEK)
00884 av_log(NULL, AV_LOG_DEBUG, "%d cum_len=%"PRId64"\n", len, ast->cum_len);
00885 #endif
00886 if(last_pos == pos)
00887 avi->non_interleaved= 1;
00888 else
00889 av_add_index_entry(st, pos, ast->cum_len / FFMAX(1, ast->sample_size), len, 0, (flags&AVIIF_INDEX) ? AVINDEX_KEYFRAME : 0);
00890 if(ast->sample_size)
00891 ast->cum_len += len;
00892 else
00893 ast->cum_len ++;
00894 last_pos= pos;
00895 }
00896 return 0;
00897 }
00898
00899 static int guess_ni_flag(AVFormatContext *s){
00900 int i;
00901 int64_t last_start=0;
00902 int64_t first_end= INT64_MAX;
00903
00904 for(i=0; i<s->nb_streams; i++){
00905 AVStream *st = s->streams[i];
00906 int n= st->nb_index_entries;
00907
00908 if(n <= 0)
00909 continue;
00910
00911 if(st->index_entries[0].pos > last_start)
00912 last_start= st->index_entries[0].pos;
00913 if(st->index_entries[n-1].pos < first_end)
00914 first_end= st->index_entries[n-1].pos;
00915 }
00916 return last_start > first_end;
00917 }
00918
00919 static int avi_load_index(AVFormatContext *s)
00920 {
00921 AVIContext *avi = s->priv_data;
00922 ByteIOContext *pb = s->pb;
00923 uint32_t tag, size;
00924 offset_t pos= url_ftell(pb);
00925
00926 url_fseek(pb, avi->movi_end, SEEK_SET);
00927 #ifdef DEBUG_SEEK
00928 printf("movi_end=0x%"PRIx64"\n", avi->movi_end);
00929 #endif
00930 for(;;) {
00931 if (url_feof(pb))
00932 break;
00933 tag = get_le32(pb);
00934 size = get_le32(pb);
00935 #ifdef DEBUG_SEEK
00936 printf("tag=%c%c%c%c size=0x%x\n",
00937 tag & 0xff,
00938 (tag >> 8) & 0xff,
00939 (tag >> 16) & 0xff,
00940 (tag >> 24) & 0xff,
00941 size);
00942 #endif
00943 switch(tag) {
00944 case MKTAG('i', 'd', 'x', '1'):
00945 if (avi_read_idx1(s, size) < 0)
00946 goto skip;
00947 else
00948 goto the_end;
00949 break;
00950 default:
00951 skip:
00952 size += (size & 1);
00953 url_fskip(pb, size);
00954 break;
00955 }
00956 }
00957 the_end:
00958 url_fseek(pb, pos, SEEK_SET);
00959 return 0;
00960 }
00961
00962 static int avi_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
00963 {
00964 AVIContext *avi = s->priv_data;
00965 AVStream *st;
00966 int i, index;
00967 int64_t pos;
00968
00969 if (!avi->index_loaded) {
00970
00971 avi_load_index(s);
00972 avi->index_loaded = 1;
00973 }
00974 assert(stream_index>= 0);
00975
00976 st = s->streams[stream_index];
00977 index= av_index_search_timestamp(st, timestamp, flags);
00978 if(index<0)
00979 return -1;
00980
00981
00982 pos = st->index_entries[index].pos;
00983 timestamp = st->index_entries[index].timestamp;
00984
00985
00986
00987 if (ENABLE_DV_DEMUXER && avi->dv_demux) {
00988
00989
00990
00991 assert(stream_index == 0);
00992
00993
00994
00995 dv_offset_reset(avi->dv_demux, timestamp);
00996
00997 url_fseek(s->pb, pos, SEEK_SET);
00998 avi->stream_index= -1;
00999 return 0;
01000 }
01001
01002 for(i = 0; i < s->nb_streams; i++) {
01003 AVStream *st2 = s->streams[i];
01004 AVIStream *ast2 = st2->priv_data;
01005
01006 ast2->packet_size=
01007 ast2->remaining= 0;
01008
01009 if (st2->nb_index_entries <= 0)
01010 continue;
01011
01012
01013 assert(st2->time_base.den == ast2->rate);
01014 assert(st2->time_base.num == ast2->scale);
01015 index = av_index_search_timestamp(
01016 st2,
01017 av_rescale(timestamp, st2->time_base.den*(int64_t)st->time_base.num, st->time_base.den * (int64_t)st2->time_base.num),
01018 flags | AVSEEK_FLAG_BACKWARD);
01019 if(index<0)
01020 index=0;
01021
01022 if(!avi->non_interleaved){
01023 while(index>0 && st2->index_entries[index].pos > pos)
01024 index--;
01025 while(index+1 < st2->nb_index_entries && st2->index_entries[index].pos < pos)
01026 index++;
01027 }
01028
01029
01030
01031 ast2->frame_offset = st2->index_entries[index].timestamp;
01032 if(ast2->sample_size)
01033 ast2->frame_offset *=ast2->sample_size;
01034 }
01035
01036
01037 url_fseek(s->pb, pos, SEEK_SET);
01038 avi->stream_index= -1;
01039 return 0;
01040 }
01041
01042 static int avi_read_close(AVFormatContext *s)
01043 {
01044 int i;
01045 AVIContext *avi = s->priv_data;
01046
01047 for(i=0;i<s->nb_streams;i++) {
01048 AVStream *st = s->streams[i];
01049 AVIStream *ast = st->priv_data;
01050 av_free(ast);
01051 av_free(st->codec->palctrl);
01052 }
01053
01054 if (avi->dv_demux)
01055 av_free(avi->dv_demux);
01056
01057 return 0;
01058 }
01059
01060 static int avi_probe(AVProbeData *p)
01061 {
01062 int i;
01063
01064
01065 for(i=0; avi_headers[i][0]; i++)
01066 if(!memcmp(p->buf , avi_headers[i] , 4) &&
01067 !memcmp(p->buf+8, avi_headers[i]+4, 4))
01068 return AVPROBE_SCORE_MAX;
01069
01070 return 0;
01071 }
01072
01073 AVInputFormat avi_demuxer = {
01074 "avi",
01075 "avi format",
01076 sizeof(AVIContext),
01077 avi_probe,
01078 avi_read_header,
01079 avi_read_packet,
01080 avi_read_close,
01081 avi_read_seek,
01082 };