00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "avformat.h"
00022 #include "avi.h"
00023 #include "dv.h"
00024 #include "riff.h"
00025
00026 #undef NDEBUG
00027 #include <assert.h>
00028
00029
00030
00031
00032 typedef struct AVIStream {
00033 int64_t frame_offset;
00034
00035 int remaining;
00036 int packet_size;
00037
00038 int scale;
00039 int rate;
00040 int sample_size;
00041
00042 int64_t cum_len;
00043
00044 int prefix;
00045 int prefix_count;
00046 } AVIStream;
00047
00048 typedef struct {
00049 int64_t riff_end;
00050 int64_t movi_end;
00051 int64_t fsize;
00052 offset_t movi_list;
00053 int index_loaded;
00054 int is_odml;
00055 int non_interleaved;
00056 int stream_index;
00057 DVDemuxContext* dv_demux;
00058 } AVIContext;
00059
00060 static const char avi_headers[][8] = {
00061 { 'R', 'I', 'F', 'F', 'A', 'V', 'I', ' ' },
00062 { 'R', 'I', 'F', 'F', 'A', 'V', 'I', 'X' },
00063 { 'R', 'I', 'F', 'F', 'A', 'V', 'I', 0x19},
00064 { 'O', 'N', '2', ' ', 'O', 'N', '2', 'f' },
00065 { 'R', 'I', 'F', 'F', 'A', 'M', 'V', ' ' },
00066 { 0 }
00067 };
00068
00069 static int avi_load_index(AVFormatContext *s);
00070 static int guess_ni_flag(AVFormatContext *s);
00071
00072 #ifdef DEBUG
00073 static void print_tag(const char *str, unsigned int tag, int size)
00074 {
00075 printf("%s: tag=%c%c%c%c size=0x%x\n",
00076 str, tag & 0xff,
00077 (tag >> 8) & 0xff,
00078 (tag >> 16) & 0xff,
00079 (tag >> 24) & 0xff,
00080 size);
00081 }
00082 #endif
00083
00084 static int get_riff(AVIContext *avi, ByteIOContext *pb)
00085 {
00086 char header[8];
00087 int i;
00088
00089
00090 get_buffer(pb, header, 4);
00091 avi->riff_end = get_le32(pb);
00092 avi->riff_end += url_ftell(pb);
00093 get_buffer(pb, header+4, 4);
00094
00095 for(i=0; avi_headers[i][0]; i++)
00096 if(!memcmp(header, avi_headers[i], 8))
00097 break;
00098 if(!avi_headers[i][0])
00099 return -1;
00100
00101 if(header[7] == 0x19)
00102 av_log(NULL, AV_LOG_INFO, "file has been generated with a totally broken muxer\n");
00103
00104 return 0;
00105 }
00106
00107 static int read_braindead_odml_indx(AVFormatContext *s, int frame_num){
00108 AVIContext *avi = s->priv_data;
00109 ByteIOContext *pb = s->pb;
00110 int longs_pre_entry= get_le16(pb);
00111 int index_sub_type = get_byte(pb);
00112 int index_type = get_byte(pb);
00113 int entries_in_use = get_le32(pb);
00114 int chunk_id = get_le32(pb);
00115 int64_t base = get_le64(pb);
00116 int stream_id= 10*((chunk_id&0xFF) - '0') + (((chunk_id>>8)&0xFF) - '0');
00117 AVStream *st;
00118 AVIStream *ast;
00119 int i;
00120 int64_t last_pos= -1;
00121 int64_t filesize= url_fsize(s->pb);
00122
00123 #ifdef DEBUG_SEEK
00124 av_log(s, AV_LOG_ERROR, "longs_pre_entry:%d index_type:%d entries_in_use:%d chunk_id:%X base:%16"PRIX64"\n",
00125 longs_pre_entry,index_type, entries_in_use, chunk_id, base);
00126 #endif
00127
00128 if(stream_id > s->nb_streams || stream_id < 0)
00129 return -1;
00130 st= s->streams[stream_id];
00131 ast = st->priv_data;
00132
00133 if(index_sub_type)
00134 return -1;
00135
00136 get_le32(pb);
00137
00138 if(index_type && longs_pre_entry != 2)
00139 return -1;
00140 if(index_type>1)
00141 return -1;
00142
00143 if(filesize > 0 && base >= filesize){
00144 av_log(s, AV_LOG_ERROR, "ODML index invalid\n");
00145 if(base>>32 == (base & 0xFFFFFFFF) && (base & 0xFFFFFFFF) < filesize && filesize <= 0xFFFFFFFF)
00146 base &= 0xFFFFFFFF;
00147 else
00148 return -1;
00149 }
00150
00151 for(i=0; i<entries_in_use; i++){
00152 if(index_type){
00153 int64_t pos= get_le32(pb) + base - 8;
00154 int len = get_le32(pb);
00155 int key= len >= 0;
00156 len &= 0x7FFFFFFF;
00157
00158 #ifdef DEBUG_SEEK
00159 av_log(s, AV_LOG_ERROR, "pos:%"PRId64", len:%X\n", pos, len);
00160 #endif
00161 if(last_pos == pos || pos == base - 8)
00162 avi->non_interleaved= 1;
00163 else
00164 av_add_index_entry(st, pos, ast->cum_len / FFMAX(1, ast->sample_size), len, 0, key ? AVINDEX_KEYFRAME : 0);
00165
00166 if(ast->sample_size)
00167 ast->cum_len += len;
00168 else
00169 ast->cum_len ++;
00170 last_pos= pos;
00171 }else{
00172 int64_t offset, pos;
00173 int duration;
00174 offset = get_le64(pb);
00175 get_le32(pb);
00176 duration = get_le32(pb);
00177 pos = url_ftell(pb);
00178
00179 url_fseek(pb, offset+8, SEEK_SET);
00180 read_braindead_odml_indx(s, frame_num);
00181 frame_num += duration;
00182
00183 url_fseek(pb, pos, SEEK_SET);
00184 }
00185 }
00186 avi->index_loaded=1;
00187 return 0;
00188 }
00189
00190 static void clean_index(AVFormatContext *s){
00191 int i;
00192 int64_t j;
00193
00194 for(i=0; i<s->nb_streams; i++){
00195 AVStream *st = s->streams[i];
00196 AVIStream *ast = st->priv_data;
00197 int n= st->nb_index_entries;
00198 int max= ast->sample_size;
00199 int64_t pos, size, ts;
00200
00201 if(n != 1 || ast->sample_size==0)
00202 continue;
00203
00204 while(max < 1024) max+=max;
00205
00206 pos= st->index_entries[0].pos;
00207 size= st->index_entries[0].size;
00208 ts= st->index_entries[0].timestamp;
00209
00210 for(j=0; j<size; j+=max){
00211 av_add_index_entry(st, pos+j, ts + j/ast->sample_size, FFMIN(max, size-j), 0, AVINDEX_KEYFRAME);
00212 }
00213 }
00214 }
00215
00216 static int avi_read_tag(ByteIOContext *pb, char *buf, int maxlen, unsigned int size)
00217 {
00218 offset_t i = url_ftell(pb);
00219 size += (size & 1);
00220 get_strz(pb, buf, maxlen);
00221 url_fseek(pb, i+size, SEEK_SET);
00222 return 0;
00223 }
00224
00225 static int avi_read_header(AVFormatContext *s, AVFormatParameters *ap)
00226 {
00227 AVIContext *avi = s->priv_data;
00228 ByteIOContext *pb = s->pb;
00229 uint32_t tag, tag1, handler;
00230 int codec_type, stream_index, frame_period, bit_rate;
00231 unsigned int size, nb_frames;
00232 int i;
00233 AVStream *st;
00234 AVIStream *ast = NULL;
00235 char str_track[4];
00236 int avih_width=0, avih_height=0;
00237 int amv_file_format=0;
00238
00239 avi->stream_index= -1;
00240
00241 if (get_riff(avi, pb) < 0)
00242 return -1;
00243
00244 avi->fsize = url_fsize(pb);
00245 if(avi->fsize<=0)
00246 avi->fsize= avi->riff_end;
00247
00248
00249 stream_index = -1;
00250 codec_type = -1;
00251 frame_period = 0;
00252 for(;;) {
00253 if (url_feof(pb))
00254 goto fail;
00255 tag = get_le32(pb);
00256 size = get_le32(pb);
00257 #ifdef DEBUG
00258 print_tag("tag", tag, size);
00259 #endif
00260
00261 switch(tag) {
00262 case MKTAG('L', 'I', 'S', 'T'):
00263
00264 tag1 = get_le32(pb);
00265 #ifdef DEBUG
00266 print_tag("list", tag1, 0);
00267 #endif
00268 if (tag1 == MKTAG('m', 'o', 'v', 'i')) {
00269 avi->movi_list = url_ftell(pb) - 4;
00270 if(size) avi->movi_end = avi->movi_list + size + (size & 1);
00271 else avi->movi_end = url_fsize(pb);
00272 #ifdef DEBUG
00273 printf("movi end=%"PRIx64"\n", avi->movi_end);
00274 #endif
00275 goto end_of_header;
00276 }
00277 break;
00278 case MKTAG('d', 'm', 'l', 'h'):
00279 avi->is_odml = 1;
00280 url_fskip(pb, size + (size & 1));
00281 break;
00282 case MKTAG('a', 'm', 'v', 'h'):
00283 amv_file_format=1;
00284 case MKTAG('a', 'v', 'i', 'h'):
00285
00286
00287 frame_period = get_le32(pb);
00288 bit_rate = get_le32(pb) * 8;
00289 get_le32(pb);
00290 avi->non_interleaved |= get_le32(pb) & AVIF_MUSTUSEINDEX;
00291
00292 url_fskip(pb, 2 * 4);
00293 get_le32(pb);
00294 get_le32(pb);
00295 avih_width=get_le32(pb);
00296 avih_height=get_le32(pb);
00297
00298 url_fskip(pb, size - 10 * 4);
00299 break;
00300 case MKTAG('s', 't', 'r', 'h'):
00301
00302
00303 tag1 = get_le32(pb);
00304 handler = get_le32(pb);
00305
00306 if(tag1 == MKTAG('p', 'a', 'd', 's')){
00307 url_fskip(pb, size - 8);
00308 break;
00309 }else{
00310 stream_index++;
00311 st = av_new_stream(s, stream_index);
00312 if (!st)
00313 goto fail;
00314
00315 ast = av_mallocz(sizeof(AVIStream));
00316 if (!ast)
00317 goto fail;
00318 st->priv_data = ast;
00319 }
00320 if(amv_file_format)
00321 tag1 = stream_index ? MKTAG('a','u','d','s') : MKTAG('v','i','d','s');
00322
00323 #ifdef DEBUG
00324 print_tag("strh", tag1, -1);
00325 #endif
00326 if(tag1 == MKTAG('i', 'a', 'v', 's') || tag1 == MKTAG('i', 'v', 'a', 's')){
00327 int64_t dv_dur;
00328
00329
00330
00331
00332
00333 if (s->nb_streams != 1)
00334 goto fail;
00335
00336 if (handler != MKTAG('d', 'v', 's', 'd') &&
00337 handler != MKTAG('d', 'v', 'h', 'd') &&
00338 handler != MKTAG('d', 'v', 's', 'l'))
00339 goto fail;
00340
00341 ast = s->streams[0]->priv_data;
00342 av_freep(&s->streams[0]->codec->extradata);
00343 av_freep(&s->streams[0]);
00344 s->nb_streams = 0;
00345 if (ENABLE_DV_DEMUXER) {
00346 avi->dv_demux = dv_init_demux(s);
00347 if (!avi->dv_demux)
00348 goto fail;
00349 }
00350 s->streams[0]->priv_data = ast;
00351 url_fskip(pb, 3 * 4);
00352 ast->scale = get_le32(pb);
00353 ast->rate = get_le32(pb);
00354 url_fskip(pb, 4);
00355
00356 dv_dur = get_le32(pb);
00357 if (ast->scale > 0 && ast->rate > 0 && dv_dur > 0) {
00358 dv_dur *= AV_TIME_BASE;
00359 s->duration = av_rescale(dv_dur, ast->scale, ast->rate);
00360 }
00361
00362
00363
00364
00365
00366 stream_index = s->nb_streams - 1;
00367 url_fskip(pb, size - 9*4);
00368 break;
00369 }
00370
00371 assert(stream_index < s->nb_streams);
00372 st->codec->stream_codec_tag= handler;
00373
00374 get_le32(pb);
00375 get_le16(pb);
00376 get_le16(pb);
00377 get_le32(pb);
00378 ast->scale = get_le32(pb);
00379 ast->rate = get_le32(pb);
00380 if(ast->scale && ast->rate){
00381 }else if(frame_period){
00382 ast->rate = 1000000;
00383 ast->scale = frame_period;
00384 }else{
00385 ast->rate = 25;
00386 ast->scale = 1;
00387 }
00388 av_set_pts_info(st, 64, ast->scale, ast->rate);
00389
00390 ast->cum_len=get_le32(pb);
00391 nb_frames = get_le32(pb);
00392
00393 st->start_time = 0;
00394 st->duration = nb_frames;
00395 get_le32(pb);
00396 get_le32(pb);
00397 ast->sample_size = get_le32(pb);
00398 ast->cum_len *= FFMAX(1, ast->sample_size);
00399
00400
00401 switch(tag1) {
00402 case MKTAG('v', 'i', 'd', 's'):
00403 codec_type = CODEC_TYPE_VIDEO;
00404
00405 ast->sample_size = 0;
00406 break;
00407 case MKTAG('a', 'u', 'd', 's'):
00408 codec_type = CODEC_TYPE_AUDIO;
00409 break;
00410 case MKTAG('t', 'x', 't', 's'):
00411
00412 codec_type = CODEC_TYPE_DATA;
00413 break;
00414 default:
00415 av_log(s, AV_LOG_ERROR, "unknown stream type %X\n", tag1);
00416 goto fail;
00417 }
00418 ast->frame_offset= ast->cum_len;
00419 url_fskip(pb, size - 12 * 4);
00420 break;
00421 case MKTAG('s', 't', 'r', 'f'):
00422
00423 if (stream_index >= (unsigned)s->nb_streams || avi->dv_demux) {
00424 url_fskip(pb, size);
00425 } else {
00426 st = s->streams[stream_index];
00427 switch(codec_type) {
00428 case CODEC_TYPE_VIDEO:
00429 if(amv_file_format){
00430 st->codec->width=avih_width;
00431 st->codec->height=avih_height;
00432 st->codec->codec_type = CODEC_TYPE_VIDEO;
00433 st->codec->codec_id = CODEC_ID_AMV;
00434 url_fskip(pb, size);
00435 break;
00436 }
00437 get_le32(pb);
00438 st->codec->width = get_le32(pb);
00439 st->codec->height = get_le32(pb);
00440 get_le16(pb);
00441 st->codec->bits_per_sample= get_le16(pb);
00442 tag1 = get_le32(pb);
00443 get_le32(pb);
00444 get_le32(pb);
00445 get_le32(pb);
00446 get_le32(pb);
00447 get_le32(pb);
00448
00449 if (tag1 == MKTAG('D', 'X', 'S', 'B')) {
00450 st->codec->codec_type = CODEC_TYPE_SUBTITLE;
00451 st->codec->codec_tag = tag1;
00452 st->codec->codec_id = CODEC_ID_XSUB;
00453 break;
00454 }
00455
00456 if(size > 10*4 && size<(1<<30)){
00457 st->codec->extradata_size= size - 10*4;
00458 st->codec->extradata= av_malloc(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
00459 get_buffer(pb, st->codec->extradata, st->codec->extradata_size);
00460 }
00461
00462 if(st->codec->extradata_size & 1)
00463 get_byte(pb);
00464
00465
00466
00467
00468 if (st->codec->extradata_size && (st->codec->bits_per_sample <= 8)) {
00469 st->codec->palctrl = av_mallocz(sizeof(AVPaletteControl));
00470 #ifdef WORDS_BIGENDIAN
00471 for (i = 0; i < FFMIN(st->codec->extradata_size, AVPALETTE_SIZE)/4; i++)
00472 st->codec->palctrl->palette[i] = bswap_32(((uint32_t*)st->codec->extradata)[i]);
00473 #else
00474 memcpy(st->codec->palctrl->palette, st->codec->extradata,
00475 FFMIN(st->codec->extradata_size, AVPALETTE_SIZE));
00476 #endif
00477 st->codec->palctrl->palette_changed = 1;
00478 }
00479
00480 #ifdef DEBUG
00481 print_tag("video", tag1, 0);
00482 #endif
00483 st->codec->codec_type = CODEC_TYPE_VIDEO;
00484 st->codec->codec_tag = tag1;
00485 st->codec->codec_id = codec_get_id(codec_bmp_tags, tag1);
00486 st->need_parsing = AVSTREAM_PARSE_HEADERS;
00487
00488 break;
00489 case CODEC_TYPE_AUDIO:
00490 get_wav_header(pb, st->codec, size);
00491 if(ast->sample_size && st->codec->block_align && ast->sample_size % st->codec->block_align)
00492 av_log(s, AV_LOG_DEBUG, "invalid sample size or block align detected\n");
00493 if (size%2)
00494 url_fskip(pb, 1);
00495
00496
00497 st->need_parsing = AVSTREAM_PARSE_TIMESTAMPS;
00498
00499 if (st->codec->codec_id == CODEC_ID_AAC && st->codec->extradata_size)
00500 st->need_parsing = AVSTREAM_PARSE_NONE;
00501
00502
00503 if (st->codec->stream_codec_tag == ff_get_fourcc("Axan")){
00504 st->codec->codec_id = CODEC_ID_XAN_DPCM;
00505 st->codec->codec_tag = 0;
00506 }
00507 if (amv_file_format)
00508 st->codec->codec_id = CODEC_ID_ADPCM_IMA_AMV;
00509 break;
00510 default:
00511 st->codec->codec_type = CODEC_TYPE_DATA;
00512 st->codec->codec_id= CODEC_ID_NONE;
00513 st->codec->codec_tag= 0;
00514 url_fskip(pb, size);
00515 break;
00516 }
00517 }
00518 break;
00519 case MKTAG('i', 'n', 'd', 'x'):
00520 i= url_ftell(pb);
00521 if(!url_is_streamed(pb) && !(s->flags & AVFMT_FLAG_IGNIDX)){
00522 read_braindead_odml_indx(s, 0);
00523 }
00524 url_fseek(pb, i+size, SEEK_SET);
00525 break;
00526 case MKTAG('v', 'p', 'r', 'p'):
00527 if(stream_index < (unsigned)s->nb_streams && size > 9*4){
00528 AVRational active, active_aspect;
00529
00530 st = s->streams[stream_index];
00531 get_le32(pb);
00532 get_le32(pb);
00533 get_le32(pb);
00534 get_le32(pb);
00535 get_le32(pb);
00536
00537 active_aspect.num= get_le16(pb);
00538 active_aspect.den= get_le16(pb);
00539 active.num = get_le32(pb);
00540 active.den = get_le32(pb);
00541 get_le32(pb);
00542
00543 if(active_aspect.num && active_aspect.den && active.num && active.den){
00544 st->codec->sample_aspect_ratio= av_div_q(active_aspect, active);
00545
00546 }
00547 size -= 9*4;
00548 }
00549 url_fseek(pb, size, SEEK_CUR);
00550 break;
00551 case MKTAG('I', 'N', 'A', 'M'):
00552 avi_read_tag(pb, s->title, sizeof(s->title), size);
00553 break;
00554 case MKTAG('I', 'A', 'R', 'T'):
00555 avi_read_tag(pb, s->author, sizeof(s->author), size);
00556 break;
00557 case MKTAG('I', 'C', 'O', 'P'):
00558 avi_read_tag(pb, s->copyright, sizeof(s->copyright), size);
00559 break;
00560 case MKTAG('I', 'C', 'M', 'T'):
00561 avi_read_tag(pb, s->comment, sizeof(s->comment), size);
00562 break;
00563 case MKTAG('I', 'G', 'N', 'R'):
00564 avi_read_tag(pb, s->genre, sizeof(s->genre), size);
00565 break;
00566 case MKTAG('I', 'P', 'R', 'D'):
00567 avi_read_tag(pb, s->album, sizeof(s->album), size);
00568 break;
00569 case MKTAG('I', 'P', 'R', 'T'):
00570 avi_read_tag(pb, str_track, sizeof(str_track), size);
00571 sscanf(str_track, "%d", &s->track);
00572 break;
00573 default:
00574 if(size > 1000000){
00575 av_log(s, AV_LOG_ERROR, "well something went wrong during header parsing, "
00576 "ill ignore it and try to continue anyway\n");
00577 avi->movi_list = url_ftell(pb) - 4;
00578 avi->movi_end = url_fsize(pb);
00579 goto end_of_header;
00580 }
00581
00582 size += (size & 1);
00583 url_fskip(pb, size);
00584 break;
00585 }
00586 }
00587 end_of_header:
00588
00589 if (stream_index != s->nb_streams - 1) {
00590 fail:
00591 for(i=0;i<s->nb_streams;i++) {
00592 av_freep(&s->streams[i]->codec->extradata);
00593 av_freep(&s->streams[i]);
00594 }
00595 return -1;
00596 }
00597
00598 if(!avi->index_loaded && !url_is_streamed(pb))
00599 avi_load_index(s);
00600 avi->index_loaded = 1;
00601 avi->non_interleaved |= guess_ni_flag(s);
00602 if(avi->non_interleaved)
00603 clean_index(s);
00604
00605 return 0;
00606 }
00607
00608 static int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
00609 {
00610 AVIContext *avi = s->priv_data;
00611 ByteIOContext *pb = s->pb;
00612 int n, d[8], size;
00613 offset_t i, sync;
00614 void* dstr;
00615
00616 if (ENABLE_DV_DEMUXER && avi->dv_demux) {
00617 size = dv_get_packet(avi->dv_demux, pkt);
00618 if (size >= 0)
00619 return size;
00620 }
00621
00622 if(avi->non_interleaved){
00623 int best_stream_index = 0;
00624 AVStream *best_st= NULL;
00625 AVIStream *best_ast;
00626 int64_t best_ts= INT64_MAX;
00627 int i;
00628
00629 for(i=0; i<s->nb_streams; i++){
00630 AVStream *st = s->streams[i];
00631 AVIStream *ast = st->priv_data;
00632 int64_t ts= ast->frame_offset;
00633
00634 if(ast->sample_size)
00635 ts /= ast->sample_size;
00636 ts= av_rescale(ts, AV_TIME_BASE * (int64_t)st->time_base.num, st->time_base.den);
00637
00638
00639 if(ts < best_ts){
00640 best_ts= ts;
00641 best_st= st;
00642 best_stream_index= i;
00643 }
00644 }
00645 best_ast = best_st->priv_data;
00646 best_ts= av_rescale(best_ts, best_st->time_base.den, AV_TIME_BASE * (int64_t)best_st->time_base.num);
00647 if(best_ast->remaining)
00648 i= av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY | AVSEEK_FLAG_BACKWARD);
00649 else
00650 i= av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY);
00651
00652
00653 if(i>=0){
00654 int64_t pos= best_st->index_entries[i].pos;
00655 pos += best_ast->packet_size - best_ast->remaining;
00656 url_fseek(s->pb, pos + 8, SEEK_SET);
00657
00658
00659 assert(best_ast->remaining <= best_ast->packet_size);
00660
00661 avi->stream_index= best_stream_index;
00662 if(!best_ast->remaining)
00663 best_ast->packet_size=
00664 best_ast->remaining= best_st->index_entries[i].size;
00665 }
00666 }
00667
00668 resync:
00669 if(avi->stream_index >= 0){
00670 AVStream *st= s->streams[ avi->stream_index ];
00671 AVIStream *ast= st->priv_data;
00672 int size;
00673
00674 if(ast->sample_size <= 1)
00675 size= INT_MAX;
00676 else if(ast->sample_size < 32)
00677 size= 64*ast->sample_size;
00678 else
00679 size= ast->sample_size;
00680
00681 if(size > ast->remaining)
00682 size= ast->remaining;
00683 av_get_packet(pb, pkt, size);
00684
00685 if (ENABLE_DV_DEMUXER && avi->dv_demux) {
00686 dstr = pkt->destruct;
00687 size = dv_produce_packet(avi->dv_demux, pkt,
00688 pkt->data, pkt->size);
00689 pkt->destruct = dstr;
00690 pkt->flags |= PKT_FLAG_KEY;
00691 } else {
00692
00693 pkt->dts = ast->frame_offset;
00694
00695 if(ast->sample_size)
00696 pkt->dts /= ast->sample_size;
00697
00698 pkt->stream_index = avi->stream_index;
00699
00700 if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
00701 AVIndexEntry *e;
00702 int index;
00703 assert(st->index_entries);
00704
00705 index= av_index_search_timestamp(st, pkt->dts, 0);
00706 e= &st->index_entries[index];
00707
00708 if(index >= 0 && e->timestamp == ast->frame_offset){
00709 if (e->flags & AVINDEX_KEYFRAME)
00710 pkt->flags |= PKT_FLAG_KEY;
00711 }
00712 } else {
00713 pkt->flags |= PKT_FLAG_KEY;
00714 }
00715 if(ast->sample_size)
00716 ast->frame_offset += pkt->size;
00717 else
00718 ast->frame_offset++;
00719 }
00720 ast->remaining -= size;
00721 if(!ast->remaining){
00722 avi->stream_index= -1;
00723 ast->packet_size= 0;
00724 }
00725
00726 return size;
00727 }
00728
00729 memset(d, -1, sizeof(int)*8);
00730 for(i=sync=url_ftell(pb); !url_feof(pb); i++) {
00731 int j;
00732
00733 for(j=0; j<7; j++)
00734 d[j]= d[j+1];
00735 d[7]= get_byte(pb);
00736
00737 size= d[4] + (d[5]<<8) + (d[6]<<16) + (d[7]<<24);
00738
00739 if( d[2] >= '0' && d[2] <= '9'
00740 && d[3] >= '0' && d[3] <= '9'){
00741 n= (d[2] - '0') * 10 + (d[3] - '0');
00742 }else{
00743 n= 100;
00744 }
00745
00746 if(i + size > avi->fsize || d[0]<0)
00747 continue;
00748
00749
00750 if( (d[0] == 'i' && d[1] == 'x' && n < s->nb_streams)
00751
00752 ||(d[0] == 'J' && d[1] == 'U' && d[2] == 'N' && d[3] == 'K')
00753 ||(d[0] == 'i' && d[1] == 'd' && d[2] == 'x' && d[3] == '1')){
00754 url_fskip(pb, size);
00755
00756 goto resync;
00757 }
00758
00759 if( d[0] >= '0' && d[0] <= '9'
00760 && d[1] >= '0' && d[1] <= '9'){
00761 n= (d[0] - '0') * 10 + (d[1] - '0');
00762 }else{
00763 n= 100;
00764 }
00765
00766
00767 if(n < s->nb_streams){
00768 AVStream *st;
00769 AVIStream *ast;
00770 st = s->streams[n];
00771 ast = st->priv_data;
00772
00773 if( (st->discard >= AVDISCARD_DEFAULT && size==0)
00774
00775 || st->discard >= AVDISCARD_ALL){
00776 if(ast->sample_size) ast->frame_offset += pkt->size;
00777 else ast->frame_offset++;
00778 url_fskip(pb, size);
00779 goto resync;
00780 }
00781
00782 if( ((ast->prefix_count<5 || sync+9 > i) && d[2]<128 && d[3]<128) ||
00783 d[2]*256+d[3] == ast->prefix
00784
00785 ) {
00786
00787
00788 if(d[2]*256+d[3] == ast->prefix)
00789 ast->prefix_count++;
00790 else{
00791 ast->prefix= d[2]*256+d[3];
00792 ast->prefix_count= 0;
00793 }
00794
00795 avi->stream_index= n;
00796 ast->packet_size= size + 8;
00797 ast->remaining= size;
00798
00799 {
00800 uint64_t pos= url_ftell(pb) - 8;
00801 if(!st->index_entries || !st->nb_index_entries || st->index_entries[st->nb_index_entries - 1].pos < pos){
00802 av_add_index_entry(st, pos, ast->frame_offset / FFMAX(1, ast->sample_size), size, 0, AVINDEX_KEYFRAME);
00803 }
00804 }
00805 goto resync;
00806 }
00807 }
00808
00809 if ( d[0] >= '0' && d[0] <= '9'
00810 && d[1] >= '0' && d[1] <= '9'
00811 && ((d[2] == 'p' && d[3] == 'c'))
00812 && n < s->nb_streams && i + size <= avi->fsize) {
00813
00814 AVStream *st;
00815 int first, clr, flags, k, p;
00816
00817 st = s->streams[n];
00818
00819 first = get_byte(pb);
00820 clr = get_byte(pb);
00821 if(!clr)
00822 clr = 256;
00823 flags = get_le16(pb);
00824 p = 4;
00825 for (k = first; k < clr + first; k++) {
00826 int r, g, b;
00827 r = get_byte(pb);
00828 g = get_byte(pb);
00829 b = get_byte(pb);
00830 get_byte(pb);
00831 st->codec->palctrl->palette[k] = b + (g << 8) + (r << 16);
00832 }
00833 st->codec->palctrl->palette_changed = 1;
00834 goto resync;
00835 }
00836
00837 }
00838
00839 return -1;
00840 }
00841
00842
00843
00844 static int avi_read_idx1(AVFormatContext *s, int size)
00845 {
00846 AVIContext *avi = s->priv_data;
00847 ByteIOContext *pb = s->pb;
00848 int nb_index_entries, i;
00849 AVStream *st;
00850 AVIStream *ast;
00851 unsigned int index, tag, flags, pos, len;
00852 unsigned last_pos= -1;
00853
00854 nb_index_entries = size / 16;
00855 if (nb_index_entries <= 0)
00856 return -1;
00857
00858
00859 for(i = 0; i < nb_index_entries; i++) {
00860 tag = get_le32(pb);
00861 flags = get_le32(pb);
00862 pos = get_le32(pb);
00863 len = get_le32(pb);
00864 #if defined(DEBUG_SEEK)
00865 av_log(NULL, AV_LOG_DEBUG, "%d: tag=0x%x flags=0x%x pos=0x%x len=%d/",
00866 i, tag, flags, pos, len);
00867 #endif
00868 if(i==0 && pos > avi->movi_list)
00869 avi->movi_list= 0;
00870 pos += avi->movi_list;
00871
00872 index = ((tag & 0xff) - '0') * 10;
00873 index += ((tag >> 8) & 0xff) - '0';
00874 if (index >= s->nb_streams)
00875 continue;
00876 st = s->streams[index];
00877 ast = st->priv_data;
00878
00879 #if defined(DEBUG_SEEK)
00880 av_log(NULL, AV_LOG_DEBUG, "%d cum_len=%"PRId64"\n", len, ast->cum_len);
00881 #endif
00882 if(last_pos == pos)
00883 avi->non_interleaved= 1;
00884 else
00885 av_add_index_entry(st, pos, ast->cum_len / FFMAX(1, ast->sample_size), len, 0, (flags&AVIIF_INDEX) ? AVINDEX_KEYFRAME : 0);
00886 if(ast->sample_size)
00887 ast->cum_len += len;
00888 else
00889 ast->cum_len ++;
00890 last_pos= pos;
00891 }
00892 return 0;
00893 }
00894
00895 static int guess_ni_flag(AVFormatContext *s){
00896 int i;
00897 int64_t last_start=0;
00898 int64_t first_end= INT64_MAX;
00899
00900 for(i=0; i<s->nb_streams; i++){
00901 AVStream *st = s->streams[i];
00902 int n= st->nb_index_entries;
00903
00904 if(n <= 0)
00905 continue;
00906
00907 if(st->index_entries[0].pos > last_start)
00908 last_start= st->index_entries[0].pos;
00909 if(st->index_entries[n-1].pos < first_end)
00910 first_end= st->index_entries[n-1].pos;
00911 }
00912 return last_start > first_end;
00913 }
00914
00915 static int avi_load_index(AVFormatContext *s)
00916 {
00917 AVIContext *avi = s->priv_data;
00918 ByteIOContext *pb = s->pb;
00919 uint32_t tag, size;
00920 offset_t pos= url_ftell(pb);
00921
00922 url_fseek(pb, avi->movi_end, SEEK_SET);
00923 #ifdef DEBUG_SEEK
00924 printf("movi_end=0x%"PRIx64"\n", avi->movi_end);
00925 #endif
00926 for(;;) {
00927 if (url_feof(pb))
00928 break;
00929 tag = get_le32(pb);
00930 size = get_le32(pb);
00931 #ifdef DEBUG_SEEK
00932 printf("tag=%c%c%c%c size=0x%x\n",
00933 tag & 0xff,
00934 (tag >> 8) & 0xff,
00935 (tag >> 16) & 0xff,
00936 (tag >> 24) & 0xff,
00937 size);
00938 #endif
00939 switch(tag) {
00940 case MKTAG('i', 'd', 'x', '1'):
00941 if (avi_read_idx1(s, size) < 0)
00942 goto skip;
00943 else
00944 goto the_end;
00945 break;
00946 default:
00947 skip:
00948 size += (size & 1);
00949 url_fskip(pb, size);
00950 break;
00951 }
00952 }
00953 the_end:
00954 url_fseek(pb, pos, SEEK_SET);
00955 return 0;
00956 }
00957
00958 static int avi_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
00959 {
00960 AVIContext *avi = s->priv_data;
00961 AVStream *st;
00962 int i, index;
00963 int64_t pos;
00964
00965 if (!avi->index_loaded) {
00966
00967 avi_load_index(s);
00968 avi->index_loaded = 1;
00969 }
00970 assert(stream_index>= 0);
00971
00972 st = s->streams[stream_index];
00973 index= av_index_search_timestamp(st, timestamp, flags);
00974 if(index<0)
00975 return -1;
00976
00977
00978 pos = st->index_entries[index].pos;
00979 timestamp = st->index_entries[index].timestamp;
00980
00981
00982
00983 if (ENABLE_DV_DEMUXER && avi->dv_demux) {
00984
00985
00986
00987 assert(stream_index == 0);
00988
00989
00990
00991 dv_offset_reset(avi->dv_demux, timestamp);
00992
00993 url_fseek(s->pb, pos, SEEK_SET);
00994 avi->stream_index= -1;
00995 return 0;
00996 }
00997
00998 for(i = 0; i < s->nb_streams; i++) {
00999 AVStream *st2 = s->streams[i];
01000 AVIStream *ast2 = st2->priv_data;
01001
01002 ast2->packet_size=
01003 ast2->remaining= 0;
01004
01005 if (st2->nb_index_entries <= 0)
01006 continue;
01007
01008
01009 assert(st2->time_base.den == ast2->rate);
01010 assert(st2->time_base.num == ast2->scale);
01011 index = av_index_search_timestamp(
01012 st2,
01013 av_rescale(timestamp, st2->time_base.den*(int64_t)st->time_base.num, st->time_base.den * (int64_t)st2->time_base.num),
01014 flags | AVSEEK_FLAG_BACKWARD);
01015 if(index<0)
01016 index=0;
01017
01018 if(!avi->non_interleaved){
01019 while(index>0 && st2->index_entries[index].pos > pos)
01020 index--;
01021 while(index+1 < st2->nb_index_entries && st2->index_entries[index].pos < pos)
01022 index++;
01023 }
01024
01025
01026
01027 ast2->frame_offset = st2->index_entries[index].timestamp;
01028 if(ast2->sample_size)
01029 ast2->frame_offset *=ast2->sample_size;
01030 }
01031
01032
01033 url_fseek(s->pb, pos, SEEK_SET);
01034 avi->stream_index= -1;
01035 return 0;
01036 }
01037
01038 static int avi_read_close(AVFormatContext *s)
01039 {
01040 int i;
01041 AVIContext *avi = s->priv_data;
01042
01043 for(i=0;i<s->nb_streams;i++) {
01044 AVStream *st = s->streams[i];
01045 AVIStream *ast = st->priv_data;
01046 av_free(ast);
01047 av_free(st->codec->palctrl);
01048 }
01049
01050 if (avi->dv_demux)
01051 av_free(avi->dv_demux);
01052
01053 return 0;
01054 }
01055
01056 static int avi_probe(AVProbeData *p)
01057 {
01058 int i;
01059
01060
01061 for(i=0; avi_headers[i][0]; i++)
01062 if(!memcmp(p->buf , avi_headers[i] , 4) &&
01063 !memcmp(p->buf+8, avi_headers[i]+4, 4))
01064 return AVPROBE_SCORE_MAX;
01065
01066 return 0;
01067 }
01068
01069 AVInputFormat avi_demuxer = {
01070 "avi",
01071 "avi format",
01072 sizeof(AVIContext),
01073 avi_probe,
01074 avi_read_header,
01075 avi_read_packet,
01076 avi_read_close,
01077 avi_read_seek,
01078 };