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