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