00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "libavutil/bswap.h"
00025 #include "libavutil/common.h"
00026 #include "libavutil/avstring.h"
00027 #include "libavutil/dict.h"
00028 #include "libavcodec/mpegaudio.h"
00029 #include "avformat.h"
00030 #include "avio_internal.h"
00031 #include "riff.h"
00032 #include "asf.h"
00033 #include "asfcrypt.h"
00034 #include "avlanguage.h"
00035
00036 typedef struct {
00037 int asfid2avid[128];
00038 ASFStream streams[128];
00039 uint32_t stream_bitrates[128];
00040 AVRational dar[128];
00041 char stream_languages[128][6];
00042
00043
00044 int packet_size_left;
00045
00046 uint64_t data_offset;
00047 uint64_t data_object_offset;
00048 uint64_t data_object_size;
00049 int index_read;
00050
00051 ASFMainHeader hdr;
00052
00053 int packet_flags;
00054 int packet_property;
00055 int packet_timestamp;
00056 int packet_segsizetype;
00057 int packet_segments;
00058 int packet_seq;
00059 int packet_replic_size;
00060 int packet_key_frame;
00061 int packet_padsize;
00062 unsigned int packet_frag_offset;
00063 unsigned int packet_frag_size;
00064 int64_t packet_frag_timestamp;
00065 int packet_multi_size;
00066 int packet_obj_size;
00067 int packet_time_delta;
00068 int packet_time_start;
00069 int64_t packet_pos;
00070
00071 int stream_index;
00072
00073 ASFStream* asf_st;
00074 } ASFContext;
00075
00076 #undef NDEBUG
00077 #include <assert.h>
00078
00079 #define ASF_MAX_STREAMS 127
00080 #define FRAME_HEADER_SIZE 17
00081
00082
00083 static const ff_asf_guid index_guid = {
00084 0x90, 0x08, 0x00, 0x33, 0xb1, 0xe5, 0xcf, 0x11, 0x89, 0xf4, 0x00, 0xa0, 0xc9, 0x03, 0x49, 0xcb
00085 };
00086
00087 static const ff_asf_guid stream_bitrate_guid = {
00088 0xce, 0x75, 0xf8, 0x7b, 0x8d, 0x46, 0xd1, 0x11, 0x8d, 0x82, 0x00, 0x60, 0x97, 0xc9, 0xa2, 0xb2
00089 };
00090
00091
00092
00093 #ifdef DEBUG
00094 #define PRINT_IF_GUID(g,cmp) \
00095 if (!ff_guidcmp(g, &cmp)) \
00096 av_dlog(NULL, "(GUID: %s) ", #cmp)
00097
00098 static void print_guid(const ff_asf_guid *g)
00099 {
00100 int i;
00101 PRINT_IF_GUID(g, ff_asf_header);
00102 else PRINT_IF_GUID(g, ff_asf_file_header);
00103 else PRINT_IF_GUID(g, ff_asf_stream_header);
00104 else PRINT_IF_GUID(g, ff_asf_audio_stream);
00105 else PRINT_IF_GUID(g, ff_asf_audio_conceal_none);
00106 else PRINT_IF_GUID(g, ff_asf_video_stream);
00107 else PRINT_IF_GUID(g, ff_asf_video_conceal_none);
00108 else PRINT_IF_GUID(g, ff_asf_command_stream);
00109 else PRINT_IF_GUID(g, ff_asf_comment_header);
00110 else PRINT_IF_GUID(g, ff_asf_codec_comment_header);
00111 else PRINT_IF_GUID(g, ff_asf_codec_comment1_header);
00112 else PRINT_IF_GUID(g, ff_asf_data_header);
00113 else PRINT_IF_GUID(g, index_guid);
00114 else PRINT_IF_GUID(g, ff_asf_head1_guid);
00115 else PRINT_IF_GUID(g, ff_asf_head2_guid);
00116 else PRINT_IF_GUID(g, ff_asf_my_guid);
00117 else PRINT_IF_GUID(g, ff_asf_ext_stream_header);
00118 else PRINT_IF_GUID(g, ff_asf_extended_content_header);
00119 else PRINT_IF_GUID(g, ff_asf_ext_stream_embed_stream_header);
00120 else PRINT_IF_GUID(g, ff_asf_ext_stream_audio_stream);
00121 else PRINT_IF_GUID(g, ff_asf_metadata_header);
00122 else PRINT_IF_GUID(g, ff_asf_marker_header);
00123 else PRINT_IF_GUID(g, stream_bitrate_guid);
00124 else PRINT_IF_GUID(g, ff_asf_language_guid);
00125 else
00126 av_dlog(NULL, "(GUID: unknown) ");
00127 for(i=0;i<16;i++)
00128 av_dlog(NULL, " 0x%02x,", (*g)[i]);
00129 av_dlog(NULL, "}\n");
00130 }
00131 #undef PRINT_IF_GUID
00132 #else
00133 #define print_guid(g)
00134 #endif
00135
00136 static int asf_probe(AVProbeData *pd)
00137 {
00138
00139 if (!ff_guidcmp(pd->buf, &ff_asf_header))
00140 return AVPROBE_SCORE_MAX;
00141 else
00142 return 0;
00143 }
00144
00145 static int get_value(AVIOContext *pb, int type){
00146 switch(type){
00147 case 2: return avio_rl32(pb);
00148 case 3: return avio_rl32(pb);
00149 case 4: return avio_rl64(pb);
00150 case 5: return avio_rl16(pb);
00151 default:return INT_MIN;
00152 }
00153 }
00154
00155 static void get_tag(AVFormatContext *s, const char *key, int type, int len)
00156 {
00157 char *value;
00158 int64_t off = avio_tell(s->pb);
00159
00160 if ((unsigned)len >= (UINT_MAX - 1)/2)
00161 return;
00162
00163 value = av_malloc(2*len+1);
00164 if (!value)
00165 goto finish;
00166
00167 if (type == 0) {
00168 avio_get_str16le(s->pb, len, value, 2*len + 1);
00169 } else if (type > 1 && type <= 5) {
00170 uint64_t num = get_value(s->pb, type);
00171 snprintf(value, len, "%"PRIu64, num);
00172 } else {
00173 av_log(s, AV_LOG_DEBUG, "Unsupported value type %d in tag %s.\n", type, key);
00174 goto finish;
00175 }
00176 if (*value)
00177 av_dict_set(&s->metadata, key, value, 0);
00178 finish:
00179 av_freep(&value);
00180 avio_seek(s->pb, off + len, SEEK_SET);
00181 }
00182
00183 static int asf_read_file_properties(AVFormatContext *s, int64_t size)
00184 {
00185 ASFContext *asf = s->priv_data;
00186 AVIOContext *pb = s->pb;
00187
00188 ff_get_guid(pb, &asf->hdr.guid);
00189 asf->hdr.file_size = avio_rl64(pb);
00190 asf->hdr.create_time = avio_rl64(pb);
00191 avio_rl64(pb);
00192 asf->hdr.play_time = avio_rl64(pb);
00193 asf->hdr.send_time = avio_rl64(pb);
00194 asf->hdr.preroll = avio_rl32(pb);
00195 asf->hdr.ignore = avio_rl32(pb);
00196 asf->hdr.flags = avio_rl32(pb);
00197 asf->hdr.min_pktsize = avio_rl32(pb);
00198 asf->hdr.max_pktsize = avio_rl32(pb);
00199 if (asf->hdr.min_pktsize >= (1U<<29))
00200 return AVERROR_INVALIDDATA;
00201 asf->hdr.max_bitrate = avio_rl32(pb);
00202 s->packet_size = asf->hdr.max_pktsize;
00203
00204 return 0;
00205 }
00206
00207 static int asf_read_stream_properties(AVFormatContext *s, int64_t size)
00208 {
00209 ASFContext *asf = s->priv_data;
00210 AVIOContext *pb = s->pb;
00211 AVStream *st;
00212 ASFStream *asf_st;
00213 ff_asf_guid g;
00214 enum AVMediaType type;
00215 int type_specific_size, sizeX;
00216 unsigned int tag1;
00217 int64_t pos1, pos2, start_time;
00218 int test_for_ext_stream_audio, is_dvr_ms_audio=0;
00219
00220 if (s->nb_streams == ASF_MAX_STREAMS) {
00221 av_log(s, AV_LOG_ERROR, "too many streams\n");
00222 return AVERROR(EINVAL);
00223 }
00224
00225 pos1 = avio_tell(pb);
00226
00227 st = av_new_stream(s, 0);
00228 if (!st)
00229 return AVERROR(ENOMEM);
00230 av_set_pts_info(st, 32, 1, 1000);
00231 asf_st = av_mallocz(sizeof(ASFStream));
00232 if (!asf_st)
00233 return AVERROR(ENOMEM);
00234 st->priv_data = asf_st;
00235 start_time = asf->hdr.preroll;
00236
00237 asf_st->stream_language_index = 128;
00238
00239 if(!(asf->hdr.flags & 0x01)) {
00240 st->duration = asf->hdr.play_time /
00241 (10000000 / 1000) - start_time;
00242 }
00243 ff_get_guid(pb, &g);
00244
00245 test_for_ext_stream_audio = 0;
00246 if (!ff_guidcmp(&g, &ff_asf_audio_stream)) {
00247 type = AVMEDIA_TYPE_AUDIO;
00248 } else if (!ff_guidcmp(&g, &ff_asf_video_stream)) {
00249 type = AVMEDIA_TYPE_VIDEO;
00250 } else if (!ff_guidcmp(&g, &ff_asf_jfif_media)) {
00251 type = AVMEDIA_TYPE_VIDEO;
00252 st->codec->codec_id = CODEC_ID_MJPEG;
00253 } else if (!ff_guidcmp(&g, &ff_asf_command_stream)) {
00254 type = AVMEDIA_TYPE_DATA;
00255 } else if (!ff_guidcmp(&g, &ff_asf_ext_stream_embed_stream_header)) {
00256 test_for_ext_stream_audio = 1;
00257 type = AVMEDIA_TYPE_UNKNOWN;
00258 } else {
00259 return -1;
00260 }
00261 ff_get_guid(pb, &g);
00262 avio_skip(pb, 8);
00263 type_specific_size = avio_rl32(pb);
00264 avio_rl32(pb);
00265 st->id = avio_rl16(pb) & 0x7f;
00266
00267 asf->asfid2avid[st->id] = s->nb_streams - 1;
00268
00269 avio_rl32(pb);
00270
00271 if (test_for_ext_stream_audio) {
00272 ff_get_guid(pb, &g);
00273 if (!ff_guidcmp(&g, &ff_asf_ext_stream_audio_stream)) {
00274 type = AVMEDIA_TYPE_AUDIO;
00275 is_dvr_ms_audio=1;
00276 ff_get_guid(pb, &g);
00277 avio_rl32(pb);
00278 avio_rl32(pb);
00279 avio_rl32(pb);
00280 ff_get_guid(pb, &g);
00281 avio_rl32(pb);
00282 }
00283 }
00284
00285 st->codec->codec_type = type;
00286 if (type == AVMEDIA_TYPE_AUDIO) {
00287 int ret = ff_get_wav_header(pb, st->codec, type_specific_size);
00288 if (ret < 0)
00289 return ret;
00290 if (is_dvr_ms_audio) {
00291
00292
00293 st->request_probe= 1;
00294 st->codec->codec_tag = 0;
00295 }
00296 if (st->codec->codec_id == CODEC_ID_AAC) {
00297 st->need_parsing = AVSTREAM_PARSE_NONE;
00298 } else {
00299 st->need_parsing = AVSTREAM_PARSE_FULL;
00300 }
00301
00302 pos2 = avio_tell(pb);
00303 if (size >= (pos2 + 8 - pos1 + 24)) {
00304 asf_st->ds_span = avio_r8(pb);
00305 asf_st->ds_packet_size = avio_rl16(pb);
00306 asf_st->ds_chunk_size = avio_rl16(pb);
00307 avio_rl16(pb);
00308 avio_r8(pb);
00309 }
00310
00311
00312
00313 if (asf_st->ds_span > 1) {
00314 if (!asf_st->ds_chunk_size
00315 || (asf_st->ds_packet_size/asf_st->ds_chunk_size <= 1)
00316 || asf_st->ds_packet_size % asf_st->ds_chunk_size)
00317 asf_st->ds_span = 0;
00318 }
00319 switch (st->codec->codec_id) {
00320 case CODEC_ID_MP3:
00321 st->codec->frame_size = MPA_FRAME_SIZE;
00322 break;
00323 case CODEC_ID_PCM_S16LE:
00324 case CODEC_ID_PCM_S16BE:
00325 case CODEC_ID_PCM_U16LE:
00326 case CODEC_ID_PCM_U16BE:
00327 case CODEC_ID_PCM_S8:
00328 case CODEC_ID_PCM_U8:
00329 case CODEC_ID_PCM_ALAW:
00330 case CODEC_ID_PCM_MULAW:
00331 st->codec->frame_size = 1;
00332 break;
00333 default:
00334
00335 st->codec->frame_size = 1;
00336 break;
00337 }
00338 } else if (type == AVMEDIA_TYPE_VIDEO &&
00339 size - (avio_tell(pb) - pos1 + 24) >= 51) {
00340 avio_rl32(pb);
00341 avio_rl32(pb);
00342 avio_r8(pb);
00343 avio_rl16(pb);
00344 sizeX= avio_rl32(pb);
00345 st->codec->width = avio_rl32(pb);
00346 st->codec->height = avio_rl32(pb);
00347
00348 avio_rl16(pb);
00349 st->codec->bits_per_coded_sample = avio_rl16(pb);
00350 tag1 = avio_rl32(pb);
00351 avio_skip(pb, 20);
00352
00353 if (sizeX > 40) {
00354 st->codec->extradata_size = sizeX - 40;
00355 st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
00356 avio_read(pb, st->codec->extradata, st->codec->extradata_size);
00357 }
00358
00359
00360
00361
00362 if (st->codec->extradata_size && (st->codec->bits_per_coded_sample <= 8)) {
00363 int av_unused i;
00364 st->codec->palctrl = av_mallocz(sizeof(AVPaletteControl));
00365 #if HAVE_BIGENDIAN
00366 for (i = 0; i < FFMIN(st->codec->extradata_size, AVPALETTE_SIZE)/4; i++)
00367 st->codec->palctrl->palette[i] = av_bswap32(((uint32_t*)st->codec->extradata)[i]);
00368 #else
00369 memcpy(st->codec->palctrl->palette, st->codec->extradata,
00370 FFMIN(st->codec->extradata_size, AVPALETTE_SIZE));
00371 #endif
00372 st->codec->palctrl->palette_changed = 1;
00373 }
00374
00375 st->codec->codec_tag = tag1;
00376 st->codec->codec_id = ff_codec_get_id(ff_codec_bmp_tags, tag1);
00377 if(tag1 == MKTAG('D', 'V', 'R', ' ')){
00378 st->need_parsing = AVSTREAM_PARSE_FULL;
00379
00380 st->codec->width =
00381 st->codec->height = 0;
00382 av_freep(&st->codec->extradata);
00383 st->codec->extradata_size=0;
00384 }
00385 if(st->codec->codec_id == CODEC_ID_H264)
00386 st->need_parsing = AVSTREAM_PARSE_FULL_ONCE;
00387 }
00388 pos2 = avio_tell(pb);
00389 avio_skip(pb, size - (pos2 - pos1 + 24));
00390
00391 return 0;
00392 }
00393
00394 static int asf_read_ext_stream_properties(AVFormatContext *s, int64_t size)
00395 {
00396 ASFContext *asf = s->priv_data;
00397 AVIOContext *pb = s->pb;
00398 ff_asf_guid g;
00399 int ext_len, payload_ext_ct, stream_ct, i;
00400 uint32_t leak_rate, stream_num;
00401 unsigned int stream_languageid_index;
00402
00403 avio_rl64(pb);
00404 avio_rl64(pb);
00405 leak_rate = avio_rl32(pb);
00406 avio_rl32(pb);
00407 avio_rl32(pb);
00408 avio_rl32(pb);
00409 avio_rl32(pb);
00410 avio_rl32(pb);
00411 avio_rl32(pb);
00412 avio_rl32(pb);
00413 stream_num = avio_rl16(pb);
00414
00415 stream_languageid_index = avio_rl16(pb);
00416 if (stream_num < 128)
00417 asf->streams[stream_num].stream_language_index = stream_languageid_index;
00418
00419 avio_rl64(pb);
00420 stream_ct = avio_rl16(pb);
00421 payload_ext_ct = avio_rl16(pb);
00422
00423 if (stream_num < 128)
00424 asf->stream_bitrates[stream_num] = leak_rate;
00425
00426 for (i=0; i<stream_ct; i++){
00427 avio_rl16(pb);
00428 ext_len = avio_rl16(pb);
00429 avio_skip(pb, ext_len);
00430 }
00431
00432 for (i=0; i<payload_ext_ct; i++){
00433 ff_get_guid(pb, &g);
00434 avio_skip(pb, 2);
00435 ext_len=avio_rl32(pb);
00436 avio_skip(pb, ext_len);
00437 }
00438
00439 return 0;
00440 }
00441
00442 static int asf_read_content_desc(AVFormatContext *s, int64_t size)
00443 {
00444 AVIOContext *pb = s->pb;
00445 int len1, len2, len3, len4, len5;
00446
00447 len1 = avio_rl16(pb);
00448 len2 = avio_rl16(pb);
00449 len3 = avio_rl16(pb);
00450 len4 = avio_rl16(pb);
00451 len5 = avio_rl16(pb);
00452 get_tag(s, "title" , 0, len1);
00453 get_tag(s, "author" , 0, len2);
00454 get_tag(s, "copyright", 0, len3);
00455 get_tag(s, "comment" , 0, len4);
00456 avio_skip(pb, len5);
00457
00458 return 0;
00459 }
00460
00461 static int asf_read_ext_content_desc(AVFormatContext *s, int64_t size)
00462 {
00463 AVIOContext *pb = s->pb;
00464 ASFContext *asf = s->priv_data;
00465 int desc_count, i, ret;
00466
00467 desc_count = avio_rl16(pb);
00468 for(i=0;i<desc_count;i++) {
00469 int name_len,value_type,value_len;
00470 char name[1024];
00471
00472 name_len = avio_rl16(pb);
00473 if (name_len%2)
00474 name_len += 1;
00475 if ((ret = avio_get_str16le(pb, name_len, name, sizeof(name))) < name_len)
00476 avio_skip(pb, name_len - ret);
00477 value_type = avio_rl16(pb);
00478 value_len = avio_rl16(pb);
00479 if (!value_type && value_len%2)
00480 value_len += 1;
00485 if (!strcmp(name, "AspectRatioX")){
00486 asf->dar[0].num= get_value(s->pb, value_type);
00487 } else if(!strcmp(name, "AspectRatioY")){
00488 asf->dar[0].den= get_value(s->pb, value_type);
00489 } else
00490 get_tag(s, name, value_type, value_len);
00491 }
00492
00493 return 0;
00494 }
00495
00496 static int asf_read_language_list(AVFormatContext *s, int64_t size)
00497 {
00498 AVIOContext *pb = s->pb;
00499 ASFContext *asf = s->priv_data;
00500 int j, ret;
00501 int stream_count = avio_rl16(pb);
00502 for(j = 0; j < stream_count; j++) {
00503 char lang[6];
00504 unsigned int lang_len = avio_r8(pb);
00505 if ((ret = avio_get_str16le(pb, lang_len, lang, sizeof(lang))) < lang_len)
00506 avio_skip(pb, lang_len - ret);
00507 if (j < 128)
00508 av_strlcpy(asf->stream_languages[j], lang, sizeof(*asf->stream_languages));
00509 }
00510
00511 return 0;
00512 }
00513
00514 static int asf_read_metadata(AVFormatContext *s, int64_t size)
00515 {
00516 AVIOContext *pb = s->pb;
00517 ASFContext *asf = s->priv_data;
00518 int n, stream_num, name_len, value_len, value_num;
00519 int ret, i;
00520 n = avio_rl16(pb);
00521
00522 for(i=0;i<n;i++) {
00523 char name[1024];
00524
00525 avio_rl16(pb);
00526 stream_num= avio_rl16(pb);
00527 name_len= avio_rl16(pb);
00528 avio_skip(pb, 2);
00529 value_len= avio_rl32(pb);
00530
00531 if ((ret = avio_get_str16le(pb, name_len, name, sizeof(name))) < name_len)
00532 avio_skip(pb, name_len - ret);
00533
00534 value_num= avio_rl16(pb);
00535 avio_skip(pb, value_len - 2);
00536
00537 if(stream_num<128){
00538 if (!strcmp(name, "AspectRatioX")) asf->dar[stream_num].num= value_num;
00539 else if(!strcmp(name, "AspectRatioY")) asf->dar[stream_num].den= value_num;
00540 }
00541 }
00542
00543 return 0;
00544 }
00545
00546 static int asf_read_marker(AVFormatContext *s, int64_t size)
00547 {
00548 AVIOContext *pb = s->pb;
00549 int i, count, name_len, ret;
00550 char name[1024];
00551
00552 avio_rl64(pb);
00553 avio_rl64(pb);
00554 count = avio_rl32(pb);
00555 avio_rl16(pb);
00556 name_len = avio_rl16(pb);
00557 for(i=0;i<name_len;i++){
00558 avio_r8(pb);
00559 }
00560
00561 for(i=0;i<count;i++){
00562 int64_t pres_time;
00563 int name_len;
00564
00565 avio_rl64(pb);
00566 pres_time = avio_rl64(pb);
00567 avio_rl16(pb);
00568 avio_rl32(pb);
00569 avio_rl32(pb);
00570 name_len = avio_rl32(pb);
00571 if ((ret = avio_get_str16le(pb, name_len * 2, name, sizeof(name))) < name_len)
00572 avio_skip(pb, name_len - ret);
00573 ff_new_chapter(s, i, (AVRational){1, 10000000}, pres_time, AV_NOPTS_VALUE, name );
00574 }
00575
00576 return 0;
00577 }
00578
00579 static int asf_read_header(AVFormatContext *s, AVFormatParameters *ap)
00580 {
00581 ASFContext *asf = s->priv_data;
00582 ff_asf_guid g;
00583 AVIOContext *pb = s->pb;
00584 int i;
00585 int64_t gsize;
00586
00587 ff_get_guid(pb, &g);
00588 if (ff_guidcmp(&g, &ff_asf_header))
00589 return -1;
00590 avio_rl64(pb);
00591 avio_rl32(pb);
00592 avio_r8(pb);
00593 avio_r8(pb);
00594 memset(&asf->asfid2avid, -1, sizeof(asf->asfid2avid));
00595 for(;;) {
00596 uint64_t gpos= avio_tell(pb);
00597 ff_get_guid(pb, &g);
00598 gsize = avio_rl64(pb);
00599 av_dlog(s, "%08"PRIx64": ", gpos);
00600 print_guid(&g);
00601 av_dlog(s, " size=0x%"PRIx64"\n", gsize);
00602 if (!ff_guidcmp(&g, &ff_asf_data_header)) {
00603 asf->data_object_offset = avio_tell(pb);
00604
00605 if (!(asf->hdr.flags & 0x01) && gsize >= 100) {
00606 asf->data_object_size = gsize - 24;
00607 } else {
00608 asf->data_object_size = (uint64_t)-1;
00609 }
00610 break;
00611 }
00612 if (gsize < 24)
00613 return -1;
00614 if (!ff_guidcmp(&g, &ff_asf_file_header)) {
00615 int ret = asf_read_file_properties(s, gsize);
00616 if (ret < 0)
00617 return ret;
00618 } else if (!ff_guidcmp(&g, &ff_asf_stream_header)) {
00619 asf_read_stream_properties(s, gsize);
00620 } else if (!ff_guidcmp(&g, &ff_asf_comment_header)) {
00621 asf_read_content_desc(s, gsize);
00622 } else if (!ff_guidcmp(&g, &ff_asf_language_guid)) {
00623 asf_read_language_list(s, gsize);
00624 } else if (!ff_guidcmp(&g, &ff_asf_extended_content_header)) {
00625 asf_read_ext_content_desc(s, gsize);
00626 } else if (!ff_guidcmp(&g, &ff_asf_metadata_header)) {
00627 asf_read_metadata(s, gsize);
00628 } else if (!ff_guidcmp(&g, &ff_asf_ext_stream_header)) {
00629 asf_read_ext_stream_properties(s, gsize);
00630
00631
00632
00633 continue;
00634 } else if (!ff_guidcmp(&g, &ff_asf_head1_guid)) {
00635 ff_get_guid(pb, &g);
00636 avio_skip(pb, 6);
00637 continue;
00638 } else if (!ff_guidcmp(&g, &ff_asf_marker_header)) {
00639 asf_read_marker(s, gsize);
00640 } else if (url_feof(pb)) {
00641 return -1;
00642 } else {
00643 if (!s->keylen) {
00644 if (!ff_guidcmp(&g, &ff_asf_content_encryption)) {
00645 av_log(s, AV_LOG_WARNING, "DRM protected stream detected, decoding will likely fail!\n");
00646 } else if (!ff_guidcmp(&g, &ff_asf_ext_content_encryption)) {
00647 av_log(s, AV_LOG_WARNING, "Ext DRM protected stream detected, decoding will likely fail!\n");
00648 } else if (!ff_guidcmp(&g, &ff_asf_digital_signature)) {
00649 av_log(s, AV_LOG_WARNING, "Digital signature detected, decoding will likely fail!\n");
00650 }
00651 }
00652 }
00653 if(avio_tell(pb) != gpos + gsize)
00654 av_log(s, AV_LOG_DEBUG, "gpos mismatch our pos=%"PRIu64", end=%"PRIu64"\n", avio_tell(pb)-gpos, gsize);
00655 avio_seek(pb, gpos + gsize, SEEK_SET);
00656 }
00657 ff_get_guid(pb, &g);
00658 avio_rl64(pb);
00659 avio_r8(pb);
00660 avio_r8(pb);
00661 if (url_feof(pb))
00662 return -1;
00663 asf->data_offset = avio_tell(pb);
00664 asf->packet_size_left = 0;
00665
00666
00667 for(i=0; i<128; i++){
00668 int stream_num= asf->asfid2avid[i];
00669 if(stream_num>=0){
00670 AVStream *st = s->streams[stream_num];
00671 if (!st->codec->bit_rate)
00672 st->codec->bit_rate = asf->stream_bitrates[i];
00673 if (asf->dar[i].num > 0 && asf->dar[i].den > 0){
00674 av_reduce(&st->sample_aspect_ratio.num,
00675 &st->sample_aspect_ratio.den,
00676 asf->dar[i].num, asf->dar[i].den, INT_MAX);
00677 } else if ((asf->dar[0].num > 0) && (asf->dar[0].den > 0) && (st->codec->codec_type==AVMEDIA_TYPE_VIDEO))
00678 av_reduce(&st->sample_aspect_ratio.num,
00679 &st->sample_aspect_ratio.den,
00680 asf->dar[0].num, asf->dar[0].den, INT_MAX);
00681
00682
00683
00684
00685 if (asf->streams[i].stream_language_index < 128) {
00686 const char *rfc1766 = asf->stream_languages[asf->streams[i].stream_language_index];
00687 if (rfc1766 && strlen(rfc1766) > 1) {
00688 const char primary_tag[3] = { rfc1766[0], rfc1766[1], '\0' };
00689 const char *iso6392 = av_convert_lang_to(primary_tag, AV_LANG_ISO639_2_BIBL);
00690 if (iso6392)
00691 av_dict_set(&st->metadata, "language", iso6392, 0);
00692 }
00693 }
00694 }
00695 }
00696
00697 ff_metadata_conv(&s->metadata, NULL, ff_asf_metadata_conv);
00698
00699 return 0;
00700 }
00701
00702 #define DO_2BITS(bits, var, defval) \
00703 switch (bits & 3) \
00704 { \
00705 case 3: var = avio_rl32(pb); rsize += 4; break; \
00706 case 2: var = avio_rl16(pb); rsize += 2; break; \
00707 case 1: var = avio_r8(pb); rsize++; break; \
00708 default: var = defval; break; \
00709 }
00710
00717 static int ff_asf_get_packet(AVFormatContext *s, AVIOContext *pb)
00718 {
00719 ASFContext *asf = s->priv_data;
00720 uint32_t packet_length, padsize;
00721 int rsize = 8;
00722 int c, d, e, off;
00723
00724
00725 off= 32768;
00726 if (s->packet_size > 0)
00727 off= (avio_tell(pb) - s->data_offset) % s->packet_size + 3;
00728
00729 c=d=e=-1;
00730 while(off-- > 0){
00731 c=d; d=e;
00732 e= avio_r8(pb);
00733 if(c == 0x82 && !d && !e)
00734 break;
00735 }
00736
00737 if (c != 0x82) {
00744 if (pb->error == AVERROR(EAGAIN))
00745 return AVERROR(EAGAIN);
00746 if (!url_feof(pb))
00747 av_log(s, AV_LOG_ERROR, "ff asf bad header %x at:%"PRId64"\n", c, avio_tell(pb));
00748 }
00749 if ((c & 0x8f) == 0x82) {
00750 if (d || e) {
00751 if (!url_feof(pb))
00752 av_log(s, AV_LOG_ERROR, "ff asf bad non zero\n");
00753 return -1;
00754 }
00755 c= avio_r8(pb);
00756 d= avio_r8(pb);
00757 rsize+=3;
00758 } else if (!pb->eof_reached) {
00759 avio_seek(pb, -1, SEEK_CUR);
00760 }
00761
00762 asf->packet_flags = c;
00763 asf->packet_property = d;
00764
00765 DO_2BITS(asf->packet_flags >> 5, packet_length, s->packet_size);
00766 DO_2BITS(asf->packet_flags >> 1, padsize, 0);
00767 DO_2BITS(asf->packet_flags >> 3, padsize, 0);
00768
00769
00770 if(!packet_length || packet_length >= (1U<<29)){
00771 av_log(s, AV_LOG_ERROR, "invalid packet_length %d at:%"PRId64"\n", packet_length, avio_tell(pb));
00772 return -1;
00773 }
00774 if(padsize >= packet_length){
00775 av_log(s, AV_LOG_ERROR, "invalid padsize %d at:%"PRId64"\n", padsize, avio_tell(pb));
00776 return -1;
00777 }
00778
00779 asf->packet_timestamp = avio_rl32(pb);
00780 avio_rl16(pb);
00781
00782
00783 if (asf->packet_flags & 0x01) {
00784 asf->packet_segsizetype = avio_r8(pb); rsize++;
00785 asf->packet_segments = asf->packet_segsizetype & 0x3f;
00786 } else {
00787 asf->packet_segments = 1;
00788 asf->packet_segsizetype = 0x80;
00789 }
00790 if (rsize > packet_length - padsize) {
00791 asf->packet_size_left = 0;
00792 av_log(s, AV_LOG_ERROR,
00793 "invalid packet header length %d for pktlen %d-%d at %"PRId64"\n",
00794 rsize, packet_length, padsize, avio_tell(pb));
00795 return -1;
00796 }
00797 asf->packet_size_left = packet_length - padsize - rsize;
00798 if (packet_length < asf->hdr.min_pktsize)
00799 padsize += asf->hdr.min_pktsize - packet_length;
00800 asf->packet_padsize = padsize;
00801 av_dlog(s, "packet: size=%d padsize=%d left=%d\n", s->packet_size, asf->packet_padsize, asf->packet_size_left);
00802 return 0;
00803 }
00804
00809 static int asf_read_frame_header(AVFormatContext *s, AVIOContext *pb){
00810 ASFContext *asf = s->priv_data;
00811 int rsize = 1;
00812 int num = avio_r8(pb);
00813 int64_t ts0;
00814
00815 asf->packet_segments--;
00816 asf->packet_key_frame = num >> 7;
00817 asf->stream_index = asf->asfid2avid[num & 0x7f];
00818
00819 DO_2BITS(asf->packet_property >> 4, asf->packet_seq, 0);
00820 DO_2BITS(asf->packet_property >> 2, asf->packet_frag_offset, 0);
00821 DO_2BITS(asf->packet_property, asf->packet_replic_size, 0);
00822
00823 if (rsize+asf->packet_replic_size > asf->packet_size_left) {
00824 av_log(s, AV_LOG_ERROR, "packet_replic_size %d is invalid\n", asf->packet_replic_size);
00825 return -1;
00826 }
00827 if (asf->packet_replic_size >= 8) {
00828 asf->packet_obj_size = avio_rl32(pb);
00829 if(asf->packet_obj_size >= (1<<24) || asf->packet_obj_size <= 0){
00830 av_log(s, AV_LOG_ERROR, "packet_obj_size invalid\n");
00831 return -1;
00832 }
00833 asf->packet_frag_timestamp = avio_rl32(pb);
00834 if(asf->packet_replic_size >= 8+38+4){
00835
00836
00837
00838 avio_skip(pb, 10);
00839 ts0= avio_rl64(pb);
00840 avio_skip(pb, 8);;
00841 avio_skip(pb, 12);
00842 avio_rl32(pb);
00843 avio_skip(pb, asf->packet_replic_size - 8 - 38 - 4);
00844 if(ts0!= -1) asf->packet_frag_timestamp= ts0/10000;
00845 else asf->packet_frag_timestamp= AV_NOPTS_VALUE;
00846 }else
00847 avio_skip(pb, asf->packet_replic_size - 8);
00848 rsize += asf->packet_replic_size;
00849 } else if (asf->packet_replic_size==1){
00850
00851 asf->packet_time_start = asf->packet_frag_offset;
00852 asf->packet_frag_offset = 0;
00853 asf->packet_frag_timestamp = asf->packet_timestamp;
00854
00855 asf->packet_time_delta = avio_r8(pb);
00856 rsize++;
00857 }else if(asf->packet_replic_size!=0){
00858 av_log(s, AV_LOG_ERROR, "unexpected packet_replic_size of %d\n", asf->packet_replic_size);
00859 return -1;
00860 }
00861 if (asf->packet_flags & 0x01) {
00862 DO_2BITS(asf->packet_segsizetype >> 6, asf->packet_frag_size, 0);
00863 if (rsize > asf->packet_size_left) {
00864 av_log(s, AV_LOG_ERROR, "packet_replic_size is invalid\n");
00865 return -1;
00866 } else if(asf->packet_frag_size > asf->packet_size_left - rsize){
00867 if (asf->packet_frag_size > asf->packet_size_left - rsize + asf->packet_padsize) {
00868 av_log(s, AV_LOG_ERROR, "packet_frag_size is invalid (%d-%d)\n", asf->packet_size_left, rsize);
00869 return -1;
00870 } else {
00871 int diff = asf->packet_frag_size - (asf->packet_size_left - rsize);
00872 asf->packet_size_left += diff;
00873 asf->packet_padsize -= diff;
00874 }
00875 }
00876
00877 } else {
00878 asf->packet_frag_size = asf->packet_size_left - rsize;
00879
00880 }
00881 if (asf->packet_replic_size == 1) {
00882 asf->packet_multi_size = asf->packet_frag_size;
00883 if (asf->packet_multi_size > asf->packet_size_left)
00884 return -1;
00885 }
00886 asf->packet_size_left -= rsize;
00887
00888
00889 return 0;
00890 }
00891
00901 static int ff_asf_parse_packet(AVFormatContext *s, AVIOContext *pb, AVPacket *pkt)
00902 {
00903 ASFContext *asf = s->priv_data;
00904 ASFStream *asf_st = 0;
00905 for (;;) {
00906 int ret;
00907 if(url_feof(pb))
00908 return AVERROR_EOF;
00909 if (asf->packet_size_left < FRAME_HEADER_SIZE
00910 || asf->packet_segments < 1) {
00911
00912 int ret = asf->packet_size_left + asf->packet_padsize;
00913
00914 assert(ret>=0);
00915
00916 avio_skip(pb, ret);
00917
00918 asf->packet_pos= avio_tell(pb);
00919 if (asf->data_object_size != (uint64_t)-1 &&
00920 (asf->packet_pos - asf->data_object_offset >= asf->data_object_size))
00921 return AVERROR_EOF;
00922 return 1;
00923 }
00924 if (asf->packet_time_start == 0) {
00925 if(asf_read_frame_header(s, pb) < 0){
00926 asf->packet_segments= 0;
00927 continue;
00928 }
00929 if (asf->stream_index < 0
00930 || s->streams[asf->stream_index]->discard >= AVDISCARD_ALL
00931 || (!asf->packet_key_frame && s->streams[asf->stream_index]->discard >= AVDISCARD_NONKEY)
00932 ) {
00933 asf->packet_time_start = 0;
00934
00935 avio_skip(pb, asf->packet_frag_size);
00936 asf->packet_size_left -= asf->packet_frag_size;
00937 if(asf->stream_index < 0)
00938 av_log(s, AV_LOG_ERROR, "ff asf skip %d (unknown stream)\n", asf->packet_frag_size);
00939 continue;
00940 }
00941 asf->asf_st = s->streams[asf->stream_index]->priv_data;
00942 }
00943 asf_st = asf->asf_st;
00944
00945 if (asf->packet_replic_size == 1) {
00946
00947 asf->packet_frag_timestamp = asf->packet_time_start;
00948 asf->packet_time_start += asf->packet_time_delta;
00949 asf->packet_obj_size = asf->packet_frag_size = avio_r8(pb);
00950 asf->packet_size_left--;
00951 asf->packet_multi_size--;
00952 if (asf->packet_multi_size < asf->packet_obj_size)
00953 {
00954 asf->packet_time_start = 0;
00955 avio_skip(pb, asf->packet_multi_size);
00956 asf->packet_size_left -= asf->packet_multi_size;
00957 continue;
00958 }
00959 asf->packet_multi_size -= asf->packet_obj_size;
00960
00961 }
00962 if(
00963 asf_st->frag_offset + asf->packet_frag_size <= asf_st->pkt.size
00964 && asf_st->frag_offset + asf->packet_frag_size > asf->packet_obj_size){
00965 av_log(s, AV_LOG_INFO, "ignoring invalid packet_obj_size (%d %d %d %d)\n",
00966 asf_st->frag_offset, asf->packet_frag_size,
00967 asf->packet_obj_size, asf_st->pkt.size);
00968 asf->packet_obj_size= asf_st->pkt.size;
00969 }
00970
00971 if ( asf_st->pkt.size != asf->packet_obj_size
00972 || asf_st->frag_offset + asf->packet_frag_size > asf_st->pkt.size) {
00973 if(asf_st->pkt.data){
00974 av_log(s, AV_LOG_INFO, "freeing incomplete packet size %d, new %d\n", asf_st->pkt.size, asf->packet_obj_size);
00975 asf_st->frag_offset = 0;
00976 av_free_packet(&asf_st->pkt);
00977 }
00978
00979 av_new_packet(&asf_st->pkt, asf->packet_obj_size);
00980 asf_st->seq = asf->packet_seq;
00981 asf_st->pkt.dts = asf->packet_frag_timestamp - asf->hdr.preroll;
00982 asf_st->pkt.stream_index = asf->stream_index;
00983 asf_st->pkt.pos =
00984 asf_st->packet_pos= asf->packet_pos;
00985
00986
00987
00988 if (s->streams[asf->stream_index]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
00989 asf->packet_key_frame = 1;
00990 if (asf->packet_key_frame)
00991 asf_st->pkt.flags |= AV_PKT_FLAG_KEY;
00992 }
00993
00994
00995
00996
00997
00998 asf->packet_size_left -= asf->packet_frag_size;
00999 if (asf->packet_size_left < 0)
01000 continue;
01001
01002 if( asf->packet_frag_offset >= asf_st->pkt.size
01003 || asf->packet_frag_size > asf_st->pkt.size - asf->packet_frag_offset){
01004 av_log(s, AV_LOG_ERROR, "packet fragment position invalid %u,%u not in %u\n",
01005 asf->packet_frag_offset, asf->packet_frag_size, asf_st->pkt.size);
01006 continue;
01007 }
01008
01009 ret = avio_read(pb, asf_st->pkt.data + asf->packet_frag_offset,
01010 asf->packet_frag_size);
01011 if (ret != asf->packet_frag_size) {
01012 if (ret < 0 || asf->packet_frag_offset + ret == 0)
01013 return ret < 0 ? ret : AVERROR_EOF;
01014 if (asf_st->ds_span > 1) {
01015
01016
01017
01018 memset(asf_st->pkt.data + asf->packet_frag_offset + ret, 0,
01019 asf->packet_frag_size - ret);
01020 ret = asf->packet_frag_size;
01021 } else
01022
01023 av_shrink_packet(&asf_st->pkt, asf->packet_frag_offset + ret);
01024 }
01025 if (s->key && s->keylen == 20)
01026 ff_asfcrypt_dec(s->key, asf_st->pkt.data + asf->packet_frag_offset,
01027 ret);
01028 asf_st->frag_offset += ret;
01029
01030 if (asf_st->frag_offset == asf_st->pkt.size) {
01031
01032 if( s->streams[asf->stream_index]->codec->codec_id == CODEC_ID_MPEG2VIDEO
01033 && asf_st->pkt.size > 100){
01034 int i;
01035 for(i=0; i<asf_st->pkt.size && !asf_st->pkt.data[i]; i++);
01036 if(i == asf_st->pkt.size){
01037 av_log(s, AV_LOG_DEBUG, "discarding ms fart\n");
01038 asf_st->frag_offset = 0;
01039 av_free_packet(&asf_st->pkt);
01040 continue;
01041 }
01042 }
01043
01044
01045 if (asf_st->ds_span > 1) {
01046 if(asf_st->pkt.size != asf_st->ds_packet_size * asf_st->ds_span){
01047 av_log(s, AV_LOG_ERROR, "pkt.size != ds_packet_size * ds_span (%d %d %d)\n", asf_st->pkt.size, asf_st->ds_packet_size, asf_st->ds_span);
01048 }else{
01049
01050 uint8_t *newdata = av_malloc(asf_st->pkt.size + FF_INPUT_BUFFER_PADDING_SIZE);
01051 if (newdata) {
01052 int offset = 0;
01053 memset(newdata + asf_st->pkt.size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
01054 while (offset < asf_st->pkt.size) {
01055 int off = offset / asf_st->ds_chunk_size;
01056 int row = off / asf_st->ds_span;
01057 int col = off % asf_st->ds_span;
01058 int idx = row + col * asf_st->ds_packet_size / asf_st->ds_chunk_size;
01059
01060
01061 assert(offset + asf_st->ds_chunk_size <= asf_st->pkt.size);
01062 assert(idx+1 <= asf_st->pkt.size / asf_st->ds_chunk_size);
01063 memcpy(newdata + offset,
01064 asf_st->pkt.data + idx * asf_st->ds_chunk_size,
01065 asf_st->ds_chunk_size);
01066 offset += asf_st->ds_chunk_size;
01067 }
01068 av_free(asf_st->pkt.data);
01069 asf_st->pkt.data = newdata;
01070 }
01071 }
01072 }
01073 asf_st->frag_offset = 0;
01074 *pkt= asf_st->pkt;
01075
01076 asf_st->pkt.size = 0;
01077 asf_st->pkt.data = 0;
01078 break;
01079 }
01080 }
01081 return 0;
01082 }
01083
01084 static int asf_read_packet(AVFormatContext *s, AVPacket *pkt)
01085 {
01086 ASFContext *asf = s->priv_data;
01087
01088 for (;;) {
01089 int ret;
01090
01091
01092 if ((ret = ff_asf_parse_packet(s, s->pb, pkt)) <= 0)
01093 return ret;
01094 if ((ret = ff_asf_get_packet(s, s->pb)) < 0)
01095 assert(asf->packet_size_left < FRAME_HEADER_SIZE || asf->packet_segments < 1);
01096 asf->packet_time_start = 0;
01097 }
01098
01099 return 0;
01100 }
01101
01102
01103
01104
01105 static void asf_reset_header(AVFormatContext *s)
01106 {
01107 ASFContext *asf = s->priv_data;
01108 ASFStream *asf_st;
01109 int i;
01110
01111 asf->packet_size_left = 0;
01112 asf->packet_segments = 0;
01113 asf->packet_flags = 0;
01114 asf->packet_property = 0;
01115 asf->packet_timestamp = 0;
01116 asf->packet_segsizetype = 0;
01117 asf->packet_segments = 0;
01118 asf->packet_seq = 0;
01119 asf->packet_replic_size = 0;
01120 asf->packet_key_frame = 0;
01121 asf->packet_padsize = 0;
01122 asf->packet_frag_offset = 0;
01123 asf->packet_frag_size = 0;
01124 asf->packet_frag_timestamp = 0;
01125 asf->packet_multi_size = 0;
01126 asf->packet_obj_size = 0;
01127 asf->packet_time_delta = 0;
01128 asf->packet_time_start = 0;
01129
01130 for(i=0; i<s->nb_streams; i++){
01131 asf_st= s->streams[i]->priv_data;
01132 av_free_packet(&asf_st->pkt);
01133 asf_st->frag_offset=0;
01134 asf_st->seq=0;
01135 }
01136 asf->asf_st= NULL;
01137 }
01138
01139 static int asf_read_close(AVFormatContext *s)
01140 {
01141 int i;
01142 asf_reset_header(s);
01143
01144 for(i=0;i<s->nb_streams;i++) {
01145 AVStream *st = s->streams[i];
01146 av_free(st->codec->palctrl);
01147 }
01148
01149 return 0;
01150 }
01151
01152 static int64_t asf_read_pts(AVFormatContext *s, int stream_index, int64_t *ppos, int64_t pos_limit)
01153 {
01154 AVPacket pkt1, *pkt = &pkt1;
01155 ASFStream *asf_st;
01156 int64_t pts;
01157 int64_t pos= *ppos;
01158 int i;
01159 int64_t start_pos[ASF_MAX_STREAMS];
01160
01161 for(i=0; i<s->nb_streams; i++){
01162 start_pos[i]= pos;
01163 }
01164
01165 if (s->packet_size > 0)
01166 pos= (pos+s->packet_size-1-s->data_offset)/s->packet_size*s->packet_size+ s->data_offset;
01167 *ppos= pos;
01168 if (avio_seek(s->pb, pos, SEEK_SET) < 0)
01169 return AV_NOPTS_VALUE;
01170
01171
01172 asf_reset_header(s);
01173 for(;;){
01174 if (av_read_frame(s, pkt) < 0){
01175 av_log(s, AV_LOG_INFO, "asf_read_pts failed\n");
01176 return AV_NOPTS_VALUE;
01177 }
01178
01179 pts= pkt->pts;
01180
01181 av_free_packet(pkt);
01182 if(pkt->flags&AV_PKT_FLAG_KEY){
01183 i= pkt->stream_index;
01184
01185 asf_st= s->streams[i]->priv_data;
01186
01187
01188 pos= asf_st->packet_pos;
01189
01190 av_add_index_entry(s->streams[i], pos, pts, pkt->size, pos - start_pos[i] + 1, AVINDEX_KEYFRAME);
01191 start_pos[i]= asf_st->packet_pos + 1;
01192
01193 if(pkt->stream_index == stream_index)
01194 break;
01195 }
01196 }
01197
01198 *ppos= pos;
01199
01200
01201 return pts;
01202 }
01203
01204 static void asf_build_simple_index(AVFormatContext *s, int stream_index)
01205 {
01206 ff_asf_guid g;
01207 ASFContext *asf = s->priv_data;
01208 int64_t current_pos= avio_tell(s->pb);
01209 int i;
01210
01211 if(avio_seek(s->pb, asf->data_object_offset + asf->data_object_size, SEEK_SET) < 0)
01212 return;
01213
01214 ff_get_guid(s->pb, &g);
01215
01216
01217
01218 while (ff_guidcmp(&g, &index_guid)) {
01219 int64_t gsize= avio_rl64(s->pb);
01220 if (gsize < 24 || url_feof(s->pb)) {
01221 avio_seek(s->pb, current_pos, SEEK_SET);
01222 return;
01223 }
01224 avio_skip(s->pb, gsize-24);
01225 ff_get_guid(s->pb, &g);
01226 }
01227
01228 {
01229 int64_t itime, last_pos=-1;
01230 int pct, ict;
01231 int64_t av_unused gsize= avio_rl64(s->pb);
01232 ff_get_guid(s->pb, &g);
01233 itime=avio_rl64(s->pb);
01234 pct=avio_rl32(s->pb);
01235 ict=avio_rl32(s->pb);
01236 av_log(s, AV_LOG_DEBUG, "itime:0x%"PRIx64", pct:%d, ict:%d\n",itime,pct,ict);
01237
01238 for (i=0;i<ict;i++){
01239 int pktnum=avio_rl32(s->pb);
01240 int pktct =avio_rl16(s->pb);
01241 int64_t pos = s->data_offset + s->packet_size*(int64_t)pktnum;
01242 int64_t index_pts= FFMAX(av_rescale(itime, i, 10000) - asf->hdr.preroll, 0);
01243
01244 if(pos != last_pos){
01245 av_log(s, AV_LOG_DEBUG, "pktnum:%d, pktct:%d pts: %"PRId64"\n", pktnum, pktct, index_pts);
01246 av_add_index_entry(s->streams[stream_index], pos, index_pts, s->packet_size, 0, AVINDEX_KEYFRAME);
01247 last_pos=pos;
01248 }
01249 }
01250 asf->index_read= 1;
01251 }
01252 avio_seek(s->pb, current_pos, SEEK_SET);
01253 }
01254
01255 static int asf_read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags)
01256 {
01257 ASFContext *asf = s->priv_data;
01258 AVStream *st = s->streams[stream_index];
01259 int64_t pos;
01260 int index;
01261
01262 if (s->packet_size <= 0)
01263 return -1;
01264
01265
01266 if(s->pb) {
01267 int ret = avio_seek_time(s->pb, stream_index, pts, flags);
01268 if(ret >= 0)
01269 asf_reset_header(s);
01270 if (ret != AVERROR(ENOSYS))
01271 return ret;
01272 }
01273
01274 if (!asf->index_read)
01275 asf_build_simple_index(s, stream_index);
01276
01277 if((asf->index_read && st->index_entries)){
01278 index= av_index_search_timestamp(st, pts, flags);
01279 if(index >= 0) {
01280
01281 pos = st->index_entries[index].pos;
01282
01283
01284 av_log(s, AV_LOG_DEBUG, "SEEKTO: %"PRId64"\n", pos);
01285 if(avio_seek(s->pb, pos, SEEK_SET) < 0)
01286 return -1;
01287 asf_reset_header(s);
01288 return 0;
01289 }
01290 }
01291
01292 if(av_seek_frame_binary(s, stream_index, pts, flags)<0)
01293 return -1;
01294 asf_reset_header(s);
01295 return 0;
01296 }
01297
01298 AVInputFormat ff_asf_demuxer = {
01299 "asf",
01300 NULL_IF_CONFIG_SMALL("ASF format"),
01301 sizeof(ASFContext),
01302 asf_probe,
01303 asf_read_header,
01304 asf_read_packet,
01305 asf_read_close,
01306 asf_read_seek,
01307 asf_read_pts,
01308 .flags = AVFMT_NOBINSEARCH | AVFMT_NOGENSEARCH,
01309 };