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