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