00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 #include <strings.h>
00023 #include "libavutil/intreadwrite.h"
00024 #include "libavutil/bswap.h"
00025 #include "libavutil/opt.h"
00026 #include "libavutil/dict.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     int dshow_block_align;            
00052 
00053     AVFormatContext *sub_ctx;
00054     AVPacket sub_pkt;
00055     uint8_t *sub_buffer;
00056 
00057     int64_t seek_pos;
00058 } AVIStream;
00059 
00060 typedef struct {
00061     const AVClass *class;
00062     int64_t  riff_end;
00063     int64_t  movi_end;
00064     int64_t  fsize;
00065     int64_t movi_list;
00066     int64_t last_pkt_pos;
00067     int index_loaded;
00068     int is_odml;
00069     int non_interleaved;
00070     int stream_index;
00071     DVDemuxContext* dv_demux;
00072     int odml_depth;
00073     int use_odml;
00074 #define MAX_ODML_DEPTH 1000
00075 } AVIContext;
00076 
00077 
00078 static const AVOption options[] = {
00079     { "use_odml", "use odml index", offsetof(AVIContext, use_odml), FF_OPT_TYPE_INT, 1, -1, 1, AV_OPT_FLAG_DECODING_PARAM},
00080     { NULL },
00081 };
00082 
00083 static const AVClass demuxer_class = {
00084     "AVI demuxer",
00085     av_default_item_name,
00086     options,
00087     LIBAVUTIL_VERSION_INT,
00088 };
00089 
00090 
00091 static const char avi_headers[][8] = {
00092     { 'R', 'I', 'F', 'F',    'A', 'V', 'I', ' ' },
00093     { 'R', 'I', 'F', 'F',    'A', 'V', 'I', 'X' },
00094     { 'R', 'I', 'F', 'F',    'A', 'V', 'I', 0x19},
00095     { 'O', 'N', '2', ' ',    'O', 'N', '2', 'f' },
00096     { 'R', 'I', 'F', 'F',    'A', 'M', 'V', ' ' },
00097     { 0 }
00098 };
00099 
00100 static int avi_load_index(AVFormatContext *s);
00101 static int guess_ni_flag(AVFormatContext *s);
00102 
00103 #define print_tag(str, tag, size)                       \
00104     av_dlog(NULL, "%s: tag=%c%c%c%c size=0x%x\n",       \
00105            str, tag & 0xff,                             \
00106            (tag >> 8) & 0xff,                           \
00107            (tag >> 16) & 0xff,                          \
00108            (tag >> 24) & 0xff,                          \
00109            size)
00110 
00111 static inline int get_duration(AVIStream *ast, int len){
00112     if(ast->sample_size){
00113         return len;
00114     }else if (ast->dshow_block_align){
00115         return (len + ast->dshow_block_align - 1)/ast->dshow_block_align;
00116     }else
00117         return 1;
00118 }
00119 
00120 static int get_riff(AVFormatContext *s, AVIOContext *pb)
00121 {
00122     AVIContext *avi = s->priv_data;
00123     char header[8];
00124     int i;
00125 
00126     
00127     avio_read(pb, header, 4);
00128     avi->riff_end = avio_rl32(pb);  
00129     avi->riff_end += avio_tell(pb); 
00130     avio_read(pb, header+4, 4);
00131 
00132     for(i=0; avi_headers[i][0]; i++)
00133         if(!memcmp(header, avi_headers[i], 8))
00134             break;
00135     if(!avi_headers[i][0])
00136         return -1;
00137 
00138     if(header[7] == 0x19)
00139         av_log(s, AV_LOG_INFO, "This file has been generated by a totally broken muxer.\n");
00140 
00141     return 0;
00142 }
00143 
00144 static int read_braindead_odml_indx(AVFormatContext *s, int frame_num){
00145     AVIContext *avi = s->priv_data;
00146     AVIOContext *pb = s->pb;
00147     int longs_pre_entry= avio_rl16(pb);
00148     int index_sub_type = avio_r8(pb);
00149     int index_type     = avio_r8(pb);
00150     int entries_in_use = avio_rl32(pb);
00151     int chunk_id       = avio_rl32(pb);
00152     int64_t base       = avio_rl64(pb);
00153     int stream_id= 10*((chunk_id&0xFF) - '0') + (((chunk_id>>8)&0xFF) - '0');
00154     AVStream *st;
00155     AVIStream *ast;
00156     int i;
00157     int64_t last_pos= -1;
00158     int64_t filesize= avio_size(s->pb);
00159 
00160     av_dlog(s, "longs_pre_entry:%d index_type:%d entries_in_use:%d chunk_id:%X base:%16"PRIX64"\n",
00161             longs_pre_entry,index_type, entries_in_use, chunk_id, base);
00162 
00163     if(stream_id >= s->nb_streams || stream_id < 0)
00164         return -1;
00165     st= s->streams[stream_id];
00166     ast = st->priv_data;
00167 
00168     if(index_sub_type)
00169         return -1;
00170 
00171     avio_rl32(pb);
00172 
00173     if(index_type && longs_pre_entry != 2)
00174         return -1;
00175     if(index_type>1)
00176         return -1;
00177 
00178     if(filesize > 0 && base >= filesize){
00179         av_log(s, AV_LOG_ERROR, "ODML index invalid\n");
00180         if(base>>32 == (base & 0xFFFFFFFF) && (base & 0xFFFFFFFF) < filesize && filesize <= 0xFFFFFFFF)
00181             base &= 0xFFFFFFFF;
00182         else
00183             return -1;
00184     }
00185 
00186     for(i=0; i<entries_in_use; i++){
00187         if(index_type){
00188             int64_t pos= avio_rl32(pb) + base - 8;
00189             int len    = avio_rl32(pb);
00190             int key= len >= 0;
00191             len &= 0x7FFFFFFF;
00192 
00193 #ifdef DEBUG_SEEK
00194             av_log(s, AV_LOG_ERROR, "pos:%"PRId64", len:%X\n", pos, len);
00195 #endif
00196             if(url_feof(pb))
00197                 return -1;
00198 
00199             if(last_pos == pos || pos == base - 8)
00200                 avi->non_interleaved= 1;
00201             if(last_pos != pos && (len || !ast->sample_size))
00202                 av_add_index_entry(st, pos, ast->cum_len, len, 0, key ? AVINDEX_KEYFRAME : 0);
00203 
00204             ast->cum_len += get_duration(ast, len);
00205             last_pos= pos;
00206         }else{
00207             int64_t offset, pos;
00208             int duration;
00209             offset = avio_rl64(pb);
00210             avio_rl32(pb);       
00211             duration = avio_rl32(pb);
00212 
00213             if(url_feof(pb))
00214                 return -1;
00215 
00216             pos = avio_tell(pb);
00217 
00218             if(avi->odml_depth > MAX_ODML_DEPTH){
00219                 av_log(s, AV_LOG_ERROR, "Too deeply nested ODML indexes\n");
00220                 return -1;
00221             }
00222 
00223             if(avio_seek(pb, offset+8, SEEK_SET) < 0)
00224                 return -1;
00225             avi->odml_depth++;
00226             read_braindead_odml_indx(s, frame_num);
00227             avi->odml_depth--;
00228             frame_num += duration;
00229 
00230             if(avio_seek(pb, pos, SEEK_SET) < 0) {
00231                 av_log(s, AV_LOG_ERROR, "Failed to restore position after reading index");
00232                 return -1;
00233             }
00234 
00235         }
00236     }
00237     avi->index_loaded=1;
00238     return 0;
00239 }
00240 
00241 static void clean_index(AVFormatContext *s){
00242     int i;
00243     int64_t j;
00244 
00245     for(i=0; i<s->nb_streams; i++){
00246         AVStream *st = s->streams[i];
00247         AVIStream *ast = st->priv_data;
00248         int n= st->nb_index_entries;
00249         int max= ast->sample_size;
00250         int64_t pos, size, ts;
00251 
00252         if(n != 1 || ast->sample_size==0)
00253             continue;
00254 
00255         while(max < 1024) max+=max;
00256 
00257         pos= st->index_entries[0].pos;
00258         size= st->index_entries[0].size;
00259         ts= st->index_entries[0].timestamp;
00260 
00261         for(j=0; j<size; j+=max){
00262             av_add_index_entry(st, pos+j, ts+j, FFMIN(max, size-j), 0, AVINDEX_KEYFRAME);
00263         }
00264     }
00265 }
00266 
00267 static int avi_read_tag(AVFormatContext *s, AVStream *st, uint32_t tag, uint32_t size)
00268 {
00269     AVIOContext *pb = s->pb;
00270     char key[5] = {0}, *value;
00271 
00272     size += (size & 1);
00273 
00274     if (size == UINT_MAX)
00275         return -1;
00276     value = av_malloc(size+1);
00277     if (!value)
00278         return -1;
00279     avio_read(pb, value, size);
00280     value[size]=0;
00281 
00282     AV_WL32(key, tag);
00283 
00284     return av_dict_set(st ? &st->metadata : &s->metadata, key, value,
00285                             AV_DICT_DONT_STRDUP_VAL);
00286 }
00287 
00288 static void avi_read_info(AVFormatContext *s, uint64_t end)
00289 {
00290     while (avio_tell(s->pb) < end) {
00291         uint32_t tag  = avio_rl32(s->pb);
00292         uint32_t size = avio_rl32(s->pb);
00293         avi_read_tag(s, NULL, tag, size);
00294     }
00295 }
00296 
00297 static const char months[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
00298                                     "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
00299 
00300 static void avi_metadata_creation_time(AVDictionary **metadata, char *date)
00301 {
00302     char month[4], time[9], buffer[64];
00303     int i, day, year;
00304     
00305     if (sscanf(date, "%*3s%*[ ]%3s%*[ ]%2d%*[ ]%8s%*[ ]%4d",
00306                month, &day, time, &year) == 4) {
00307         for (i=0; i<12; i++)
00308             if (!strcasecmp(month, months[i])) {
00309                 snprintf(buffer, sizeof(buffer), "%.4d-%.2d-%.2d %s",
00310                          year, i+1, day, time);
00311                 av_dict_set(metadata, "creation_time", buffer, 0);
00312             }
00313     } else if (date[4] == '/' && date[7] == '/') {
00314         date[4] = date[7] = '-';
00315         av_dict_set(metadata, "creation_time", date, 0);
00316     }
00317 }
00318 
00319 static void avi_read_nikon(AVFormatContext *s, uint64_t end)
00320 {
00321     while (avio_tell(s->pb) < end) {
00322         uint32_t tag  = avio_rl32(s->pb);
00323         uint32_t size = avio_rl32(s->pb);
00324         switch (tag) {
00325         case MKTAG('n', 'c', 't', 'g'): {  
00326             uint64_t tag_end = avio_tell(s->pb) + size;
00327             while (avio_tell(s->pb) < tag_end) {
00328                 uint16_t tag  = avio_rl16(s->pb);
00329                 uint16_t size = avio_rl16(s->pb);
00330                 const char *name = NULL;
00331                 char buffer[64] = {0};
00332                 size -= avio_read(s->pb, buffer,
00333                                    FFMIN(size, sizeof(buffer)-1));
00334                 switch (tag) {
00335                 case 0x03:  name = "maker";  break;
00336                 case 0x04:  name = "model";  break;
00337                 case 0x13:  name = "creation_time";
00338                     if (buffer[4] == ':' && buffer[7] == ':')
00339                         buffer[4] = buffer[7] = '-';
00340                     break;
00341                 }
00342                 if (name)
00343                     av_dict_set(&s->metadata, name, buffer, 0);
00344                 avio_skip(s->pb, size);
00345             }
00346             break;
00347         }
00348         default:
00349             avio_skip(s->pb, size);
00350             break;
00351         }
00352     }
00353 }
00354 
00355 static int avi_read_header(AVFormatContext *s, AVFormatParameters *ap)
00356 {
00357     AVIContext *avi = s->priv_data;
00358     AVIOContext *pb = s->pb;
00359     unsigned int tag, tag1, handler;
00360     int codec_type, stream_index, frame_period;
00361     unsigned int size;
00362     int i;
00363     AVStream *st;
00364     AVIStream *ast = NULL;
00365     int avih_width=0, avih_height=0;
00366     int amv_file_format=0;
00367     uint64_t list_end = 0;
00368     int ret;
00369 
00370     avi->stream_index= -1;
00371 
00372     if (get_riff(s, pb) < 0)
00373         return -1;
00374 
00375     av_log(avi, AV_LOG_DEBUG, "use odml:%d\n", avi->use_odml);
00376 
00377     avi->fsize = avio_size(pb);
00378     if(avi->fsize<=0)
00379         avi->fsize= avi->riff_end == 8 ? INT64_MAX : avi->riff_end;
00380 
00381     
00382     stream_index = -1;
00383     codec_type = -1;
00384     frame_period = 0;
00385     for(;;) {
00386         if (url_feof(pb))
00387             goto fail;
00388         tag = avio_rl32(pb);
00389         size = avio_rl32(pb);
00390 
00391         print_tag("tag", tag, size);
00392 
00393         switch(tag) {
00394         case MKTAG('L', 'I', 'S', 'T'):
00395             list_end = avio_tell(pb) + size;
00396             
00397             tag1 = avio_rl32(pb);
00398 
00399             print_tag("list", tag1, 0);
00400 
00401             if (tag1 == MKTAG('m', 'o', 'v', 'i')) {
00402                 avi->movi_list = avio_tell(pb) - 4;
00403                 if(size) avi->movi_end = avi->movi_list + size + (size & 1);
00404                 else     avi->movi_end = avio_size(pb);
00405                 av_dlog(NULL, "movi end=%"PRIx64"\n", avi->movi_end);
00406                 goto end_of_header;
00407             }
00408             else if (tag1 == MKTAG('I', 'N', 'F', 'O'))
00409                 avi_read_info(s, list_end);
00410             else if (tag1 == MKTAG('n', 'c', 'd', 't'))
00411                 avi_read_nikon(s, list_end);
00412 
00413             break;
00414         case MKTAG('I', 'D', 'I', 'T'): {
00415             unsigned char date[64] = {0};
00416             size += (size & 1);
00417             size -= avio_read(pb, date, FFMIN(size, sizeof(date)-1));
00418             avio_skip(pb, size);
00419             avi_metadata_creation_time(&s->metadata, date);
00420             break;
00421         }
00422         case MKTAG('d', 'm', 'l', 'h'):
00423             avi->is_odml = 1;
00424             avio_skip(pb, size + (size & 1));
00425             break;
00426         case MKTAG('a', 'm', 'v', 'h'):
00427             amv_file_format=1;
00428         case MKTAG('a', 'v', 'i', 'h'):
00429             
00430             
00431             frame_period = avio_rl32(pb);
00432             avio_rl32(pb); 
00433             avio_rl32(pb);
00434             avi->non_interleaved |= avio_rl32(pb) & AVIF_MUSTUSEINDEX;
00435 
00436             avio_skip(pb, 2 * 4);
00437             avio_rl32(pb);
00438             avio_rl32(pb);
00439             avih_width=avio_rl32(pb);
00440             avih_height=avio_rl32(pb);
00441 
00442             avio_skip(pb, size - 10 * 4);
00443             break;
00444         case MKTAG('s', 't', 'r', 'h'):
00445             
00446 
00447             tag1 = avio_rl32(pb);
00448             handler = avio_rl32(pb); 
00449 
00450             if(tag1 == MKTAG('p', 'a', 'd', 's')){
00451                 avio_skip(pb, size - 8);
00452                 break;
00453             }else{
00454                 stream_index++;
00455                 st = av_new_stream(s, stream_index);
00456                 if (!st)
00457                     goto fail;
00458 
00459                 ast = av_mallocz(sizeof(AVIStream));
00460                 if (!ast)
00461                     goto fail;
00462                 st->priv_data = ast;
00463             }
00464             if(amv_file_format)
00465                 tag1 = stream_index ? MKTAG('a','u','d','s') : MKTAG('v','i','d','s');
00466 
00467             print_tag("strh", tag1, -1);
00468 
00469             if(tag1 == MKTAG('i', 'a', 'v', 's') || tag1 == MKTAG('i', 'v', 'a', 's')){
00470                 int64_t dv_dur;
00471 
00472                 
00473 
00474 
00475 
00476                 if (s->nb_streams != 1)
00477                     goto fail;
00478 
00479                 if (handler != MKTAG('d', 'v', 's', 'd') &&
00480                     handler != MKTAG('d', 'v', 'h', 'd') &&
00481                     handler != MKTAG('d', 'v', 's', 'l'))
00482                    goto fail;
00483 
00484                 ast = s->streams[0]->priv_data;
00485                 av_freep(&s->streams[0]->codec->extradata);
00486                 av_freep(&s->streams[0]->codec);
00487                 av_freep(&s->streams[0]);
00488                 s->nb_streams = 0;
00489                 if (CONFIG_DV_DEMUXER) {
00490                     avi->dv_demux = dv_init_demux(s);
00491                     if (!avi->dv_demux)
00492                         goto fail;
00493                 }
00494                 s->streams[0]->priv_data = ast;
00495                 avio_skip(pb, 3 * 4);
00496                 ast->scale = avio_rl32(pb);
00497                 ast->rate = avio_rl32(pb);
00498                 avio_skip(pb, 4);  
00499 
00500                 dv_dur = avio_rl32(pb);
00501                 if (ast->scale > 0 && ast->rate > 0 && dv_dur > 0) {
00502                     dv_dur *= AV_TIME_BASE;
00503                     s->duration = av_rescale(dv_dur, ast->scale, ast->rate);
00504                 }
00505                 
00506 
00507 
00508 
00509 
00510                 stream_index = s->nb_streams - 1;
00511                 avio_skip(pb, size - 9*4);
00512                 break;
00513             }
00514 
00515             assert(stream_index < s->nb_streams);
00516             st->codec->stream_codec_tag= handler;
00517 
00518             avio_rl32(pb); 
00519             avio_rl16(pb); 
00520             avio_rl16(pb); 
00521             avio_rl32(pb); 
00522             ast->scale = avio_rl32(pb);
00523             ast->rate = avio_rl32(pb);
00524             if(!(ast->scale && ast->rate)){
00525                 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);
00526                 if(frame_period){
00527                     ast->rate = 1000000;
00528                     ast->scale = frame_period;
00529                 }else{
00530                     ast->rate = 25;
00531                     ast->scale = 1;
00532                 }
00533             }
00534             av_set_pts_info(st, 64, ast->scale, ast->rate);
00535 
00536             ast->cum_len=avio_rl32(pb); 
00537             st->nb_frames = avio_rl32(pb);
00538 
00539             st->start_time = 0;
00540             avio_rl32(pb); 
00541             avio_rl32(pb); 
00542             ast->sample_size = avio_rl32(pb); 
00543             ast->cum_len *= FFMAX(1, ast->sample_size);
00544 
00545 
00546             switch(tag1) {
00547             case MKTAG('v', 'i', 'd', 's'):
00548                 codec_type = AVMEDIA_TYPE_VIDEO;
00549 
00550                 ast->sample_size = 0;
00551                 break;
00552             case MKTAG('a', 'u', 'd', 's'):
00553                 codec_type = AVMEDIA_TYPE_AUDIO;
00554                 break;
00555             case MKTAG('t', 'x', 't', 's'):
00556                 codec_type = AVMEDIA_TYPE_SUBTITLE;
00557                 break;
00558             case MKTAG('d', 'a', 't', 's'):
00559                 codec_type = AVMEDIA_TYPE_DATA;
00560                 break;
00561             default:
00562                 av_log(s, AV_LOG_INFO, "unknown stream type %X\n", tag1);
00563             }
00564             if(ast->sample_size == 0)
00565                 st->duration = st->nb_frames;
00566             ast->frame_offset= ast->cum_len;
00567             avio_skip(pb, size - 12 * 4);
00568             break;
00569         case MKTAG('s', 't', 'r', 'f'):
00570             
00571             if (stream_index >= (unsigned)s->nb_streams || avi->dv_demux) {
00572                 avio_skip(pb, size);
00573             } else {
00574                 uint64_t cur_pos = avio_tell(pb);
00575                 if (cur_pos < list_end)
00576                     size = FFMIN(size, list_end - cur_pos);
00577                 st = s->streams[stream_index];
00578                 switch(codec_type) {
00579                 case AVMEDIA_TYPE_VIDEO:
00580                     if(amv_file_format){
00581                         st->codec->width=avih_width;
00582                         st->codec->height=avih_height;
00583                         st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00584                         st->codec->codec_id = CODEC_ID_AMV;
00585                         avio_skip(pb, size);
00586                         break;
00587                     }
00588                     tag1 = ff_get_bmp_header(pb, st);
00589 
00590                     if (tag1 == MKTAG('D', 'X', 'S', 'B') || tag1 == MKTAG('D','X','S','A')) {
00591                         st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
00592                         st->codec->codec_tag = tag1;
00593                         st->codec->codec_id = CODEC_ID_XSUB;
00594                         break;
00595                     }
00596 
00597                     if(size > 10*4 && size<(1<<30)){
00598                         st->codec->extradata_size= size - 10*4;
00599                         st->codec->extradata= av_malloc(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
00600                         if (!st->codec->extradata) {
00601                             st->codec->extradata_size= 0;
00602                             return AVERROR(ENOMEM);
00603                         }
00604                         avio_read(pb, st->codec->extradata, st->codec->extradata_size);
00605                     }
00606 
00607                     if(st->codec->extradata_size & 1) 
00608                         avio_r8(pb);
00609 
00610                     
00611                     
00612                     
00613                     if (st->codec->extradata_size && (st->codec->bits_per_coded_sample <= 8)) {
00614                         int pal_size = (1 << st->codec->bits_per_coded_sample) << 2;
00615                         const uint8_t *pal_src;
00616 
00617                         pal_size = FFMIN(pal_size, st->codec->extradata_size);
00618                         pal_src = st->codec->extradata + st->codec->extradata_size - pal_size;
00619 #if HAVE_BIGENDIAN
00620                         for (i = 0; i < pal_size/4; i++)
00621                             ast->pal[i] = AV_RL32(pal_src+4*i);
00622 #else
00623                         memcpy(ast->pal, pal_src, pal_size);
00624 #endif
00625                         ast->has_pal = 1;
00626                     }
00627 
00628                     print_tag("video", tag1, 0);
00629 
00630                     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00631                     st->codec->codec_tag = tag1;
00632                     st->codec->codec_id = ff_codec_get_id(ff_codec_bmp_tags, tag1);
00633                     st->need_parsing = AVSTREAM_PARSE_HEADERS; 
00634                     
00635                     if(tag1 == MKTAG('A', 'V', 'R', 'n') &&
00636                        st->codec->extradata_size >= 31 &&
00637                        !memcmp(&st->codec->extradata[28], "1:1", 3))
00638                         st->codec->codec_id = CODEC_ID_RAWVIDEO;
00639 
00640                     if(st->codec->codec_tag==0 && st->codec->height > 0 && st->codec->extradata_size < 1U<<30){
00641                         st->codec->extradata_size+= 9;
00642                         st->codec->extradata= av_realloc_f(st->codec->extradata, 1, st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
00643                         if(st->codec->extradata)
00644                             memcpy(st->codec->extradata + st->codec->extradata_size - 9, "BottomUp", 9);
00645                     }
00646                     st->codec->height= FFABS(st->codec->height);
00647 
00648 
00649                     break;
00650                 case AVMEDIA_TYPE_AUDIO:
00651                     ret = ff_get_wav_header(pb, st->codec, size);
00652                     if (ret < 0)
00653                         return ret;
00654                     ast->dshow_block_align= st->codec->block_align;
00655                     if(ast->sample_size && st->codec->block_align && ast->sample_size != st->codec->block_align){
00656                         av_log(s, AV_LOG_WARNING, "sample size (%d) != block align (%d)\n", ast->sample_size, st->codec->block_align);
00657                         ast->sample_size= st->codec->block_align;
00658                     }
00659                     if (size&1) 
00660                         avio_skip(pb, 1);
00661                     
00662 
00663                     st->need_parsing = AVSTREAM_PARSE_TIMESTAMPS;
00664                     
00665 
00666 
00667                     if (st->codec->codec_id == CODEC_ID_AAC && st->codec->extradata_size)
00668                         st->need_parsing = AVSTREAM_PARSE_NONE;
00669                     
00670 
00671                     if (st->codec->stream_codec_tag == AV_RL32("Axan")){
00672                         st->codec->codec_id  = CODEC_ID_XAN_DPCM;
00673                         st->codec->codec_tag = 0;
00674                     }
00675                     if (amv_file_format){
00676                         st->codec->codec_id  = CODEC_ID_ADPCM_IMA_AMV;
00677                         ast->dshow_block_align = 0;
00678                     }
00679                     break;
00680                 case AVMEDIA_TYPE_SUBTITLE:
00681                     st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
00682                     st->request_probe= 1;
00683                     break;
00684                 default:
00685                     st->codec->codec_type = AVMEDIA_TYPE_DATA;
00686                     st->codec->codec_id= CODEC_ID_NONE;
00687                     st->codec->codec_tag= 0;
00688                     avio_skip(pb, size);
00689                     break;
00690                 }
00691             }
00692             break;
00693         case MKTAG('i', 'n', 'd', 'x'):
00694             i= avio_tell(pb);
00695             if(pb->seekable && !(s->flags & AVFMT_FLAG_IGNIDX) && avi->use_odml){
00696                 read_braindead_odml_indx(s, 0);
00697             }
00698             avio_seek(pb, i+size, SEEK_SET);
00699             break;
00700         case MKTAG('v', 'p', 'r', 'p'):
00701             if(stream_index < (unsigned)s->nb_streams && size > 9*4){
00702                 AVRational active, active_aspect;
00703 
00704                 st = s->streams[stream_index];
00705                 avio_rl32(pb);
00706                 avio_rl32(pb);
00707                 avio_rl32(pb);
00708                 avio_rl32(pb);
00709                 avio_rl32(pb);
00710 
00711                 active_aspect.den= avio_rl16(pb);
00712                 active_aspect.num= avio_rl16(pb);
00713                 active.num       = avio_rl32(pb);
00714                 active.den       = avio_rl32(pb);
00715                 avio_rl32(pb); 
00716 
00717                 if(active_aspect.num && active_aspect.den && active.num && active.den){
00718                     st->sample_aspect_ratio= av_div_q(active_aspect, active);
00719 
00720                 }
00721                 size -= 9*4;
00722             }
00723             avio_skip(pb, size);
00724             break;
00725         case MKTAG('s', 't', 'r', 'n'):
00726             if(s->nb_streams){
00727                 avi_read_tag(s, s->streams[s->nb_streams-1], tag, size);
00728                 break;
00729             }
00730         default:
00731             if(size > 1000000){
00732                 av_log(s, AV_LOG_ERROR, "Something went wrong during header parsing, "
00733                                         "I will ignore it and try to continue anyway.\n");
00734                 avi->movi_list = avio_tell(pb) - 4;
00735                 avi->movi_end  = avio_size(pb);
00736                 goto end_of_header;
00737             }
00738             
00739             size += (size & 1);
00740             avio_skip(pb, size);
00741             break;
00742         }
00743     }
00744  end_of_header:
00745     
00746     if (stream_index != s->nb_streams - 1) {
00747     fail:
00748         return -1;
00749     }
00750 
00751     if(!avi->index_loaded && pb->seekable)
00752         avi_load_index(s);
00753     avi->index_loaded = 1;
00754     avi->non_interleaved |= guess_ni_flag(s) | (s->flags & AVFMT_FLAG_SORT_DTS);
00755     for(i=0; i<s->nb_streams; i++){
00756         AVStream *st = s->streams[i];
00757         if(st->nb_index_entries)
00758             break;
00759     }
00760     
00761     
00762     if(avi->dv_demux)
00763         avi->non_interleaved=0;
00764     if(i==s->nb_streams && avi->non_interleaved) {
00765         av_log(s, AV_LOG_WARNING, "non-interleaved AVI without index, switching to interleaved\n");
00766         avi->non_interleaved=0;
00767     }
00768 
00769     if(avi->non_interleaved) {
00770         av_log(s, AV_LOG_INFO, "non-interleaved AVI\n");
00771         clean_index(s);
00772     }
00773 
00774     ff_metadata_conv_ctx(s, NULL, ff_avi_metadata_conv);
00775 
00776     return 0;
00777 }
00778 
00779 static int read_gab2_sub(AVStream *st, AVPacket *pkt) {
00780     if (!strcmp(pkt->data, "GAB2") && AV_RL16(pkt->data+5) == 2) {
00781         uint8_t desc[256];
00782         int score = AVPROBE_SCORE_MAX / 2, ret;
00783         AVIStream *ast = st->priv_data;
00784         AVInputFormat *sub_demuxer;
00785         AVRational time_base;
00786         AVIOContext *pb = avio_alloc_context( pkt->data + 7,
00787                                               pkt->size - 7,
00788                                               0, NULL, NULL, NULL, NULL);
00789         AVProbeData pd;
00790         unsigned int desc_len = avio_rl32(pb);
00791 
00792         if (desc_len > pb->buf_end - pb->buf_ptr)
00793             goto error;
00794 
00795         ret = avio_get_str16le(pb, desc_len, desc, sizeof(desc));
00796         avio_skip(pb, desc_len - ret);
00797         if (*desc)
00798             av_dict_set(&st->metadata, "title", desc, 0);
00799 
00800         avio_rl16(pb);   
00801         avio_rl32(pb);   
00802 
00803         pd = (AVProbeData) { .buf = pb->buf_ptr, .buf_size = pb->buf_end - pb->buf_ptr };
00804         if (!(sub_demuxer = av_probe_input_format2(&pd, 1, &score)))
00805             goto error;
00806 
00807         if (!(ast->sub_ctx = avformat_alloc_context()))
00808             goto error;
00809 
00810         ast->sub_ctx->pb      = pb;
00811         if (!avformat_open_input(&ast->sub_ctx, "", sub_demuxer, NULL)) {
00812             av_read_packet(ast->sub_ctx, &ast->sub_pkt);
00813             *st->codec = *ast->sub_ctx->streams[0]->codec;
00814             ast->sub_ctx->streams[0]->codec->extradata = NULL;
00815             time_base = ast->sub_ctx->streams[0]->time_base;
00816             av_set_pts_info(st, 64, time_base.num, time_base.den);
00817         }
00818         ast->sub_buffer = pkt->data;
00819         memset(pkt, 0, sizeof(*pkt));
00820         return 1;
00821 error:
00822         av_freep(&pb);
00823     }
00824     return 0;
00825 }
00826 
00827 static AVStream *get_subtitle_pkt(AVFormatContext *s, AVStream *next_st,
00828                                   AVPacket *pkt)
00829 {
00830     AVIStream *ast, *next_ast = next_st->priv_data;
00831     int64_t ts, next_ts, ts_min = INT64_MAX;
00832     AVStream *st, *sub_st = NULL;
00833     int i;
00834 
00835     next_ts = av_rescale_q(next_ast->frame_offset, next_st->time_base,
00836                            AV_TIME_BASE_Q);
00837 
00838     for (i=0; i<s->nb_streams; i++) {
00839         st  = s->streams[i];
00840         ast = st->priv_data;
00841         if (st->discard < AVDISCARD_ALL && ast && ast->sub_pkt.data) {
00842             ts = av_rescale_q(ast->sub_pkt.dts, st->time_base, AV_TIME_BASE_Q);
00843             if (ts <= next_ts && ts < ts_min) {
00844                 ts_min = ts;
00845                 sub_st = st;
00846             }
00847         }
00848     }
00849 
00850     if (sub_st) {
00851         ast = sub_st->priv_data;
00852         *pkt = ast->sub_pkt;
00853         pkt->stream_index = sub_st->index;
00854         if (av_read_packet(ast->sub_ctx, &ast->sub_pkt) < 0)
00855             ast->sub_pkt.data = NULL;
00856     }
00857     return sub_st;
00858 }
00859 
00860 static int get_stream_idx(int *d){
00861     if(    d[0] >= '0' && d[0] <= '9'
00862         && d[1] >= '0' && d[1] <= '9'){
00863         return (d[0] - '0') * 10 + (d[1] - '0');
00864     }else{
00865         return 100; 
00866     }
00867 }
00868 
00869 static int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
00870 {
00871     AVIContext *avi = s->priv_data;
00872     AVIOContext *pb = s->pb;
00873     int n, d[8];
00874     unsigned int size;
00875     int64_t i, sync;
00876     void* dstr;
00877 
00878     if (CONFIG_DV_DEMUXER && avi->dv_demux) {
00879         int size = dv_get_packet(avi->dv_demux, pkt);
00880         if (size >= 0)
00881             return size;
00882     }
00883 
00884     if(avi->non_interleaved){
00885         int best_stream_index = 0;
00886         AVStream *best_st= NULL;
00887         AVIStream *best_ast;
00888         int64_t best_ts= INT64_MAX;
00889         int i;
00890 
00891         for(i=0; i<s->nb_streams; i++){
00892             AVStream *st = s->streams[i];
00893             AVIStream *ast = st->priv_data;
00894             int64_t ts= ast->frame_offset;
00895             int64_t last_ts;
00896 
00897             if(!st->nb_index_entries)
00898                 continue;
00899 
00900             last_ts = st->index_entries[st->nb_index_entries - 1].timestamp;
00901             if(!ast->remaining && ts > last_ts)
00902                 continue;
00903 
00904             ts = av_rescale_q(ts, st->time_base, (AVRational){FFMAX(1, ast->sample_size), AV_TIME_BASE});
00905 
00906 
00907             if(ts < best_ts){
00908                 best_ts= ts;
00909                 best_st= st;
00910                 best_stream_index= i;
00911             }
00912         }
00913         if(!best_st)
00914             return -1;
00915 
00916         best_ast = best_st->priv_data;
00917         best_ts = av_rescale_q(best_ts, (AVRational){FFMAX(1, best_ast->sample_size), AV_TIME_BASE}, best_st->time_base);
00918         if(best_ast->remaining)
00919             i= av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY | AVSEEK_FLAG_BACKWARD);
00920         else{
00921             i= av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY);
00922             if(i>=0)
00923                 best_ast->frame_offset= best_st->index_entries[i].timestamp;
00924         }
00925 
00926 
00927         if(i>=0){
00928             int64_t pos= best_st->index_entries[i].pos;
00929             pos += best_ast->packet_size - best_ast->remaining;
00930             avio_seek(s->pb, pos + 8, SEEK_SET);
00931 
00932 
00933             assert(best_ast->remaining <= best_ast->packet_size);
00934 
00935             avi->stream_index= best_stream_index;
00936             if(!best_ast->remaining)
00937                 best_ast->packet_size=
00938                 best_ast->remaining= best_st->index_entries[i].size;
00939         }
00940     }
00941 
00942 resync:
00943     if(avi->stream_index >= 0){
00944         AVStream *st= s->streams[ avi->stream_index ];
00945         AVIStream *ast= st->priv_data;
00946         int size, err;
00947 
00948         if(get_subtitle_pkt(s, st, pkt))
00949             return 0;
00950 
00951         if(ast->sample_size <= 1) 
00952             size= INT_MAX;
00953         else if(ast->sample_size < 32)
00954             
00955             size= 1024*ast->sample_size;
00956         else
00957             size= ast->sample_size;
00958 
00959         if(size > ast->remaining)
00960             size= ast->remaining;
00961         avi->last_pkt_pos= avio_tell(pb);
00962         err= av_get_packet(pb, pkt, size);
00963         if(err<0)
00964             return err;
00965 
00966         if(ast->has_pal && pkt->data && pkt->size<(unsigned)INT_MAX/2){
00967             uint8_t *pal;
00968             pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE);
00969             if(!pal){
00970                 av_log(s, AV_LOG_ERROR, "Failed to allocate data for palette\n");
00971             }else{
00972                 memcpy(pal, ast->pal, AVPALETTE_SIZE);
00973                 ast->has_pal = 0;
00974             }
00975         }
00976 
00977         if (CONFIG_DV_DEMUXER && avi->dv_demux) {
00978             dstr = pkt->destruct;
00979             size = dv_produce_packet(avi->dv_demux, pkt,
00980                                     pkt->data, pkt->size, pkt->pos);
00981             pkt->destruct = dstr;
00982             pkt->flags |= AV_PKT_FLAG_KEY;
00983             if (size < 0)
00984                 av_free_packet(pkt);
00985         } else if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE
00986                    && !st->codec->codec_tag && read_gab2_sub(st, pkt)) {
00987             ast->frame_offset++;
00988             avi->stream_index = -1;
00989             ast->remaining = 0;
00990             goto resync;
00991         } else {
00992             
00993             pkt->dts = ast->frame_offset;
00994 
00995             if(ast->sample_size)
00996                 pkt->dts /= ast->sample_size;
00997 
00998             pkt->stream_index = avi->stream_index;
00999 
01000             if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
01001                 AVIndexEntry *e;
01002                 int index;
01003                 assert(st->index_entries);
01004 
01005                 index= av_index_search_timestamp(st, ast->frame_offset, 0);
01006                 e= &st->index_entries[index];
01007 
01008                 if(index >= 0 && e->timestamp == ast->frame_offset){
01009                     if (index == st->nb_index_entries-1){
01010                         int key=1;
01011                         int i;
01012                         uint32_t state=-1;
01013                         for(i=0; i<FFMIN(size,256); i++){
01014                             if(st->codec->codec_id == CODEC_ID_MPEG4){
01015                                 if(state == 0x1B6){
01016                                     key= !(pkt->data[i]&0xC0);
01017                                     break;
01018                                 }
01019                             }else
01020                                 break;
01021                             state= (state<<8) + pkt->data[i];
01022                         }
01023                         if(!key)
01024                             e->flags &= ~AVINDEX_KEYFRAME;
01025                     }
01026                     if (e->flags & AVINDEX_KEYFRAME)
01027                         pkt->flags |= AV_PKT_FLAG_KEY;
01028                 }
01029             } else {
01030                 pkt->flags |= AV_PKT_FLAG_KEY;
01031             }
01032             ast->frame_offset += get_duration(ast, pkt->size);
01033         }
01034         ast->remaining -= size;
01035         if(!ast->remaining){
01036             avi->stream_index= -1;
01037             ast->packet_size= 0;
01038         }
01039 
01040         if(!avi->non_interleaved && pkt->pos >= 0 && ast->seek_pos > pkt->pos){
01041             av_free_packet(pkt);
01042             goto resync;
01043         }
01044         ast->seek_pos= 0;
01045 
01046         return size;
01047     }
01048 
01049     memset(d, -1, sizeof(int)*8);
01050     for(i=sync=avio_tell(pb); !url_feof(pb); i++) {
01051         int j;
01052 
01053         for(j=0; j<7; j++)
01054             d[j]= d[j+1];
01055         d[7]= avio_r8(pb);
01056 
01057         size= d[4] + (d[5]<<8) + (d[6]<<16) + (d[7]<<24);
01058 
01059         n= get_stream_idx(d+2);
01060 
01061         if(i + (uint64_t)size > avi->fsize || d[0]<0)
01062             continue;
01063 
01064         
01065         if(  (d[0] == 'i' && d[1] == 'x' && n < s->nb_streams)
01066         
01067            ||(d[0] == 'J' && d[1] == 'U' && d[2] == 'N' && d[3] == 'K')
01068            ||(d[0] == 'i' && d[1] == 'd' && d[2] == 'x' && d[3] == '1')){
01069             avio_skip(pb, size);
01070 
01071             goto resync;
01072         }
01073 
01074         
01075         if(d[0] == 'L' && d[1] == 'I' && d[2] == 'S' && d[3] == 'T'){
01076             avio_skip(pb, 4);
01077             goto resync;
01078         }
01079 
01080         n= get_stream_idx(d);
01081 
01082         if(!((i-avi->last_pkt_pos)&1) && get_stream_idx(d+1) < s->nb_streams)
01083             continue;
01084 
01085         
01086         if(d[2] == 'i' && d[3] == 'x' && n < s->nb_streams){
01087             avio_skip(pb, size);
01088             goto resync;
01089         }
01090 
01091         
01092         if(n < s->nb_streams){
01093             AVStream *st;
01094             AVIStream *ast;
01095             st = s->streams[n];
01096             ast = st->priv_data;
01097 
01098             if(s->nb_streams>=2){
01099                 AVStream *st1  = s->streams[1];
01100                 AVIStream *ast1= st1->priv_data;
01101                 
01102                 if(   d[2] == 'w' && d[3] == 'b'
01103                    && n==0
01104                    && st ->codec->codec_type == AVMEDIA_TYPE_VIDEO
01105                    && st1->codec->codec_type == AVMEDIA_TYPE_AUDIO
01106                    && ast->prefix == 'd'*256+'c'
01107                    && (d[2]*256+d[3] == ast1->prefix || !ast1->prefix_count)
01108                   ){
01109                     n=1;
01110                     st = st1;
01111                     ast = ast1;
01112                     av_log(s, AV_LOG_WARNING, "Invalid stream + prefix combination, assuming audio.\n");
01113                 }
01114             }
01115 
01116 
01117             if(   (st->discard >= AVDISCARD_DEFAULT && size==0)
01118                 
01119                || st->discard >= AVDISCARD_ALL){
01120                 ast->frame_offset += get_duration(ast, size);
01121                 avio_skip(pb, size);
01122                 goto resync;
01123             }
01124 
01125             if (d[2] == 'p' && d[3] == 'c' && size<=4*256+4) {
01126                 int k = avio_r8(pb);
01127                 int last = (k + avio_r8(pb) - 1) & 0xFF;
01128 
01129                 avio_rl16(pb); 
01130 
01131                 for (; k <= last; k++)
01132                     ast->pal[k] = avio_rb32(pb)>>8;
01133                 ast->has_pal= 1;
01134                 goto resync;
01135             } else if(   ((ast->prefix_count<5 || sync+9 > i) && d[2]<128 && d[3]<128) ||
01136                          d[2]*256+d[3] == ast->prefix 
01137 
01138 ) {
01139 
01140 
01141                 if(d[2]*256+d[3] == ast->prefix)
01142                     ast->prefix_count++;
01143                 else{
01144                     ast->prefix= d[2]*256+d[3];
01145                     ast->prefix_count= 0;
01146                 }
01147 
01148                 avi->stream_index= n;
01149                 ast->packet_size= size + 8;
01150                 ast->remaining= size;
01151 
01152                 if(size || !ast->sample_size){
01153                     uint64_t pos= avio_tell(pb) - 8;
01154                     if(!st->index_entries || !st->nb_index_entries || st->index_entries[st->nb_index_entries - 1].pos < pos){
01155                         av_add_index_entry(st, pos, ast->frame_offset, size, 0, AVINDEX_KEYFRAME);
01156                     }
01157                 }
01158                 goto resync;
01159             }
01160         }
01161     }
01162 
01163     return AVERROR_EOF;
01164 }
01165 
01166 
01167 
01168 static int avi_read_idx1(AVFormatContext *s, int size)
01169 {
01170     AVIContext *avi = s->priv_data;
01171     AVIOContext *pb = s->pb;
01172     int nb_index_entries, i;
01173     AVStream *st;
01174     AVIStream *ast;
01175     unsigned int index, tag, flags, pos, len;
01176     unsigned last_pos= -1;
01177 
01178     nb_index_entries = size / 16;
01179     if (nb_index_entries <= 0)
01180         return -1;
01181 
01182     
01183     for(i = 0; i < nb_index_entries; i++) {
01184         tag = avio_rl32(pb);
01185         flags = avio_rl32(pb);
01186         pos = avio_rl32(pb);
01187         len = avio_rl32(pb);
01188         av_dlog(s, "%d: tag=0x%x flags=0x%x pos=0x%x len=%d/",
01189                 i, tag, flags, pos, len);
01190         if(i==0 && pos > avi->movi_list)
01191             avi->movi_list= 0; 
01192         pos += avi->movi_list;
01193 
01194         index = ((tag & 0xff) - '0') * 10;
01195         index += ((tag >> 8) & 0xff) - '0';
01196         if (index >= s->nb_streams)
01197             continue;
01198         st = s->streams[index];
01199         ast = st->priv_data;
01200 
01201 #if defined(DEBUG_SEEK)
01202         av_log(s, AV_LOG_DEBUG, "%d cum_len=%"PRId64"\n", len, ast->cum_len);
01203 #endif
01204         if(url_feof(pb))
01205             return -1;
01206 
01207         if(last_pos == pos)
01208             avi->non_interleaved= 1;
01209         else if(len || !ast->sample_size)
01210             av_add_index_entry(st, pos, ast->cum_len, len, 0, (flags&AVIIF_INDEX) ? AVINDEX_KEYFRAME : 0);
01211         ast->cum_len += get_duration(ast, len);
01212         last_pos= pos;
01213     }
01214     return 0;
01215 }
01216 
01217 static int guess_ni_flag(AVFormatContext *s){
01218     int i;
01219     int64_t last_start=0;
01220     int64_t first_end= INT64_MAX;
01221     int64_t oldpos= avio_tell(s->pb);
01222 
01223     for(i=0; i<s->nb_streams; i++){
01224         AVStream *st = s->streams[i];
01225         int n= st->nb_index_entries;
01226         unsigned int size;
01227 
01228         if(n <= 0)
01229             continue;
01230 
01231         if(n >= 2){
01232             int64_t pos= st->index_entries[0].pos;
01233             avio_seek(s->pb, pos + 4, SEEK_SET);
01234             size= avio_rl32(s->pb);
01235             if(pos + size > st->index_entries[1].pos)
01236                 last_start= INT64_MAX;
01237         }
01238 
01239         if(st->index_entries[0].pos > last_start)
01240             last_start= st->index_entries[0].pos;
01241         if(st->index_entries[n-1].pos < first_end)
01242             first_end= st->index_entries[n-1].pos;
01243     }
01244     avio_seek(s->pb, oldpos, SEEK_SET);
01245     return last_start > first_end;
01246 }
01247 
01248 static int avi_load_index(AVFormatContext *s)
01249 {
01250     AVIContext *avi = s->priv_data;
01251     AVIOContext *pb = s->pb;
01252     uint32_t tag, size;
01253     int64_t pos= avio_tell(pb);
01254     int ret = -1;
01255 
01256     if (avio_seek(pb, avi->movi_end, SEEK_SET) < 0)
01257         goto the_end; 
01258     av_dlog(s, "movi_end=0x%"PRIx64"\n", avi->movi_end);
01259     for(;;) {
01260         if (url_feof(pb))
01261             break;
01262         tag = avio_rl32(pb);
01263         size = avio_rl32(pb);
01264         av_dlog(s, "tag=%c%c%c%c size=0x%x\n",
01265                  tag        & 0xff,
01266                 (tag >>  8) & 0xff,
01267                 (tag >> 16) & 0xff,
01268                 (tag >> 24) & 0xff,
01269                 size);
01270         switch(tag) {
01271         case MKTAG('i', 'd', 'x', '1'):
01272             if (avi_read_idx1(s, size) < 0)
01273                 goto skip;
01274             ret = 0;
01275                 goto the_end;
01276             break;
01277         default:
01278         skip:
01279             size += (size & 1);
01280             if (avio_skip(pb, size) < 0)
01281                 goto the_end; 
01282             break;
01283         }
01284     }
01285  the_end:
01286     avio_seek(pb, pos, SEEK_SET);
01287     return ret;
01288 }
01289 
01290 static void seek_subtitle(AVStream *st, AVStream *st2, int64_t timestamp)
01291 {
01292     AVIStream *ast2 = st2->priv_data;
01293     int64_t ts2 = av_rescale_q(timestamp, st->time_base, st2->time_base);
01294     av_free_packet(&ast2->sub_pkt);
01295     if (avformat_seek_file(ast2->sub_ctx, 0, INT64_MIN, ts2, ts2, 0) >= 0 ||
01296         avformat_seek_file(ast2->sub_ctx, 0, ts2, ts2, INT64_MAX, 0) >= 0)
01297         av_read_packet(ast2->sub_ctx, &ast2->sub_pkt);
01298 }
01299 
01300 static int avi_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
01301 {
01302     AVIContext *avi = s->priv_data;
01303     AVStream *st;
01304     int i, index;
01305     int64_t pos, pos_min;
01306     AVIStream *ast;
01307 
01308     if (!avi->index_loaded) {
01309         
01310         avi_load_index(s);
01311         avi->index_loaded = 1;
01312     }
01313     assert(stream_index>= 0);
01314 
01315     st = s->streams[stream_index];
01316     ast= st->priv_data;
01317     index= av_index_search_timestamp(st, timestamp * FFMAX(ast->sample_size, 1), flags);
01318     if(index<0)
01319         return -1;
01320 
01321     
01322     pos = st->index_entries[index].pos;
01323     timestamp = st->index_entries[index].timestamp / FFMAX(ast->sample_size, 1);
01324 
01325 
01326 
01327     if (CONFIG_DV_DEMUXER && avi->dv_demux) {
01328         
01329         
01330         
01331         assert(stream_index == 0);
01332 
01333         if(avio_seek(s->pb, pos, SEEK_SET) < 0)
01334             return -1;
01335 
01336         
01337         
01338         dv_offset_reset(avi->dv_demux, timestamp);
01339 
01340         avi->stream_index= -1;
01341         return 0;
01342     }
01343 
01344     pos_min= pos;
01345     for(i = 0; i < s->nb_streams; i++) {
01346         AVStream *st2 = s->streams[i];
01347         AVIStream *ast2 = st2->priv_data;
01348 
01349         ast2->packet_size=
01350         ast2->remaining= 0;
01351 
01352         if (ast2->sub_ctx) {
01353             seek_subtitle(st, st2, timestamp);
01354             continue;
01355         }
01356 
01357         if (st2->nb_index_entries <= 0)
01358             continue;
01359 
01360 
01361         assert((int64_t)st2->time_base.num*ast2->rate == (int64_t)st2->time_base.den*ast2->scale);
01362         index = av_index_search_timestamp(
01363                 st2,
01364                 av_rescale_q(timestamp, st->time_base, st2->time_base) * FFMAX(ast2->sample_size, 1),
01365                 flags | AVSEEK_FLAG_BACKWARD | (st2->codec->codec_type != AVMEDIA_TYPE_VIDEO ? AVSEEK_FLAG_ANY : 0));
01366         if(index<0)
01367             index=0;
01368         ast2->seek_pos= st2->index_entries[index].pos;
01369         pos_min= FFMIN(pos_min,ast2->seek_pos);
01370     }
01371     for(i = 0; i < s->nb_streams; i++) {
01372         AVStream *st2 = s->streams[i];
01373         AVIStream *ast2 = st2->priv_data;
01374 
01375         if (ast2->sub_ctx || st2->nb_index_entries <= 0)
01376             continue;
01377 
01378         index = av_index_search_timestamp(
01379                 st2,
01380                 av_rescale_q(timestamp, st->time_base, st2->time_base) * FFMAX(ast2->sample_size, 1),
01381                 flags | AVSEEK_FLAG_BACKWARD | (st2->codec->codec_type != AVMEDIA_TYPE_VIDEO ? AVSEEK_FLAG_ANY : 0));
01382         if(index<0)
01383             index=0;
01384         while(index>0 && st2->index_entries[index-1].pos >= pos_min)
01385             index--;
01386         ast2->frame_offset = st2->index_entries[index].timestamp;
01387     }
01388 
01389     
01390     if (avio_seek(s->pb, pos_min, SEEK_SET) < 0)
01391         return -1;
01392     avi->stream_index= -1;
01393     return 0;
01394 }
01395 
01396 static int avi_read_close(AVFormatContext *s)
01397 {
01398     int i;
01399     AVIContext *avi = s->priv_data;
01400 
01401     for(i=0;i<s->nb_streams;i++) {
01402         AVStream *st = s->streams[i];
01403         AVIStream *ast = st->priv_data;
01404         if (ast) {
01405             if (ast->sub_ctx) {
01406                 av_freep(&ast->sub_ctx->pb);
01407                 av_close_input_file(ast->sub_ctx);
01408             }
01409             av_free(ast->sub_buffer);
01410             av_free_packet(&ast->sub_pkt);
01411         }
01412     }
01413 
01414     av_free(avi->dv_demux);
01415 
01416     return 0;
01417 }
01418 
01419 static int avi_probe(AVProbeData *p)
01420 {
01421     int i;
01422 
01423     
01424     for(i=0; avi_headers[i][0]; i++)
01425         if(!memcmp(p->buf  , avi_headers[i]  , 4) &&
01426            !memcmp(p->buf+8, avi_headers[i]+4, 4))
01427             return AVPROBE_SCORE_MAX;
01428 
01429     return 0;
01430 }
01431 
01432 AVInputFormat ff_avi_demuxer = {
01433     "avi",
01434     NULL_IF_CONFIG_SMALL("AVI format"),
01435     sizeof(AVIContext),
01436     avi_probe,
01437     avi_read_header,
01438     avi_read_packet,
01439     avi_read_close,
01440     avi_read_seek,
01441     .priv_class = &demuxer_class,
01442 };