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