00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/common.h"
00023 #include "libavcodec/mpegaudio.h"
00024 #include "avformat.h"
00025 #include "riff.h"
00026 #include "asf.h"
00027 #include "asfcrypt.h"
00028
00029 void ff_mms_set_stream_selection(URLContext *h, AVFormatContext *format);
00030
00031 #undef NDEBUG
00032 #include <assert.h>
00033
00034 #define FRAME_HEADER_SIZE 17
00035
00036
00037 static const GUID index_guid = {
00038 0x90, 0x08, 0x00, 0x33, 0xb1, 0xe5, 0xcf, 0x11, 0x89, 0xf4, 0x00, 0xa0, 0xc9, 0x03, 0x49, 0xcb
00039 };
00040
00041 static const GUID stream_bitrate_guid = {
00042 0xce, 0x75, 0xf8, 0x7b, 0x8d, 0x46, 0xd1, 0x11, 0x8d, 0x82, 0x00, 0x60, 0x97, 0xc9, 0xa2, 0xb2
00043 };
00044
00045
00046
00047
00048
00049 #ifdef DEBUG
00050 #define PRINT_IF_GUID(g,cmp) \
00051 if (!memcmp(g, &cmp, sizeof(GUID))) \
00052 dprintf(NULL, "(GUID: %s) ", #cmp)
00053
00054 static void print_guid(const GUID *g)
00055 {
00056 int i;
00057 PRINT_IF_GUID(g, ff_asf_header);
00058 else PRINT_IF_GUID(g, ff_asf_file_header);
00059 else PRINT_IF_GUID(g, ff_asf_stream_header);
00060 else PRINT_IF_GUID(g, ff_asf_audio_stream);
00061 else PRINT_IF_GUID(g, ff_asf_audio_conceal_none);
00062 else PRINT_IF_GUID(g, ff_asf_video_stream);
00063 else PRINT_IF_GUID(g, ff_asf_video_conceal_none);
00064 else PRINT_IF_GUID(g, ff_asf_command_stream);
00065 else PRINT_IF_GUID(g, ff_asf_comment_header);
00066 else PRINT_IF_GUID(g, ff_asf_codec_comment_header);
00067 else PRINT_IF_GUID(g, ff_asf_codec_comment1_header);
00068 else PRINT_IF_GUID(g, ff_asf_data_header);
00069 else PRINT_IF_GUID(g, index_guid);
00070 else PRINT_IF_GUID(g, ff_asf_head1_guid);
00071 else PRINT_IF_GUID(g, ff_asf_head2_guid);
00072 else PRINT_IF_GUID(g, ff_asf_my_guid);
00073 else PRINT_IF_GUID(g, ff_asf_ext_stream_header);
00074 else PRINT_IF_GUID(g, ff_asf_extended_content_header);
00075 else PRINT_IF_GUID(g, ff_asf_ext_stream_embed_stream_header);
00076 else PRINT_IF_GUID(g, ff_asf_ext_stream_audio_stream);
00077 else PRINT_IF_GUID(g, ff_asf_metadata_header);
00078 else PRINT_IF_GUID(g, stream_bitrate_guid);
00079 else
00080 dprintf(NULL, "(GUID: unknown) ");
00081 for(i=0;i<16;i++)
00082 dprintf(NULL, " 0x%02x,", (*g)[i]);
00083 dprintf(NULL, "}\n");
00084 }
00085 #undef PRINT_IF_GUID
00086 #else
00087 #define print_guid(g)
00088 #endif
00089
00090 static void get_guid(ByteIOContext *s, GUID *g)
00091 {
00092 assert(sizeof(*g) == 16);
00093 get_buffer(s, *g, sizeof(*g));
00094 }
00095
00096 #if 0
00097 static void get_str16(ByteIOContext *pb, char *buf, int buf_size)
00098 {
00099 int len, c;
00100 char *q;
00101
00102 len = get_le16(pb);
00103 q = buf;
00104 while (len > 0) {
00105 c = get_le16(pb);
00106 if ((q - buf) < buf_size - 1)
00107 *q++ = c;
00108 len--;
00109 }
00110 *q = '\0';
00111 }
00112 #endif
00113
00114 static void get_str16_nolen(ByteIOContext *pb, int len, char *buf, int buf_size)
00115 {
00116 char* q = buf;
00117 len /= 2;
00118 while (len--) {
00119 uint8_t tmp;
00120 PUT_UTF8(get_le16(pb), tmp, if (q - buf < buf_size - 1) *q++ = tmp;)
00121 }
00122 *q = '\0';
00123 }
00124
00125 static int asf_probe(AVProbeData *pd)
00126 {
00127
00128 if (!memcmp(pd->buf, &ff_asf_header, sizeof(GUID)))
00129 return AVPROBE_SCORE_MAX;
00130 else
00131 return 0;
00132 }
00133
00134 static int get_value(ByteIOContext *pb, int type){
00135 switch(type){
00136 case 2: return get_le32(pb);
00137 case 3: return get_le32(pb);
00138 case 4: return get_le64(pb);
00139 case 5: return get_le16(pb);
00140 default:return INT_MIN;
00141 }
00142 }
00143
00144 static void get_tag(AVFormatContext *s, const char *key, int type, int len)
00145 {
00146 char value[1024];
00147 if (type <= 1) {
00148 get_str16_nolen(s->pb, len, value, sizeof(value));
00149 } else if (type <= 5) {
00150 uint64_t num = get_value(s->pb, type);
00151 snprintf(value, sizeof(value), "%"PRIu64, num);
00152 } else {
00153 url_fskip(s->pb, len);
00154 return;
00155 }
00156 if (strncmp(key, "WM/", 3))
00157 key += 3;
00158 av_metadata_set(&s->metadata, key, value);
00159 }
00160
00161 static int asf_read_header(AVFormatContext *s, AVFormatParameters *ap)
00162 {
00163 ASFContext *asf = s->priv_data;
00164 GUID g;
00165 ByteIOContext *pb = s->pb;
00166 AVStream *st;
00167 ASFStream *asf_st;
00168 int size, i;
00169 int64_t gsize;
00170 AVRational dar[128];
00171 uint32_t bitrate[128];
00172
00173 memset(dar, 0, sizeof(dar));
00174 memset(bitrate, 0, sizeof(bitrate));
00175
00176 get_guid(pb, &g);
00177 if (memcmp(&g, &ff_asf_header, sizeof(GUID)))
00178 return -1;
00179 get_le64(pb);
00180 get_le32(pb);
00181 get_byte(pb);
00182 get_byte(pb);
00183 memset(&asf->asfid2avid, -1, sizeof(asf->asfid2avid));
00184 for(;;) {
00185 get_guid(pb, &g);
00186 gsize = get_le64(pb);
00187 dprintf(s, "%08"PRIx64": ", url_ftell(pb) - 24);
00188 print_guid(&g);
00189 dprintf(s, " size=0x%"PRIx64"\n", gsize);
00190 if (!memcmp(&g, &ff_asf_data_header, sizeof(GUID))) {
00191 asf->data_object_offset = url_ftell(pb);
00192
00193 if (!(asf->hdr.flags & 0x01) && gsize >= 100) {
00194 asf->data_object_size = gsize - 24;
00195 } else {
00196 asf->data_object_size = (uint64_t)-1;
00197 }
00198 break;
00199 }
00200 if (gsize < 24)
00201 return -1;
00202 if (!memcmp(&g, &ff_asf_file_header, sizeof(GUID))) {
00203 get_guid(pb, &asf->hdr.guid);
00204 asf->hdr.file_size = get_le64(pb);
00205 asf->hdr.create_time = get_le64(pb);
00206 asf->nb_packets = get_le64(pb);
00207 asf->hdr.play_time = get_le64(pb);
00208 asf->hdr.send_time = get_le64(pb);
00209 asf->hdr.preroll = get_le32(pb);
00210 asf->hdr.ignore = get_le32(pb);
00211 asf->hdr.flags = get_le32(pb);
00212 asf->hdr.min_pktsize = get_le32(pb);
00213 asf->hdr.max_pktsize = get_le32(pb);
00214 asf->hdr.max_bitrate = get_le32(pb);
00215 s->packet_size = asf->hdr.max_pktsize;
00216 } else if (!memcmp(&g, &ff_asf_stream_header, sizeof(GUID))) {
00217 enum CodecType type;
00218 int type_specific_size, sizeX;
00219 uint64_t total_size;
00220 unsigned int tag1;
00221 int64_t pos1, pos2, start_time;
00222 int test_for_ext_stream_audio, is_dvr_ms_audio=0;
00223
00224 pos1 = url_ftell(pb);
00225
00226 st = av_new_stream(s, 0);
00227 if (!st)
00228 return AVERROR(ENOMEM);
00229 av_set_pts_info(st, 32, 1, 1000);
00230 asf_st = av_mallocz(sizeof(ASFStream));
00231 if (!asf_st)
00232 return AVERROR(ENOMEM);
00233 st->priv_data = asf_st;
00234 start_time = asf->hdr.preroll;
00235
00236 if(!(asf->hdr.flags & 0x01)) {
00237 st->duration = asf->hdr.send_time /
00238 (10000000 / 1000) - start_time;
00239 }
00240 get_guid(pb, &g);
00241
00242 test_for_ext_stream_audio = 0;
00243 if (!memcmp(&g, &ff_asf_audio_stream, sizeof(GUID))) {
00244 type = CODEC_TYPE_AUDIO;
00245 } else if (!memcmp(&g, &ff_asf_video_stream, sizeof(GUID))) {
00246 type = CODEC_TYPE_VIDEO;
00247 } else if (!memcmp(&g, &ff_asf_command_stream, sizeof(GUID))) {
00248 type = CODEC_TYPE_DATA;
00249 } else if (!memcmp(&g, &ff_asf_ext_stream_embed_stream_header, sizeof(GUID))) {
00250 test_for_ext_stream_audio = 1;
00251 type = CODEC_TYPE_UNKNOWN;
00252 } else {
00253 return -1;
00254 }
00255 get_guid(pb, &g);
00256 total_size = get_le64(pb);
00257 type_specific_size = get_le32(pb);
00258 get_le32(pb);
00259 st->id = get_le16(pb) & 0x7f;
00260
00261 asf->asfid2avid[st->id] = s->nb_streams - 1;
00262
00263 get_le32(pb);
00264
00265 if (test_for_ext_stream_audio) {
00266 get_guid(pb, &g);
00267 if (!memcmp(&g, &ff_asf_ext_stream_audio_stream, sizeof(GUID))) {
00268 type = CODEC_TYPE_AUDIO;
00269 is_dvr_ms_audio=1;
00270 get_guid(pb, &g);
00271 get_le32(pb);
00272 get_le32(pb);
00273 get_le32(pb);
00274 get_guid(pb, &g);
00275 get_le32(pb);
00276 }
00277 }
00278
00279 st->codec->codec_type = type;
00280 if (type == CODEC_TYPE_AUDIO) {
00281 get_wav_header(pb, st->codec, type_specific_size);
00282 if (is_dvr_ms_audio) {
00283
00284
00285 st->codec->codec_id = CODEC_ID_PROBE;
00286 st->codec->codec_tag = 0;
00287 }
00288 if (st->codec->codec_id == CODEC_ID_AAC) {
00289 st->need_parsing = AVSTREAM_PARSE_NONE;
00290 } else {
00291 st->need_parsing = AVSTREAM_PARSE_FULL;
00292 }
00293
00294 pos2 = url_ftell(pb);
00295 if (gsize >= (pos2 + 8 - pos1 + 24)) {
00296 asf_st->ds_span = get_byte(pb);
00297 asf_st->ds_packet_size = get_le16(pb);
00298 asf_st->ds_chunk_size = get_le16(pb);
00299 get_le16(pb);
00300 get_byte(pb);
00301 }
00302
00303
00304
00305 if (asf_st->ds_span > 1) {
00306 if (!asf_st->ds_chunk_size
00307 || (asf_st->ds_packet_size/asf_st->ds_chunk_size <= 1)
00308 || asf_st->ds_packet_size % asf_st->ds_chunk_size)
00309 asf_st->ds_span = 0;
00310 }
00311 switch (st->codec->codec_id) {
00312 case CODEC_ID_MP3:
00313 st->codec->frame_size = MPA_FRAME_SIZE;
00314 break;
00315 case CODEC_ID_PCM_S16LE:
00316 case CODEC_ID_PCM_S16BE:
00317 case CODEC_ID_PCM_U16LE:
00318 case CODEC_ID_PCM_U16BE:
00319 case CODEC_ID_PCM_S8:
00320 case CODEC_ID_PCM_U8:
00321 case CODEC_ID_PCM_ALAW:
00322 case CODEC_ID_PCM_MULAW:
00323 st->codec->frame_size = 1;
00324 break;
00325 default:
00326
00327 st->codec->frame_size = 1;
00328 break;
00329 }
00330 } else if (type == CODEC_TYPE_VIDEO) {
00331 get_le32(pb);
00332 get_le32(pb);
00333 get_byte(pb);
00334 size = get_le16(pb);
00335 sizeX= get_le32(pb);
00336 st->codec->width = get_le32(pb);
00337 st->codec->height = get_le32(pb);
00338
00339 get_le16(pb);
00340 st->codec->bits_per_coded_sample = get_le16(pb);
00341 tag1 = get_le32(pb);
00342 url_fskip(pb, 20);
00343
00344 size= sizeX;
00345 if (size > 40) {
00346 st->codec->extradata_size = size - 40;
00347 st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
00348 get_buffer(pb, st->codec->extradata, st->codec->extradata_size);
00349 }
00350
00351
00352
00353
00354 if (st->codec->extradata_size && (st->codec->bits_per_coded_sample <= 8)) {
00355 st->codec->palctrl = av_mallocz(sizeof(AVPaletteControl));
00356 #ifdef WORDS_BIGENDIAN
00357 for (i = 0; i < FFMIN(st->codec->extradata_size, AVPALETTE_SIZE)/4; i++)
00358 st->codec->palctrl->palette[i] = bswap_32(((uint32_t*)st->codec->extradata)[i]);
00359 #else
00360 memcpy(st->codec->palctrl->palette, st->codec->extradata,
00361 FFMIN(st->codec->extradata_size, AVPALETTE_SIZE));
00362 #endif
00363 st->codec->palctrl->palette_changed = 1;
00364 }
00365
00366 st->codec->codec_tag = tag1;
00367 st->codec->codec_id = codec_get_id(codec_bmp_tags, tag1);
00368 if(tag1 == MKTAG('D', 'V', 'R', ' '))
00369 st->need_parsing = AVSTREAM_PARSE_FULL;
00370 }
00371 pos2 = url_ftell(pb);
00372 url_fskip(pb, gsize - (pos2 - pos1 + 24));
00373 } else if (!memcmp(&g, &ff_asf_comment_header, sizeof(GUID))) {
00374 int len1, len2, len3, len4, len5;
00375
00376 len1 = get_le16(pb);
00377 len2 = get_le16(pb);
00378 len3 = get_le16(pb);
00379 len4 = get_le16(pb);
00380 len5 = get_le16(pb);
00381 get_tag(s, "title" , 0, len1);
00382 get_tag(s, "author" , 0, len2);
00383 get_tag(s, "copyright", 0, len3);
00384 get_tag(s, "comment" , 0, len4);
00385 url_fskip(pb, len5);
00386 } else if (!memcmp(&g, &stream_bitrate_guid, sizeof(GUID))) {
00387 int stream_count = get_le16(pb);
00388 int j;
00389
00390
00391
00392 for(j = 0; j < stream_count; j++) {
00393 int flags, bitrate, stream_id;
00394
00395 flags= get_le16(pb);
00396 bitrate= get_le32(pb);
00397 stream_id= (flags & 0x7f);
00398
00399 asf->stream_bitrates[stream_id]= bitrate;
00400 }
00401 } else if (!memcmp(&g, &ff_asf_extended_content_header, sizeof(GUID))) {
00402 int desc_count, i;
00403
00404 desc_count = get_le16(pb);
00405 for(i=0;i<desc_count;i++)
00406 {
00407 int name_len,value_type,value_len;
00408 char name[1024];
00409
00410 name_len = get_le16(pb);
00411 get_str16_nolen(pb, name_len, name, sizeof(name));
00412 value_type = get_le16(pb);
00413 value_len = get_le16(pb);
00414 get_tag(s, name, value_type, value_len);
00415 }
00416 } else if (!memcmp(&g, &ff_asf_metadata_header, sizeof(GUID))) {
00417 int n, stream_num, name_len, value_len, value_type, value_num;
00418 n = get_le16(pb);
00419
00420 for(i=0;i<n;i++) {
00421 char name[1024];
00422
00423 get_le16(pb);
00424 stream_num= get_le16(pb);
00425 name_len= get_le16(pb);
00426 value_type= get_le16(pb);
00427 value_len= get_le32(pb);
00428
00429 get_str16_nolen(pb, name_len, name, sizeof(name));
00430
00431 value_num= get_le16(pb);
00432 url_fskip(pb, value_len - 2);
00433
00434 if(stream_num<128){
00435 if (!strcmp(name, "AspectRatioX")) dar[stream_num].num= value_num;
00436 else if(!strcmp(name, "AspectRatioY")) dar[stream_num].den= value_num;
00437 }
00438 }
00439 } else if (!memcmp(&g, &ff_asf_ext_stream_header, sizeof(GUID))) {
00440 int ext_len, payload_ext_ct, stream_ct;
00441 uint32_t ext_d, leak_rate, stream_num;
00442 int64_t pos_ex_st;
00443 pos_ex_st = url_ftell(pb);
00444
00445 get_le64(pb);
00446 get_le64(pb);
00447 leak_rate = get_le32(pb);
00448 get_le32(pb);
00449 get_le32(pb);
00450 get_le32(pb);
00451 get_le32(pb);
00452 get_le32(pb);
00453 get_le32(pb);
00454 get_le32(pb);
00455 stream_num = get_le16(pb);
00456 get_le16(pb);
00457 get_le64(pb);
00458 stream_ct = get_le16(pb);
00459 payload_ext_ct = get_le16(pb);
00460
00461 if (stream_num < 128)
00462 bitrate[stream_num] = leak_rate;
00463
00464 for (i=0; i<stream_ct; i++){
00465 get_le16(pb);
00466 ext_len = get_le16(pb);
00467 url_fseek(pb, ext_len, SEEK_CUR);
00468 }
00469
00470 for (i=0; i<payload_ext_ct; i++){
00471 get_guid(pb, &g);
00472 ext_d=get_le16(pb);
00473 ext_len=get_le32(pb);
00474 url_fseek(pb, ext_len, SEEK_CUR);
00475 }
00476
00477
00478
00479 } else if (!memcmp(&g, &ff_asf_head1_guid, sizeof(GUID))) {
00480 int v1, v2;
00481 get_guid(pb, &g);
00482 v1 = get_le32(pb);
00483 v2 = get_le16(pb);
00484 #if 0
00485 } else if (!memcmp(&g, &ff_asf_codec_comment_header, sizeof(GUID))) {
00486 int len, v1, n, num;
00487 char str[256], *q;
00488 char tag[16];
00489
00490 get_guid(pb, &g);
00491 print_guid(&g);
00492
00493 n = get_le32(pb);
00494 for(i=0;i<n;i++) {
00495 num = get_le16(pb);
00496 get_str16(pb, str, sizeof(str));
00497 get_str16(pb, str, sizeof(str));
00498 len = get_le16(pb);
00499 q = tag;
00500 while (len > 0) {
00501 v1 = get_byte(pb);
00502 if ((q - tag) < sizeof(tag) - 1)
00503 *q++ = v1;
00504 len--;
00505 }
00506 *q = '\0';
00507 }
00508 #endif
00509 } else if (url_feof(pb)) {
00510 return -1;
00511 } else {
00512 url_fseek(pb, gsize - 24, SEEK_CUR);
00513 }
00514 }
00515 get_guid(pb, &g);
00516 get_le64(pb);
00517 get_byte(pb);
00518 get_byte(pb);
00519 if (url_feof(pb))
00520 return -1;
00521 asf->data_offset = url_ftell(pb);
00522 asf->packet_size_left = 0;
00523
00524
00525 for(i=0; i<128; i++){
00526 int stream_num= asf->asfid2avid[i];
00527 if(stream_num>=0){
00528 AVStream *st = s->streams[stream_num];
00529 if (!st->codec->bit_rate)
00530 st->codec->bit_rate = bitrate[i];
00531 if (dar[i].num > 0 && dar[i].den > 0)
00532 av_reduce(&st->sample_aspect_ratio.num,
00533 &st->sample_aspect_ratio.den,
00534 dar[i].num, dar[i].den, INT_MAX);
00535
00536 }
00537 }
00538
00539 return 0;
00540 }
00541
00542 #define DO_2BITS(bits, var, defval) \
00543 switch (bits & 3) \
00544 { \
00545 case 3: var = get_le32(pb); rsize += 4; break; \
00546 case 2: var = get_le16(pb); rsize += 2; break; \
00547 case 1: var = get_byte(pb); rsize++; break; \
00548 default: var = defval; break; \
00549 }
00550
00551 int ff_asf_get_packet(AVFormatContext *s, ByteIOContext *pb)
00552 {
00553 ASFContext *asf = s->priv_data;
00554 uint32_t packet_length, padsize;
00555 int rsize = 8;
00556 int c, d, e, off;
00557
00558 off= 32768;
00559 if (s->packet_size > 0)
00560 off= (url_ftell(pb) - s->data_offset) % s->packet_size + 3;
00561
00562 c=d=e=-1;
00563 while(off-- > 0){
00564 c=d; d=e;
00565 e= get_byte(pb);
00566 if(c == 0x82 && !d && !e)
00567 break;
00568 }
00569
00570 if (c != 0x82) {
00571 if (!url_feof(pb))
00572 av_log(s, AV_LOG_ERROR, "ff asf bad header %x at:%"PRId64"\n", c, url_ftell(pb));
00573 }
00574 if ((c & 0x8f) == 0x82) {
00575 if (d || e) {
00576 if (!url_feof(pb))
00577 av_log(s, AV_LOG_ERROR, "ff asf bad non zero\n");
00578 return -1;
00579 }
00580 c= get_byte(pb);
00581 d= get_byte(pb);
00582 rsize+=3;
00583 }else{
00584 url_fseek(pb, -1, SEEK_CUR);
00585 }
00586
00587 asf->packet_flags = c;
00588 asf->packet_property = d;
00589
00590 DO_2BITS(asf->packet_flags >> 5, packet_length, s->packet_size);
00591 DO_2BITS(asf->packet_flags >> 1, padsize, 0);
00592 DO_2BITS(asf->packet_flags >> 3, padsize, 0);
00593
00594
00595 if(!packet_length || packet_length >= (1U<<29)){
00596 av_log(s, AV_LOG_ERROR, "invalid packet_length %d at:%"PRId64"\n", packet_length, url_ftell(pb));
00597 return -1;
00598 }
00599 if(padsize >= packet_length){
00600 av_log(s, AV_LOG_ERROR, "invalid padsize %d at:%"PRId64"\n", padsize, url_ftell(pb));
00601 return -1;
00602 }
00603
00604 asf->packet_timestamp = get_le32(pb);
00605 get_le16(pb);
00606
00607
00608 if (asf->packet_flags & 0x01) {
00609 asf->packet_segsizetype = get_byte(pb); rsize++;
00610 asf->packet_segments = asf->packet_segsizetype & 0x3f;
00611 } else {
00612 asf->packet_segments = 1;
00613 asf->packet_segsizetype = 0x80;
00614 }
00615 asf->packet_size_left = packet_length - padsize - rsize;
00616 if (packet_length < asf->hdr.min_pktsize)
00617 padsize += asf->hdr.min_pktsize - packet_length;
00618 asf->packet_padsize = padsize;
00619 dprintf(s, "packet: size=%d padsize=%d left=%d\n", s->packet_size, asf->packet_padsize, asf->packet_size_left);
00620 return 0;
00621 }
00622
00627 static int asf_read_frame_header(AVFormatContext *s, ByteIOContext *pb){
00628 ASFContext *asf = s->priv_data;
00629 int rsize = 1;
00630 int num = get_byte(pb);
00631 int64_t ts0, ts1;
00632
00633 asf->packet_segments--;
00634 asf->packet_key_frame = num >> 7;
00635 asf->stream_index = asf->asfid2avid[num & 0x7f];
00636
00637 DO_2BITS(asf->packet_property >> 4, asf->packet_seq, 0);
00638 DO_2BITS(asf->packet_property >> 2, asf->packet_frag_offset, 0);
00639 DO_2BITS(asf->packet_property, asf->packet_replic_size, 0);
00640
00641 if (asf->packet_replic_size >= 8) {
00642 asf->packet_obj_size = get_le32(pb);
00643 if(asf->packet_obj_size >= (1<<24) || asf->packet_obj_size <= 0){
00644 av_log(s, AV_LOG_ERROR, "packet_obj_size invalid\n");
00645 return -1;
00646 }
00647 asf->packet_frag_timestamp = get_le32(pb);
00648 if(asf->packet_replic_size >= 8+38+4){
00649
00650
00651
00652 url_fskip(pb, 10);
00653 ts0= get_le64(pb);
00654 ts1= get_le64(pb);
00655 url_fskip(pb, 12);
00656 get_le32(pb);
00657 url_fskip(pb, asf->packet_replic_size - 8 - 38 - 4);
00658 if(ts0!= -1) asf->packet_frag_timestamp= ts0/10000;
00659 else asf->packet_frag_timestamp= AV_NOPTS_VALUE;
00660 }else
00661 url_fskip(pb, asf->packet_replic_size - 8);
00662 rsize += asf->packet_replic_size;
00663 } else if (asf->packet_replic_size==1){
00664
00665 asf->packet_time_start = asf->packet_frag_offset;
00666 asf->packet_frag_offset = 0;
00667 asf->packet_frag_timestamp = asf->packet_timestamp;
00668
00669 asf->packet_time_delta = get_byte(pb);
00670 rsize++;
00671 }else if(asf->packet_replic_size!=0){
00672 av_log(s, AV_LOG_ERROR, "unexpected packet_replic_size of %d\n", asf->packet_replic_size);
00673 return -1;
00674 }
00675 if (asf->packet_flags & 0x01) {
00676 DO_2BITS(asf->packet_segsizetype >> 6, asf->packet_frag_size, 0);
00677 if(asf->packet_frag_size > asf->packet_size_left - rsize){
00678 av_log(s, AV_LOG_ERROR, "packet_frag_size is invalid\n");
00679 return -1;
00680 }
00681
00682 } else {
00683 asf->packet_frag_size = asf->packet_size_left - rsize;
00684
00685 }
00686 if (asf->packet_replic_size == 1) {
00687 asf->packet_multi_size = asf->packet_frag_size;
00688 if (asf->packet_multi_size > asf->packet_size_left)
00689 return -1;
00690 }
00691 asf->packet_size_left -= rsize;
00692
00693
00694 return 0;
00695 }
00696
00697 int ff_asf_parse_packet(AVFormatContext *s, ByteIOContext *pb, AVPacket *pkt)
00698 {
00699 ASFContext *asf = s->priv_data;
00700 ASFStream *asf_st = 0;
00701 for (;;) {
00702 if(url_feof(pb))
00703 return AVERROR(EIO);
00704 if (asf->packet_size_left < FRAME_HEADER_SIZE
00705 || asf->packet_segments < 1) {
00706
00707 int ret = asf->packet_size_left + asf->packet_padsize;
00708
00709 assert(ret>=0);
00710
00711 url_fskip(pb, ret);
00712
00713 asf->packet_pos= url_ftell(pb);
00714 if (asf->data_object_size != (uint64_t)-1 &&
00715 (asf->packet_pos - asf->data_object_offset >= asf->data_object_size))
00716 return AVERROR(EIO);
00717 return 1;
00718 }
00719 if (asf->packet_time_start == 0) {
00720 if(asf_read_frame_header(s, pb) < 0){
00721 asf->packet_segments= 0;
00722 continue;
00723 }
00724 if (asf->stream_index < 0
00725 || s->streams[asf->stream_index]->discard >= AVDISCARD_ALL
00726 || (!asf->packet_key_frame && s->streams[asf->stream_index]->discard >= AVDISCARD_NONKEY)
00727 ) {
00728 asf->packet_time_start = 0;
00729
00730 url_fskip(pb, asf->packet_frag_size);
00731 asf->packet_size_left -= asf->packet_frag_size;
00732 if(asf->stream_index < 0)
00733 av_log(s, AV_LOG_ERROR, "ff asf skip %d (unknown stream)\n", asf->packet_frag_size);
00734 continue;
00735 }
00736 asf->asf_st = s->streams[asf->stream_index]->priv_data;
00737 }
00738 asf_st = asf->asf_st;
00739
00740 if (asf->packet_replic_size == 1) {
00741
00742 asf->packet_frag_timestamp = asf->packet_time_start;
00743 asf->packet_time_start += asf->packet_time_delta;
00744 asf->packet_obj_size = asf->packet_frag_size = get_byte(pb);
00745 asf->packet_size_left--;
00746 asf->packet_multi_size--;
00747 if (asf->packet_multi_size < asf->packet_obj_size)
00748 {
00749 asf->packet_time_start = 0;
00750 url_fskip(pb, asf->packet_multi_size);
00751 asf->packet_size_left -= asf->packet_multi_size;
00752 continue;
00753 }
00754 asf->packet_multi_size -= asf->packet_obj_size;
00755
00756 }
00757 if(
00758 asf_st->frag_offset + asf->packet_frag_size <= asf_st->pkt.size
00759 && asf_st->frag_offset + asf->packet_frag_size > asf->packet_obj_size){
00760 av_log(s, AV_LOG_INFO, "ignoring invalid packet_obj_size (%d %d %d %d)\n",
00761 asf_st->frag_offset, asf->packet_frag_size,
00762 asf->packet_obj_size, asf_st->pkt.size);
00763 asf->packet_obj_size= asf_st->pkt.size;
00764 }
00765
00766 if ( asf_st->pkt.size != asf->packet_obj_size
00767 || asf_st->frag_offset + asf->packet_frag_size > asf_st->pkt.size) {
00768 if(asf_st->pkt.data){
00769 av_log(s, AV_LOG_INFO, "freeing incomplete packet size %d, new %d\n", asf_st->pkt.size, asf->packet_obj_size);
00770 asf_st->frag_offset = 0;
00771 av_free_packet(&asf_st->pkt);
00772 }
00773
00774 av_new_packet(&asf_st->pkt, asf->packet_obj_size);
00775 asf_st->seq = asf->packet_seq;
00776 asf_st->pkt.dts = asf->packet_frag_timestamp;
00777 asf_st->pkt.stream_index = asf->stream_index;
00778 asf_st->pkt.pos =
00779 asf_st->packet_pos= asf->packet_pos;
00780
00781
00782
00783 if (s->streams[asf->stream_index]->codec->codec_type == CODEC_TYPE_AUDIO)
00784 asf->packet_key_frame = 1;
00785 if (asf->packet_key_frame)
00786 asf_st->pkt.flags |= PKT_FLAG_KEY;
00787 }
00788
00789
00790
00791
00792
00793 asf->packet_size_left -= asf->packet_frag_size;
00794 if (asf->packet_size_left < 0)
00795 continue;
00796
00797 if( asf->packet_frag_offset >= asf_st->pkt.size
00798 || asf->packet_frag_size > asf_st->pkt.size - asf->packet_frag_offset){
00799 av_log(s, AV_LOG_ERROR, "packet fragment position invalid %u,%u not in %u\n",
00800 asf->packet_frag_offset, asf->packet_frag_size, asf_st->pkt.size);
00801 continue;
00802 }
00803
00804 get_buffer(pb, asf_st->pkt.data + asf->packet_frag_offset,
00805 asf->packet_frag_size);
00806 if (s->key && s->keylen == 20)
00807 ff_asfcrypt_dec(s->key, asf_st->pkt.data + asf->packet_frag_offset,
00808 asf->packet_frag_size);
00809 asf_st->frag_offset += asf->packet_frag_size;
00810
00811 if (asf_st->frag_offset == asf_st->pkt.size) {
00812
00813 if( s->streams[asf->stream_index]->codec->codec_id == CODEC_ID_MPEG2VIDEO
00814 && asf_st->pkt.size > 100){
00815 int i;
00816 for(i=0; i<asf_st->pkt.size && !asf_st->pkt.data[i]; i++);
00817 if(i == asf_st->pkt.size){
00818 av_log(s, AV_LOG_DEBUG, "discarding ms fart\n");
00819 asf_st->frag_offset = 0;
00820 av_free_packet(&asf_st->pkt);
00821 continue;
00822 }
00823 }
00824
00825
00826 if (asf_st->ds_span > 1) {
00827 if(asf_st->pkt.size != asf_st->ds_packet_size * asf_st->ds_span){
00828 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);
00829 }else{
00830
00831 uint8_t *newdata = av_malloc(asf_st->pkt.size);
00832 if (newdata) {
00833 int offset = 0;
00834 while (offset < asf_st->pkt.size) {
00835 int off = offset / asf_st->ds_chunk_size;
00836 int row = off / asf_st->ds_span;
00837 int col = off % asf_st->ds_span;
00838 int idx = row + col * asf_st->ds_packet_size / asf_st->ds_chunk_size;
00839
00840
00841 assert(offset + asf_st->ds_chunk_size <= asf_st->pkt.size);
00842 assert(idx+1 <= asf_st->pkt.size / asf_st->ds_chunk_size);
00843 memcpy(newdata + offset,
00844 asf_st->pkt.data + idx * asf_st->ds_chunk_size,
00845 asf_st->ds_chunk_size);
00846 offset += asf_st->ds_chunk_size;
00847 }
00848 av_free(asf_st->pkt.data);
00849 asf_st->pkt.data = newdata;
00850 }
00851 }
00852 }
00853 asf_st->frag_offset = 0;
00854 *pkt= asf_st->pkt;
00855
00856 asf_st->pkt.size = 0;
00857 asf_st->pkt.data = 0;
00858 break;
00859 }
00860 }
00861 return 0;
00862 }
00863
00864 static int asf_read_packet(AVFormatContext *s, AVPacket *pkt)
00865 {
00866 ASFContext *asf = s->priv_data;
00867
00868 for (;;) {
00869 int ret;
00870
00871
00872 if ((ret = ff_asf_parse_packet(s, s->pb, pkt)) <= 0)
00873 return ret;
00874 if ((ret = ff_asf_get_packet(s, s->pb)) < 0)
00875 assert(asf->packet_size_left < FRAME_HEADER_SIZE || asf->packet_segments < 1);
00876 asf->packet_time_start = 0;
00877 }
00878
00879 return 0;
00880 }
00881
00882
00883
00884
00885 static void asf_reset_header(AVFormatContext *s)
00886 {
00887 ASFContext *asf = s->priv_data;
00888 ASFStream *asf_st;
00889 int i;
00890
00891 asf->packet_nb_frames = 0;
00892 asf->packet_size_left = 0;
00893 asf->packet_segments = 0;
00894 asf->packet_flags = 0;
00895 asf->packet_property = 0;
00896 asf->packet_timestamp = 0;
00897 asf->packet_segsizetype = 0;
00898 asf->packet_segments = 0;
00899 asf->packet_seq = 0;
00900 asf->packet_replic_size = 0;
00901 asf->packet_key_frame = 0;
00902 asf->packet_padsize = 0;
00903 asf->packet_frag_offset = 0;
00904 asf->packet_frag_size = 0;
00905 asf->packet_frag_timestamp = 0;
00906 asf->packet_multi_size = 0;
00907 asf->packet_obj_size = 0;
00908 asf->packet_time_delta = 0;
00909 asf->packet_time_start = 0;
00910
00911 for(i=0; i<s->nb_streams; i++){
00912 asf_st= s->streams[i]->priv_data;
00913 av_free_packet(&asf_st->pkt);
00914 asf_st->frag_offset=0;
00915 asf_st->seq=0;
00916 }
00917 asf->asf_st= NULL;
00918 }
00919
00920 static int asf_read_close(AVFormatContext *s)
00921 {
00922 int i;
00923
00924 asf_reset_header(s);
00925 for(i=0;i<s->nb_streams;i++) {
00926 AVStream *st = s->streams[i];
00927 av_free(st->codec->palctrl);
00928 }
00929 return 0;
00930 }
00931
00932 static int64_t asf_read_pts(AVFormatContext *s, int stream_index, int64_t *ppos, int64_t pos_limit)
00933 {
00934 AVPacket pkt1, *pkt = &pkt1;
00935 ASFStream *asf_st;
00936 int64_t pts;
00937 int64_t pos= *ppos;
00938 int i;
00939 int64_t start_pos[s->nb_streams];
00940
00941 for(i=0; i<s->nb_streams; i++){
00942 start_pos[i]= pos;
00943 }
00944
00945 if (s->packet_size > 0)
00946 pos= (pos+s->packet_size-1-s->data_offset)/s->packet_size*s->packet_size+ s->data_offset;
00947 *ppos= pos;
00948 url_fseek(s->pb, pos, SEEK_SET);
00949
00950
00951 asf_reset_header(s);
00952 for(;;){
00953 if (av_read_frame(s, pkt) < 0){
00954 av_log(s, AV_LOG_INFO, "asf_read_pts failed\n");
00955 return AV_NOPTS_VALUE;
00956 }
00957
00958 pts= pkt->pts;
00959
00960 av_free_packet(pkt);
00961 if(pkt->flags&PKT_FLAG_KEY){
00962 i= pkt->stream_index;
00963
00964 asf_st= s->streams[i]->priv_data;
00965
00966
00967 pos= asf_st->packet_pos;
00968
00969 av_add_index_entry(s->streams[i], pos, pts, pkt->size, pos - start_pos[i] + 1, AVINDEX_KEYFRAME);
00970 start_pos[i]= asf_st->packet_pos + 1;
00971
00972 if(pkt->stream_index == stream_index)
00973 break;
00974 }
00975 }
00976
00977 *ppos= pos;
00978
00979
00980 return pts;
00981 }
00982
00983 static void asf_build_simple_index(AVFormatContext *s, int stream_index)
00984 {
00985 GUID g;
00986 ASFContext *asf = s->priv_data;
00987 int64_t gsize, itime;
00988 int64_t pos, current_pos, index_pts;
00989 int i;
00990 int pct,ict;
00991
00992 current_pos = url_ftell(s->pb);
00993
00994 url_fseek(s->pb, asf->data_object_offset + asf->data_object_size, SEEK_SET);
00995 get_guid(s->pb, &g);
00996 if (!memcmp(&g, &index_guid, sizeof(GUID))) {
00997 gsize = get_le64(s->pb);
00998 get_guid(s->pb, &g);
00999 itime=get_le64(s->pb);
01000 pct=get_le32(s->pb);
01001 ict=get_le32(s->pb);
01002 av_log(s, AV_LOG_DEBUG, "itime:0x%"PRIx64", pct:%d, ict:%d\n",itime,pct,ict);
01003
01004 for (i=0;i<ict;i++){
01005 int pktnum=get_le32(s->pb);
01006 int pktct =get_le16(s->pb);
01007 av_log(s, AV_LOG_DEBUG, "pktnum:%d, pktct:%d\n", pktnum, pktct);
01008
01009 pos=s->data_offset + s->packet_size*(int64_t)pktnum;
01010 index_pts=av_rescale(itime, i, 10000);
01011
01012 av_add_index_entry(s->streams[stream_index], pos, index_pts, s->packet_size, 0, AVINDEX_KEYFRAME);
01013 }
01014 asf->index_read= 1;
01015 }
01016 url_fseek(s->pb, current_pos, SEEK_SET);
01017 }
01018
01019 static int asf_read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags)
01020 {
01021 ASFContext *asf = s->priv_data;
01022 AVStream *st = s->streams[stream_index];
01023 int64_t pos;
01024 int index;
01025
01026 if (s->packet_size <= 0)
01027 return -1;
01028
01029
01030 if(s->pb) {
01031 int ret = av_url_read_fseek(s->pb, stream_index, pts, flags);
01032 if(ret >= 0)
01033 asf_reset_header(s);
01034 if (ret != AVERROR(ENOSYS))
01035 return ret;
01036 }
01037
01038 if (!asf->index_read)
01039 asf_build_simple_index(s, stream_index);
01040
01041 if(!(asf->index_read && st->index_entries)){
01042 if(av_seek_frame_binary(s, stream_index, pts, flags)<0)
01043 return -1;
01044 }else{
01045 index= av_index_search_timestamp(st, pts, flags);
01046 if(index<0)
01047 return -1;
01048
01049
01050 pos = st->index_entries[index].pos;
01051 pts = st->index_entries[index].timestamp;
01052
01053
01054
01055
01056
01057
01058
01059
01060
01061
01062
01063
01064
01065
01066
01067
01068
01069
01070
01071
01072
01073
01074
01075 av_log(s, AV_LOG_DEBUG, "SEEKTO: %"PRId64"\n", pos);
01076 url_fseek(s->pb, pos, SEEK_SET);
01077 }
01078 asf_reset_header(s);
01079 return 0;
01080 }
01081
01082 AVInputFormat asf_demuxer = {
01083 "asf",
01084 NULL_IF_CONFIG_SMALL("ASF format"),
01085 sizeof(ASFContext),
01086 asf_probe,
01087 asf_read_header,
01088 asf_read_packet,
01089 asf_read_close,
01090 asf_read_seek,
01091 asf_read_pts,
01092 .metadata_conv = ff_asf_metadata_conv,
01093 };