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