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 av_dlog(s, "%"PRIu32" %"PRIu32" %d\n",
00549 ast->rate, ast->scale, ast->sample_size);
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 unsigned esize;
00583 if (cur_pos < list_end)
00584 size = FFMIN(size, list_end - cur_pos);
00585 st = s->streams[stream_index];
00586 switch(codec_type) {
00587 case AVMEDIA_TYPE_VIDEO:
00588 if(amv_file_format){
00589 st->codec->width=avih_width;
00590 st->codec->height=avih_height;
00591 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00592 st->codec->codec_id = AV_CODEC_ID_AMV;
00593 avio_skip(pb, size);
00594 break;
00595 }
00596 tag1 = ff_get_bmp_header(pb, st, &esize);
00597
00598 if (tag1 == MKTAG('D', 'X', 'S', 'B') || tag1 == MKTAG('D','X','S','A')) {
00599 st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
00600 st->codec->codec_tag = tag1;
00601 st->codec->codec_id = AV_CODEC_ID_XSUB;
00602 break;
00603 }
00604
00605 if(size > 10*4 && size<(1<<30) && size < avi->fsize){
00606 if(esize == size-1 && (esize&1)) st->codec->extradata_size= esize - 10*4;
00607 else st->codec->extradata_size= size - 10*4;
00608 st->codec->extradata= av_malloc(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
00609 if (!st->codec->extradata) {
00610 st->codec->extradata_size= 0;
00611 return AVERROR(ENOMEM);
00612 }
00613 avio_read(pb, st->codec->extradata, st->codec->extradata_size);
00614 }
00615
00616 if(st->codec->extradata_size & 1)
00617 avio_r8(pb);
00618
00619
00620
00621
00622 if (st->codec->extradata_size && (st->codec->bits_per_coded_sample <= 8)) {
00623 int pal_size = (1 << st->codec->bits_per_coded_sample) << 2;
00624 const uint8_t *pal_src;
00625
00626 pal_size = FFMIN(pal_size, st->codec->extradata_size);
00627 pal_src = st->codec->extradata + st->codec->extradata_size - pal_size;
00628 for (i = 0; i < pal_size/4; i++)
00629 ast->pal[i] = 0xFFU<<24 | AV_RL32(pal_src+4*i);
00630 ast->has_pal = 1;
00631 }
00632
00633 print_tag("video", tag1, 0);
00634
00635 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00636 st->codec->codec_tag = tag1;
00637 st->codec->codec_id = ff_codec_get_id(ff_codec_bmp_tags, tag1);
00638 st->need_parsing = AVSTREAM_PARSE_HEADERS;
00639
00640 if(st->codec->codec_tag==0 && st->codec->height > 0 && st->codec->extradata_size < 1U<<30){
00641 st->codec->extradata_size+= 9;
00642 st->codec->extradata= av_realloc_f(st->codec->extradata, 1, st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
00643 if(st->codec->extradata)
00644 memcpy(st->codec->extradata + st->codec->extradata_size - 9, "BottomUp", 9);
00645 }
00646 st->codec->height= FFABS(st->codec->height);
00647
00648
00649 break;
00650 case AVMEDIA_TYPE_AUDIO:
00651 ret = ff_get_wav_header(pb, st->codec, size);
00652 if (ret < 0)
00653 return ret;
00654 ast->dshow_block_align= st->codec->block_align;
00655 if(ast->sample_size && st->codec->block_align && ast->sample_size != st->codec->block_align){
00656 av_log(s, AV_LOG_WARNING, "sample size (%d) != block align (%d)\n", ast->sample_size, st->codec->block_align);
00657 ast->sample_size= st->codec->block_align;
00658 }
00659 if (size&1)
00660 avio_skip(pb, 1);
00661
00662
00663 st->need_parsing = AVSTREAM_PARSE_TIMESTAMPS;
00664
00665
00666
00667 if (st->codec->codec_id == AV_CODEC_ID_AAC && st->codec->extradata_size)
00668 st->need_parsing = AVSTREAM_PARSE_NONE;
00669
00670
00671 if (st->codec->stream_codec_tag == AV_RL32("Axan")){
00672 st->codec->codec_id = AV_CODEC_ID_XAN_DPCM;
00673 st->codec->codec_tag = 0;
00674 ast->dshow_block_align = 0;
00675 }
00676 if (amv_file_format){
00677 st->codec->codec_id = AV_CODEC_ID_ADPCM_IMA_AMV;
00678 ast->dshow_block_align = 0;
00679 }
00680 if(st->codec->codec_id == AV_CODEC_ID_AAC && ast->dshow_block_align <= 4 && ast->dshow_block_align) {
00681 av_log(s, AV_LOG_DEBUG, "overriding invalid dshow_block_align of %d\n", ast->dshow_block_align);
00682 ast->dshow_block_align = 0;
00683 }
00684 if(st->codec->codec_id == AV_CODEC_ID_AAC && ast->dshow_block_align == 1024 && ast->sample_size == 1024 ||
00685 st->codec->codec_id == AV_CODEC_ID_AAC && ast->dshow_block_align == 4096 && ast->sample_size == 4096 ||
00686 st->codec->codec_id == AV_CODEC_ID_MP3 && ast->dshow_block_align == 1152 && ast->sample_size == 1152) {
00687 av_log(s, AV_LOG_DEBUG, "overriding sample_size\n");
00688 ast->sample_size = 0;
00689 }
00690 break;
00691 case AVMEDIA_TYPE_SUBTITLE:
00692 st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
00693 st->request_probe= 1;
00694 avio_skip(pb, size);
00695 break;
00696 default:
00697 st->codec->codec_type = AVMEDIA_TYPE_DATA;
00698 st->codec->codec_id= AV_CODEC_ID_NONE;
00699 st->codec->codec_tag= 0;
00700 avio_skip(pb, size);
00701 break;
00702 }
00703 }
00704 break;
00705 case MKTAG('s', 't', 'r', 'd'):
00706 if (stream_index >= (unsigned)s->nb_streams || s->streams[stream_index]->codec->extradata_size) {
00707 avio_skip(pb, size);
00708 } else {
00709 uint64_t cur_pos = avio_tell(pb);
00710 if (cur_pos < list_end)
00711 size = FFMIN(size, list_end - cur_pos);
00712 st = s->streams[stream_index];
00713
00714 if(size<(1<<30)){
00715 st->codec->extradata_size= size;
00716 st->codec->extradata= av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
00717 if (!st->codec->extradata) {
00718 st->codec->extradata_size= 0;
00719 return AVERROR(ENOMEM);
00720 }
00721 avio_read(pb, st->codec->extradata, st->codec->extradata_size);
00722 }
00723
00724 if(st->codec->extradata_size & 1)
00725 avio_r8(pb);
00726 }
00727 break;
00728 case MKTAG('i', 'n', 'd', 'x'):
00729 i= avio_tell(pb);
00730 if(pb->seekable && !(s->flags & AVFMT_FLAG_IGNIDX) && avi->use_odml &&
00731 read_braindead_odml_indx(s, 0) < 0 && (s->error_recognition & AV_EF_EXPLODE))
00732 goto fail;
00733 avio_seek(pb, i+size, SEEK_SET);
00734 break;
00735 case MKTAG('v', 'p', 'r', 'p'):
00736 if(stream_index < (unsigned)s->nb_streams && size > 9*4){
00737 AVRational active, active_aspect;
00738
00739 st = s->streams[stream_index];
00740 avio_rl32(pb);
00741 avio_rl32(pb);
00742 avio_rl32(pb);
00743 avio_rl32(pb);
00744 avio_rl32(pb);
00745
00746 active_aspect.den= avio_rl16(pb);
00747 active_aspect.num= avio_rl16(pb);
00748 active.num = avio_rl32(pb);
00749 active.den = avio_rl32(pb);
00750 avio_rl32(pb);
00751
00752 if(active_aspect.num && active_aspect.den && active.num && active.den){
00753 st->sample_aspect_ratio= av_div_q(active_aspect, active);
00754 av_dlog(s, "vprp %d/%d %d/%d\n",
00755 active_aspect.num, active_aspect.den,
00756 active.num, active.den);
00757 }
00758 size -= 9*4;
00759 }
00760 avio_skip(pb, size);
00761 break;
00762 case MKTAG('s', 't', 'r', 'n'):
00763 if(s->nb_streams){
00764 avi_read_tag(s, s->streams[s->nb_streams-1], tag, size);
00765 break;
00766 }
00767 default:
00768 if(size > 1000000){
00769 av_log(s, AV_LOG_ERROR, "Something went wrong during header parsing, "
00770 "I will ignore it and try to continue anyway.\n");
00771 if (s->error_recognition & AV_EF_EXPLODE)
00772 goto fail;
00773 avi->movi_list = avio_tell(pb) - 4;
00774 avi->movi_end = avi->fsize;
00775 goto end_of_header;
00776 }
00777
00778 size += (size & 1);
00779 avio_skip(pb, size);
00780 break;
00781 }
00782 }
00783 end_of_header:
00784
00785 if (stream_index != s->nb_streams - 1) {
00786 fail:
00787 return -1;
00788 }
00789
00790 if(!avi->index_loaded && pb->seekable)
00791 avi_load_index(s);
00792 avi->index_loaded |= 1;
00793 avi->non_interleaved |= guess_ni_flag(s) | (s->flags & AVFMT_FLAG_SORT_DTS);
00794 for(i=0; i<s->nb_streams; i++){
00795 AVStream *st = s->streams[i];
00796 if(st->nb_index_entries)
00797 break;
00798 }
00799
00800
00801 if(avi->dv_demux)
00802 avi->non_interleaved=0;
00803 if(i==s->nb_streams && avi->non_interleaved) {
00804 av_log(s, AV_LOG_WARNING, "non-interleaved AVI without index, switching to interleaved\n");
00805 avi->non_interleaved=0;
00806 }
00807
00808 if(avi->non_interleaved) {
00809 av_log(s, AV_LOG_INFO, "non-interleaved AVI\n");
00810 clean_index(s);
00811 }
00812
00813 ff_metadata_conv_ctx(s, NULL, avi_metadata_conv);
00814 ff_metadata_conv_ctx(s, NULL, ff_riff_info_conv);
00815
00816 return 0;
00817 }
00818
00819 static int read_gab2_sub(AVStream *st, AVPacket *pkt) {
00820 if (pkt->data && !strcmp(pkt->data, "GAB2") && AV_RL16(pkt->data+5) == 2) {
00821 uint8_t desc[256];
00822 int score = AVPROBE_SCORE_MAX / 2, ret;
00823 AVIStream *ast = st->priv_data;
00824 AVInputFormat *sub_demuxer;
00825 AVRational time_base;
00826 AVIOContext *pb = avio_alloc_context( pkt->data + 7,
00827 pkt->size - 7,
00828 0, NULL, NULL, NULL, NULL);
00829 AVProbeData pd;
00830 unsigned int desc_len = avio_rl32(pb);
00831
00832 if (desc_len > pb->buf_end - pb->buf_ptr)
00833 goto error;
00834
00835 ret = avio_get_str16le(pb, desc_len, desc, sizeof(desc));
00836 avio_skip(pb, desc_len - ret);
00837 if (*desc)
00838 av_dict_set(&st->metadata, "title", desc, 0);
00839
00840 avio_rl16(pb);
00841 avio_rl32(pb);
00842
00843 pd = (AVProbeData) { .buf = pb->buf_ptr, .buf_size = pb->buf_end - pb->buf_ptr };
00844 if (!(sub_demuxer = av_probe_input_format2(&pd, 1, &score)))
00845 goto error;
00846
00847 if (!(ast->sub_ctx = avformat_alloc_context()))
00848 goto error;
00849
00850 ast->sub_ctx->pb = pb;
00851 if (!avformat_open_input(&ast->sub_ctx, "", sub_demuxer, NULL)) {
00852 ff_read_packet(ast->sub_ctx, &ast->sub_pkt);
00853 *st->codec = *ast->sub_ctx->streams[0]->codec;
00854 ast->sub_ctx->streams[0]->codec->extradata = NULL;
00855 time_base = ast->sub_ctx->streams[0]->time_base;
00856 avpriv_set_pts_info(st, 64, time_base.num, time_base.den);
00857 }
00858 ast->sub_buffer = pkt->data;
00859 memset(pkt, 0, sizeof(*pkt));
00860 return 1;
00861 error:
00862 av_freep(&pb);
00863 }
00864 return 0;
00865 }
00866
00867 static AVStream *get_subtitle_pkt(AVFormatContext *s, AVStream *next_st,
00868 AVPacket *pkt)
00869 {
00870 AVIStream *ast, *next_ast = next_st->priv_data;
00871 int64_t ts, next_ts, ts_min = INT64_MAX;
00872 AVStream *st, *sub_st = NULL;
00873 int i;
00874
00875 next_ts = av_rescale_q(next_ast->frame_offset, next_st->time_base,
00876 AV_TIME_BASE_Q);
00877
00878 for (i=0; i<s->nb_streams; i++) {
00879 st = s->streams[i];
00880 ast = st->priv_data;
00881 if (st->discard < AVDISCARD_ALL && ast && ast->sub_pkt.data) {
00882 ts = av_rescale_q(ast->sub_pkt.dts, st->time_base, AV_TIME_BASE_Q);
00883 if (ts <= next_ts && ts < ts_min) {
00884 ts_min = ts;
00885 sub_st = st;
00886 }
00887 }
00888 }
00889
00890 if (sub_st) {
00891 ast = sub_st->priv_data;
00892 *pkt = ast->sub_pkt;
00893 pkt->stream_index = sub_st->index;
00894 if (ff_read_packet(ast->sub_ctx, &ast->sub_pkt) < 0)
00895 ast->sub_pkt.data = NULL;
00896 }
00897 return sub_st;
00898 }
00899
00900 static int get_stream_idx(int *d){
00901 if( d[0] >= '0' && d[0] <= '9'
00902 && d[1] >= '0' && d[1] <= '9'){
00903 return (d[0] - '0') * 10 + (d[1] - '0');
00904 }else{
00905 return 100;
00906 }
00907 }
00908
00913 static int avi_sync(AVFormatContext *s, int exit_early)
00914 {
00915 AVIContext *avi = s->priv_data;
00916 AVIOContext *pb = s->pb;
00917 int n;
00918 unsigned int d[8];
00919 unsigned int size;
00920 int64_t i, sync;
00921
00922 start_sync:
00923 memset(d, -1, sizeof(d));
00924 for(i=sync=avio_tell(pb); !url_feof(pb); i++) {
00925 int j;
00926
00927 for(j=0; j<7; j++)
00928 d[j]= d[j+1];
00929 d[7]= avio_r8(pb);
00930
00931 size= d[4] + (d[5]<<8) + (d[6]<<16) + (d[7]<<24);
00932
00933 n= get_stream_idx(d+2);
00934 av_dlog(s, "%X %X %X %X %X %X %X %X %"PRId64" %u %d\n",
00935 d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7], i, size, n);
00936 if(i + (uint64_t)size > avi->fsize || d[0] > 127)
00937 continue;
00938
00939
00940 if( (d[0] == 'i' && d[1] == 'x' && n < s->nb_streams)
00941
00942 ||(d[0] == 'J' && d[1] == 'U' && d[2] == 'N' && d[3] == 'K')
00943 ||(d[0] == 'i' && d[1] == 'd' && d[2] == 'x' && d[3] == '1')){
00944 avio_skip(pb, size);
00945 goto start_sync;
00946 }
00947
00948
00949 if(d[0] == 'L' && d[1] == 'I' && d[2] == 'S' && d[3] == 'T'){
00950 avio_skip(pb, 4);
00951 goto start_sync;
00952 }
00953
00954 n= get_stream_idx(d);
00955
00956 if(!((i-avi->last_pkt_pos)&1) && get_stream_idx(d+1) < s->nb_streams)
00957 continue;
00958
00959
00960 if(d[2] == 'i' && d[3] == 'x' && n < s->nb_streams){
00961 avio_skip(pb, size);
00962 goto start_sync;
00963 }
00964
00965
00966 if(n < s->nb_streams){
00967 AVStream *st;
00968 AVIStream *ast;
00969 st = s->streams[n];
00970 ast = st->priv_data;
00971
00972 if (!ast) {
00973 av_log(s, AV_LOG_WARNING, "Skiping foreign stream %d packet\n", n);
00974 continue;
00975 }
00976
00977 if(s->nb_streams>=2){
00978 AVStream *st1 = s->streams[1];
00979 AVIStream *ast1= st1->priv_data;
00980
00981 if( d[2] == 'w' && d[3] == 'b'
00982 && n==0
00983 && st ->codec->codec_type == AVMEDIA_TYPE_VIDEO
00984 && st1->codec->codec_type == AVMEDIA_TYPE_AUDIO
00985 && ast->prefix == 'd'*256+'c'
00986 && (d[2]*256+d[3] == ast1->prefix || !ast1->prefix_count)
00987 ){
00988 n=1;
00989 st = st1;
00990 ast = ast1;
00991 av_log(s, AV_LOG_WARNING, "Invalid stream + prefix combination, assuming audio.\n");
00992 }
00993 }
00994
00995
00996 if( (st->discard >= AVDISCARD_DEFAULT && size==0)
00997
00998 || st->discard >= AVDISCARD_ALL){
00999 if (!exit_early) {
01000 ast->frame_offset += get_duration(ast, size);
01001 }
01002 avio_skip(pb, size);
01003 goto start_sync;
01004 }
01005
01006 if (d[2] == 'p' && d[3] == 'c' && size<=4*256+4) {
01007 int k = avio_r8(pb);
01008 int last = (k + avio_r8(pb) - 1) & 0xFF;
01009
01010 avio_rl16(pb);
01011
01012 for (; k <= last; k++)
01013 ast->pal[k] = 0xFFU<<24 | avio_rb32(pb)>>8;
01014 ast->has_pal= 1;
01015 goto start_sync;
01016 } else if( ((ast->prefix_count<5 || sync+9 > i) && d[2]<128 && d[3]<128) ||
01017 d[2]*256+d[3] == ast->prefix
01018
01019 ) {
01020
01021 if (exit_early)
01022 return 0;
01023 if(d[2]*256+d[3] == ast->prefix)
01024 ast->prefix_count++;
01025 else{
01026 ast->prefix= d[2]*256+d[3];
01027 ast->prefix_count= 0;
01028 }
01029
01030 avi->stream_index= n;
01031 ast->packet_size= size + 8;
01032 ast->remaining= size;
01033
01034 if(size || !ast->sample_size){
01035 uint64_t pos= avio_tell(pb) - 8;
01036 if(!st->index_entries || !st->nb_index_entries || st->index_entries[st->nb_index_entries - 1].pos < pos){
01037 av_add_index_entry(st, pos, ast->frame_offset, size, 0, AVINDEX_KEYFRAME);
01038 }
01039 }
01040 return 0;
01041 }
01042 }
01043 }
01044
01045 if(pb->error)
01046 return pb->error;
01047 return AVERROR_EOF;
01048 }
01049
01050 static int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
01051 {
01052 AVIContext *avi = s->priv_data;
01053 AVIOContext *pb = s->pb;
01054 int err;
01055 void* dstr;
01056
01057 if (CONFIG_DV_DEMUXER && avi->dv_demux) {
01058 int size = avpriv_dv_get_packet(avi->dv_demux, pkt);
01059 if (size >= 0)
01060 return size;
01061 }
01062
01063 if(avi->non_interleaved){
01064 int best_stream_index = 0;
01065 AVStream *best_st= NULL;
01066 AVIStream *best_ast;
01067 int64_t best_ts= INT64_MAX;
01068 int i;
01069
01070 for(i=0; i<s->nb_streams; i++){
01071 AVStream *st = s->streams[i];
01072 AVIStream *ast = st->priv_data;
01073 int64_t ts= ast->frame_offset;
01074 int64_t last_ts;
01075
01076 if(!st->nb_index_entries)
01077 continue;
01078
01079 last_ts = st->index_entries[st->nb_index_entries - 1].timestamp;
01080 if(!ast->remaining && ts > last_ts)
01081 continue;
01082
01083 ts = av_rescale_q(ts, st->time_base, (AVRational){FFMAX(1, ast->sample_size), AV_TIME_BASE});
01084
01085 av_dlog(s, "%"PRId64" %d/%d %"PRId64"\n", ts,
01086 st->time_base.num, st->time_base.den, ast->frame_offset);
01087 if(ts < best_ts){
01088 best_ts= ts;
01089 best_st= st;
01090 best_stream_index= i;
01091 }
01092 }
01093 if(!best_st)
01094 return AVERROR_EOF;
01095
01096 best_ast = best_st->priv_data;
01097 best_ts = best_ast->frame_offset;
01098 if(best_ast->remaining)
01099 i= av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY | AVSEEK_FLAG_BACKWARD);
01100 else{
01101 i= av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY);
01102 if(i>=0)
01103 best_ast->frame_offset= best_st->index_entries[i].timestamp;
01104 }
01105
01106 if(i>=0){
01107 int64_t pos= best_st->index_entries[i].pos;
01108 pos += best_ast->packet_size - best_ast->remaining;
01109 if(avio_seek(s->pb, pos + 8, SEEK_SET) < 0)
01110 return AVERROR_EOF;
01111
01112 av_assert0(best_ast->remaining <= best_ast->packet_size);
01113
01114 avi->stream_index= best_stream_index;
01115 if(!best_ast->remaining)
01116 best_ast->packet_size=
01117 best_ast->remaining= best_st->index_entries[i].size;
01118 }
01119 else
01120 return AVERROR_EOF;
01121 }
01122
01123 resync:
01124 if(avi->stream_index >= 0){
01125 AVStream *st= s->streams[ avi->stream_index ];
01126 AVIStream *ast= st->priv_data;
01127 int size, err;
01128
01129 if(get_subtitle_pkt(s, st, pkt))
01130 return 0;
01131
01132 if(ast->sample_size <= 1)
01133 size= INT_MAX;
01134 else if(ast->sample_size < 32)
01135
01136 size= 1024*ast->sample_size;
01137 else
01138 size= ast->sample_size;
01139
01140 if(size > ast->remaining)
01141 size= ast->remaining;
01142 avi->last_pkt_pos= avio_tell(pb);
01143 err= av_get_packet(pb, pkt, size);
01144 if(err<0)
01145 return err;
01146 size = err;
01147
01148 if(ast->has_pal && pkt->size<(unsigned)INT_MAX/2){
01149 uint8_t *pal;
01150 pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE);
01151 if(!pal){
01152 av_log(s, AV_LOG_ERROR, "Failed to allocate data for palette\n");
01153 }else{
01154 memcpy(pal, ast->pal, AVPALETTE_SIZE);
01155 ast->has_pal = 0;
01156 }
01157 }
01158
01159 if (CONFIG_DV_DEMUXER && avi->dv_demux) {
01160 dstr = pkt->destruct;
01161 size = avpriv_dv_produce_packet(avi->dv_demux, pkt,
01162 pkt->data, pkt->size, pkt->pos);
01163 pkt->destruct = dstr;
01164 pkt->flags |= AV_PKT_FLAG_KEY;
01165 if (size < 0)
01166 av_free_packet(pkt);
01167 } else if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE
01168 && !st->codec->codec_tag && read_gab2_sub(st, pkt)) {
01169 ast->frame_offset++;
01170 avi->stream_index = -1;
01171 ast->remaining = 0;
01172 goto resync;
01173 } else {
01174
01175 pkt->dts = ast->frame_offset;
01176
01177 if(ast->sample_size)
01178 pkt->dts /= ast->sample_size;
01179 av_dlog(s, "dts:%"PRId64" offset:%"PRId64" %d/%d smpl_siz:%d base:%d st:%d size:%d\n",
01180 pkt->dts, ast->frame_offset, ast->scale, ast->rate,
01181 ast->sample_size, AV_TIME_BASE, avi->stream_index, size);
01182 pkt->stream_index = avi->stream_index;
01183
01184 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
01185 AVIndexEntry *e;
01186 int index;
01187 av_assert0(st->index_entries);
01188
01189 index= av_index_search_timestamp(st, ast->frame_offset, 0);
01190 e= &st->index_entries[index];
01191
01192 if(index >= 0 && e->timestamp == ast->frame_offset){
01193 if (index == st->nb_index_entries-1){
01194 int key=1;
01195 int i;
01196 uint32_t state=-1;
01197 for(i=0; i<FFMIN(size,256); i++){
01198 if(st->codec->codec_id == AV_CODEC_ID_MPEG4){
01199 if(state == 0x1B6){
01200 key= !(pkt->data[i]&0xC0);
01201 break;
01202 }
01203 }else
01204 break;
01205 state= (state<<8) + pkt->data[i];
01206 }
01207 if(!key)
01208 e->flags &= ~AVINDEX_KEYFRAME;
01209 }
01210 if (e->flags & AVINDEX_KEYFRAME)
01211 pkt->flags |= AV_PKT_FLAG_KEY;
01212 }
01213 } else {
01214 pkt->flags |= AV_PKT_FLAG_KEY;
01215 }
01216 ast->frame_offset += get_duration(ast, pkt->size);
01217 }
01218 ast->remaining -= err;
01219 if(!ast->remaining){
01220 avi->stream_index= -1;
01221 ast->packet_size= 0;
01222 }
01223
01224 if(!avi->non_interleaved && pkt->pos >= 0 && ast->seek_pos > pkt->pos){
01225 av_free_packet(pkt);
01226 goto resync;
01227 }
01228 ast->seek_pos= 0;
01229
01230 if(!avi->non_interleaved && st->nb_index_entries>1 && avi->index_loaded>1){
01231 int64_t dts= av_rescale_q(pkt->dts, st->time_base, AV_TIME_BASE_Q);
01232
01233 if(avi->dts_max - dts > 2*AV_TIME_BASE){
01234 avi->non_interleaved= 1;
01235 av_log(s, AV_LOG_INFO, "Switching to NI mode, due to poor interleaving\n");
01236 }else if(avi->dts_max < dts)
01237 avi->dts_max = dts;
01238 }
01239
01240 return 0;
01241 }
01242
01243 if ((err = avi_sync(s, 0)) < 0)
01244 return err;
01245 goto resync;
01246 }
01247
01248
01249
01250 static int avi_read_idx1(AVFormatContext *s, int size)
01251 {
01252 AVIContext *avi = s->priv_data;
01253 AVIOContext *pb = s->pb;
01254 int nb_index_entries, i;
01255 AVStream *st;
01256 AVIStream *ast;
01257 unsigned int index, tag, flags, pos, len, first_packet = 1;
01258 unsigned last_pos= -1;
01259 unsigned last_idx= -1;
01260 int64_t idx1_pos, first_packet_pos = 0, data_offset = 0;
01261 int anykey = 0;
01262
01263 nb_index_entries = size / 16;
01264 if (nb_index_entries <= 0)
01265 return -1;
01266
01267 idx1_pos = avio_tell(pb);
01268 avio_seek(pb, avi->movi_list+4, SEEK_SET);
01269 if (avi_sync(s, 1) == 0) {
01270 first_packet_pos = avio_tell(pb) - 8;
01271 }
01272 avi->stream_index = -1;
01273 avio_seek(pb, idx1_pos, SEEK_SET);
01274
01275 if (s->nb_streams == 1 && s->streams[0]->codec->codec_tag == AV_RL32("MMES")){
01276 first_packet_pos = 0;
01277 data_offset = avi->movi_list;
01278 }
01279
01280
01281 for(i = 0; i < nb_index_entries; i++) {
01282 if(url_feof(pb))
01283 return -1;
01284
01285 tag = avio_rl32(pb);
01286 flags = avio_rl32(pb);
01287 pos = avio_rl32(pb);
01288 len = avio_rl32(pb);
01289 av_dlog(s, "%d: tag=0x%x flags=0x%x pos=0x%x len=%d/",
01290 i, tag, flags, pos, len);
01291
01292 index = ((tag & 0xff) - '0') * 10;
01293 index += ((tag >> 8) & 0xff) - '0';
01294 if (index >= s->nb_streams)
01295 continue;
01296 st = s->streams[index];
01297 ast = st->priv_data;
01298
01299 if(first_packet && first_packet_pos && len) {
01300 data_offset = first_packet_pos - pos;
01301 first_packet = 0;
01302 }
01303 pos += data_offset;
01304
01305 av_dlog(s, "%d cum_len=%"PRId64"\n", len, ast->cum_len);
01306
01307
01308
01309
01310 if(last_pos == pos)
01311 avi->non_interleaved= 1;
01312 if(last_idx != pos && len) {
01313 av_add_index_entry(st, pos, ast->cum_len, len, 0, (flags&AVIIF_INDEX) ? AVINDEX_KEYFRAME : 0);
01314 last_idx= pos;
01315 }
01316 ast->cum_len += get_duration(ast, len);
01317 last_pos= pos;
01318 anykey |= flags&AVIIF_INDEX;
01319 }
01320 if (!anykey) {
01321 for (index = 0; index < s->nb_streams; index++) {
01322 st = s->streams[index];
01323 if (st->nb_index_entries)
01324 st->index_entries[0].flags |= AVINDEX_KEYFRAME;
01325 }
01326 }
01327 return 0;
01328 }
01329
01330 static int guess_ni_flag(AVFormatContext *s){
01331 int i;
01332 int64_t last_start=0;
01333 int64_t first_end= INT64_MAX;
01334 int64_t oldpos= avio_tell(s->pb);
01335 int *idx;
01336 int64_t min_pos, pos;
01337
01338 for(i=0; i<s->nb_streams; i++){
01339 AVStream *st = s->streams[i];
01340 int n= st->nb_index_entries;
01341 unsigned int size;
01342
01343 if(n <= 0)
01344 continue;
01345
01346 if(n >= 2){
01347 int64_t pos= st->index_entries[0].pos;
01348 avio_seek(s->pb, pos + 4, SEEK_SET);
01349 size= avio_rl32(s->pb);
01350 if(pos + size > st->index_entries[1].pos)
01351 last_start= INT64_MAX;
01352 }
01353
01354 if(st->index_entries[0].pos > last_start)
01355 last_start= st->index_entries[0].pos;
01356 if(st->index_entries[n-1].pos < first_end)
01357 first_end= st->index_entries[n-1].pos;
01358 }
01359 avio_seek(s->pb, oldpos, SEEK_SET);
01360 if (last_start > first_end)
01361 return 1;
01362 idx= av_mallocz(sizeof(*idx) * s->nb_streams);
01363 for (min_pos=pos=0; min_pos!=INT64_MAX; pos= min_pos+1LU) {
01364 int64_t max_dts = INT64_MIN/2, min_dts= INT64_MAX/2;
01365 min_pos = INT64_MAX;
01366
01367 for (i=0; i<s->nb_streams; i++) {
01368 AVStream *st = s->streams[i];
01369 int n= st->nb_index_entries;
01370 while (idx[i]<n && st->index_entries[idx[i]].pos < pos)
01371 idx[i]++;
01372 if (idx[i] < n) {
01373 min_dts = FFMIN(min_dts, av_rescale_q(st->index_entries[idx[i]].timestamp, st->time_base, AV_TIME_BASE_Q));
01374 min_pos = FFMIN(min_pos, st->index_entries[idx[i]].pos);
01375 }
01376 if (idx[i])
01377 max_dts = FFMAX(max_dts, av_rescale_q(st->index_entries[idx[i]-1].timestamp, st->time_base, AV_TIME_BASE_Q));
01378 }
01379 if(max_dts - min_dts > 2*AV_TIME_BASE) {
01380 av_free(idx);
01381 return 1;
01382 }
01383 }
01384 av_free(idx);
01385 return 0;
01386 }
01387
01388 static int avi_load_index(AVFormatContext *s)
01389 {
01390 AVIContext *avi = s->priv_data;
01391 AVIOContext *pb = s->pb;
01392 uint32_t tag, size;
01393 int64_t pos= avio_tell(pb);
01394 int64_t next;
01395 int ret = -1;
01396
01397 if (avio_seek(pb, avi->movi_end, SEEK_SET) < 0)
01398 goto the_end;
01399 av_dlog(s, "movi_end=0x%"PRIx64"\n", avi->movi_end);
01400 for(;;) {
01401 tag = avio_rl32(pb);
01402 size = avio_rl32(pb);
01403 if (url_feof(pb))
01404 break;
01405 next = avio_tell(pb) + size + (size & 1);
01406
01407 av_dlog(s, "tag=%c%c%c%c size=0x%x\n",
01408 tag & 0xff,
01409 (tag >> 8) & 0xff,
01410 (tag >> 16) & 0xff,
01411 (tag >> 24) & 0xff,
01412 size);
01413
01414 if (tag == MKTAG('i', 'd', 'x', '1') &&
01415 avi_read_idx1(s, size) >= 0) {
01416 avi->index_loaded=2;
01417 ret = 0;
01418 }else if(tag == MKTAG('L', 'I', 'S', 'T')) {
01419 uint32_t tag1 = avio_rl32(pb);
01420
01421 if (tag1 == MKTAG('I', 'N', 'F', 'O'))
01422 ff_read_riff_info(s, size - 4);
01423 }else if(!ret)
01424 break;
01425
01426 if (avio_seek(pb, next, SEEK_SET) < 0)
01427 break;
01428 }
01429 the_end:
01430 avio_seek(pb, pos, SEEK_SET);
01431 return ret;
01432 }
01433
01434 static void seek_subtitle(AVStream *st, AVStream *st2, int64_t timestamp)
01435 {
01436 AVIStream *ast2 = st2->priv_data;
01437 int64_t ts2 = av_rescale_q(timestamp, st->time_base, st2->time_base);
01438 av_free_packet(&ast2->sub_pkt);
01439 if (avformat_seek_file(ast2->sub_ctx, 0, INT64_MIN, ts2, ts2, 0) >= 0 ||
01440 avformat_seek_file(ast2->sub_ctx, 0, ts2, ts2, INT64_MAX, 0) >= 0)
01441 ff_read_packet(ast2->sub_ctx, &ast2->sub_pkt);
01442 }
01443
01444 static int avi_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
01445 {
01446 AVIContext *avi = s->priv_data;
01447 AVStream *st;
01448 int i, index;
01449 int64_t pos, pos_min;
01450 AVIStream *ast;
01451
01452 if (!avi->index_loaded) {
01453
01454 avi_load_index(s);
01455 avi->index_loaded |= 1;
01456 }
01457 av_assert0(stream_index>= 0);
01458
01459 st = s->streams[stream_index];
01460 ast= st->priv_data;
01461 index= av_index_search_timestamp(st, timestamp * FFMAX(ast->sample_size, 1), flags);
01462 if (index<0) {
01463 if (st->nb_index_entries > 0)
01464 av_log(s, AV_LOG_DEBUG, "Failed to find timestamp %"PRId64 " in index %"PRId64 " .. %"PRId64 "\n",
01465 timestamp * FFMAX(ast->sample_size, 1),
01466 st->index_entries[0].timestamp,
01467 st->index_entries[st->nb_index_entries - 1].timestamp);
01468 return -1;
01469 }
01470
01471
01472 pos = st->index_entries[index].pos;
01473 timestamp = st->index_entries[index].timestamp / FFMAX(ast->sample_size, 1);
01474
01475 av_dlog(s, "XX %"PRId64" %d %"PRId64"\n",
01476 timestamp, index, st->index_entries[index].timestamp);
01477
01478 if (CONFIG_DV_DEMUXER && avi->dv_demux) {
01479
01480
01481
01482 av_assert0(stream_index == 0);
01483
01484 if(avio_seek(s->pb, pos, SEEK_SET) < 0)
01485 return -1;
01486
01487
01488
01489 ff_dv_offset_reset(avi->dv_demux, timestamp);
01490
01491 avi->stream_index= -1;
01492 return 0;
01493 }
01494
01495 pos_min= pos;
01496 for(i = 0; i < s->nb_streams; i++) {
01497 AVStream *st2 = s->streams[i];
01498 AVIStream *ast2 = st2->priv_data;
01499
01500 ast2->packet_size=
01501 ast2->remaining= 0;
01502
01503 if (ast2->sub_ctx) {
01504 seek_subtitle(st, st2, timestamp);
01505 continue;
01506 }
01507
01508 if (st2->nb_index_entries <= 0)
01509 continue;
01510
01511
01512 av_assert0((int64_t)st2->time_base.num*ast2->rate == (int64_t)st2->time_base.den*ast2->scale);
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 ast2->seek_pos= st2->index_entries[index].pos;
01520 pos_min= FFMIN(pos_min,ast2->seek_pos);
01521 }
01522 for(i = 0; i < s->nb_streams; i++) {
01523 AVStream *st2 = s->streams[i];
01524 AVIStream *ast2 = st2->priv_data;
01525
01526 if (ast2->sub_ctx || st2->nb_index_entries <= 0)
01527 continue;
01528
01529 index = av_index_search_timestamp(
01530 st2,
01531 av_rescale_q(timestamp, st->time_base, st2->time_base) * FFMAX(ast2->sample_size, 1),
01532 flags | AVSEEK_FLAG_BACKWARD | (st2->codec->codec_type != AVMEDIA_TYPE_VIDEO ? AVSEEK_FLAG_ANY : 0));
01533 if(index<0)
01534 index=0;
01535 while(!avi->non_interleaved && index>0 && st2->index_entries[index-1].pos >= pos_min)
01536 index--;
01537 ast2->frame_offset = st2->index_entries[index].timestamp;
01538 }
01539
01540
01541 if (avio_seek(s->pb, pos_min, SEEK_SET) < 0) {
01542 av_log(s, AV_LOG_ERROR, "Seek failed\n");
01543 return -1;
01544 }
01545 avi->stream_index= -1;
01546 avi->dts_max= INT_MIN;
01547 return 0;
01548 }
01549
01550 static int avi_read_close(AVFormatContext *s)
01551 {
01552 int i;
01553 AVIContext *avi = s->priv_data;
01554
01555 for(i=0;i<s->nb_streams;i++) {
01556 AVStream *st = s->streams[i];
01557 AVIStream *ast = st->priv_data;
01558 if (ast) {
01559 if (ast->sub_ctx) {
01560 av_freep(&ast->sub_ctx->pb);
01561 avformat_close_input(&ast->sub_ctx);
01562 }
01563 av_free(ast->sub_buffer);
01564 av_free_packet(&ast->sub_pkt);
01565 }
01566 }
01567
01568 av_free(avi->dv_demux);
01569
01570 return 0;
01571 }
01572
01573 static int avi_probe(AVProbeData *p)
01574 {
01575 int i;
01576
01577
01578 for(i=0; avi_headers[i][0]; i++)
01579 if(!memcmp(p->buf , avi_headers[i] , 4) &&
01580 !memcmp(p->buf+8, avi_headers[i]+4, 4))
01581 return AVPROBE_SCORE_MAX;
01582
01583 return 0;
01584 }
01585
01586 AVInputFormat ff_avi_demuxer = {
01587 .name = "avi",
01588 .long_name = NULL_IF_CONFIG_SMALL("AVI (Audio Video Interleaved)"),
01589 .priv_data_size = sizeof(AVIContext),
01590 .read_probe = avi_probe,
01591 .read_header = avi_read_header,
01592 .read_packet = avi_read_packet,
01593 .read_close = avi_read_close,
01594 .read_seek = avi_read_seek,
01595 .priv_class = &demuxer_class,
01596 };