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