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 #if HAVE_BIGENDIAN
00365 for (i = 0; i < FFMIN(st->codec->extradata_size, AVPALETTE_SIZE)/4; i++)
00366 asf_st->palette[i] = av_bswap32(((uint32_t*)st->codec->extradata)[i]);
00367 #else
00368 memcpy(asf_st->palette, st->codec->extradata,
00369 FFMIN(st->codec->extradata_size, AVPALETTE_SIZE));
00370 #endif
00371 asf_st->palette_changed = 1;
00372 }
00373
00374 st->codec->codec_tag = tag1;
00375 st->codec->codec_id = ff_codec_get_id(ff_codec_bmp_tags, tag1);
00376 if(tag1 == MKTAG('D', 'V', 'R', ' ')){
00377 st->need_parsing = AVSTREAM_PARSE_FULL;
00378
00379 st->codec->width =
00380 st->codec->height = 0;
00381 av_freep(&st->codec->extradata);
00382 st->codec->extradata_size=0;
00383 }
00384 if(st->codec->codec_id == CODEC_ID_H264)
00385 st->need_parsing = AVSTREAM_PARSE_FULL_ONCE;
00386 }
00387 pos2 = avio_tell(pb);
00388 avio_skip(pb, size - (pos2 - pos1 + 24));
00389
00390 return 0;
00391 }
00392
00393 static int asf_read_ext_stream_properties(AVFormatContext *s, int64_t size)
00394 {
00395 ASFContext *asf = s->priv_data;
00396 AVIOContext *pb = s->pb;
00397 ff_asf_guid g;
00398 int ext_len, payload_ext_ct, stream_ct, i;
00399 uint32_t leak_rate, stream_num;
00400 unsigned int stream_languageid_index;
00401
00402 avio_rl64(pb);
00403 avio_rl64(pb);
00404 leak_rate = avio_rl32(pb);
00405 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 stream_num = avio_rl16(pb);
00413
00414 stream_languageid_index = avio_rl16(pb);
00415 if (stream_num < 128)
00416 asf->streams[stream_num].stream_language_index = stream_languageid_index;
00417
00418 avio_rl64(pb);
00419 stream_ct = avio_rl16(pb);
00420 payload_ext_ct = avio_rl16(pb);
00421
00422 if (stream_num < 128)
00423 asf->stream_bitrates[stream_num] = leak_rate;
00424
00425 for (i=0; i<stream_ct; i++){
00426 avio_rl16(pb);
00427 ext_len = avio_rl16(pb);
00428 avio_skip(pb, ext_len);
00429 }
00430
00431 for (i=0; i<payload_ext_ct; i++){
00432 ff_get_guid(pb, &g);
00433 avio_skip(pb, 2);
00434 ext_len=avio_rl32(pb);
00435 avio_skip(pb, ext_len);
00436 }
00437
00438 return 0;
00439 }
00440
00441 static int asf_read_content_desc(AVFormatContext *s, int64_t size)
00442 {
00443 AVIOContext *pb = s->pb;
00444 int len1, len2, len3, len4, len5;
00445
00446 len1 = avio_rl16(pb);
00447 len2 = avio_rl16(pb);
00448 len3 = avio_rl16(pb);
00449 len4 = avio_rl16(pb);
00450 len5 = avio_rl16(pb);
00451 get_tag(s, "title" , 0, len1);
00452 get_tag(s, "author" , 0, len2);
00453 get_tag(s, "copyright", 0, len3);
00454 get_tag(s, "comment" , 0, len4);
00455 avio_skip(pb, len5);
00456
00457 return 0;
00458 }
00459
00460 static int asf_read_ext_content_desc(AVFormatContext *s, int64_t size)
00461 {
00462 AVIOContext *pb = s->pb;
00463 ASFContext *asf = s->priv_data;
00464 int desc_count, i, ret;
00465
00466 desc_count = avio_rl16(pb);
00467 for(i=0;i<desc_count;i++) {
00468 int name_len,value_type,value_len;
00469 char name[1024];
00470
00471 name_len = avio_rl16(pb);
00472 if (name_len%2)
00473 name_len += 1;
00474 if ((ret = avio_get_str16le(pb, name_len, name, sizeof(name))) < name_len)
00475 avio_skip(pb, name_len - ret);
00476 value_type = avio_rl16(pb);
00477 value_len = avio_rl16(pb);
00478 if (!value_type && value_len%2)
00479 value_len += 1;
00484 if (!strcmp(name, "AspectRatioX")){
00485 asf->dar[0].num= get_value(s->pb, value_type);
00486 } else if(!strcmp(name, "AspectRatioY")){
00487 asf->dar[0].den= get_value(s->pb, value_type);
00488 } else
00489 get_tag(s, name, value_type, value_len);
00490 }
00491
00492 return 0;
00493 }
00494
00495 static int asf_read_language_list(AVFormatContext *s, int64_t size)
00496 {
00497 AVIOContext *pb = s->pb;
00498 ASFContext *asf = s->priv_data;
00499 int j, ret;
00500 int stream_count = avio_rl16(pb);
00501 for(j = 0; j < stream_count; j++) {
00502 char lang[6];
00503 unsigned int lang_len = avio_r8(pb);
00504 if ((ret = avio_get_str16le(pb, lang_len, lang, sizeof(lang))) < lang_len)
00505 avio_skip(pb, lang_len - ret);
00506 if (j < 128)
00507 av_strlcpy(asf->stream_languages[j], lang, sizeof(*asf->stream_languages));
00508 }
00509
00510 return 0;
00511 }
00512
00513 static int asf_read_metadata(AVFormatContext *s, int64_t size)
00514 {
00515 AVIOContext *pb = s->pb;
00516 ASFContext *asf = s->priv_data;
00517 int n, stream_num, name_len, value_len, value_num;
00518 int ret, i;
00519 n = avio_rl16(pb);
00520
00521 for(i=0;i<n;i++) {
00522 char name[1024];
00523
00524 avio_rl16(pb);
00525 stream_num= avio_rl16(pb);
00526 name_len= avio_rl16(pb);
00527 avio_skip(pb, 2);
00528 value_len= avio_rl32(pb);
00529
00530 if ((ret = avio_get_str16le(pb, name_len, name, sizeof(name))) < name_len)
00531 avio_skip(pb, name_len - ret);
00532
00533 value_num= avio_rl16(pb);
00534 avio_skip(pb, value_len - 2);
00535
00536 if(stream_num<128){
00537 if (!strcmp(name, "AspectRatioX")) asf->dar[stream_num].num= value_num;
00538 else if(!strcmp(name, "AspectRatioY")) asf->dar[stream_num].den= value_num;
00539 }
00540 }
00541
00542 return 0;
00543 }
00544
00545 static int asf_read_marker(AVFormatContext *s, int64_t size)
00546 {
00547 AVIOContext *pb = s->pb;
00548 int i, count, name_len, ret;
00549 char name[1024];
00550
00551 avio_rl64(pb);
00552 avio_rl64(pb);
00553 count = avio_rl32(pb);
00554 avio_rl16(pb);
00555 name_len = avio_rl16(pb);
00556 for(i=0;i<name_len;i++){
00557 avio_r8(pb);
00558 }
00559
00560 for(i=0;i<count;i++){
00561 int64_t pres_time;
00562 int name_len;
00563
00564 avio_rl64(pb);
00565 pres_time = avio_rl64(pb);
00566 avio_rl16(pb);
00567 avio_rl32(pb);
00568 avio_rl32(pb);
00569 name_len = avio_rl32(pb);
00570 if ((ret = avio_get_str16le(pb, name_len * 2, name, sizeof(name))) < name_len)
00571 avio_skip(pb, name_len - ret);
00572 ff_new_chapter(s, i, (AVRational){1, 10000000}, pres_time, AV_NOPTS_VALUE, name );
00573 }
00574
00575 return 0;
00576 }
00577
00578 static int asf_read_header(AVFormatContext *s, AVFormatParameters *ap)
00579 {
00580 ASFContext *asf = s->priv_data;
00581 ff_asf_guid g;
00582 AVIOContext *pb = s->pb;
00583 int i;
00584 int64_t gsize;
00585
00586 ff_get_guid(pb, &g);
00587 if (ff_guidcmp(&g, &ff_asf_header))
00588 return -1;
00589 avio_rl64(pb);
00590 avio_rl32(pb);
00591 avio_r8(pb);
00592 avio_r8(pb);
00593 memset(&asf->asfid2avid, -1, sizeof(asf->asfid2avid));
00594 for(;;) {
00595 uint64_t gpos= avio_tell(pb);
00596 ff_get_guid(pb, &g);
00597 gsize = avio_rl64(pb);
00598 av_dlog(s, "%08"PRIx64": ", gpos);
00599 print_guid(&g);
00600 av_dlog(s, " size=0x%"PRIx64"\n", gsize);
00601 if (!ff_guidcmp(&g, &ff_asf_data_header)) {
00602 asf->data_object_offset = avio_tell(pb);
00603
00604 if (!(asf->hdr.flags & 0x01) && gsize >= 100) {
00605 asf->data_object_size = gsize - 24;
00606 } else {
00607 asf->data_object_size = (uint64_t)-1;
00608 }
00609 break;
00610 }
00611 if (gsize < 24)
00612 return -1;
00613 if (!ff_guidcmp(&g, &ff_asf_file_header)) {
00614 int ret = asf_read_file_properties(s, gsize);
00615 if (ret < 0)
00616 return ret;
00617 } else if (!ff_guidcmp(&g, &ff_asf_stream_header)) {
00618 asf_read_stream_properties(s, gsize);
00619 } else if (!ff_guidcmp(&g, &ff_asf_comment_header)) {
00620 asf_read_content_desc(s, gsize);
00621 } else if (!ff_guidcmp(&g, &ff_asf_language_guid)) {
00622 asf_read_language_list(s, gsize);
00623 } else if (!ff_guidcmp(&g, &ff_asf_extended_content_header)) {
00624 asf_read_ext_content_desc(s, gsize);
00625 } else if (!ff_guidcmp(&g, &ff_asf_metadata_header)) {
00626 asf_read_metadata(s, gsize);
00627 } else if (!ff_guidcmp(&g, &ff_asf_ext_stream_header)) {
00628 asf_read_ext_stream_properties(s, gsize);
00629
00630
00631
00632 continue;
00633 } else if (!ff_guidcmp(&g, &ff_asf_head1_guid)) {
00634 ff_get_guid(pb, &g);
00635 avio_skip(pb, 6);
00636 continue;
00637 } else if (!ff_guidcmp(&g, &ff_asf_marker_header)) {
00638 asf_read_marker(s, gsize);
00639 } else if (url_feof(pb)) {
00640 return -1;
00641 } else {
00642 if (!s->keylen) {
00643 if (!ff_guidcmp(&g, &ff_asf_content_encryption)) {
00644 av_log(s, AV_LOG_WARNING, "DRM protected stream detected, decoding will likely fail!\n");
00645 } else if (!ff_guidcmp(&g, &ff_asf_ext_content_encryption)) {
00646 av_log(s, AV_LOG_WARNING, "Ext DRM protected stream detected, decoding will likely fail!\n");
00647 } else if (!ff_guidcmp(&g, &ff_asf_digital_signature)) {
00648 av_log(s, AV_LOG_WARNING, "Digital signature detected, decoding will likely fail!\n");
00649 }
00650 }
00651 }
00652 if(avio_tell(pb) != gpos + gsize)
00653 av_log(s, AV_LOG_DEBUG, "gpos mismatch our pos=%"PRIu64", end=%"PRIu64"\n", avio_tell(pb)-gpos, gsize);
00654 avio_seek(pb, gpos + gsize, SEEK_SET);
00655 }
00656 ff_get_guid(pb, &g);
00657 avio_rl64(pb);
00658 avio_r8(pb);
00659 avio_r8(pb);
00660 if (url_feof(pb))
00661 return -1;
00662 asf->data_offset = avio_tell(pb);
00663 asf->packet_size_left = 0;
00664
00665
00666 for(i=0; i<128; i++){
00667 int stream_num= asf->asfid2avid[i];
00668 if(stream_num>=0){
00669 AVStream *st = s->streams[stream_num];
00670 if (!st->codec->bit_rate)
00671 st->codec->bit_rate = asf->stream_bitrates[i];
00672 if (asf->dar[i].num > 0 && asf->dar[i].den > 0){
00673 av_reduce(&st->sample_aspect_ratio.num,
00674 &st->sample_aspect_ratio.den,
00675 asf->dar[i].num, asf->dar[i].den, INT_MAX);
00676 } else if ((asf->dar[0].num > 0) && (asf->dar[0].den > 0) && (st->codec->codec_type==AVMEDIA_TYPE_VIDEO))
00677 av_reduce(&st->sample_aspect_ratio.num,
00678 &st->sample_aspect_ratio.den,
00679 asf->dar[0].num, asf->dar[0].den, INT_MAX);
00680
00681
00682
00683
00684 if (asf->streams[i].stream_language_index < 128) {
00685 const char *rfc1766 = asf->stream_languages[asf->streams[i].stream_language_index];
00686 if (rfc1766 && strlen(rfc1766) > 1) {
00687 const char primary_tag[3] = { rfc1766[0], rfc1766[1], '\0' };
00688 const char *iso6392 = av_convert_lang_to(primary_tag, AV_LANG_ISO639_2_BIBL);
00689 if (iso6392)
00690 av_dict_set(&st->metadata, "language", iso6392, 0);
00691 }
00692 }
00693 }
00694 }
00695
00696 ff_metadata_conv(&s->metadata, NULL, ff_asf_metadata_conv);
00697
00698 return 0;
00699 }
00700
00701 #define DO_2BITS(bits, var, defval) \
00702 switch (bits & 3) \
00703 { \
00704 case 3: var = avio_rl32(pb); rsize += 4; break; \
00705 case 2: var = avio_rl16(pb); rsize += 2; break; \
00706 case 1: var = avio_r8(pb); rsize++; break; \
00707 default: var = defval; break; \
00708 }
00709
00716 static int ff_asf_get_packet(AVFormatContext *s, AVIOContext *pb)
00717 {
00718 ASFContext *asf = s->priv_data;
00719 uint32_t packet_length, padsize;
00720 int rsize = 8;
00721 int c, d, e, off;
00722
00723
00724 off= 32768;
00725 if (s->packet_size > 0)
00726 off= (avio_tell(pb) - s->data_offset) % s->packet_size + 3;
00727
00728 c=d=e=-1;
00729 while(off-- > 0){
00730 c=d; d=e;
00731 e= avio_r8(pb);
00732 if(c == 0x82 && !d && !e)
00733 break;
00734 }
00735
00736 if (c != 0x82) {
00743 if (pb->error == AVERROR(EAGAIN))
00744 return AVERROR(EAGAIN);
00745 if (!url_feof(pb))
00746 av_log(s, AV_LOG_ERROR, "ff asf bad header %x at:%"PRId64"\n", c, avio_tell(pb));
00747 }
00748 if ((c & 0x8f) == 0x82) {
00749 if (d || e) {
00750 if (!url_feof(pb))
00751 av_log(s, AV_LOG_ERROR, "ff asf bad non zero\n");
00752 return -1;
00753 }
00754 c= avio_r8(pb);
00755 d= avio_r8(pb);
00756 rsize+=3;
00757 } else if (!pb->eof_reached) {
00758 avio_seek(pb, -1, SEEK_CUR);
00759 }
00760
00761 asf->packet_flags = c;
00762 asf->packet_property = d;
00763
00764 DO_2BITS(asf->packet_flags >> 5, packet_length, s->packet_size);
00765 DO_2BITS(asf->packet_flags >> 1, padsize, 0);
00766 DO_2BITS(asf->packet_flags >> 3, padsize, 0);
00767
00768
00769 if(!packet_length || packet_length >= (1U<<29)){
00770 av_log(s, AV_LOG_ERROR, "invalid packet_length %d at:%"PRId64"\n", packet_length, avio_tell(pb));
00771 return -1;
00772 }
00773 if(padsize >= packet_length){
00774 av_log(s, AV_LOG_ERROR, "invalid padsize %d at:%"PRId64"\n", padsize, avio_tell(pb));
00775 return -1;
00776 }
00777
00778 asf->packet_timestamp = avio_rl32(pb);
00779 avio_rl16(pb);
00780
00781
00782 if (asf->packet_flags & 0x01) {
00783 asf->packet_segsizetype = avio_r8(pb); rsize++;
00784 asf->packet_segments = asf->packet_segsizetype & 0x3f;
00785 } else {
00786 asf->packet_segments = 1;
00787 asf->packet_segsizetype = 0x80;
00788 }
00789 if (rsize > packet_length - padsize) {
00790 asf->packet_size_left = 0;
00791 av_log(s, AV_LOG_ERROR,
00792 "invalid packet header length %d for pktlen %d-%d at %"PRId64"\n",
00793 rsize, packet_length, padsize, avio_tell(pb));
00794 return -1;
00795 }
00796 asf->packet_size_left = packet_length - padsize - rsize;
00797 if (packet_length < asf->hdr.min_pktsize)
00798 padsize += asf->hdr.min_pktsize - packet_length;
00799 asf->packet_padsize = padsize;
00800 av_dlog(s, "packet: size=%d padsize=%d left=%d\n", s->packet_size, asf->packet_padsize, asf->packet_size_left);
00801 return 0;
00802 }
00803
00808 static int asf_read_frame_header(AVFormatContext *s, AVIOContext *pb){
00809 ASFContext *asf = s->priv_data;
00810 int rsize = 1;
00811 int num = avio_r8(pb);
00812 int64_t ts0;
00813
00814 asf->packet_segments--;
00815 asf->packet_key_frame = num >> 7;
00816 asf->stream_index = asf->asfid2avid[num & 0x7f];
00817
00818 DO_2BITS(asf->packet_property >> 4, asf->packet_seq, 0);
00819 DO_2BITS(asf->packet_property >> 2, asf->packet_frag_offset, 0);
00820 DO_2BITS(asf->packet_property, asf->packet_replic_size, 0);
00821
00822 if (rsize+asf->packet_replic_size > asf->packet_size_left) {
00823 av_log(s, AV_LOG_ERROR, "packet_replic_size %d is invalid\n", asf->packet_replic_size);
00824 return -1;
00825 }
00826 if (asf->packet_replic_size >= 8) {
00827 asf->packet_obj_size = avio_rl32(pb);
00828 if(asf->packet_obj_size >= (1<<24) || asf->packet_obj_size <= 0){
00829 av_log(s, AV_LOG_ERROR, "packet_obj_size invalid\n");
00830 return -1;
00831 }
00832 asf->packet_frag_timestamp = avio_rl32(pb);
00833 if(asf->packet_replic_size >= 8+38+4){
00834
00835
00836
00837 avio_skip(pb, 10);
00838 ts0= avio_rl64(pb);
00839 avio_skip(pb, 8);;
00840 avio_skip(pb, 12);
00841 avio_rl32(pb);
00842 avio_skip(pb, asf->packet_replic_size - 8 - 38 - 4);
00843 if(ts0!= -1) asf->packet_frag_timestamp= ts0/10000;
00844 else asf->packet_frag_timestamp= AV_NOPTS_VALUE;
00845 }else
00846 avio_skip(pb, asf->packet_replic_size - 8);
00847 rsize += asf->packet_replic_size;
00848 } else if (asf->packet_replic_size==1){
00849
00850 asf->packet_time_start = asf->packet_frag_offset;
00851 asf->packet_frag_offset = 0;
00852 asf->packet_frag_timestamp = asf->packet_timestamp;
00853
00854 asf->packet_time_delta = avio_r8(pb);
00855 rsize++;
00856 }else if(asf->packet_replic_size!=0){
00857 av_log(s, AV_LOG_ERROR, "unexpected packet_replic_size of %d\n", asf->packet_replic_size);
00858 return -1;
00859 }
00860 if (asf->packet_flags & 0x01) {
00861 DO_2BITS(asf->packet_segsizetype >> 6, asf->packet_frag_size, 0);
00862 if (rsize > asf->packet_size_left) {
00863 av_log(s, AV_LOG_ERROR, "packet_replic_size is invalid\n");
00864 return -1;
00865 } else if(asf->packet_frag_size > asf->packet_size_left - rsize){
00866 if (asf->packet_frag_size > asf->packet_size_left - rsize + asf->packet_padsize) {
00867 av_log(s, AV_LOG_ERROR, "packet_frag_size is invalid (%d-%d)\n", asf->packet_size_left, rsize);
00868 return -1;
00869 } else {
00870 int diff = asf->packet_frag_size - (asf->packet_size_left - rsize);
00871 asf->packet_size_left += diff;
00872 asf->packet_padsize -= diff;
00873 }
00874 }
00875
00876 } else {
00877 asf->packet_frag_size = asf->packet_size_left - rsize;
00878
00879 }
00880 if (asf->packet_replic_size == 1) {
00881 asf->packet_multi_size = asf->packet_frag_size;
00882 if (asf->packet_multi_size > asf->packet_size_left)
00883 return -1;
00884 }
00885 asf->packet_size_left -= rsize;
00886
00887
00888 return 0;
00889 }
00890
00900 static int ff_asf_parse_packet(AVFormatContext *s, AVIOContext *pb, AVPacket *pkt)
00901 {
00902 ASFContext *asf = s->priv_data;
00903 ASFStream *asf_st = 0;
00904 for (;;) {
00905 int ret;
00906 if(url_feof(pb))
00907 return AVERROR_EOF;
00908 if (asf->packet_size_left < FRAME_HEADER_SIZE
00909 || asf->packet_segments < 1) {
00910
00911 int ret = asf->packet_size_left + asf->packet_padsize;
00912
00913 assert(ret>=0);
00914
00915 avio_skip(pb, ret);
00916
00917 asf->packet_pos= avio_tell(pb);
00918 if (asf->data_object_size != (uint64_t)-1 &&
00919 (asf->packet_pos - asf->data_object_offset >= asf->data_object_size))
00920 return AVERROR_EOF;
00921 return 1;
00922 }
00923 if (asf->packet_time_start == 0) {
00924 if(asf_read_frame_header(s, pb) < 0){
00925 asf->packet_segments= 0;
00926 continue;
00927 }
00928 if (asf->stream_index < 0
00929 || s->streams[asf->stream_index]->discard >= AVDISCARD_ALL
00930 || (!asf->packet_key_frame && s->streams[asf->stream_index]->discard >= AVDISCARD_NONKEY)
00931 ) {
00932 asf->packet_time_start = 0;
00933
00934 avio_skip(pb, asf->packet_frag_size);
00935 asf->packet_size_left -= asf->packet_frag_size;
00936 if(asf->stream_index < 0)
00937 av_log(s, AV_LOG_ERROR, "ff asf skip %d (unknown stream)\n", asf->packet_frag_size);
00938 continue;
00939 }
00940 asf->asf_st = s->streams[asf->stream_index]->priv_data;
00941 }
00942 asf_st = asf->asf_st;
00943
00944 if (asf->packet_replic_size == 1) {
00945
00946 asf->packet_frag_timestamp = asf->packet_time_start;
00947 asf->packet_time_start += asf->packet_time_delta;
00948 asf->packet_obj_size = asf->packet_frag_size = avio_r8(pb);
00949 asf->packet_size_left--;
00950 asf->packet_multi_size--;
00951 if (asf->packet_multi_size < asf->packet_obj_size)
00952 {
00953 asf->packet_time_start = 0;
00954 avio_skip(pb, asf->packet_multi_size);
00955 asf->packet_size_left -= asf->packet_multi_size;
00956 continue;
00957 }
00958 asf->packet_multi_size -= asf->packet_obj_size;
00959
00960 }
00961 if(
00962 asf_st->frag_offset + asf->packet_frag_size <= asf_st->pkt.size
00963 && asf_st->frag_offset + asf->packet_frag_size > asf->packet_obj_size){
00964 av_log(s, AV_LOG_INFO, "ignoring invalid packet_obj_size (%d %d %d %d)\n",
00965 asf_st->frag_offset, asf->packet_frag_size,
00966 asf->packet_obj_size, asf_st->pkt.size);
00967 asf->packet_obj_size= asf_st->pkt.size;
00968 }
00969
00970 if ( asf_st->pkt.size != asf->packet_obj_size
00971 || asf_st->frag_offset + asf->packet_frag_size > asf_st->pkt.size) {
00972 if(asf_st->pkt.data){
00973 av_log(s, AV_LOG_INFO, "freeing incomplete packet size %d, new %d\n", asf_st->pkt.size, asf->packet_obj_size);
00974 asf_st->frag_offset = 0;
00975 av_free_packet(&asf_st->pkt);
00976 }
00977
00978 av_new_packet(&asf_st->pkt, asf->packet_obj_size);
00979 asf_st->seq = asf->packet_seq;
00980 asf_st->pkt.dts = asf->packet_frag_timestamp - asf->hdr.preroll;
00981 asf_st->pkt.stream_index = asf->stream_index;
00982 asf_st->pkt.pos =
00983 asf_st->packet_pos= asf->packet_pos;
00984 if (asf_st->pkt.data && asf_st->palette_changed) {
00985 uint8_t *pal;
00986 pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE,
00987 AVPALETTE_SIZE);
00988 if (!pal) {
00989 av_log(s, AV_LOG_ERROR, "Cannot append palette to packet\n");
00990 } else {
00991 memcpy(pal, asf_st->palette, AVPALETTE_SIZE);
00992 asf_st->palette_changed = 0;
00993 }
00994 }
00995
00996
00997
00998 if (s->streams[asf->stream_index]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
00999 asf->packet_key_frame = 1;
01000 if (asf->packet_key_frame)
01001 asf_st->pkt.flags |= AV_PKT_FLAG_KEY;
01002 }
01003
01004
01005
01006
01007
01008 asf->packet_size_left -= asf->packet_frag_size;
01009 if (asf->packet_size_left < 0)
01010 continue;
01011
01012 if( asf->packet_frag_offset >= asf_st->pkt.size
01013 || asf->packet_frag_size > asf_st->pkt.size - asf->packet_frag_offset){
01014 av_log(s, AV_LOG_ERROR, "packet fragment position invalid %u,%u not in %u\n",
01015 asf->packet_frag_offset, asf->packet_frag_size, asf_st->pkt.size);
01016 continue;
01017 }
01018
01019 ret = avio_read(pb, asf_st->pkt.data + asf->packet_frag_offset,
01020 asf->packet_frag_size);
01021 if (ret != asf->packet_frag_size) {
01022 if (ret < 0 || asf->packet_frag_offset + ret == 0)
01023 return ret < 0 ? ret : AVERROR_EOF;
01024 if (asf_st->ds_span > 1) {
01025
01026
01027
01028 memset(asf_st->pkt.data + asf->packet_frag_offset + ret, 0,
01029 asf->packet_frag_size - ret);
01030 ret = asf->packet_frag_size;
01031 } else
01032
01033 av_shrink_packet(&asf_st->pkt, asf->packet_frag_offset + ret);
01034 }
01035 if (s->key && s->keylen == 20)
01036 ff_asfcrypt_dec(s->key, asf_st->pkt.data + asf->packet_frag_offset,
01037 ret);
01038 asf_st->frag_offset += ret;
01039
01040 if (asf_st->frag_offset == asf_st->pkt.size) {
01041
01042 if( s->streams[asf->stream_index]->codec->codec_id == CODEC_ID_MPEG2VIDEO
01043 && asf_st->pkt.size > 100){
01044 int i;
01045 for(i=0; i<asf_st->pkt.size && !asf_st->pkt.data[i]; i++);
01046 if(i == asf_st->pkt.size){
01047 av_log(s, AV_LOG_DEBUG, "discarding ms fart\n");
01048 asf_st->frag_offset = 0;
01049 av_free_packet(&asf_st->pkt);
01050 continue;
01051 }
01052 }
01053
01054
01055 if (asf_st->ds_span > 1) {
01056 if(asf_st->pkt.size != asf_st->ds_packet_size * asf_st->ds_span){
01057 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);
01058 }else{
01059
01060 uint8_t *newdata = av_malloc(asf_st->pkt.size + FF_INPUT_BUFFER_PADDING_SIZE);
01061 if (newdata) {
01062 int offset = 0;
01063 memset(newdata + asf_st->pkt.size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
01064 while (offset < asf_st->pkt.size) {
01065 int off = offset / asf_st->ds_chunk_size;
01066 int row = off / asf_st->ds_span;
01067 int col = off % asf_st->ds_span;
01068 int idx = row + col * asf_st->ds_packet_size / asf_st->ds_chunk_size;
01069
01070
01071 assert(offset + asf_st->ds_chunk_size <= asf_st->pkt.size);
01072 assert(idx+1 <= asf_st->pkt.size / asf_st->ds_chunk_size);
01073 memcpy(newdata + offset,
01074 asf_st->pkt.data + idx * asf_st->ds_chunk_size,
01075 asf_st->ds_chunk_size);
01076 offset += asf_st->ds_chunk_size;
01077 }
01078 av_free(asf_st->pkt.data);
01079 asf_st->pkt.data = newdata;
01080 }
01081 }
01082 }
01083 asf_st->frag_offset = 0;
01084 *pkt= asf_st->pkt;
01085
01086 asf_st->pkt.size = 0;
01087 asf_st->pkt.data = 0;
01088 break;
01089 }
01090 }
01091 return 0;
01092 }
01093
01094 static int asf_read_packet(AVFormatContext *s, AVPacket *pkt)
01095 {
01096 ASFContext *asf = s->priv_data;
01097
01098 for (;;) {
01099 int ret;
01100
01101
01102 if ((ret = ff_asf_parse_packet(s, s->pb, pkt)) <= 0)
01103 return ret;
01104 if ((ret = ff_asf_get_packet(s, s->pb)) < 0)
01105 assert(asf->packet_size_left < FRAME_HEADER_SIZE || asf->packet_segments < 1);
01106 asf->packet_time_start = 0;
01107 }
01108
01109 return 0;
01110 }
01111
01112
01113
01114
01115 static void asf_reset_header(AVFormatContext *s)
01116 {
01117 ASFContext *asf = s->priv_data;
01118 ASFStream *asf_st;
01119 int i;
01120
01121 asf->packet_size_left = 0;
01122 asf->packet_segments = 0;
01123 asf->packet_flags = 0;
01124 asf->packet_property = 0;
01125 asf->packet_timestamp = 0;
01126 asf->packet_segsizetype = 0;
01127 asf->packet_segments = 0;
01128 asf->packet_seq = 0;
01129 asf->packet_replic_size = 0;
01130 asf->packet_key_frame = 0;
01131 asf->packet_padsize = 0;
01132 asf->packet_frag_offset = 0;
01133 asf->packet_frag_size = 0;
01134 asf->packet_frag_timestamp = 0;
01135 asf->packet_multi_size = 0;
01136 asf->packet_obj_size = 0;
01137 asf->packet_time_delta = 0;
01138 asf->packet_time_start = 0;
01139
01140 for(i=0; i<s->nb_streams; i++){
01141 asf_st= s->streams[i]->priv_data;
01142 av_free_packet(&asf_st->pkt);
01143 asf_st->frag_offset=0;
01144 asf_st->seq=0;
01145 }
01146 asf->asf_st= NULL;
01147 }
01148
01149 static int asf_read_close(AVFormatContext *s)
01150 {
01151 asf_reset_header(s);
01152
01153 return 0;
01154 }
01155
01156 static int64_t asf_read_pts(AVFormatContext *s, int stream_index, int64_t *ppos, int64_t pos_limit)
01157 {
01158 AVPacket pkt1, *pkt = &pkt1;
01159 ASFStream *asf_st;
01160 int64_t pts;
01161 int64_t pos= *ppos;
01162 int i;
01163 int64_t start_pos[ASF_MAX_STREAMS];
01164
01165 for(i=0; i<s->nb_streams; i++){
01166 start_pos[i]= pos;
01167 }
01168
01169 if (s->packet_size > 0)
01170 pos= (pos+s->packet_size-1-s->data_offset)/s->packet_size*s->packet_size+ s->data_offset;
01171 *ppos= pos;
01172 if (avio_seek(s->pb, pos, SEEK_SET) < 0)
01173 return AV_NOPTS_VALUE;
01174
01175
01176 asf_reset_header(s);
01177 for(;;){
01178 if (av_read_frame(s, pkt) < 0){
01179 av_log(s, AV_LOG_INFO, "asf_read_pts failed\n");
01180 return AV_NOPTS_VALUE;
01181 }
01182
01183 pts= pkt->pts;
01184
01185 av_free_packet(pkt);
01186 if(pkt->flags&AV_PKT_FLAG_KEY){
01187 i= pkt->stream_index;
01188
01189 asf_st= s->streams[i]->priv_data;
01190
01191
01192 pos= asf_st->packet_pos;
01193
01194 av_add_index_entry(s->streams[i], pos, pts, pkt->size, pos - start_pos[i] + 1, AVINDEX_KEYFRAME);
01195 start_pos[i]= asf_st->packet_pos + 1;
01196
01197 if(pkt->stream_index == stream_index)
01198 break;
01199 }
01200 }
01201
01202 *ppos= pos;
01203
01204
01205 return pts;
01206 }
01207
01208 static void asf_build_simple_index(AVFormatContext *s, int stream_index)
01209 {
01210 ff_asf_guid g;
01211 ASFContext *asf = s->priv_data;
01212 int64_t current_pos= avio_tell(s->pb);
01213 int i;
01214
01215 if(avio_seek(s->pb, asf->data_object_offset + asf->data_object_size, SEEK_SET) < 0)
01216 return;
01217
01218 ff_get_guid(s->pb, &g);
01219
01220
01221
01222 while (ff_guidcmp(&g, &index_guid)) {
01223 int64_t gsize= avio_rl64(s->pb);
01224 if (gsize < 24 || url_feof(s->pb)) {
01225 avio_seek(s->pb, current_pos, SEEK_SET);
01226 return;
01227 }
01228 avio_skip(s->pb, gsize-24);
01229 ff_get_guid(s->pb, &g);
01230 }
01231
01232 {
01233 int64_t itime, last_pos=-1;
01234 int pct, ict;
01235 int64_t av_unused gsize= avio_rl64(s->pb);
01236 ff_get_guid(s->pb, &g);
01237 itime=avio_rl64(s->pb);
01238 pct=avio_rl32(s->pb);
01239 ict=avio_rl32(s->pb);
01240 av_log(s, AV_LOG_DEBUG, "itime:0x%"PRIx64", pct:%d, ict:%d\n",itime,pct,ict);
01241
01242 for (i=0;i<ict;i++){
01243 int pktnum=avio_rl32(s->pb);
01244 int pktct =avio_rl16(s->pb);
01245 int64_t pos = s->data_offset + s->packet_size*(int64_t)pktnum;
01246 int64_t index_pts= FFMAX(av_rescale(itime, i, 10000) - asf->hdr.preroll, 0);
01247
01248 if(pos != last_pos){
01249 av_log(s, AV_LOG_DEBUG, "pktnum:%d, pktct:%d pts: %"PRId64"\n", pktnum, pktct, index_pts);
01250 av_add_index_entry(s->streams[stream_index], pos, index_pts, s->packet_size, 0, AVINDEX_KEYFRAME);
01251 last_pos=pos;
01252 }
01253 }
01254 asf->index_read= 1;
01255 }
01256 avio_seek(s->pb, current_pos, SEEK_SET);
01257 }
01258
01259 static int asf_read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags)
01260 {
01261 ASFContext *asf = s->priv_data;
01262 AVStream *st = s->streams[stream_index];
01263 int64_t pos;
01264 int index;
01265
01266 if (s->packet_size <= 0)
01267 return -1;
01268
01269
01270 if(s->pb) {
01271 int ret = avio_seek_time(s->pb, stream_index, pts, flags);
01272 if(ret >= 0)
01273 asf_reset_header(s);
01274 if (ret != AVERROR(ENOSYS))
01275 return ret;
01276 }
01277
01278 if (!asf->index_read)
01279 asf_build_simple_index(s, stream_index);
01280
01281 if((asf->index_read && st->index_entries)){
01282 index= av_index_search_timestamp(st, pts, flags);
01283 if(index >= 0) {
01284
01285 pos = st->index_entries[index].pos;
01286
01287
01288 av_log(s, AV_LOG_DEBUG, "SEEKTO: %"PRId64"\n", pos);
01289 if(avio_seek(s->pb, pos, SEEK_SET) < 0)
01290 return -1;
01291 asf_reset_header(s);
01292 return 0;
01293 }
01294 }
01295
01296 if(av_seek_frame_binary(s, stream_index, pts, flags)<0)
01297 return -1;
01298 asf_reset_header(s);
01299 return 0;
01300 }
01301
01302 AVInputFormat ff_asf_demuxer = {
01303 "asf",
01304 NULL_IF_CONFIG_SMALL("ASF format"),
01305 sizeof(ASFContext),
01306 asf_probe,
01307 asf_read_header,
01308 asf_read_packet,
01309 asf_read_close,
01310 asf_read_seek,
01311 asf_read_pts,
01312 .flags = AVFMT_NOBINSEARCH | AVFMT_NOGENSEARCH,
01313 };