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