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)
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 print_tag("tag", tag, size);
00391
00392 switch(tag) {
00393 case MKTAG('L', 'I', 'S', 'T'):
00394 list_end = avio_tell(pb) + size;
00395
00396 tag1 = avio_rl32(pb);
00397
00398 print_tag("list", tag1, 0);
00399
00400 if (tag1 == MKTAG('m', 'o', 'v', 'i')) {
00401 avi->movi_list = avio_tell(pb) - 4;
00402 if(size) avi->movi_end = avi->movi_list + size + (size & 1);
00403 else avi->movi_end = avi->fsize;
00404 av_dlog(NULL, "movi end=%"PRIx64"\n", avi->movi_end);
00405 goto end_of_header;
00406 }
00407 else if (tag1 == MKTAG('I', 'N', 'F', 'O'))
00408 ff_read_riff_info(s, size - 4);
00409 else if (tag1 == MKTAG('n', 'c', 'd', 't'))
00410 avi_read_nikon(s, list_end);
00411
00412 break;
00413 case MKTAG('I', 'D', 'I', 'T'): {
00414 unsigned char date[64] = {0};
00415 size += (size & 1);
00416 size -= avio_read(pb, date, FFMIN(size, sizeof(date)-1));
00417 avio_skip(pb, size);
00418 avi_metadata_creation_time(&s->metadata, date);
00419 break;
00420 }
00421 case MKTAG('d', 'm', 'l', 'h'):
00422 avi->is_odml = 1;
00423 avio_skip(pb, size + (size & 1));
00424 break;
00425 case MKTAG('a', 'm', 'v', 'h'):
00426 amv_file_format=1;
00427 case MKTAG('a', 'v', 'i', 'h'):
00428
00429
00430 frame_period = avio_rl32(pb);
00431 avio_rl32(pb);
00432 avio_rl32(pb);
00433 avi->non_interleaved |= avio_rl32(pb) & AVIF_MUSTUSEINDEX;
00434
00435 avio_skip(pb, 2 * 4);
00436 avio_rl32(pb);
00437 avio_rl32(pb);
00438 avih_width=avio_rl32(pb);
00439 avih_height=avio_rl32(pb);
00440
00441 avio_skip(pb, size - 10 * 4);
00442 break;
00443 case MKTAG('s', 't', 'r', 'h'):
00444
00445
00446 tag1 = avio_rl32(pb);
00447 handler = avio_rl32(pb);
00448
00449 if(tag1 == MKTAG('p', 'a', 'd', 's')){
00450 avio_skip(pb, size - 8);
00451 break;
00452 }else{
00453 stream_index++;
00454 st = avformat_new_stream(s, NULL);
00455 if (!st)
00456 goto fail;
00457
00458 st->id = stream_index;
00459 ast = av_mallocz(sizeof(AVIStream));
00460 if (!ast)
00461 goto fail;
00462 st->priv_data = ast;
00463 }
00464 if(amv_file_format)
00465 tag1 = stream_index ? MKTAG('a','u','d','s') : MKTAG('v','i','d','s');
00466
00467 print_tag("strh", tag1, -1);
00468
00469 if(tag1 == MKTAG('i', 'a', 'v', 's') || tag1 == MKTAG('i', 'v', 'a', 's')){
00470 int64_t dv_dur;
00471
00472
00473
00474
00475
00476 if (s->nb_streams != 1)
00477 goto fail;
00478
00479 if (handler != MKTAG('d', 'v', 's', 'd') &&
00480 handler != MKTAG('d', 'v', 'h', 'd') &&
00481 handler != MKTAG('d', 'v', 's', 'l'))
00482 goto fail;
00483
00484 ast = s->streams[0]->priv_data;
00485 av_freep(&s->streams[0]->codec->extradata);
00486 av_freep(&s->streams[0]->codec);
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 assert(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 = 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 = 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(tag1 == MKTAG('A', 'V', 'R', 'n') &&
00640 st->codec->extradata_size >= 31 &&
00641 !memcmp(&st->codec->extradata[28], "1:1", 3))
00642 st->codec->codec_id = CODEC_ID_RAWVIDEO;
00643
00644 if(st->codec->codec_tag==0 && st->codec->height > 0 && st->codec->extradata_size < 1U<<30){
00645 st->codec->extradata_size+= 9;
00646 st->codec->extradata= av_realloc_f(st->codec->extradata, 1, st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
00647 if(st->codec->extradata)
00648 memcpy(st->codec->extradata + st->codec->extradata_size - 9, "BottomUp", 9);
00649 }
00650 st->codec->height= FFABS(st->codec->height);
00651
00652
00653 break;
00654 case AVMEDIA_TYPE_AUDIO:
00655 ret = ff_get_wav_header(pb, st->codec, size);
00656 if (ret < 0)
00657 return ret;
00658 ast->dshow_block_align= st->codec->block_align;
00659 if(ast->sample_size && st->codec->block_align && ast->sample_size != st->codec->block_align){
00660 av_log(s, AV_LOG_WARNING, "sample size (%d) != block align (%d)\n", ast->sample_size, st->codec->block_align);
00661 ast->sample_size= st->codec->block_align;
00662 }
00663 if (size&1)
00664 avio_skip(pb, 1);
00665
00666
00667 st->need_parsing = AVSTREAM_PARSE_TIMESTAMPS;
00668
00669
00670
00671 if (st->codec->codec_id == CODEC_ID_AAC && st->codec->extradata_size)
00672 st->need_parsing = AVSTREAM_PARSE_NONE;
00673
00674
00675 if (st->codec->stream_codec_tag == AV_RL32("Axan")){
00676 st->codec->codec_id = CODEC_ID_XAN_DPCM;
00677 st->codec->codec_tag = 0;
00678 ast->dshow_block_align = 0;
00679 }
00680 if (amv_file_format){
00681 st->codec->codec_id = CODEC_ID_ADPCM_IMA_AMV;
00682 ast->dshow_block_align = 0;
00683 }
00684 break;
00685 case AVMEDIA_TYPE_SUBTITLE:
00686 st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
00687 st->request_probe= 1;
00688 break;
00689 default:
00690 st->codec->codec_type = AVMEDIA_TYPE_DATA;
00691 st->codec->codec_id= CODEC_ID_NONE;
00692 st->codec->codec_tag= 0;
00693 avio_skip(pb, size);
00694 break;
00695 }
00696 }
00697 break;
00698 case MKTAG('s', 't', 'r', 'd'):
00699 if (stream_index >= (unsigned)s->nb_streams || s->streams[stream_index]->codec->extradata_size) {
00700 avio_skip(pb, size);
00701 } else {
00702 uint64_t cur_pos = avio_tell(pb);
00703 if (cur_pos < list_end)
00704 size = FFMIN(size, list_end - cur_pos);
00705 st = s->streams[stream_index];
00706
00707 if(size<(1<<30)){
00708 st->codec->extradata_size= size;
00709 st->codec->extradata= av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
00710 if (!st->codec->extradata) {
00711 st->codec->extradata_size= 0;
00712 return AVERROR(ENOMEM);
00713 }
00714 avio_read(pb, st->codec->extradata, st->codec->extradata_size);
00715 }
00716
00717 if(st->codec->extradata_size & 1)
00718 avio_r8(pb);
00719 }
00720 break;
00721 case MKTAG('i', 'n', 'd', 'x'):
00722 i= avio_tell(pb);
00723 if(pb->seekable && !(s->flags & AVFMT_FLAG_IGNIDX) && avi->use_odml &&
00724 read_braindead_odml_indx(s, 0) < 0 && (s->error_recognition & AV_EF_EXPLODE))
00725 goto fail;
00726 avio_seek(pb, i+size, SEEK_SET);
00727 break;
00728 case MKTAG('v', 'p', 'r', 'p'):
00729 if(stream_index < (unsigned)s->nb_streams && size > 9*4){
00730 AVRational active, active_aspect;
00731
00732 st = s->streams[stream_index];
00733 avio_rl32(pb);
00734 avio_rl32(pb);
00735 avio_rl32(pb);
00736 avio_rl32(pb);
00737 avio_rl32(pb);
00738
00739 active_aspect.den= avio_rl16(pb);
00740 active_aspect.num= avio_rl16(pb);
00741 active.num = avio_rl32(pb);
00742 active.den = avio_rl32(pb);
00743 avio_rl32(pb);
00744
00745 if(active_aspect.num && active_aspect.den && active.num && active.den){
00746 st->sample_aspect_ratio= av_div_q(active_aspect, active);
00747
00748 }
00749 size -= 9*4;
00750 }
00751 avio_skip(pb, size);
00752 break;
00753 case MKTAG('s', 't', 'r', 'n'):
00754 if(s->nb_streams){
00755 avi_read_tag(s, s->streams[s->nb_streams-1], tag, size);
00756 break;
00757 }
00758 default:
00759 if(size > 1000000){
00760 av_log(s, AV_LOG_ERROR, "Something went wrong during header parsing, "
00761 "I will ignore it and try to continue anyway.\n");
00762 if (s->error_recognition & AV_EF_EXPLODE)
00763 goto fail;
00764 avi->movi_list = avio_tell(pb) - 4;
00765 avi->movi_end = avi->fsize;
00766 goto end_of_header;
00767 }
00768
00769 size += (size & 1);
00770 avio_skip(pb, size);
00771 break;
00772 }
00773 }
00774 end_of_header:
00775
00776 if (stream_index != s->nb_streams - 1) {
00777 fail:
00778 return -1;
00779 }
00780
00781 if(!avi->index_loaded && pb->seekable)
00782 avi_load_index(s);
00783 avi->index_loaded |= 1;
00784 avi->non_interleaved |= guess_ni_flag(s) | (s->flags & AVFMT_FLAG_SORT_DTS);
00785 for(i=0; i<s->nb_streams; i++){
00786 AVStream *st = s->streams[i];
00787 if(st->nb_index_entries)
00788 break;
00789 }
00790
00791
00792 if(avi->dv_demux)
00793 avi->non_interleaved=0;
00794 if(i==s->nb_streams && avi->non_interleaved) {
00795 av_log(s, AV_LOG_WARNING, "non-interleaved AVI without index, switching to interleaved\n");
00796 avi->non_interleaved=0;
00797 }
00798
00799 if(avi->non_interleaved) {
00800 av_log(s, AV_LOG_INFO, "non-interleaved AVI\n");
00801 clean_index(s);
00802 }
00803
00804 ff_metadata_conv_ctx(s, NULL, avi_metadata_conv);
00805 ff_metadata_conv_ctx(s, NULL, ff_riff_info_conv);
00806
00807 return 0;
00808 }
00809
00810 static int read_gab2_sub(AVStream *st, AVPacket *pkt) {
00811 if (!strcmp(pkt->data, "GAB2") && AV_RL16(pkt->data+5) == 2) {
00812 uint8_t desc[256];
00813 int score = AVPROBE_SCORE_MAX / 2, ret;
00814 AVIStream *ast = st->priv_data;
00815 AVInputFormat *sub_demuxer;
00816 AVRational time_base;
00817 AVIOContext *pb = avio_alloc_context( pkt->data + 7,
00818 pkt->size - 7,
00819 0, NULL, NULL, NULL, NULL);
00820 AVProbeData pd;
00821 unsigned int desc_len = avio_rl32(pb);
00822
00823 if (desc_len > pb->buf_end - pb->buf_ptr)
00824 goto error;
00825
00826 ret = avio_get_str16le(pb, desc_len, desc, sizeof(desc));
00827 avio_skip(pb, desc_len - ret);
00828 if (*desc)
00829 av_dict_set(&st->metadata, "title", desc, 0);
00830
00831 avio_rl16(pb);
00832 avio_rl32(pb);
00833
00834 pd = (AVProbeData) { .buf = pb->buf_ptr, .buf_size = pb->buf_end - pb->buf_ptr };
00835 if (!(sub_demuxer = av_probe_input_format2(&pd, 1, &score)))
00836 goto error;
00837
00838 if (!(ast->sub_ctx = avformat_alloc_context()))
00839 goto error;
00840
00841 ast->sub_ctx->pb = pb;
00842 if (!avformat_open_input(&ast->sub_ctx, "", sub_demuxer, NULL)) {
00843 ff_read_packet(ast->sub_ctx, &ast->sub_pkt);
00844 *st->codec = *ast->sub_ctx->streams[0]->codec;
00845 ast->sub_ctx->streams[0]->codec->extradata = NULL;
00846 time_base = ast->sub_ctx->streams[0]->time_base;
00847 avpriv_set_pts_info(st, 64, time_base.num, time_base.den);
00848 }
00849 ast->sub_buffer = pkt->data;
00850 memset(pkt, 0, sizeof(*pkt));
00851 return 1;
00852 error:
00853 av_freep(&pb);
00854 }
00855 return 0;
00856 }
00857
00858 static AVStream *get_subtitle_pkt(AVFormatContext *s, AVStream *next_st,
00859 AVPacket *pkt)
00860 {
00861 AVIStream *ast, *next_ast = next_st->priv_data;
00862 int64_t ts, next_ts, ts_min = INT64_MAX;
00863 AVStream *st, *sub_st = NULL;
00864 int i;
00865
00866 next_ts = av_rescale_q(next_ast->frame_offset, next_st->time_base,
00867 AV_TIME_BASE_Q);
00868
00869 for (i=0; i<s->nb_streams; i++) {
00870 st = s->streams[i];
00871 ast = st->priv_data;
00872 if (st->discard < AVDISCARD_ALL && ast && ast->sub_pkt.data) {
00873 ts = av_rescale_q(ast->sub_pkt.dts, st->time_base, AV_TIME_BASE_Q);
00874 if (ts <= next_ts && ts < ts_min) {
00875 ts_min = ts;
00876 sub_st = st;
00877 }
00878 }
00879 }
00880
00881 if (sub_st) {
00882 ast = sub_st->priv_data;
00883 *pkt = ast->sub_pkt;
00884 pkt->stream_index = sub_st->index;
00885 if (ff_read_packet(ast->sub_ctx, &ast->sub_pkt) < 0)
00886 ast->sub_pkt.data = NULL;
00887 }
00888 return sub_st;
00889 }
00890
00891 static int get_stream_idx(int *d){
00892 if( d[0] >= '0' && d[0] <= '9'
00893 && d[1] >= '0' && d[1] <= '9'){
00894 return (d[0] - '0') * 10 + (d[1] - '0');
00895 }else{
00896 return 100;
00897 }
00898 }
00899
00904 static int avi_sync(AVFormatContext *s, int exit_early)
00905 {
00906 AVIContext *avi = s->priv_data;
00907 AVIOContext *pb = s->pb;
00908 int n;
00909 unsigned int d[8];
00910 unsigned int size;
00911 int64_t i, sync;
00912
00913 start_sync:
00914 memset(d, -1, sizeof(d));
00915 for(i=sync=avio_tell(pb); !url_feof(pb); i++) {
00916 int j;
00917
00918 for(j=0; j<7; j++)
00919 d[j]= d[j+1];
00920 d[7]= avio_r8(pb);
00921
00922 size= d[4] + (d[5]<<8) + (d[6]<<16) + (d[7]<<24);
00923
00924 n= get_stream_idx(d+2);
00925
00926 if(i + (uint64_t)size > avi->fsize || d[0] > 127)
00927 continue;
00928
00929
00930 if( (d[0] == 'i' && d[1] == 'x' && n < s->nb_streams)
00931
00932 ||(d[0] == 'J' && d[1] == 'U' && d[2] == 'N' && d[3] == 'K')
00933 ||(d[0] == 'i' && d[1] == 'd' && d[2] == 'x' && d[3] == '1')){
00934 avio_skip(pb, size);
00935
00936 goto start_sync;
00937 }
00938
00939
00940 if(d[0] == 'L' && d[1] == 'I' && d[2] == 'S' && d[3] == 'T'){
00941 avio_skip(pb, 4);
00942 goto start_sync;
00943 }
00944
00945 n= get_stream_idx(d);
00946
00947 if(!((i-avi->last_pkt_pos)&1) && get_stream_idx(d+1) < s->nb_streams)
00948 continue;
00949
00950
00951 if(d[2] == 'i' && d[3] == 'x' && n < s->nb_streams){
00952 avio_skip(pb, size);
00953 goto start_sync;
00954 }
00955
00956
00957 if(n < s->nb_streams){
00958 AVStream *st;
00959 AVIStream *ast;
00960 st = s->streams[n];
00961 ast = st->priv_data;
00962
00963 if (!ast) {
00964 av_log(s, AV_LOG_WARNING, "Skiping foreign stream %d packet\n", n);
00965 continue;
00966 }
00967
00968 if(s->nb_streams>=2){
00969 AVStream *st1 = s->streams[1];
00970 AVIStream *ast1= st1->priv_data;
00971
00972 if( d[2] == 'w' && d[3] == 'b'
00973 && n==0
00974 && st ->codec->codec_type == AVMEDIA_TYPE_VIDEO
00975 && st1->codec->codec_type == AVMEDIA_TYPE_AUDIO
00976 && ast->prefix == 'd'*256+'c'
00977 && (d[2]*256+d[3] == ast1->prefix || !ast1->prefix_count)
00978 ){
00979 n=1;
00980 st = st1;
00981 ast = ast1;
00982 av_log(s, AV_LOG_WARNING, "Invalid stream + prefix combination, assuming audio.\n");
00983 }
00984 }
00985
00986
00987 if( (st->discard >= AVDISCARD_DEFAULT && size==0)
00988
00989 || st->discard >= AVDISCARD_ALL){
00990 if (!exit_early) {
00991 ast->frame_offset += get_duration(ast, size);
00992 }
00993 avio_skip(pb, size);
00994 goto start_sync;
00995 }
00996
00997 if (d[2] == 'p' && d[3] == 'c' && size<=4*256+4) {
00998 int k = avio_r8(pb);
00999 int last = (k + avio_r8(pb) - 1) & 0xFF;
01000
01001 avio_rl16(pb);
01002
01003 for (; k <= last; k++)
01004 ast->pal[k] = 0xFF<<24 | avio_rb32(pb)>>8;
01005 ast->has_pal= 1;
01006 goto start_sync;
01007 } else if( ((ast->prefix_count<5 || sync+9 > i) && d[2]<128 && d[3]<128) ||
01008 d[2]*256+d[3] == ast->prefix
01009
01010 ) {
01011
01012 if (exit_early)
01013 return 0;
01014
01015 if(d[2]*256+d[3] == ast->prefix)
01016 ast->prefix_count++;
01017 else{
01018 ast->prefix= d[2]*256+d[3];
01019 ast->prefix_count= 0;
01020 }
01021
01022 avi->stream_index= n;
01023 ast->packet_size= size + 8;
01024 ast->remaining= size;
01025
01026 if(size || !ast->sample_size){
01027 uint64_t pos= avio_tell(pb) - 8;
01028 if(!st->index_entries || !st->nb_index_entries || st->index_entries[st->nb_index_entries - 1].pos < pos){
01029 av_add_index_entry(st, pos, ast->frame_offset, size, 0, AVINDEX_KEYFRAME);
01030 }
01031 }
01032 return 0;
01033 }
01034 }
01035 }
01036
01037 if(pb->error)
01038 return pb->error;
01039 return AVERROR_EOF;
01040 }
01041
01042 static int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
01043 {
01044 AVIContext *avi = s->priv_data;
01045 AVIOContext *pb = s->pb;
01046 int err;
01047 void* dstr;
01048
01049 if (CONFIG_DV_DEMUXER && avi->dv_demux) {
01050 int size = avpriv_dv_get_packet(avi->dv_demux, pkt);
01051 if (size >= 0)
01052 return size;
01053 }
01054
01055 if(avi->non_interleaved){
01056 int best_stream_index = 0;
01057 AVStream *best_st= NULL;
01058 AVIStream *best_ast;
01059 int64_t best_ts= INT64_MAX;
01060 int i;
01061
01062 for(i=0; i<s->nb_streams; i++){
01063 AVStream *st = s->streams[i];
01064 AVIStream *ast = st->priv_data;
01065 int64_t ts= ast->frame_offset;
01066 int64_t last_ts;
01067
01068 if(!st->nb_index_entries)
01069 continue;
01070
01071 last_ts = st->index_entries[st->nb_index_entries - 1].timestamp;
01072 if(!ast->remaining && ts > last_ts)
01073 continue;
01074
01075 ts = av_rescale_q(ts, st->time_base, (AVRational){FFMAX(1, ast->sample_size), AV_TIME_BASE});
01076
01077
01078 if(ts < best_ts){
01079 best_ts= ts;
01080 best_st= st;
01081 best_stream_index= i;
01082 }
01083 }
01084 if(!best_st)
01085 return AVERROR_EOF;
01086
01087 best_ast = best_st->priv_data;
01088 best_ts = best_ast->frame_offset;
01089 if(best_ast->remaining)
01090 i= av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY | AVSEEK_FLAG_BACKWARD);
01091 else{
01092 i= av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY);
01093 if(i>=0)
01094 best_ast->frame_offset= best_st->index_entries[i].timestamp;
01095 }
01096
01097
01098 if(i>=0){
01099 int64_t pos= best_st->index_entries[i].pos;
01100 pos += best_ast->packet_size - best_ast->remaining;
01101 if(avio_seek(s->pb, pos + 8, SEEK_SET) < 0)
01102 return AVERROR_EOF;
01103
01104
01105 assert(best_ast->remaining <= best_ast->packet_size);
01106
01107 avi->stream_index= best_stream_index;
01108 if(!best_ast->remaining)
01109 best_ast->packet_size=
01110 best_ast->remaining= best_st->index_entries[i].size;
01111 }
01112 else
01113 return AVERROR_EOF;
01114 }
01115
01116 resync:
01117 if(avi->stream_index >= 0){
01118 AVStream *st= s->streams[ avi->stream_index ];
01119 AVIStream *ast= st->priv_data;
01120 int size, err;
01121
01122 if(get_subtitle_pkt(s, st, pkt))
01123 return 0;
01124
01125 if(ast->sample_size <= 1)
01126 size= INT_MAX;
01127 else if(ast->sample_size < 32)
01128
01129 size= 1024*ast->sample_size;
01130 else
01131 size= ast->sample_size;
01132
01133 if(size > ast->remaining)
01134 size= ast->remaining;
01135 avi->last_pkt_pos= avio_tell(pb);
01136 err= av_get_packet(pb, pkt, size);
01137 if(err<0)
01138 return err;
01139 size = err;
01140
01141 if(ast->has_pal && pkt->data && pkt->size<(unsigned)INT_MAX/2){
01142 uint8_t *pal;
01143 pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE);
01144 if(!pal){
01145 av_log(s, AV_LOG_ERROR, "Failed to allocate data for palette\n");
01146 }else{
01147 memcpy(pal, ast->pal, AVPALETTE_SIZE);
01148 ast->has_pal = 0;
01149 }
01150 }
01151
01152 if (CONFIG_DV_DEMUXER && avi->dv_demux) {
01153 dstr = pkt->destruct;
01154 size = avpriv_dv_produce_packet(avi->dv_demux, pkt,
01155 pkt->data, pkt->size, pkt->pos);
01156 pkt->destruct = dstr;
01157 pkt->flags |= AV_PKT_FLAG_KEY;
01158 if (size < 0)
01159 av_free_packet(pkt);
01160 } else if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE
01161 && !st->codec->codec_tag && read_gab2_sub(st, pkt)) {
01162 ast->frame_offset++;
01163 avi->stream_index = -1;
01164 ast->remaining = 0;
01165 goto resync;
01166 } else {
01167
01168 pkt->dts = ast->frame_offset;
01169
01170 if(ast->sample_size)
01171 pkt->dts /= ast->sample_size;
01172
01173 pkt->stream_index = avi->stream_index;
01174
01175 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
01176 AVIndexEntry *e;
01177 int index;
01178 assert(st->index_entries);
01179
01180 index= av_index_search_timestamp(st, ast->frame_offset, 0);
01181 e= &st->index_entries[index];
01182
01183 if(index >= 0 && e->timestamp == ast->frame_offset){
01184 if (index == st->nb_index_entries-1){
01185 int key=1;
01186 int i;
01187 uint32_t state=-1;
01188 for(i=0; i<FFMIN(size,256); i++){
01189 if(st->codec->codec_id == CODEC_ID_MPEG4){
01190 if(state == 0x1B6){
01191 key= !(pkt->data[i]&0xC0);
01192 break;
01193 }
01194 }else
01195 break;
01196 state= (state<<8) + pkt->data[i];
01197 }
01198 if(!key)
01199 e->flags &= ~AVINDEX_KEYFRAME;
01200 }
01201 if (e->flags & AVINDEX_KEYFRAME)
01202 pkt->flags |= AV_PKT_FLAG_KEY;
01203 }
01204 } else {
01205 pkt->flags |= AV_PKT_FLAG_KEY;
01206 }
01207 ast->frame_offset += get_duration(ast, pkt->size);
01208 }
01209 ast->remaining -= size;
01210 if(!ast->remaining){
01211 avi->stream_index= -1;
01212 ast->packet_size= 0;
01213 }
01214
01215 if(!avi->non_interleaved && pkt->pos >= 0 && ast->seek_pos > pkt->pos){
01216 av_free_packet(pkt);
01217 goto resync;
01218 }
01219 ast->seek_pos= 0;
01220
01221 if(!avi->non_interleaved && st->nb_index_entries>1 && avi->index_loaded>1){
01222 int64_t dts= av_rescale_q(pkt->dts, st->time_base, AV_TIME_BASE_Q);
01223
01224 if(avi->dts_max - dts > 2*AV_TIME_BASE){
01225 avi->non_interleaved= 1;
01226 av_log(s, AV_LOG_INFO, "Switching to NI mode, due to poor interleaving\n");
01227 }else if(avi->dts_max < dts)
01228 avi->dts_max = dts;
01229 }
01230
01231 return size;
01232 }
01233
01234 if ((err = avi_sync(s, 0)) < 0)
01235 return err;
01236 goto resync;
01237 }
01238
01239
01240
01241 static int avi_read_idx1(AVFormatContext *s, int size)
01242 {
01243 AVIContext *avi = s->priv_data;
01244 AVIOContext *pb = s->pb;
01245 int nb_index_entries, i;
01246 AVStream *st;
01247 AVIStream *ast;
01248 unsigned int index, tag, flags, pos, len, first_packet = 1;
01249 unsigned last_pos= -1;
01250 unsigned last_idx= -1;
01251 int64_t idx1_pos, first_packet_pos = 0, data_offset = 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 }
01304 return 0;
01305 }
01306
01307 static int guess_ni_flag(AVFormatContext *s){
01308 int i;
01309 int64_t last_start=0;
01310 int64_t first_end= INT64_MAX;
01311 int64_t oldpos= avio_tell(s->pb);
01312 int *idx;
01313 int64_t min_pos, pos;
01314
01315 for(i=0; i<s->nb_streams; i++){
01316 AVStream *st = s->streams[i];
01317 int n= st->nb_index_entries;
01318 unsigned int size;
01319
01320 if(n <= 0)
01321 continue;
01322
01323 if(n >= 2){
01324 int64_t pos= st->index_entries[0].pos;
01325 avio_seek(s->pb, pos + 4, SEEK_SET);
01326 size= avio_rl32(s->pb);
01327 if(pos + size > st->index_entries[1].pos)
01328 last_start= INT64_MAX;
01329 }
01330
01331 if(st->index_entries[0].pos > last_start)
01332 last_start= st->index_entries[0].pos;
01333 if(st->index_entries[n-1].pos < first_end)
01334 first_end= st->index_entries[n-1].pos;
01335 }
01336 avio_seek(s->pb, oldpos, SEEK_SET);
01337 if (last_start > first_end)
01338 return 1;
01339 idx= av_mallocz(sizeof(*idx) * s->nb_streams);
01340 for (min_pos=pos=0; min_pos!=INT64_MAX; pos= min_pos+1) {
01341 int64_t max_dts = INT64_MIN/2, min_dts= INT64_MAX/2;
01342 min_pos = INT64_MAX;
01343
01344 for (i=0; i<s->nb_streams; i++) {
01345 AVStream *st = s->streams[i];
01346 int n= st->nb_index_entries;
01347 while (idx[i]<n && st->index_entries[idx[i]].pos < pos)
01348 idx[i]++;
01349 if (idx[i] < n) {
01350 min_dts = FFMIN(min_dts, av_rescale_q(st->index_entries[idx[i]].timestamp, st->time_base, AV_TIME_BASE_Q));
01351 min_pos = FFMIN(min_pos, st->index_entries[idx[i]].pos);
01352 }
01353 if (idx[i])
01354 max_dts = FFMAX(max_dts, av_rescale_q(st->index_entries[idx[i]-1].timestamp, st->time_base, AV_TIME_BASE_Q));
01355 }
01356 if(max_dts - min_dts > 2*AV_TIME_BASE) {
01357 av_free(idx);
01358 return 1;
01359 }
01360 }
01361 av_free(idx);
01362 return 0;
01363 }
01364
01365 static int avi_load_index(AVFormatContext *s)
01366 {
01367 AVIContext *avi = s->priv_data;
01368 AVIOContext *pb = s->pb;
01369 uint32_t tag, size;
01370 int64_t pos= avio_tell(pb);
01371 int ret = -1;
01372
01373 if (avio_seek(pb, avi->movi_end, SEEK_SET) < 0)
01374 goto the_end;
01375 av_dlog(s, "movi_end=0x%"PRIx64"\n", avi->movi_end);
01376 for(;;) {
01377 if (url_feof(pb))
01378 break;
01379 tag = avio_rl32(pb);
01380 size = avio_rl32(pb);
01381 av_dlog(s, "tag=%c%c%c%c size=0x%x\n",
01382 tag & 0xff,
01383 (tag >> 8) & 0xff,
01384 (tag >> 16) & 0xff,
01385 (tag >> 24) & 0xff,
01386 size);
01387
01388 if (tag == MKTAG('i', 'd', 'x', '1') &&
01389 avi_read_idx1(s, size) >= 0) {
01390 avi->index_loaded=2;
01391 ret = 0;
01392 break;
01393 }
01394
01395 size += (size & 1);
01396 if (avio_skip(pb, size) < 0)
01397 break;
01398 }
01399 the_end:
01400 avio_seek(pb, pos, SEEK_SET);
01401 return ret;
01402 }
01403
01404 static void seek_subtitle(AVStream *st, AVStream *st2, int64_t timestamp)
01405 {
01406 AVIStream *ast2 = st2->priv_data;
01407 int64_t ts2 = av_rescale_q(timestamp, st->time_base, st2->time_base);
01408 av_free_packet(&ast2->sub_pkt);
01409 if (avformat_seek_file(ast2->sub_ctx, 0, INT64_MIN, ts2, ts2, 0) >= 0 ||
01410 avformat_seek_file(ast2->sub_ctx, 0, ts2, ts2, INT64_MAX, 0) >= 0)
01411 ff_read_packet(ast2->sub_ctx, &ast2->sub_pkt);
01412 }
01413
01414 static int avi_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
01415 {
01416 AVIContext *avi = s->priv_data;
01417 AVStream *st;
01418 int i, index;
01419 int64_t pos, pos_min;
01420 AVIStream *ast;
01421
01422 if (!avi->index_loaded) {
01423
01424 avi_load_index(s);
01425 avi->index_loaded |= 1;
01426 }
01427 assert(stream_index>= 0);
01428
01429 st = s->streams[stream_index];
01430 ast= st->priv_data;
01431 index= av_index_search_timestamp(st, timestamp * FFMAX(ast->sample_size, 1), flags);
01432 if (index<0) {
01433 if (st->nb_index_entries > 0)
01434 av_log(s, AV_LOG_DEBUG, "Failed to find timestamp %"PRId64 " in index %"PRId64 " .. %"PRId64 "\n",
01435 timestamp * FFMAX(ast->sample_size, 1),
01436 st->index_entries[0].timestamp,
01437 st->index_entries[st->nb_index_entries - 1].timestamp);
01438 return -1;
01439 }
01440
01441
01442 pos = st->index_entries[index].pos;
01443 timestamp = st->index_entries[index].timestamp / FFMAX(ast->sample_size, 1);
01444
01445
01446
01447 if (CONFIG_DV_DEMUXER && avi->dv_demux) {
01448
01449
01450
01451 assert(stream_index == 0);
01452
01453 if(avio_seek(s->pb, pos, SEEK_SET) < 0)
01454 return -1;
01455
01456
01457
01458 ff_dv_offset_reset(avi->dv_demux, timestamp);
01459
01460 avi->stream_index= -1;
01461 return 0;
01462 }
01463
01464 pos_min= pos;
01465 for(i = 0; i < s->nb_streams; i++) {
01466 AVStream *st2 = s->streams[i];
01467 AVIStream *ast2 = st2->priv_data;
01468
01469 ast2->packet_size=
01470 ast2->remaining= 0;
01471
01472 if (ast2->sub_ctx) {
01473 seek_subtitle(st, st2, timestamp);
01474 continue;
01475 }
01476
01477 if (st2->nb_index_entries <= 0)
01478 continue;
01479
01480
01481 assert((int64_t)st2->time_base.num*ast2->rate == (int64_t)st2->time_base.den*ast2->scale);
01482 index = av_index_search_timestamp(
01483 st2,
01484 av_rescale_q(timestamp, st->time_base, st2->time_base) * FFMAX(ast2->sample_size, 1),
01485 flags | AVSEEK_FLAG_BACKWARD | (st2->codec->codec_type != AVMEDIA_TYPE_VIDEO ? AVSEEK_FLAG_ANY : 0));
01486 if(index<0)
01487 index=0;
01488 ast2->seek_pos= st2->index_entries[index].pos;
01489 pos_min= FFMIN(pos_min,ast2->seek_pos);
01490 }
01491 for(i = 0; i < s->nb_streams; i++) {
01492 AVStream *st2 = s->streams[i];
01493 AVIStream *ast2 = st2->priv_data;
01494
01495 if (ast2->sub_ctx || st2->nb_index_entries <= 0)
01496 continue;
01497
01498 index = av_index_search_timestamp(
01499 st2,
01500 av_rescale_q(timestamp, st->time_base, st2->time_base) * FFMAX(ast2->sample_size, 1),
01501 flags | AVSEEK_FLAG_BACKWARD | (st2->codec->codec_type != AVMEDIA_TYPE_VIDEO ? AVSEEK_FLAG_ANY : 0));
01502 if(index<0)
01503 index=0;
01504 while(!avi->non_interleaved && index>0 && st2->index_entries[index-1].pos >= pos_min)
01505 index--;
01506 ast2->frame_offset = st2->index_entries[index].timestamp;
01507 }
01508
01509
01510 if (avio_seek(s->pb, pos_min, SEEK_SET) < 0) {
01511 av_log(s, AV_LOG_ERROR, "Seek failed\n");
01512 return -1;
01513 }
01514 avi->stream_index= -1;
01515 avi->dts_max= INT_MIN;
01516 return 0;
01517 }
01518
01519 static int avi_read_close(AVFormatContext *s)
01520 {
01521 int i;
01522 AVIContext *avi = s->priv_data;
01523
01524 for(i=0;i<s->nb_streams;i++) {
01525 AVStream *st = s->streams[i];
01526 AVIStream *ast = st->priv_data;
01527 if (ast) {
01528 if (ast->sub_ctx) {
01529 av_freep(&ast->sub_ctx->pb);
01530 avformat_close_input(&ast->sub_ctx);
01531 }
01532 av_free(ast->sub_buffer);
01533 av_free_packet(&ast->sub_pkt);
01534 }
01535 }
01536
01537 av_free(avi->dv_demux);
01538
01539 return 0;
01540 }
01541
01542 static int avi_probe(AVProbeData *p)
01543 {
01544 int i;
01545
01546
01547 for(i=0; avi_headers[i][0]; i++)
01548 if(!memcmp(p->buf , avi_headers[i] , 4) &&
01549 !memcmp(p->buf+8, avi_headers[i]+4, 4))
01550 return AVPROBE_SCORE_MAX;
01551
01552 return 0;
01553 }
01554
01555 AVInputFormat ff_avi_demuxer = {
01556 .name = "avi",
01557 .long_name = NULL_IF_CONFIG_SMALL("AVI format"),
01558 .priv_data_size = sizeof(AVIContext),
01559 .read_probe = avi_probe,
01560 .read_header = avi_read_header,
01561 .read_packet = avi_read_packet,
01562 .read_close = avi_read_close,
01563 .read_seek = avi_read_seek,
01564 .priv_class = &demuxer_class,
01565 };