Libav 0.7.1
|
00001 /* 00002 * AVI demuxer 00003 * Copyright (c) 2001 Fabrice Bellard 00004 * 00005 * This file is part of Libav. 00006 * 00007 * Libav is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * Libav is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with Libav; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 */ 00021 00022 #include <strings.h> 00023 #include "libavutil/intreadwrite.h" 00024 #include "libavutil/bswap.h" 00025 #include "libavutil/dict.h" 00026 #include "avformat.h" 00027 #include "avi.h" 00028 #include "dv.h" 00029 #include "riff.h" 00030 00031 #undef NDEBUG 00032 #include <assert.h> 00033 00034 typedef struct AVIStream { 00035 int64_t frame_offset; /* current frame (video) or byte (audio) counter 00036 (used to compute the pts) */ 00037 int remaining; 00038 int packet_size; 00039 00040 int scale; 00041 int rate; 00042 int sample_size; /* size of one sample (or packet) (in the rate/scale sense) in bytes */ 00043 00044 int64_t cum_len; /* temporary storage (used during seek) */ 00045 00046 int prefix; 00047 int prefix_count; 00048 uint32_t pal[256]; 00049 int has_pal; 00050 int dshow_block_align; 00051 00052 AVFormatContext *sub_ctx; 00053 AVPacket sub_pkt; 00054 uint8_t *sub_buffer; 00055 } AVIStream; 00056 00057 typedef struct { 00058 int64_t riff_end; 00059 int64_t movi_end; 00060 int64_t fsize; 00061 int64_t movi_list; 00062 int64_t last_pkt_pos; 00063 int index_loaded; 00064 int is_odml; 00065 int non_interleaved; 00066 int stream_index; 00067 DVDemuxContext* dv_demux; 00068 int odml_depth; 00069 #define MAX_ODML_DEPTH 1000 00070 } AVIContext; 00071 00072 static const char avi_headers[][8] = { 00073 { 'R', 'I', 'F', 'F', 'A', 'V', 'I', ' ' }, 00074 { 'R', 'I', 'F', 'F', 'A', 'V', 'I', 'X' }, 00075 { 'R', 'I', 'F', 'F', 'A', 'V', 'I', 0x19}, 00076 { 'O', 'N', '2', ' ', 'O', 'N', '2', 'f' }, 00077 { 'R', 'I', 'F', 'F', 'A', 'M', 'V', ' ' }, 00078 { 0 } 00079 }; 00080 00081 static int avi_load_index(AVFormatContext *s); 00082 static int guess_ni_flag(AVFormatContext *s); 00083 00084 #define print_tag(str, tag, size) \ 00085 av_dlog(NULL, "%s: tag=%c%c%c%c size=0x%x\n", \ 00086 str, tag & 0xff, \ 00087 (tag >> 8) & 0xff, \ 00088 (tag >> 16) & 0xff, \ 00089 (tag >> 24) & 0xff, \ 00090 size) 00091 00092 static inline int get_duration(AVIStream *ast, int len){ 00093 if(ast->sample_size){ 00094 return len; 00095 }else if (ast->dshow_block_align){ 00096 return (len + ast->dshow_block_align - 1)/ast->dshow_block_align; 00097 }else 00098 return 1; 00099 } 00100 00101 static int get_riff(AVFormatContext *s, AVIOContext *pb) 00102 { 00103 AVIContext *avi = s->priv_data; 00104 char header[8]; 00105 int i; 00106 00107 /* check RIFF header */ 00108 avio_read(pb, header, 4); 00109 avi->riff_end = avio_rl32(pb); /* RIFF chunk size */ 00110 avi->riff_end += avio_tell(pb); /* RIFF chunk end */ 00111 avio_read(pb, header+4, 4); 00112 00113 for(i=0; avi_headers[i][0]; i++) 00114 if(!memcmp(header, avi_headers[i], 8)) 00115 break; 00116 if(!avi_headers[i][0]) 00117 return -1; 00118 00119 if(header[7] == 0x19) 00120 av_log(s, AV_LOG_INFO, "This file has been generated by a totally broken muxer.\n"); 00121 00122 return 0; 00123 } 00124 00125 static int read_braindead_odml_indx(AVFormatContext *s, int frame_num){ 00126 AVIContext *avi = s->priv_data; 00127 AVIOContext *pb = s->pb; 00128 int longs_pre_entry= avio_rl16(pb); 00129 int index_sub_type = avio_r8(pb); 00130 int index_type = avio_r8(pb); 00131 int entries_in_use = avio_rl32(pb); 00132 int chunk_id = avio_rl32(pb); 00133 int64_t base = avio_rl64(pb); 00134 int stream_id= 10*((chunk_id&0xFF) - '0') + (((chunk_id>>8)&0xFF) - '0'); 00135 AVStream *st; 00136 AVIStream *ast; 00137 int i; 00138 int64_t last_pos= -1; 00139 int64_t filesize= avio_size(s->pb); 00140 00141 av_dlog(s, "longs_pre_entry:%d index_type:%d entries_in_use:%d chunk_id:%X base:%16"PRIX64"\n", 00142 longs_pre_entry,index_type, entries_in_use, chunk_id, base); 00143 00144 if(stream_id >= s->nb_streams || stream_id < 0) 00145 return -1; 00146 st= s->streams[stream_id]; 00147 ast = st->priv_data; 00148 00149 if(index_sub_type) 00150 return -1; 00151 00152 avio_rl32(pb); 00153 00154 if(index_type && longs_pre_entry != 2) 00155 return -1; 00156 if(index_type>1) 00157 return -1; 00158 00159 if(filesize > 0 && base >= filesize){ 00160 av_log(s, AV_LOG_ERROR, "ODML index invalid\n"); 00161 if(base>>32 == (base & 0xFFFFFFFF) && (base & 0xFFFFFFFF) < filesize && filesize <= 0xFFFFFFFF) 00162 base &= 0xFFFFFFFF; 00163 else 00164 return -1; 00165 } 00166 00167 for(i=0; i<entries_in_use; i++){ 00168 if(index_type){ 00169 int64_t pos= avio_rl32(pb) + base - 8; 00170 int len = avio_rl32(pb); 00171 int key= len >= 0; 00172 len &= 0x7FFFFFFF; 00173 00174 av_dlog(s, "pos:%"PRId64", len:%X\n", pos, len); 00175 00176 if(pb->eof_reached) 00177 return -1; 00178 00179 if(last_pos == pos || pos == base - 8) 00180 avi->non_interleaved= 1; 00181 if(last_pos != pos && (len || !ast->sample_size)) 00182 av_add_index_entry(st, pos, ast->cum_len, len, 0, key ? AVINDEX_KEYFRAME : 0); 00183 00184 ast->cum_len += get_duration(ast, len); 00185 last_pos= pos; 00186 }else{ 00187 int64_t offset, pos; 00188 int duration; 00189 offset = avio_rl64(pb); 00190 avio_rl32(pb); /* size */ 00191 duration = avio_rl32(pb); 00192 00193 if(pb->eof_reached) 00194 return -1; 00195 00196 pos = avio_tell(pb); 00197 00198 if(avi->odml_depth > MAX_ODML_DEPTH){ 00199 av_log(s, AV_LOG_ERROR, "Too deeply nested ODML indexes\n"); 00200 return -1; 00201 } 00202 00203 avio_seek(pb, offset+8, SEEK_SET); 00204 avi->odml_depth++; 00205 read_braindead_odml_indx(s, frame_num); 00206 avi->odml_depth--; 00207 frame_num += duration; 00208 00209 avio_seek(pb, pos, SEEK_SET); 00210 } 00211 } 00212 avi->index_loaded=1; 00213 return 0; 00214 } 00215 00216 static void clean_index(AVFormatContext *s){ 00217 int i; 00218 int64_t j; 00219 00220 for(i=0; i<s->nb_streams; i++){ 00221 AVStream *st = s->streams[i]; 00222 AVIStream *ast = st->priv_data; 00223 int n= st->nb_index_entries; 00224 int max= ast->sample_size; 00225 int64_t pos, size, ts; 00226 00227 if(n != 1 || ast->sample_size==0) 00228 continue; 00229 00230 while(max < 1024) max+=max; 00231 00232 pos= st->index_entries[0].pos; 00233 size= st->index_entries[0].size; 00234 ts= st->index_entries[0].timestamp; 00235 00236 for(j=0; j<size; j+=max){ 00237 av_add_index_entry(st, pos+j, ts+j, FFMIN(max, size-j), 0, AVINDEX_KEYFRAME); 00238 } 00239 } 00240 } 00241 00242 static int avi_read_tag(AVFormatContext *s, AVStream *st, uint32_t tag, uint32_t size) 00243 { 00244 AVIOContext *pb = s->pb; 00245 char key[5] = {0}, *value; 00246 00247 size += (size & 1); 00248 00249 if (size == UINT_MAX) 00250 return -1; 00251 value = av_malloc(size+1); 00252 if (!value) 00253 return -1; 00254 avio_read(pb, value, size); 00255 value[size]=0; 00256 00257 AV_WL32(key, tag); 00258 00259 return av_dict_set(st ? &st->metadata : &s->metadata, key, value, 00260 AV_DICT_DONT_STRDUP_VAL); 00261 } 00262 00263 static void avi_read_info(AVFormatContext *s, uint64_t end) 00264 { 00265 while (avio_tell(s->pb) < end) { 00266 uint32_t tag = avio_rl32(s->pb); 00267 uint32_t size = avio_rl32(s->pb); 00268 avi_read_tag(s, NULL, tag, size); 00269 } 00270 } 00271 00272 static const char months[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", 00273 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; 00274 00275 static void avi_metadata_creation_time(AVDictionary **metadata, char *date) 00276 { 00277 char month[4], time[9], buffer[64]; 00278 int i, day, year; 00279 /* parse standard AVI date format (ie. "Mon Mar 10 15:04:43 2003") */ 00280 if (sscanf(date, "%*3s%*[ ]%3s%*[ ]%2d%*[ ]%8s%*[ ]%4d", 00281 month, &day, time, &year) == 4) { 00282 for (i=0; i<12; i++) 00283 if (!strcasecmp(month, months[i])) { 00284 snprintf(buffer, sizeof(buffer), "%.4d-%.2d-%.2d %s", 00285 year, i+1, day, time); 00286 av_dict_set(metadata, "creation_time", buffer, 0); 00287 } 00288 } else if (date[4] == '/' && date[7] == '/') { 00289 date[4] = date[7] = '-'; 00290 av_dict_set(metadata, "creation_time", date, 0); 00291 } 00292 } 00293 00294 static void avi_read_nikon(AVFormatContext *s, uint64_t end) 00295 { 00296 while (avio_tell(s->pb) < end) { 00297 uint32_t tag = avio_rl32(s->pb); 00298 uint32_t size = avio_rl32(s->pb); 00299 switch (tag) { 00300 case MKTAG('n', 'c', 't', 'g'): { /* Nikon Tags */ 00301 uint64_t tag_end = avio_tell(s->pb) + size; 00302 while (avio_tell(s->pb) < tag_end) { 00303 uint16_t tag = avio_rl16(s->pb); 00304 uint16_t size = avio_rl16(s->pb); 00305 const char *name = NULL; 00306 char buffer[64] = {0}; 00307 size -= avio_read(s->pb, buffer, 00308 FFMIN(size, sizeof(buffer)-1)); 00309 switch (tag) { 00310 case 0x03: name = "maker"; break; 00311 case 0x04: name = "model"; break; 00312 case 0x13: name = "creation_time"; 00313 if (buffer[4] == ':' && buffer[7] == ':') 00314 buffer[4] = buffer[7] = '-'; 00315 break; 00316 } 00317 if (name) 00318 av_dict_set(&s->metadata, name, buffer, 0); 00319 avio_skip(s->pb, size); 00320 } 00321 break; 00322 } 00323 default: 00324 avio_skip(s->pb, size); 00325 break; 00326 } 00327 } 00328 } 00329 00330 static int avi_read_header(AVFormatContext *s, AVFormatParameters *ap) 00331 { 00332 AVIContext *avi = s->priv_data; 00333 AVIOContext *pb = s->pb; 00334 unsigned int tag, tag1, handler; 00335 int codec_type, stream_index, frame_period; 00336 unsigned int size; 00337 int i; 00338 AVStream *st; 00339 AVIStream *ast = NULL; 00340 int avih_width=0, avih_height=0; 00341 int amv_file_format=0; 00342 uint64_t list_end = 0; 00343 int ret; 00344 00345 avi->stream_index= -1; 00346 00347 if (get_riff(s, pb) < 0) 00348 return -1; 00349 00350 avi->fsize = avio_size(pb); 00351 if(avi->fsize<=0) 00352 avi->fsize= avi->riff_end == 8 ? INT64_MAX : avi->riff_end; 00353 00354 /* first list tag */ 00355 stream_index = -1; 00356 codec_type = -1; 00357 frame_period = 0; 00358 for(;;) { 00359 if (pb->eof_reached) 00360 goto fail; 00361 tag = avio_rl32(pb); 00362 size = avio_rl32(pb); 00363 00364 print_tag("tag", tag, size); 00365 00366 switch(tag) { 00367 case MKTAG('L', 'I', 'S', 'T'): 00368 list_end = avio_tell(pb) + size; 00369 /* Ignored, except at start of video packets. */ 00370 tag1 = avio_rl32(pb); 00371 00372 print_tag("list", tag1, 0); 00373 00374 if (tag1 == MKTAG('m', 'o', 'v', 'i')) { 00375 avi->movi_list = avio_tell(pb) - 4; 00376 if(size) avi->movi_end = avi->movi_list + size + (size & 1); 00377 else avi->movi_end = avio_size(pb); 00378 av_dlog(NULL, "movi end=%"PRIx64"\n", avi->movi_end); 00379 goto end_of_header; 00380 } 00381 else if (tag1 == MKTAG('I', 'N', 'F', 'O')) 00382 avi_read_info(s, list_end); 00383 else if (tag1 == MKTAG('n', 'c', 'd', 't')) 00384 avi_read_nikon(s, list_end); 00385 00386 break; 00387 case MKTAG('I', 'D', 'I', 'T'): { 00388 unsigned char date[64] = {0}; 00389 size += (size & 1); 00390 size -= avio_read(pb, date, FFMIN(size, sizeof(date)-1)); 00391 avio_skip(pb, size); 00392 avi_metadata_creation_time(&s->metadata, date); 00393 break; 00394 } 00395 case MKTAG('d', 'm', 'l', 'h'): 00396 avi->is_odml = 1; 00397 avio_skip(pb, size + (size & 1)); 00398 break; 00399 case MKTAG('a', 'm', 'v', 'h'): 00400 amv_file_format=1; 00401 case MKTAG('a', 'v', 'i', 'h'): 00402 /* AVI header */ 00403 /* using frame_period is bad idea */ 00404 frame_period = avio_rl32(pb); 00405 avio_skip(pb, 4); 00406 avio_rl32(pb); 00407 avi->non_interleaved |= avio_rl32(pb) & AVIF_MUSTUSEINDEX; 00408 00409 avio_skip(pb, 2 * 4); 00410 avio_rl32(pb); 00411 avio_rl32(pb); 00412 avih_width=avio_rl32(pb); 00413 avih_height=avio_rl32(pb); 00414 00415 avio_skip(pb, size - 10 * 4); 00416 break; 00417 case MKTAG('s', 't', 'r', 'h'): 00418 /* stream header */ 00419 00420 tag1 = avio_rl32(pb); 00421 handler = avio_rl32(pb); /* codec tag */ 00422 00423 if(tag1 == MKTAG('p', 'a', 'd', 's')){ 00424 avio_skip(pb, size - 8); 00425 break; 00426 }else{ 00427 stream_index++; 00428 st = av_new_stream(s, stream_index); 00429 if (!st) 00430 goto fail; 00431 00432 ast = av_mallocz(sizeof(AVIStream)); 00433 if (!ast) 00434 goto fail; 00435 st->priv_data = ast; 00436 } 00437 if(amv_file_format) 00438 tag1 = stream_index ? MKTAG('a','u','d','s') : MKTAG('v','i','d','s'); 00439 00440 print_tag("strh", tag1, -1); 00441 00442 if(tag1 == MKTAG('i', 'a', 'v', 's') || tag1 == MKTAG('i', 'v', 'a', 's')){ 00443 int64_t dv_dur; 00444 00445 /* 00446 * After some consideration -- I don't think we 00447 * have to support anything but DV in type1 AVIs. 00448 */ 00449 if (s->nb_streams != 1) 00450 goto fail; 00451 00452 if (handler != MKTAG('d', 'v', 's', 'd') && 00453 handler != MKTAG('d', 'v', 'h', 'd') && 00454 handler != MKTAG('d', 'v', 's', 'l')) 00455 goto fail; 00456 00457 ast = s->streams[0]->priv_data; 00458 av_freep(&s->streams[0]->codec->extradata); 00459 av_freep(&s->streams[0]->codec); 00460 av_freep(&s->streams[0]); 00461 s->nb_streams = 0; 00462 if (CONFIG_DV_DEMUXER) { 00463 avi->dv_demux = dv_init_demux(s); 00464 if (!avi->dv_demux) 00465 goto fail; 00466 } 00467 s->streams[0]->priv_data = ast; 00468 avio_skip(pb, 3 * 4); 00469 ast->scale = avio_rl32(pb); 00470 ast->rate = avio_rl32(pb); 00471 avio_skip(pb, 4); /* start time */ 00472 00473 dv_dur = avio_rl32(pb); 00474 if (ast->scale > 0 && ast->rate > 0 && dv_dur > 0) { 00475 dv_dur *= AV_TIME_BASE; 00476 s->duration = av_rescale(dv_dur, ast->scale, ast->rate); 00477 } 00478 /* 00479 * else, leave duration alone; timing estimation in utils.c 00480 * will make a guess based on bitrate. 00481 */ 00482 00483 stream_index = s->nb_streams - 1; 00484 avio_skip(pb, size - 9*4); 00485 break; 00486 } 00487 00488 assert(stream_index < s->nb_streams); 00489 st->codec->stream_codec_tag= handler; 00490 00491 avio_rl32(pb); /* flags */ 00492 avio_rl16(pb); /* priority */ 00493 avio_rl16(pb); /* language */ 00494 avio_rl32(pb); /* initial frame */ 00495 ast->scale = avio_rl32(pb); 00496 ast->rate = avio_rl32(pb); 00497 if(!(ast->scale && ast->rate)){ 00498 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); 00499 if(frame_period){ 00500 ast->rate = 1000000; 00501 ast->scale = frame_period; 00502 }else{ 00503 ast->rate = 25; 00504 ast->scale = 1; 00505 } 00506 } 00507 av_set_pts_info(st, 64, ast->scale, ast->rate); 00508 00509 ast->cum_len=avio_rl32(pb); /* start */ 00510 st->nb_frames = avio_rl32(pb); 00511 00512 st->start_time = 0; 00513 avio_rl32(pb); /* buffer size */ 00514 avio_rl32(pb); /* quality */ 00515 ast->sample_size = avio_rl32(pb); /* sample ssize */ 00516 ast->cum_len *= FFMAX(1, ast->sample_size); 00517 // av_log(s, AV_LOG_DEBUG, "%d %d %d %d\n", ast->rate, ast->scale, ast->start, ast->sample_size); 00518 00519 switch(tag1) { 00520 case MKTAG('v', 'i', 'd', 's'): 00521 codec_type = AVMEDIA_TYPE_VIDEO; 00522 00523 ast->sample_size = 0; 00524 break; 00525 case MKTAG('a', 'u', 'd', 's'): 00526 codec_type = AVMEDIA_TYPE_AUDIO; 00527 break; 00528 case MKTAG('t', 'x', 't', 's'): 00529 codec_type = AVMEDIA_TYPE_SUBTITLE; 00530 break; 00531 case MKTAG('d', 'a', 't', 's'): 00532 codec_type = AVMEDIA_TYPE_DATA; 00533 break; 00534 default: 00535 av_log(s, AV_LOG_ERROR, "unknown stream type %X\n", tag1); 00536 goto fail; 00537 } 00538 if(ast->sample_size == 0) 00539 st->duration = st->nb_frames; 00540 ast->frame_offset= ast->cum_len; 00541 avio_skip(pb, size - 12 * 4); 00542 break; 00543 case MKTAG('s', 't', 'r', 'f'): 00544 /* stream header */ 00545 if (stream_index >= (unsigned)s->nb_streams || avi->dv_demux) { 00546 avio_skip(pb, size); 00547 } else { 00548 uint64_t cur_pos = avio_tell(pb); 00549 if (cur_pos < list_end) 00550 size = FFMIN(size, list_end - cur_pos); 00551 st = s->streams[stream_index]; 00552 switch(codec_type) { 00553 case AVMEDIA_TYPE_VIDEO: 00554 if(amv_file_format){ 00555 st->codec->width=avih_width; 00556 st->codec->height=avih_height; 00557 st->codec->codec_type = AVMEDIA_TYPE_VIDEO; 00558 st->codec->codec_id = CODEC_ID_AMV; 00559 avio_skip(pb, size); 00560 break; 00561 } 00562 tag1 = ff_get_bmp_header(pb, st); 00563 00564 if (tag1 == MKTAG('D', 'X', 'S', 'B') || tag1 == MKTAG('D','X','S','A')) { 00565 st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE; 00566 st->codec->codec_tag = tag1; 00567 st->codec->codec_id = CODEC_ID_XSUB; 00568 break; 00569 } 00570 00571 if(size > 10*4 && size<(1<<30)){ 00572 st->codec->extradata_size= size - 10*4; 00573 st->codec->extradata= av_malloc(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE); 00574 if (!st->codec->extradata) { 00575 st->codec->extradata_size= 0; 00576 return AVERROR(ENOMEM); 00577 } 00578 avio_read(pb, st->codec->extradata, st->codec->extradata_size); 00579 } 00580 00581 if(st->codec->extradata_size & 1) //FIXME check if the encoder really did this correctly 00582 avio_r8(pb); 00583 00584 /* Extract palette from extradata if bpp <= 8. */ 00585 /* This code assumes that extradata contains only palette. */ 00586 /* This is true for all paletted codecs implemented in Libav. */ 00587 if (st->codec->extradata_size && (st->codec->bits_per_coded_sample <= 8)) { 00588 int pal_size = (1 << st->codec->bits_per_coded_sample) << 2; 00589 const uint8_t *pal_src; 00590 00591 pal_size = FFMIN(pal_size, st->codec->extradata_size); 00592 pal_src = st->codec->extradata + st->codec->extradata_size - pal_size; 00593 #if HAVE_BIGENDIAN 00594 for (i = 0; i < pal_size/4; i++) 00595 ast->pal[i] = av_bswap32(((uint32_t*)pal_src)[i]); 00596 #else 00597 memcpy(ast->pal, pal_src, pal_size); 00598 #endif 00599 ast->has_pal = 1; 00600 } 00601 00602 print_tag("video", tag1, 0); 00603 00604 st->codec->codec_type = AVMEDIA_TYPE_VIDEO; 00605 st->codec->codec_tag = tag1; 00606 st->codec->codec_id = ff_codec_get_id(ff_codec_bmp_tags, tag1); 00607 st->need_parsing = AVSTREAM_PARSE_HEADERS; // This is needed to get the pict type which is necessary for generating correct pts. 00608 // Support "Resolution 1:1" for Avid AVI Codec 00609 if(tag1 == MKTAG('A', 'V', 'R', 'n') && 00610 st->codec->extradata_size >= 31 && 00611 !memcmp(&st->codec->extradata[28], "1:1", 3)) 00612 st->codec->codec_id = CODEC_ID_RAWVIDEO; 00613 00614 if(st->codec->codec_tag==0 && st->codec->height > 0 && st->codec->extradata_size < 1U<<30){ 00615 st->codec->extradata_size+= 9; 00616 st->codec->extradata= av_realloc(st->codec->extradata, st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE); 00617 if(st->codec->extradata) 00618 memcpy(st->codec->extradata + st->codec->extradata_size - 9, "BottomUp", 9); 00619 } 00620 st->codec->height= FFABS(st->codec->height); 00621 00622 // avio_skip(pb, size - 5 * 4); 00623 break; 00624 case AVMEDIA_TYPE_AUDIO: 00625 ret = ff_get_wav_header(pb, st->codec, size); 00626 if (ret < 0) 00627 return ret; 00628 ast->dshow_block_align= st->codec->block_align; 00629 if(ast->sample_size && st->codec->block_align && ast->sample_size != st->codec->block_align){ 00630 av_log(s, AV_LOG_WARNING, "sample size (%d) != block align (%d)\n", ast->sample_size, st->codec->block_align); 00631 ast->sample_size= st->codec->block_align; 00632 } 00633 if (size&1) /* 2-aligned (fix for Stargate SG-1 - 3x18 - Shades of Grey.avi) */ 00634 avio_skip(pb, 1); 00635 /* Force parsing as several audio frames can be in 00636 * one packet and timestamps refer to packet start. */ 00637 st->need_parsing = AVSTREAM_PARSE_TIMESTAMPS; 00638 /* ADTS header is in extradata, AAC without header must be 00639 * stored as exact frames. Parser not needed and it will 00640 * fail. */ 00641 if (st->codec->codec_id == CODEC_ID_AAC && st->codec->extradata_size) 00642 st->need_parsing = AVSTREAM_PARSE_NONE; 00643 /* AVI files with Xan DPCM audio (wrongly) declare PCM 00644 * audio in the header but have Axan as stream_code_tag. */ 00645 if (st->codec->stream_codec_tag == AV_RL32("Axan")){ 00646 st->codec->codec_id = CODEC_ID_XAN_DPCM; 00647 st->codec->codec_tag = 0; 00648 } 00649 if (amv_file_format){ 00650 st->codec->codec_id = CODEC_ID_ADPCM_IMA_AMV; 00651 ast->dshow_block_align = 0; 00652 } 00653 break; 00654 case AVMEDIA_TYPE_SUBTITLE: 00655 st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE; 00656 st->codec->codec_id = CODEC_ID_PROBE; 00657 break; 00658 default: 00659 st->codec->codec_type = AVMEDIA_TYPE_DATA; 00660 st->codec->codec_id= CODEC_ID_NONE; 00661 st->codec->codec_tag= 0; 00662 avio_skip(pb, size); 00663 break; 00664 } 00665 } 00666 break; 00667 case MKTAG('i', 'n', 'd', 'x'): 00668 i= avio_tell(pb); 00669 if(pb->seekable && !(s->flags & AVFMT_FLAG_IGNIDX)){ 00670 read_braindead_odml_indx(s, 0); 00671 } 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); //nbFieldsPerFrame 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 //av_log(s, AV_LOG_ERROR, "vprp %d/%d %d/%d\n", active_aspect.num, active_aspect.den, active.num, active.den); 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 avi->movi_list = avio_tell(pb) - 4; 00709 avi->movi_end = avio_size(pb); 00710 goto end_of_header; 00711 } 00712 /* skip tag */ 00713 size += (size & 1); 00714 avio_skip(pb, size); 00715 break; 00716 } 00717 } 00718 end_of_header: 00719 /* check stream number */ 00720 if (stream_index != s->nb_streams - 1) { 00721 fail: 00722 return -1; 00723 } 00724 00725 if(!avi->index_loaded && pb->seekable) 00726 avi_load_index(s); 00727 avi->index_loaded = 1; 00728 avi->non_interleaved |= guess_ni_flag(s); 00729 for(i=0; i<s->nb_streams; i++){ 00730 AVStream *st = s->streams[i]; 00731 if(st->nb_index_entries) 00732 break; 00733 } 00734 if(i==s->nb_streams && avi->non_interleaved) { 00735 av_log(s, AV_LOG_WARNING, "non-interleaved AVI without index, switching to interleaved\n"); 00736 avi->non_interleaved=0; 00737 } 00738 00739 if(avi->non_interleaved) { 00740 av_log(s, AV_LOG_INFO, "non-interleaved AVI\n"); 00741 clean_index(s); 00742 } 00743 00744 ff_metadata_conv_ctx(s, NULL, ff_avi_metadata_conv); 00745 00746 return 0; 00747 } 00748 00749 static int read_gab2_sub(AVStream *st, AVPacket *pkt) { 00750 if (!strcmp(pkt->data, "GAB2") && AV_RL16(pkt->data+5) == 2) { 00751 uint8_t desc[256]; 00752 int score = AVPROBE_SCORE_MAX / 2, ret; 00753 AVIStream *ast = st->priv_data; 00754 AVInputFormat *sub_demuxer; 00755 AVRational time_base; 00756 AVIOContext *pb = avio_alloc_context( pkt->data + 7, 00757 pkt->size - 7, 00758 0, NULL, NULL, NULL, NULL); 00759 AVProbeData pd; 00760 unsigned int desc_len = avio_rl32(pb); 00761 00762 if (desc_len > pb->buf_end - pb->buf_ptr) 00763 goto error; 00764 00765 ret = avio_get_str16le(pb, desc_len, desc, sizeof(desc)); 00766 avio_skip(pb, desc_len - ret); 00767 if (*desc) 00768 av_dict_set(&st->metadata, "title", desc, 0); 00769 00770 avio_rl16(pb); /* flags? */ 00771 avio_rl32(pb); /* data size */ 00772 00773 pd = (AVProbeData) { .buf = pb->buf_ptr, .buf_size = pb->buf_end - pb->buf_ptr }; 00774 if (!(sub_demuxer = av_probe_input_format2(&pd, 1, &score))) 00775 goto error; 00776 00777 if (!(ast->sub_ctx = avformat_alloc_context())) 00778 goto error; 00779 00780 ast->sub_ctx->pb = pb; 00781 if (!avformat_open_input(&ast->sub_ctx, "", sub_demuxer, NULL)) { 00782 av_read_packet(ast->sub_ctx, &ast->sub_pkt); 00783 *st->codec = *ast->sub_ctx->streams[0]->codec; 00784 ast->sub_ctx->streams[0]->codec->extradata = NULL; 00785 time_base = ast->sub_ctx->streams[0]->time_base; 00786 av_set_pts_info(st, 64, time_base.num, time_base.den); 00787 } 00788 ast->sub_buffer = pkt->data; 00789 memset(pkt, 0, sizeof(*pkt)); 00790 return 1; 00791 error: 00792 av_freep(&pb); 00793 } 00794 return 0; 00795 } 00796 00797 static AVStream *get_subtitle_pkt(AVFormatContext *s, AVStream *next_st, 00798 AVPacket *pkt) 00799 { 00800 AVIStream *ast, *next_ast = next_st->priv_data; 00801 int64_t ts, next_ts, ts_min = INT64_MAX; 00802 AVStream *st, *sub_st = NULL; 00803 int i; 00804 00805 next_ts = av_rescale_q(next_ast->frame_offset, next_st->time_base, 00806 AV_TIME_BASE_Q); 00807 00808 for (i=0; i<s->nb_streams; i++) { 00809 st = s->streams[i]; 00810 ast = st->priv_data; 00811 if (st->discard < AVDISCARD_ALL && ast && ast->sub_pkt.data) { 00812 ts = av_rescale_q(ast->sub_pkt.dts, st->time_base, AV_TIME_BASE_Q); 00813 if (ts <= next_ts && ts < ts_min) { 00814 ts_min = ts; 00815 sub_st = st; 00816 } 00817 } 00818 } 00819 00820 if (sub_st) { 00821 ast = sub_st->priv_data; 00822 *pkt = ast->sub_pkt; 00823 pkt->stream_index = sub_st->index; 00824 if (av_read_packet(ast->sub_ctx, &ast->sub_pkt) < 0) 00825 ast->sub_pkt.data = NULL; 00826 } 00827 return sub_st; 00828 } 00829 00830 static int get_stream_idx(int *d){ 00831 if( d[0] >= '0' && d[0] <= '9' 00832 && d[1] >= '0' && d[1] <= '9'){ 00833 return (d[0] - '0') * 10 + (d[1] - '0'); 00834 }else{ 00835 return 100; //invalid stream ID 00836 } 00837 } 00838 00839 static int avi_read_packet(AVFormatContext *s, AVPacket *pkt) 00840 { 00841 AVIContext *avi = s->priv_data; 00842 AVIOContext *pb = s->pb; 00843 int n, d[8]; 00844 unsigned int size; 00845 int64_t i, sync; 00846 void* dstr; 00847 00848 if (CONFIG_DV_DEMUXER && avi->dv_demux) { 00849 int size = dv_get_packet(avi->dv_demux, pkt); 00850 if (size >= 0) 00851 return size; 00852 } 00853 00854 if(avi->non_interleaved){ 00855 int best_stream_index = 0; 00856 AVStream *best_st= NULL; 00857 AVIStream *best_ast; 00858 int64_t best_ts= INT64_MAX; 00859 int i; 00860 00861 for(i=0; i<s->nb_streams; i++){ 00862 AVStream *st = s->streams[i]; 00863 AVIStream *ast = st->priv_data; 00864 int64_t ts= ast->frame_offset; 00865 int64_t last_ts; 00866 00867 if(!st->nb_index_entries) 00868 continue; 00869 00870 last_ts = st->index_entries[st->nb_index_entries - 1].timestamp; 00871 if(!ast->remaining && ts > last_ts) 00872 continue; 00873 00874 ts = av_rescale_q(ts, st->time_base, (AVRational){FFMAX(1, ast->sample_size), AV_TIME_BASE}); 00875 00876 // av_log(s, AV_LOG_DEBUG, "%"PRId64" %d/%d %"PRId64"\n", ts, st->time_base.num, st->time_base.den, ast->frame_offset); 00877 if(ts < best_ts){ 00878 best_ts= ts; 00879 best_st= st; 00880 best_stream_index= i; 00881 } 00882 } 00883 if(!best_st) 00884 return -1; 00885 00886 best_ast = best_st->priv_data; 00887 best_ts = av_rescale_q(best_ts, (AVRational){FFMAX(1, best_ast->sample_size), AV_TIME_BASE}, best_st->time_base); 00888 if(best_ast->remaining) 00889 i= av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY | AVSEEK_FLAG_BACKWARD); 00890 else{ 00891 i= av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY); 00892 if(i>=0) 00893 best_ast->frame_offset= best_st->index_entries[i].timestamp; 00894 } 00895 00896 // av_log(s, AV_LOG_DEBUG, "%d\n", i); 00897 if(i>=0){ 00898 int64_t pos= best_st->index_entries[i].pos; 00899 pos += best_ast->packet_size - best_ast->remaining; 00900 avio_seek(s->pb, pos + 8, SEEK_SET); 00901 // av_log(s, AV_LOG_DEBUG, "pos=%"PRId64"\n", pos); 00902 00903 assert(best_ast->remaining <= best_ast->packet_size); 00904 00905 avi->stream_index= best_stream_index; 00906 if(!best_ast->remaining) 00907 best_ast->packet_size= 00908 best_ast->remaining= best_st->index_entries[i].size; 00909 } 00910 } 00911 00912 resync: 00913 if(avi->stream_index >= 0){ 00914 AVStream *st= s->streams[ avi->stream_index ]; 00915 AVIStream *ast= st->priv_data; 00916 int size, err; 00917 00918 if(get_subtitle_pkt(s, st, pkt)) 00919 return 0; 00920 00921 if(ast->sample_size <= 1) // minorityreport.AVI block_align=1024 sample_size=1 IMA-ADPCM 00922 size= INT_MAX; 00923 else if(ast->sample_size < 32) 00924 // arbitrary multiplier to avoid tiny packets for raw PCM data 00925 size= 1024*ast->sample_size; 00926 else 00927 size= ast->sample_size; 00928 00929 if(size > ast->remaining) 00930 size= ast->remaining; 00931 avi->last_pkt_pos= avio_tell(pb); 00932 err= av_get_packet(pb, pkt, size); 00933 if(err<0) 00934 return err; 00935 00936 if(ast->has_pal && pkt->data && pkt->size<(unsigned)INT_MAX/2){ 00937 uint8_t *pal; 00938 pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE); 00939 if(!pal){ 00940 av_log(s, AV_LOG_ERROR, "Failed to allocate data for palette\n"); 00941 }else{ 00942 memcpy(pal, ast->pal, AVPALETTE_SIZE); 00943 ast->has_pal = 0; 00944 } 00945 } 00946 00947 if (CONFIG_DV_DEMUXER && avi->dv_demux) { 00948 dstr = pkt->destruct; 00949 size = dv_produce_packet(avi->dv_demux, pkt, 00950 pkt->data, pkt->size); 00951 pkt->destruct = dstr; 00952 pkt->flags |= AV_PKT_FLAG_KEY; 00953 if (size < 0) 00954 av_free_packet(pkt); 00955 } else if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE 00956 && !st->codec->codec_tag && read_gab2_sub(st, pkt)) { 00957 ast->frame_offset++; 00958 avi->stream_index = -1; 00959 ast->remaining = 0; 00960 goto resync; 00961 } else { 00962 /* XXX: How to handle B-frames in AVI? */ 00963 pkt->dts = ast->frame_offset; 00964 // pkt->dts += ast->start; 00965 if(ast->sample_size) 00966 pkt->dts /= ast->sample_size; 00967 //av_log(s, AV_LOG_DEBUG, "dts:%"PRId64" offset:%"PRId64" %d/%d smpl_siz:%d base:%d st:%d size:%d\n", pkt->dts, ast->frame_offset, ast->scale, ast->rate, ast->sample_size, AV_TIME_BASE, avi->stream_index, size); 00968 pkt->stream_index = avi->stream_index; 00969 00970 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) { 00971 AVIndexEntry *e; 00972 int index; 00973 assert(st->index_entries); 00974 00975 index= av_index_search_timestamp(st, ast->frame_offset, 0); 00976 e= &st->index_entries[index]; 00977 00978 if(index >= 0 && e->timestamp == ast->frame_offset){ 00979 if (e->flags & AVINDEX_KEYFRAME) 00980 pkt->flags |= AV_PKT_FLAG_KEY; 00981 } 00982 } else { 00983 pkt->flags |= AV_PKT_FLAG_KEY; 00984 } 00985 ast->frame_offset += get_duration(ast, pkt->size); 00986 } 00987 ast->remaining -= err; 00988 if(!ast->remaining){ 00989 avi->stream_index= -1; 00990 ast->packet_size= 0; 00991 } 00992 00993 return size; 00994 } 00995 00996 memset(d, -1, sizeof(int)*8); 00997 for(i=sync=avio_tell(pb); !pb->eof_reached; i++) { 00998 int j; 00999 01000 for(j=0; j<7; j++) 01001 d[j]= d[j+1]; 01002 d[7]= avio_r8(pb); 01003 01004 size= d[4] + (d[5]<<8) + (d[6]<<16) + (d[7]<<24); 01005 01006 n= get_stream_idx(d+2); 01007 //av_log(s, AV_LOG_DEBUG, "%X %X %X %X %X %X %X %X %"PRId64" %d %d\n", d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7], i, size, n); 01008 if(i + (uint64_t)size > avi->fsize || d[0]<0) 01009 continue; 01010 01011 //parse ix## 01012 if( (d[0] == 'i' && d[1] == 'x' && n < s->nb_streams) 01013 //parse JUNK 01014 ||(d[0] == 'J' && d[1] == 'U' && d[2] == 'N' && d[3] == 'K') 01015 ||(d[0] == 'i' && d[1] == 'd' && d[2] == 'x' && d[3] == '1')){ 01016 avio_skip(pb, size); 01017 //av_log(s, AV_LOG_DEBUG, "SKIP\n"); 01018 goto resync; 01019 } 01020 01021 //parse stray LIST 01022 if(d[0] == 'L' && d[1] == 'I' && d[2] == 'S' && d[3] == 'T'){ 01023 avio_skip(pb, 4); 01024 goto resync; 01025 } 01026 01027 n= get_stream_idx(d); 01028 01029 if(!((i-avi->last_pkt_pos)&1) && get_stream_idx(d+1) < s->nb_streams) 01030 continue; 01031 01032 //detect ##ix chunk and skip 01033 if(d[2] == 'i' && d[3] == 'x' && n < s->nb_streams){ 01034 avio_skip(pb, size); 01035 goto resync; 01036 } 01037 01038 //parse ##dc/##wb 01039 if(n < s->nb_streams){ 01040 AVStream *st; 01041 AVIStream *ast; 01042 st = s->streams[n]; 01043 ast = st->priv_data; 01044 01045 if(s->nb_streams>=2){ 01046 AVStream *st1 = s->streams[1]; 01047 AVIStream *ast1= st1->priv_data; 01048 //workaround for broken small-file-bug402.avi 01049 if( d[2] == 'w' && d[3] == 'b' 01050 && n==0 01051 && st ->codec->codec_type == AVMEDIA_TYPE_VIDEO 01052 && st1->codec->codec_type == AVMEDIA_TYPE_AUDIO 01053 && ast->prefix == 'd'*256+'c' 01054 && (d[2]*256+d[3] == ast1->prefix || !ast1->prefix_count) 01055 ){ 01056 n=1; 01057 st = st1; 01058 ast = ast1; 01059 av_log(s, AV_LOG_WARNING, "Invalid stream + prefix combination, assuming audio.\n"); 01060 } 01061 } 01062 01063 01064 if( (st->discard >= AVDISCARD_DEFAULT && size==0) 01065 /*|| (st->discard >= AVDISCARD_NONKEY && !(pkt->flags & AV_PKT_FLAG_KEY))*/ //FIXME needs a little reordering 01066 || st->discard >= AVDISCARD_ALL){ 01067 ast->frame_offset += get_duration(ast, size); 01068 avio_skip(pb, size); 01069 goto resync; 01070 } 01071 01072 if (d[2] == 'p' && d[3] == 'c' && size<=4*256+4) { 01073 int k = avio_r8(pb); 01074 int last = (k + avio_r8(pb) - 1) & 0xFF; 01075 01076 avio_rl16(pb); //flags 01077 01078 for (; k <= last; k++) 01079 ast->pal[k] = avio_rb32(pb)>>8;// b + (g << 8) + (r << 16); 01080 ast->has_pal= 1; 01081 goto resync; 01082 } else if( ((ast->prefix_count<5 || sync+9 > i) && d[2]<128 && d[3]<128) || 01083 d[2]*256+d[3] == ast->prefix /*|| 01084 (d[2] == 'd' && d[3] == 'c') || 01085 (d[2] == 'w' && d[3] == 'b')*/) { 01086 01087 //av_log(s, AV_LOG_DEBUG, "OK\n"); 01088 if(d[2]*256+d[3] == ast->prefix) 01089 ast->prefix_count++; 01090 else{ 01091 ast->prefix= d[2]*256+d[3]; 01092 ast->prefix_count= 0; 01093 } 01094 01095 avi->stream_index= n; 01096 ast->packet_size= size + 8; 01097 ast->remaining= size; 01098 01099 if(size || !ast->sample_size){ 01100 uint64_t pos= avio_tell(pb) - 8; 01101 if(!st->index_entries || !st->nb_index_entries || st->index_entries[st->nb_index_entries - 1].pos < pos){ 01102 av_add_index_entry(st, pos, ast->frame_offset, size, 0, AVINDEX_KEYFRAME); 01103 } 01104 } 01105 goto resync; 01106 } 01107 } 01108 } 01109 01110 return AVERROR_EOF; 01111 } 01112 01113 /* XXX: We make the implicit supposition that the positions are sorted 01114 for each stream. */ 01115 static int avi_read_idx1(AVFormatContext *s, int size) 01116 { 01117 AVIContext *avi = s->priv_data; 01118 AVIOContext *pb = s->pb; 01119 int nb_index_entries, i; 01120 AVStream *st; 01121 AVIStream *ast; 01122 unsigned int index, tag, flags, pos, len; 01123 unsigned last_pos= -1; 01124 01125 nb_index_entries = size / 16; 01126 if (nb_index_entries <= 0) 01127 return -1; 01128 01129 /* Read the entries and sort them in each stream component. */ 01130 for(i = 0; i < nb_index_entries; i++) { 01131 tag = avio_rl32(pb); 01132 flags = avio_rl32(pb); 01133 pos = avio_rl32(pb); 01134 len = avio_rl32(pb); 01135 av_dlog(s, "%d: tag=0x%x flags=0x%x pos=0x%x len=%d/", 01136 i, tag, flags, pos, len); 01137 if(i==0 && pos > avi->movi_list) 01138 avi->movi_list= 0; //FIXME better check 01139 pos += avi->movi_list; 01140 01141 index = ((tag & 0xff) - '0') * 10; 01142 index += ((tag >> 8) & 0xff) - '0'; 01143 if (index >= s->nb_streams) 01144 continue; 01145 st = s->streams[index]; 01146 ast = st->priv_data; 01147 01148 av_dlog(s, "%d cum_len=%"PRId64"\n", len, ast->cum_len); 01149 01150 if(pb->eof_reached) 01151 return -1; 01152 01153 if(last_pos == pos) 01154 avi->non_interleaved= 1; 01155 else if(len || !ast->sample_size) 01156 av_add_index_entry(st, pos, ast->cum_len, len, 0, (flags&AVIIF_INDEX) ? AVINDEX_KEYFRAME : 0); 01157 ast->cum_len += get_duration(ast, len); 01158 last_pos= pos; 01159 } 01160 return 0; 01161 } 01162 01163 static int guess_ni_flag(AVFormatContext *s){ 01164 int i; 01165 int64_t last_start=0; 01166 int64_t first_end= INT64_MAX; 01167 int64_t oldpos= avio_tell(s->pb); 01168 01169 for(i=0; i<s->nb_streams; i++){ 01170 AVStream *st = s->streams[i]; 01171 int n= st->nb_index_entries; 01172 unsigned int size; 01173 01174 if(n <= 0) 01175 continue; 01176 01177 if(n >= 2){ 01178 int64_t pos= st->index_entries[0].pos; 01179 avio_seek(s->pb, pos + 4, SEEK_SET); 01180 size= avio_rl32(s->pb); 01181 if(pos + size > st->index_entries[1].pos) 01182 last_start= INT64_MAX; 01183 } 01184 01185 if(st->index_entries[0].pos > last_start) 01186 last_start= st->index_entries[0].pos; 01187 if(st->index_entries[n-1].pos < first_end) 01188 first_end= st->index_entries[n-1].pos; 01189 } 01190 avio_seek(s->pb, oldpos, SEEK_SET); 01191 return last_start > first_end; 01192 } 01193 01194 static int avi_load_index(AVFormatContext *s) 01195 { 01196 AVIContext *avi = s->priv_data; 01197 AVIOContext *pb = s->pb; 01198 uint32_t tag, size; 01199 int64_t pos= avio_tell(pb); 01200 int ret = -1; 01201 01202 if (avio_seek(pb, avi->movi_end, SEEK_SET) < 0) 01203 goto the_end; // maybe truncated file 01204 av_dlog(s, "movi_end=0x%"PRIx64"\n", avi->movi_end); 01205 for(;;) { 01206 if (pb->eof_reached) 01207 break; 01208 tag = avio_rl32(pb); 01209 size = avio_rl32(pb); 01210 av_dlog(s, "tag=%c%c%c%c size=0x%x\n", 01211 tag & 0xff, 01212 (tag >> 8) & 0xff, 01213 (tag >> 16) & 0xff, 01214 (tag >> 24) & 0xff, 01215 size); 01216 switch(tag) { 01217 case MKTAG('i', 'd', 'x', '1'): 01218 if (avi_read_idx1(s, size) < 0) 01219 goto skip; 01220 ret = 0; 01221 goto the_end; 01222 break; 01223 default: 01224 skip: 01225 size += (size & 1); 01226 if (avio_skip(pb, size) < 0) 01227 goto the_end; // something is wrong here 01228 break; 01229 } 01230 } 01231 the_end: 01232 avio_seek(pb, pos, SEEK_SET); 01233 return ret; 01234 } 01235 01236 static void seek_subtitle(AVStream *st, AVStream *st2, int64_t timestamp) 01237 { 01238 AVIStream *ast2 = st2->priv_data; 01239 int64_t ts2 = av_rescale_q(timestamp, st->time_base, st2->time_base); 01240 av_free_packet(&ast2->sub_pkt); 01241 if (avformat_seek_file(ast2->sub_ctx, 0, INT64_MIN, ts2, ts2, 0) >= 0 || 01242 avformat_seek_file(ast2->sub_ctx, 0, ts2, ts2, INT64_MAX, 0) >= 0) 01243 av_read_packet(ast2->sub_ctx, &ast2->sub_pkt); 01244 } 01245 01246 static int avi_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags) 01247 { 01248 AVIContext *avi = s->priv_data; 01249 AVStream *st; 01250 int i, index; 01251 int64_t pos; 01252 AVIStream *ast; 01253 01254 if (!avi->index_loaded) { 01255 /* we only load the index on demand */ 01256 avi_load_index(s); 01257 avi->index_loaded = 1; 01258 } 01259 assert(stream_index>= 0); 01260 01261 st = s->streams[stream_index]; 01262 ast= st->priv_data; 01263 index= av_index_search_timestamp(st, timestamp * FFMAX(ast->sample_size, 1), flags); 01264 if(index<0) 01265 return -1; 01266 01267 /* find the position */ 01268 pos = st->index_entries[index].pos; 01269 timestamp = st->index_entries[index].timestamp / FFMAX(ast->sample_size, 1); 01270 01271 // av_log(s, AV_LOG_DEBUG, "XX %"PRId64" %d %"PRId64"\n", timestamp, index, st->index_entries[index].timestamp); 01272 01273 if (CONFIG_DV_DEMUXER && avi->dv_demux) { 01274 /* One and only one real stream for DV in AVI, and it has video */ 01275 /* offsets. Calling with other stream indexes should have failed */ 01276 /* the av_index_search_timestamp call above. */ 01277 assert(stream_index == 0); 01278 01279 /* Feed the DV video stream version of the timestamp to the */ 01280 /* DV demux so it can synthesize correct timestamps. */ 01281 dv_offset_reset(avi->dv_demux, timestamp); 01282 01283 avio_seek(s->pb, pos, SEEK_SET); 01284 avi->stream_index= -1; 01285 return 0; 01286 } 01287 01288 for(i = 0; i < s->nb_streams; i++) { 01289 AVStream *st2 = s->streams[i]; 01290 AVIStream *ast2 = st2->priv_data; 01291 01292 ast2->packet_size= 01293 ast2->remaining= 0; 01294 01295 if (ast2->sub_ctx) { 01296 seek_subtitle(st, st2, timestamp); 01297 continue; 01298 } 01299 01300 if (st2->nb_index_entries <= 0) 01301 continue; 01302 01303 // assert(st2->codec->block_align); 01304 assert((int64_t)st2->time_base.num*ast2->rate == (int64_t)st2->time_base.den*ast2->scale); 01305 index = av_index_search_timestamp( 01306 st2, 01307 av_rescale_q(timestamp, st->time_base, st2->time_base) * FFMAX(ast2->sample_size, 1), 01308 flags | AVSEEK_FLAG_BACKWARD); 01309 if(index<0) 01310 index=0; 01311 01312 if(!avi->non_interleaved){ 01313 while(index>0 && st2->index_entries[index].pos > pos) 01314 index--; 01315 while(index+1 < st2->nb_index_entries && st2->index_entries[index].pos < pos) 01316 index++; 01317 } 01318 01319 // av_log(s, AV_LOG_DEBUG, "%"PRId64" %d %"PRId64"\n", timestamp, index, st2->index_entries[index].timestamp); 01320 /* extract the current frame number */ 01321 ast2->frame_offset = st2->index_entries[index].timestamp; 01322 } 01323 01324 /* do the seek */ 01325 avio_seek(s->pb, pos, SEEK_SET); 01326 avi->stream_index= -1; 01327 return 0; 01328 } 01329 01330 static int avi_read_close(AVFormatContext *s) 01331 { 01332 int i; 01333 AVIContext *avi = s->priv_data; 01334 01335 for(i=0;i<s->nb_streams;i++) { 01336 AVStream *st = s->streams[i]; 01337 AVIStream *ast = st->priv_data; 01338 if (ast) { 01339 if (ast->sub_ctx) { 01340 av_freep(&ast->sub_ctx->pb); 01341 av_close_input_file(ast->sub_ctx); 01342 } 01343 av_free(ast->sub_buffer); 01344 av_free_packet(&ast->sub_pkt); 01345 } 01346 } 01347 01348 av_free(avi->dv_demux); 01349 01350 return 0; 01351 } 01352 01353 static int avi_probe(AVProbeData *p) 01354 { 01355 int i; 01356 01357 /* check file header */ 01358 for(i=0; avi_headers[i][0]; i++) 01359 if(!memcmp(p->buf , avi_headers[i] , 4) && 01360 !memcmp(p->buf+8, avi_headers[i]+4, 4)) 01361 return AVPROBE_SCORE_MAX; 01362 01363 return 0; 01364 } 01365 01366 AVInputFormat ff_avi_demuxer = { 01367 "avi", 01368 NULL_IF_CONFIG_SMALL("AVI format"), 01369 sizeof(AVIContext), 01370 avi_probe, 01371 avi_read_header, 01372 avi_read_packet, 01373 avi_read_close, 01374 avi_read_seek, 01375 };