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 {
00754 if (pkt->size >= 7 &&
00755 !strcmp(pkt->data, "GAB2") && AV_RL16(pkt->data + 5) == 2) {
00756 uint8_t desc[256];
00757 int score = AVPROBE_SCORE_MAX / 2, ret;
00758 AVIStream *ast = st->priv_data;
00759 AVInputFormat *sub_demuxer;
00760 AVRational time_base;
00761 AVIOContext *pb = avio_alloc_context( pkt->data + 7,
00762 pkt->size - 7,
00763 0, NULL, NULL, NULL, NULL);
00764 AVProbeData pd;
00765 unsigned int desc_len = avio_rl32(pb);
00766
00767 if (desc_len > pb->buf_end - pb->buf_ptr)
00768 goto error;
00769
00770 ret = avio_get_str16le(pb, desc_len, desc, sizeof(desc));
00771 avio_skip(pb, desc_len - ret);
00772 if (*desc)
00773 av_dict_set(&st->metadata, "title", desc, 0);
00774
00775 avio_rl16(pb);
00776 avio_rl32(pb);
00777
00778 pd = (AVProbeData) { .buf = pb->buf_ptr, .buf_size = pb->buf_end - pb->buf_ptr };
00779 if (!(sub_demuxer = av_probe_input_format2(&pd, 1, &score)))
00780 goto error;
00781
00782 if (!(ast->sub_ctx = avformat_alloc_context()))
00783 goto error;
00784
00785 ast->sub_ctx->pb = pb;
00786 if (!avformat_open_input(&ast->sub_ctx, "", sub_demuxer, NULL)) {
00787 av_read_packet(ast->sub_ctx, &ast->sub_pkt);
00788 *st->codec = *ast->sub_ctx->streams[0]->codec;
00789 ast->sub_ctx->streams[0]->codec->extradata = NULL;
00790 time_base = ast->sub_ctx->streams[0]->time_base;
00791 avpriv_set_pts_info(st, 64, time_base.num, time_base.den);
00792 }
00793 ast->sub_buffer = pkt->data;
00794 memset(pkt, 0, sizeof(*pkt));
00795 return 1;
00796 error:
00797 av_freep(&pb);
00798 }
00799 return 0;
00800 }
00801
00802 static AVStream *get_subtitle_pkt(AVFormatContext *s, AVStream *next_st,
00803 AVPacket *pkt)
00804 {
00805 AVIStream *ast, *next_ast = next_st->priv_data;
00806 int64_t ts, next_ts, ts_min = INT64_MAX;
00807 AVStream *st, *sub_st = NULL;
00808 int i;
00809
00810 next_ts = av_rescale_q(next_ast->frame_offset, next_st->time_base,
00811 AV_TIME_BASE_Q);
00812
00813 for (i=0; i<s->nb_streams; i++) {
00814 st = s->streams[i];
00815 ast = st->priv_data;
00816 if (st->discard < AVDISCARD_ALL && ast && ast->sub_pkt.data) {
00817 ts = av_rescale_q(ast->sub_pkt.dts, st->time_base, AV_TIME_BASE_Q);
00818 if (ts <= next_ts && ts < ts_min) {
00819 ts_min = ts;
00820 sub_st = st;
00821 }
00822 }
00823 }
00824
00825 if (sub_st) {
00826 ast = sub_st->priv_data;
00827 *pkt = ast->sub_pkt;
00828 pkt->stream_index = sub_st->index;
00829 if (av_read_packet(ast->sub_ctx, &ast->sub_pkt) < 0)
00830 ast->sub_pkt.data = NULL;
00831 }
00832 return sub_st;
00833 }
00834
00835 static int get_stream_idx(int *d){
00836 if( d[0] >= '0' && d[0] <= '9'
00837 && d[1] >= '0' && d[1] <= '9'){
00838 return (d[0] - '0') * 10 + (d[1] - '0');
00839 }else{
00840 return 100;
00841 }
00842 }
00843
00844 static int avi_sync(AVFormatContext *s, int exit_early)
00845 {
00846 AVIContext *avi = s->priv_data;
00847 AVIOContext *pb = s->pb;
00848 int n;
00849 unsigned int d[8];
00850 unsigned int size;
00851 int64_t i, sync;
00852
00853 start_sync:
00854 memset(d, -1, sizeof(d));
00855 for(i=sync=avio_tell(pb); !pb->eof_reached; i++) {
00856 int j;
00857
00858 for(j=0; j<7; j++)
00859 d[j]= d[j+1];
00860 d[7]= avio_r8(pb);
00861
00862 size= d[4] + (d[5]<<8) + (d[6]<<16) + (d[7]<<24);
00863
00864 n= get_stream_idx(d+2);
00865
00866 if(i + (uint64_t)size > avi->fsize || d[0] > 127)
00867 continue;
00868
00869
00870 if( (d[0] == 'i' && d[1] == 'x' && n < s->nb_streams)
00871
00872 ||(d[0] == 'J' && d[1] == 'U' && d[2] == 'N' && d[3] == 'K')
00873 ||(d[0] == 'i' && d[1] == 'd' && d[2] == 'x' && d[3] == '1')){
00874 avio_skip(pb, size);
00875
00876 goto start_sync;
00877 }
00878
00879
00880 if(d[0] == 'L' && d[1] == 'I' && d[2] == 'S' && d[3] == 'T'){
00881 avio_skip(pb, 4);
00882 goto start_sync;
00883 }
00884
00885 n = avi->dv_demux ? 0 : get_stream_idx(d);
00886
00887 if(!((i-avi->last_pkt_pos)&1) && get_stream_idx(d+1) < s->nb_streams)
00888 continue;
00889
00890
00891 if(d[2] == 'i' && d[3] == 'x' && n < s->nb_streams){
00892 avio_skip(pb, size);
00893 goto start_sync;
00894 }
00895
00896
00897 if(n < s->nb_streams){
00898 AVStream *st;
00899 AVIStream *ast;
00900 st = s->streams[n];
00901 ast = st->priv_data;
00902
00903 if(s->nb_streams>=2){
00904 AVStream *st1 = s->streams[1];
00905 AVIStream *ast1= st1->priv_data;
00906
00907 if( d[2] == 'w' && d[3] == 'b'
00908 && n==0
00909 && st ->codec->codec_type == AVMEDIA_TYPE_VIDEO
00910 && st1->codec->codec_type == AVMEDIA_TYPE_AUDIO
00911 && ast->prefix == 'd'*256+'c'
00912 && (d[2]*256+d[3] == ast1->prefix || !ast1->prefix_count)
00913 ){
00914 n=1;
00915 st = st1;
00916 ast = ast1;
00917 av_log(s, AV_LOG_WARNING, "Invalid stream + prefix combination, assuming audio.\n");
00918 }
00919 }
00920
00921
00922 if (!avi->dv_demux &&
00923 ((st->discard >= AVDISCARD_DEFAULT && size==0)
00924
00925
00926
00927 || st->discard >= AVDISCARD_ALL)) {
00928 if (!exit_early) {
00929 ast->frame_offset += get_duration(ast, size);
00930 }
00931 avio_skip(pb, size);
00932 goto start_sync;
00933 }
00934
00935 if (d[2] == 'p' && d[3] == 'c' && size<=4*256+4) {
00936 int k = avio_r8(pb);
00937 int last = (k + avio_r8(pb) - 1) & 0xFF;
00938
00939 avio_rl16(pb);
00940
00941 for (; k <= last; k++)
00942 ast->pal[k] = avio_rb32(pb)>>8;
00943 ast->has_pal= 1;
00944 goto start_sync;
00945 } else if( ((ast->prefix_count<5 || sync+9 > i) && d[2]<128 && d[3]<128) ||
00946 d[2]*256+d[3] == ast->prefix
00947
00948 ) {
00949
00950 if (exit_early)
00951 return 0;
00952
00953 if(d[2]*256+d[3] == ast->prefix)
00954 ast->prefix_count++;
00955 else{
00956 ast->prefix= d[2]*256+d[3];
00957 ast->prefix_count= 0;
00958 }
00959
00960 avi->stream_index= n;
00961 ast->packet_size= size + 8;
00962 ast->remaining= size;
00963
00964 if(size || !ast->sample_size){
00965 uint64_t pos= avio_tell(pb) - 8;
00966 if(!st->index_entries || !st->nb_index_entries || st->index_entries[st->nb_index_entries - 1].pos < pos){
00967 av_add_index_entry(st, pos, ast->frame_offset, size, 0, AVINDEX_KEYFRAME);
00968 }
00969 }
00970 return 0;
00971 }
00972 }
00973 }
00974
00975 return AVERROR_EOF;
00976 }
00977
00978 static int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
00979 {
00980 AVIContext *avi = s->priv_data;
00981 AVIOContext *pb = s->pb;
00982 int err;
00983 void* dstr;
00984
00985 if (CONFIG_DV_DEMUXER && avi->dv_demux) {
00986 int size = avpriv_dv_get_packet(avi->dv_demux, pkt);
00987 if (size >= 0)
00988 return size;
00989 else
00990 goto resync;
00991 }
00992
00993 if(avi->non_interleaved){
00994 int best_stream_index = 0;
00995 AVStream *best_st= NULL;
00996 AVIStream *best_ast;
00997 int64_t best_ts= INT64_MAX;
00998 int i;
00999
01000 for(i=0; i<s->nb_streams; i++){
01001 AVStream *st = s->streams[i];
01002 AVIStream *ast = st->priv_data;
01003 int64_t ts= ast->frame_offset;
01004 int64_t last_ts;
01005
01006 if(!st->nb_index_entries)
01007 continue;
01008
01009 last_ts = st->index_entries[st->nb_index_entries - 1].timestamp;
01010 if(!ast->remaining && ts > last_ts)
01011 continue;
01012
01013 ts = av_rescale_q(ts, st->time_base, (AVRational){FFMAX(1, ast->sample_size), AV_TIME_BASE});
01014
01015
01016 if(ts < best_ts){
01017 best_ts= ts;
01018 best_st= st;
01019 best_stream_index= i;
01020 }
01021 }
01022 if(!best_st)
01023 return -1;
01024
01025 best_ast = best_st->priv_data;
01026 best_ts = av_rescale_q(best_ts, (AVRational){FFMAX(1, best_ast->sample_size), AV_TIME_BASE}, best_st->time_base);
01027 if(best_ast->remaining)
01028 i= av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY | AVSEEK_FLAG_BACKWARD);
01029 else{
01030 i= av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY);
01031 if(i>=0)
01032 best_ast->frame_offset= best_st->index_entries[i].timestamp;
01033 }
01034
01035
01036 if(i>=0){
01037 int64_t pos= best_st->index_entries[i].pos;
01038 pos += best_ast->packet_size - best_ast->remaining;
01039 avio_seek(s->pb, pos + 8, SEEK_SET);
01040
01041
01042 assert(best_ast->remaining <= best_ast->packet_size);
01043
01044 avi->stream_index= best_stream_index;
01045 if(!best_ast->remaining)
01046 best_ast->packet_size=
01047 best_ast->remaining= best_st->index_entries[i].size;
01048 }
01049 }
01050
01051 resync:
01052 if(avi->stream_index >= 0){
01053 AVStream *st= s->streams[ avi->stream_index ];
01054 AVIStream *ast= st->priv_data;
01055 int size, err;
01056
01057 if(get_subtitle_pkt(s, st, pkt))
01058 return 0;
01059
01060 if(ast->sample_size <= 1)
01061 size= INT_MAX;
01062 else if(ast->sample_size < 32)
01063
01064 size= 1024*ast->sample_size;
01065 else
01066 size= ast->sample_size;
01067
01068 if(size > ast->remaining)
01069 size= ast->remaining;
01070 avi->last_pkt_pos= avio_tell(pb);
01071 err= av_get_packet(pb, pkt, size);
01072 if(err<0)
01073 return err;
01074
01075 if(ast->has_pal && pkt->data && pkt->size<(unsigned)INT_MAX/2){
01076 uint8_t *pal;
01077 pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE);
01078 if(!pal){
01079 av_log(s, AV_LOG_ERROR, "Failed to allocate data for palette\n");
01080 }else{
01081 memcpy(pal, ast->pal, AVPALETTE_SIZE);
01082 ast->has_pal = 0;
01083 }
01084 }
01085
01086 if (CONFIG_DV_DEMUXER && avi->dv_demux) {
01087 dstr = pkt->destruct;
01088 size = avpriv_dv_produce_packet(avi->dv_demux, pkt,
01089 pkt->data, pkt->size);
01090 pkt->destruct = dstr;
01091 pkt->flags |= AV_PKT_FLAG_KEY;
01092 if (size < 0)
01093 av_free_packet(pkt);
01094 } else if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE
01095 && !st->codec->codec_tag && read_gab2_sub(st, pkt)) {
01096 ast->frame_offset++;
01097 avi->stream_index = -1;
01098 ast->remaining = 0;
01099 goto resync;
01100 } else {
01101
01102 pkt->dts = ast->frame_offset;
01103
01104 if(ast->sample_size)
01105 pkt->dts /= ast->sample_size;
01106
01107 pkt->stream_index = avi->stream_index;
01108
01109 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
01110 AVIndexEntry *e;
01111 int index;
01112 assert(st->index_entries);
01113
01114 index= av_index_search_timestamp(st, ast->frame_offset, 0);
01115 e= &st->index_entries[index];
01116
01117 if(index >= 0 && e->timestamp == ast->frame_offset){
01118 if (e->flags & AVINDEX_KEYFRAME)
01119 pkt->flags |= AV_PKT_FLAG_KEY;
01120 }
01121 } else {
01122 pkt->flags |= AV_PKT_FLAG_KEY;
01123 }
01124 ast->frame_offset += get_duration(ast, pkt->size);
01125 }
01126 ast->remaining -= err;
01127 if(!ast->remaining){
01128 avi->stream_index= -1;
01129 ast->packet_size= 0;
01130 }
01131
01132 return 0;
01133 }
01134
01135 if ((err = avi_sync(s, 0)) < 0)
01136 return err;
01137 goto resync;
01138 }
01139
01140
01141
01142 static int avi_read_idx1(AVFormatContext *s, int size)
01143 {
01144 AVIContext *avi = s->priv_data;
01145 AVIOContext *pb = s->pb;
01146 int nb_index_entries, i;
01147 AVStream *st;
01148 AVIStream *ast;
01149 unsigned int index, tag, flags, pos, len, first_packet = 1;
01150 unsigned last_pos= -1;
01151 int64_t idx1_pos, first_packet_pos = 0, data_offset = 0;
01152
01153 nb_index_entries = size / 16;
01154 if (nb_index_entries <= 0)
01155 return -1;
01156
01157 idx1_pos = avio_tell(pb);
01158 avio_seek(pb, avi->movi_list+4, SEEK_SET);
01159 if (avi_sync(s, 1) == 0) {
01160 first_packet_pos = avio_tell(pb) - 8;
01161 }
01162 avi->stream_index = -1;
01163 avio_seek(pb, idx1_pos, SEEK_SET);
01164
01165
01166 for(i = 0; i < nb_index_entries; i++) {
01167 tag = avio_rl32(pb);
01168 flags = avio_rl32(pb);
01169 pos = avio_rl32(pb);
01170 len = avio_rl32(pb);
01171 av_dlog(s, "%d: tag=0x%x flags=0x%x pos=0x%x len=%d/",
01172 i, tag, flags, pos, len);
01173
01174 index = ((tag & 0xff) - '0') * 10;
01175 index += ((tag >> 8) & 0xff) - '0';
01176 if (index >= s->nb_streams)
01177 continue;
01178 st = s->streams[index];
01179 ast = st->priv_data;
01180
01181 if(first_packet && first_packet_pos && len) {
01182 data_offset = first_packet_pos - pos;
01183 first_packet = 0;
01184 }
01185 pos += data_offset;
01186
01187 av_dlog(s, "%d cum_len=%"PRId64"\n", len, ast->cum_len);
01188
01189 if(pb->eof_reached)
01190 return -1;
01191
01192 if(last_pos == pos)
01193 avi->non_interleaved= 1;
01194 else if(len || !ast->sample_size)
01195 av_add_index_entry(st, pos, ast->cum_len, len, 0, (flags&AVIIF_INDEX) ? AVINDEX_KEYFRAME : 0);
01196 ast->cum_len += get_duration(ast, len);
01197 last_pos= pos;
01198 }
01199 return 0;
01200 }
01201
01202 static int guess_ni_flag(AVFormatContext *s){
01203 int i;
01204 int64_t last_start=0;
01205 int64_t first_end= INT64_MAX;
01206 int64_t oldpos= avio_tell(s->pb);
01207
01208 for(i=0; i<s->nb_streams; i++){
01209 AVStream *st = s->streams[i];
01210 int n= st->nb_index_entries;
01211 unsigned int size;
01212
01213 if(n <= 0)
01214 continue;
01215
01216 if(n >= 2){
01217 int64_t pos= st->index_entries[0].pos;
01218 avio_seek(s->pb, pos + 4, SEEK_SET);
01219 size= avio_rl32(s->pb);
01220 if(pos + size > st->index_entries[1].pos)
01221 last_start= INT64_MAX;
01222 }
01223
01224 if(st->index_entries[0].pos > last_start)
01225 last_start= st->index_entries[0].pos;
01226 if(st->index_entries[n-1].pos < first_end)
01227 first_end= st->index_entries[n-1].pos;
01228 }
01229 avio_seek(s->pb, oldpos, SEEK_SET);
01230 return last_start > first_end;
01231 }
01232
01233 static int avi_load_index(AVFormatContext *s)
01234 {
01235 AVIContext *avi = s->priv_data;
01236 AVIOContext *pb = s->pb;
01237 uint32_t tag, size;
01238 int64_t pos= avio_tell(pb);
01239 int ret = -1;
01240
01241 if (avio_seek(pb, avi->movi_end, SEEK_SET) < 0)
01242 goto the_end;
01243 av_dlog(s, "movi_end=0x%"PRIx64"\n", avi->movi_end);
01244 for(;;) {
01245 if (pb->eof_reached)
01246 break;
01247 tag = avio_rl32(pb);
01248 size = avio_rl32(pb);
01249 av_dlog(s, "tag=%c%c%c%c size=0x%x\n",
01250 tag & 0xff,
01251 (tag >> 8) & 0xff,
01252 (tag >> 16) & 0xff,
01253 (tag >> 24) & 0xff,
01254 size);
01255
01256 if (tag == MKTAG('i', 'd', 'x', '1') &&
01257 avi_read_idx1(s, size) >= 0) {
01258 ret = 0;
01259 break;
01260 }
01261
01262 size += (size & 1);
01263 if (avio_skip(pb, size) < 0)
01264 break;
01265 }
01266 the_end:
01267 avio_seek(pb, pos, SEEK_SET);
01268 return ret;
01269 }
01270
01271 static void seek_subtitle(AVStream *st, AVStream *st2, int64_t timestamp)
01272 {
01273 AVIStream *ast2 = st2->priv_data;
01274 int64_t ts2 = av_rescale_q(timestamp, st->time_base, st2->time_base);
01275 av_free_packet(&ast2->sub_pkt);
01276 if (avformat_seek_file(ast2->sub_ctx, 0, INT64_MIN, ts2, ts2, 0) >= 0 ||
01277 avformat_seek_file(ast2->sub_ctx, 0, ts2, ts2, INT64_MAX, 0) >= 0)
01278 av_read_packet(ast2->sub_ctx, &ast2->sub_pkt);
01279 }
01280
01281 static int avi_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
01282 {
01283 AVIContext *avi = s->priv_data;
01284 AVStream *st;
01285 int i, index;
01286 int64_t pos;
01287 AVIStream *ast;
01288
01289
01290
01291
01292 if (avi->dv_demux)
01293 stream_index = 0;
01294
01295 if (!avi->index_loaded) {
01296
01297 avi_load_index(s);
01298 avi->index_loaded = 1;
01299 }
01300
01301 st = s->streams[stream_index];
01302 ast= st->priv_data;
01303 index= av_index_search_timestamp(st, timestamp * FFMAX(ast->sample_size, 1), flags);
01304 if(index<0)
01305 return -1;
01306
01307
01308 pos = st->index_entries[index].pos;
01309 timestamp = st->index_entries[index].timestamp / FFMAX(ast->sample_size, 1);
01310
01311
01312
01313 if (CONFIG_DV_DEMUXER && avi->dv_demux) {
01314
01315
01316
01317
01318
01319
01320 dv_offset_reset(avi->dv_demux, timestamp);
01321
01322 avio_seek(s->pb, pos, SEEK_SET);
01323 avi->stream_index= -1;
01324 return 0;
01325 }
01326
01327 for(i = 0; i < s->nb_streams; i++) {
01328 AVStream *st2 = s->streams[i];
01329 AVIStream *ast2 = st2->priv_data;
01330
01331 ast2->packet_size=
01332 ast2->remaining= 0;
01333
01334 if (ast2->sub_ctx) {
01335 seek_subtitle(st, st2, timestamp);
01336 continue;
01337 }
01338
01339 if (st2->nb_index_entries <= 0)
01340 continue;
01341
01342
01343 assert((int64_t)st2->time_base.num*ast2->rate == (int64_t)st2->time_base.den*ast2->scale);
01344 index = av_index_search_timestamp(
01345 st2,
01346 av_rescale_q(timestamp, st->time_base, st2->time_base) * FFMAX(ast2->sample_size, 1),
01347 flags | AVSEEK_FLAG_BACKWARD);
01348 if(index<0)
01349 index=0;
01350
01351 if(!avi->non_interleaved){
01352 while(index>0 && st2->index_entries[index].pos > pos)
01353 index--;
01354 while(index+1 < st2->nb_index_entries && st2->index_entries[index].pos < pos)
01355 index++;
01356 }
01357
01358
01359
01360 ast2->frame_offset = st2->index_entries[index].timestamp;
01361 }
01362
01363
01364 avio_seek(s->pb, pos, SEEK_SET);
01365 avi->stream_index= -1;
01366 return 0;
01367 }
01368
01369 static int avi_read_close(AVFormatContext *s)
01370 {
01371 int i;
01372 AVIContext *avi = s->priv_data;
01373
01374 for(i=0;i<s->nb_streams;i++) {
01375 AVStream *st = s->streams[i];
01376 AVIStream *ast = st->priv_data;
01377 if (ast) {
01378 if (ast->sub_ctx) {
01379 av_freep(&ast->sub_ctx->pb);
01380 avformat_close_input(&ast->sub_ctx);
01381 }
01382 av_free(ast->sub_buffer);
01383 av_free_packet(&ast->sub_pkt);
01384 }
01385 }
01386
01387 av_free(avi->dv_demux);
01388
01389 return 0;
01390 }
01391
01392 static int avi_probe(AVProbeData *p)
01393 {
01394 int i;
01395
01396
01397 for(i=0; avi_headers[i][0]; i++)
01398 if(!memcmp(p->buf , avi_headers[i] , 4) &&
01399 !memcmp(p->buf+8, avi_headers[i]+4, 4))
01400 return AVPROBE_SCORE_MAX;
01401
01402 return 0;
01403 }
01404
01405 AVInputFormat ff_avi_demuxer = {
01406 .name = "avi",
01407 .long_name = NULL_IF_CONFIG_SMALL("AVI format"),
01408 .priv_data_size = sizeof(AVIContext),
01409 .read_probe = avi_probe,
01410 .read_header = avi_read_header,
01411 .read_packet = avi_read_packet,
01412 .read_close = avi_read_close,
01413 .read_seek = avi_read_seek,
01414 };