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