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