00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00028 #include "libavutil/base64.h"
00029 #include "libavutil/avstring.h"
00030 #include "libavutil/intreadwrite.h"
00031 #include "rtp.h"
00032 #include "rtpdec_formats.h"
00033 #include "rtsp.h"
00034 #include "asf.h"
00035 #include "avio_internal.h"
00036 #include "internal.h"
00037
00045 static int rtp_asf_fix_header(uint8_t *buf, int len)
00046 {
00047 uint8_t *p = buf, *end = buf + len;
00048
00049 if (len < sizeof(ff_asf_guid) * 2 + 22 ||
00050 memcmp(p, ff_asf_header, sizeof(ff_asf_guid))) {
00051 return -1;
00052 }
00053 p += sizeof(ff_asf_guid) + 14;
00054 do {
00055 uint64_t chunksize = AV_RL64(p + sizeof(ff_asf_guid));
00056 if (memcmp(p, ff_asf_file_header, sizeof(ff_asf_guid))) {
00057 if (chunksize > end - p)
00058 return -1;
00059 p += chunksize;
00060 continue;
00061 }
00062
00063
00064 p += 6 * 8 + 3 * 4 + sizeof(ff_asf_guid) * 2;
00065 if (p + 8 <= end && AV_RL32(p) == AV_RL32(p + 4)) {
00066
00067 AV_WL32(p, 0);
00068 return 0;
00069 }
00070 break;
00071 } while (end - p >= sizeof(ff_asf_guid) + 8);
00072
00073 return -1;
00074 }
00075
00082 static int packetizer_read(void *opaque, uint8_t *buf, int buf_size)
00083 {
00084 return AVERROR(EAGAIN);
00085 }
00086
00087 static void init_packetizer(AVIOContext *pb, uint8_t *buf, int len)
00088 {
00089 ffio_init_context(pb, buf, len, 0, NULL, packetizer_read, NULL, NULL);
00090
00091
00092 pb->pos = len;
00093 pb->buf_end = buf + len;
00094 }
00095
00096 int ff_wms_parse_sdp_a_line(AVFormatContext *s, const char *p)
00097 {
00098 int ret = 0;
00099 if (av_strstart(p, "pgmpu:data:application/vnd.ms.wms-hdr.asfv1;base64,", &p)) {
00100 AVIOContext pb;
00101 RTSPState *rt = s->priv_data;
00102 int len = strlen(p) * 6 / 8;
00103 char *buf = av_mallocz(len);
00104 av_base64_decode(buf, p, len);
00105
00106 if (rtp_asf_fix_header(buf, len) < 0)
00107 av_log(s, AV_LOG_ERROR,
00108 "Failed to fix invalid RTSP-MS/ASF min_pktsize\n");
00109 init_packetizer(&pb, buf, len);
00110 if (rt->asf_ctx) {
00111 av_close_input_file(rt->asf_ctx);
00112 rt->asf_ctx = NULL;
00113 }
00114 if (!(rt->asf_ctx = avformat_alloc_context()))
00115 return AVERROR(ENOMEM);
00116 rt->asf_ctx->pb = &pb;
00117 ret = avformat_open_input(&rt->asf_ctx, "", &ff_asf_demuxer, NULL);
00118 if (ret < 0)
00119 return ret;
00120 av_dict_copy(&s->metadata, rt->asf_ctx->metadata, 0);
00121 rt->asf_pb_pos = avio_tell(&pb);
00122 av_free(buf);
00123 rt->asf_ctx->pb = NULL;
00124 }
00125 return ret;
00126 }
00127
00128 static int asfrtp_parse_sdp_line(AVFormatContext *s, int stream_index,
00129 PayloadContext *asf, const char *line)
00130 {
00131 if (av_strstart(line, "stream:", &line)) {
00132 RTSPState *rt = s->priv_data;
00133
00134 s->streams[stream_index]->id = strtol(line, NULL, 10);
00135
00136 if (rt->asf_ctx) {
00137 int i;
00138
00139 for (i = 0; i < rt->asf_ctx->nb_streams; i++) {
00140 if (s->streams[stream_index]->id == rt->asf_ctx->streams[i]->id) {
00141 *s->streams[stream_index]->codec =
00142 *rt->asf_ctx->streams[i]->codec;
00143 rt->asf_ctx->streams[i]->codec->extradata_size = 0;
00144 rt->asf_ctx->streams[i]->codec->extradata = NULL;
00145 avpriv_set_pts_info(s->streams[stream_index], 32, 1, 1000);
00146 }
00147 }
00148 }
00149 }
00150
00151 return 0;
00152 }
00153
00154 struct PayloadContext {
00155 AVIOContext *pktbuf, pb;
00156 uint8_t *buf;
00157 };
00158
00164 static int asfrtp_parse_packet(AVFormatContext *s, PayloadContext *asf,
00165 AVStream *st, AVPacket *pkt,
00166 uint32_t *timestamp,
00167 const uint8_t *buf, int len, int flags)
00168 {
00169 AVIOContext *pb = &asf->pb;
00170 int res, mflags, len_off;
00171 RTSPState *rt = s->priv_data;
00172
00173 if (!rt->asf_ctx)
00174 return -1;
00175
00176 if (len > 0) {
00177 int off, out_len = 0;
00178
00179 if (len < 4)
00180 return -1;
00181
00182 av_freep(&asf->buf);
00183
00184 ffio_init_context(pb, buf, len, 0, NULL, NULL, NULL, NULL);
00185
00186 while (avio_tell(pb) + 4 < len) {
00187 int start_off = avio_tell(pb);
00188
00189 mflags = avio_r8(pb);
00190 if (mflags & 0x80)
00191 flags |= RTP_FLAG_KEY;
00192 len_off = avio_rb24(pb);
00193 if (mflags & 0x20)
00194 avio_skip(pb, 4);
00195 if (mflags & 0x10)
00196 avio_skip(pb, 4);
00197 if (mflags & 0x8)
00198 avio_skip(pb, 4);
00199 off = avio_tell(pb);
00200
00201 if (!(mflags & 0x40)) {
00208 if (asf->pktbuf && len_off != avio_tell(asf->pktbuf)) {
00209 uint8_t *p;
00210 avio_close_dyn_buf(asf->pktbuf, &p);
00211 asf->pktbuf = NULL;
00212 av_free(p);
00213 }
00214 if (!len_off && !asf->pktbuf &&
00215 (res = avio_open_dyn_buf(&asf->pktbuf)) < 0)
00216 return res;
00217 if (!asf->pktbuf)
00218 return AVERROR(EIO);
00219
00220 avio_write(asf->pktbuf, buf + off, len - off);
00221 avio_skip(pb, len - off);
00222 if (!(flags & RTP_FLAG_MARKER))
00223 return -1;
00224 out_len = avio_close_dyn_buf(asf->pktbuf, &asf->buf);
00225 asf->pktbuf = NULL;
00226 } else {
00235 int cur_len = start_off + len_off - off;
00236 int prev_len = out_len;
00237 void *newmem;
00238 out_len += cur_len;
00239 if (FFMIN(cur_len, len - off) < 0)
00240 return -1;
00241 newmem = av_realloc(asf->buf, out_len);
00242 if (!newmem)
00243 return -1;
00244 asf->buf = newmem;
00245 memcpy(asf->buf + prev_len, buf + off,
00246 FFMIN(cur_len, len - off));
00247 avio_skip(pb, cur_len);
00248 }
00249 }
00250
00251 init_packetizer(pb, asf->buf, out_len);
00252 pb->pos += rt->asf_pb_pos;
00253 pb->eof_reached = 0;
00254 rt->asf_ctx->pb = pb;
00255 }
00256
00257 for (;;) {
00258 int i;
00259
00260 res = av_read_packet(rt->asf_ctx, pkt);
00261 rt->asf_pb_pos = avio_tell(pb);
00262 if (res != 0)
00263 break;
00264 for (i = 0; i < s->nb_streams; i++) {
00265 if (s->streams[i]->id == rt->asf_ctx->streams[pkt->stream_index]->id) {
00266 pkt->stream_index = i;
00267 return 1;
00268 }
00269 }
00270 av_free_packet(pkt);
00271 }
00272
00273 return res == 1 ? -1 : res;
00274 }
00275
00276 static PayloadContext *asfrtp_new_context(void)
00277 {
00278 return av_mallocz(sizeof(PayloadContext));
00279 }
00280
00281 static void asfrtp_free_context(PayloadContext *asf)
00282 {
00283 if (asf->pktbuf) {
00284 uint8_t *p = NULL;
00285 avio_close_dyn_buf(asf->pktbuf, &p);
00286 asf->pktbuf = NULL;
00287 av_free(p);
00288 }
00289 av_freep(&asf->buf);
00290 av_free(asf);
00291 }
00292
00293 #define RTP_ASF_HANDLER(n, s, t) \
00294 RTPDynamicProtocolHandler ff_ms_rtp_ ## n ## _handler = { \
00295 .enc_name = s, \
00296 .codec_type = t, \
00297 .codec_id = CODEC_ID_NONE, \
00298 .parse_sdp_a_line = asfrtp_parse_sdp_line, \
00299 .alloc = asfrtp_new_context, \
00300 .free = asfrtp_free_context, \
00301 .parse_packet = asfrtp_parse_packet, \
00302 }
00303
00304 RTP_ASF_HANDLER(asf_pfv, "x-asf-pf", AVMEDIA_TYPE_VIDEO);
00305 RTP_ASF_HANDLER(asf_pfa, "x-asf-pf", AVMEDIA_TYPE_AUDIO);