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 avformat_close_input(&rt->asf_ctx);
00112 }
00113 if (!(rt->asf_ctx = avformat_alloc_context()))
00114 return AVERROR(ENOMEM);
00115 rt->asf_ctx->pb = &pb;
00116 ret = avformat_open_input(&rt->asf_ctx, "", &ff_asf_demuxer, NULL);
00117 if (ret < 0)
00118 return ret;
00119 av_dict_copy(&s->metadata, rt->asf_ctx->metadata, 0);
00120 rt->asf_pb_pos = avio_tell(&pb);
00121 av_free(buf);
00122 rt->asf_ctx->pb = NULL;
00123 }
00124 return ret;
00125 }
00126
00127 static int asfrtp_parse_sdp_line(AVFormatContext *s, int stream_index,
00128 PayloadContext *asf, const char *line)
00129 {
00130 if (av_strstart(line, "stream:", &line)) {
00131 RTSPState *rt = s->priv_data;
00132
00133 s->streams[stream_index]->id = strtol(line, NULL, 10);
00134
00135 if (rt->asf_ctx) {
00136 int i;
00137
00138 for (i = 0; i < rt->asf_ctx->nb_streams; i++) {
00139 if (s->streams[stream_index]->id == rt->asf_ctx->streams[i]->id) {
00140 *s->streams[stream_index]->codec =
00141 *rt->asf_ctx->streams[i]->codec;
00142 rt->asf_ctx->streams[i]->codec->extradata_size = 0;
00143 rt->asf_ctx->streams[i]->codec->extradata = NULL;
00144 avpriv_set_pts_info(s->streams[stream_index], 32, 1, 1000);
00145 }
00146 }
00147 }
00148 }
00149
00150 return 0;
00151 }
00152
00153 struct PayloadContext {
00154 AVIOContext *pktbuf, pb;
00155 uint8_t *buf;
00156 };
00157
00163 static int asfrtp_parse_packet(AVFormatContext *s, PayloadContext *asf,
00164 AVStream *st, AVPacket *pkt,
00165 uint32_t *timestamp,
00166 const uint8_t *buf, int len, int flags)
00167 {
00168 AVIOContext *pb = &asf->pb;
00169 int res, mflags, len_off;
00170 RTSPState *rt = s->priv_data;
00171
00172 if (!rt->asf_ctx)
00173 return -1;
00174
00175 if (len > 0) {
00176 int off, out_len = 0;
00177
00178 if (len < 4)
00179 return -1;
00180
00181 av_freep(&asf->buf);
00182
00183 ffio_init_context(pb, buf, len, 0, NULL, NULL, NULL, NULL);
00184
00185 while (avio_tell(pb) + 4 < len) {
00186 int start_off = avio_tell(pb);
00187
00188 mflags = avio_r8(pb);
00189 if (mflags & 0x80)
00190 flags |= RTP_FLAG_KEY;
00191 len_off = avio_rb24(pb);
00192 if (mflags & 0x20)
00193 avio_skip(pb, 4);
00194 if (mflags & 0x10)
00195 avio_skip(pb, 4);
00196 if (mflags & 0x8)
00197 avio_skip(pb, 4);
00198 off = avio_tell(pb);
00199
00200 if (!(mflags & 0x40)) {
00207 if (asf->pktbuf && len_off != avio_tell(asf->pktbuf)) {
00208 uint8_t *p;
00209 avio_close_dyn_buf(asf->pktbuf, &p);
00210 asf->pktbuf = NULL;
00211 av_free(p);
00212 }
00213 if (!len_off && !asf->pktbuf &&
00214 (res = avio_open_dyn_buf(&asf->pktbuf)) < 0)
00215 return res;
00216 if (!asf->pktbuf)
00217 return AVERROR(EIO);
00218
00219 avio_write(asf->pktbuf, buf + off, len - off);
00220 avio_skip(pb, len - off);
00221 if (!(flags & RTP_FLAG_MARKER))
00222 return -1;
00223 out_len = avio_close_dyn_buf(asf->pktbuf, &asf->buf);
00224 asf->pktbuf = NULL;
00225 } else {
00234 int cur_len = start_off + len_off - off;
00235 int prev_len = out_len;
00236 void *newmem;
00237 out_len += cur_len;
00238 if (FFMIN(cur_len, len - off) < 0)
00239 return -1;
00240 newmem = av_realloc(asf->buf, out_len);
00241 if (!newmem)
00242 return -1;
00243 asf->buf = newmem;
00244 memcpy(asf->buf + prev_len, buf + off,
00245 FFMIN(cur_len, len - off));
00246 avio_skip(pb, cur_len);
00247 }
00248 }
00249
00250 init_packetizer(pb, asf->buf, out_len);
00251 pb->pos += rt->asf_pb_pos;
00252 pb->eof_reached = 0;
00253 rt->asf_ctx->pb = pb;
00254 }
00255
00256 for (;;) {
00257 int i;
00258
00259 res = av_read_packet(rt->asf_ctx, pkt);
00260 rt->asf_pb_pos = avio_tell(pb);
00261 if (res != 0)
00262 break;
00263 for (i = 0; i < s->nb_streams; i++) {
00264 if (s->streams[i]->id == rt->asf_ctx->streams[pkt->stream_index]->id) {
00265 pkt->stream_index = i;
00266 return 1;
00267 }
00268 }
00269 av_free_packet(pkt);
00270 }
00271
00272 return res == 1 ? -1 : res;
00273 }
00274
00275 static PayloadContext *asfrtp_new_context(void)
00276 {
00277 return av_mallocz(sizeof(PayloadContext));
00278 }
00279
00280 static void asfrtp_free_context(PayloadContext *asf)
00281 {
00282 if (asf->pktbuf) {
00283 uint8_t *p = NULL;
00284 avio_close_dyn_buf(asf->pktbuf, &p);
00285 asf->pktbuf = NULL;
00286 av_free(p);
00287 }
00288 av_freep(&asf->buf);
00289 av_free(asf);
00290 }
00291
00292 #define RTP_ASF_HANDLER(n, s, t) \
00293 RTPDynamicProtocolHandler ff_ms_rtp_ ## n ## _handler = { \
00294 .enc_name = s, \
00295 .codec_type = t, \
00296 .codec_id = CODEC_ID_NONE, \
00297 .parse_sdp_a_line = asfrtp_parse_sdp_line, \
00298 .alloc = asfrtp_new_context, \
00299 .free = asfrtp_free_context, \
00300 .parse_packet = asfrtp_parse_packet, \
00301 }
00302
00303 RTP_ASF_HANDLER(asf_pfv, "x-asf-pf", AVMEDIA_TYPE_VIDEO);
00304 RTP_ASF_HANDLER(asf_pfa, "x-asf-pf", AVMEDIA_TYPE_AUDIO);