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
00044 static int rtp_asf_fix_header(uint8_t *buf, int len)
00045 {
00046 uint8_t *p = buf, *end = buf + len;
00047
00048 if (len < sizeof(ff_asf_guid) * 2 + 22 ||
00049 memcmp(p, ff_asf_header, sizeof(ff_asf_guid))) {
00050 return -1;
00051 }
00052 p += sizeof(ff_asf_guid) + 14;
00053 do {
00054 uint64_t chunksize = AV_RL64(p + sizeof(ff_asf_guid));
00055 if (memcmp(p, ff_asf_file_header, sizeof(ff_asf_guid))) {
00056 if (chunksize > end - p)
00057 return -1;
00058 p += chunksize;
00059 continue;
00060 }
00061
00062
00063 p += 6 * 8 + 3 * 4 + sizeof(ff_asf_guid) * 2;
00064 if (p + 8 <= end && AV_RL32(p) == AV_RL32(p + 4)) {
00065
00066 AV_WL32(p, 0);
00067 return 0;
00068 }
00069 break;
00070 } while (end - p >= sizeof(ff_asf_guid) + 8);
00071
00072 return -1;
00073 }
00074
00081 static int packetizer_read(void *opaque, uint8_t *buf, int buf_size)
00082 {
00083 return AVERROR(EAGAIN);
00084 }
00085
00086 static void init_packetizer(AVIOContext *pb, uint8_t *buf, int len)
00087 {
00088 ffio_init_context(pb, buf, len, 0, NULL, packetizer_read, NULL, NULL);
00089
00090
00091 pb->pos = len;
00092 pb->buf_end = buf + len;
00093 }
00094
00095 int ff_wms_parse_sdp_a_line(AVFormatContext *s, const char *p)
00096 {
00097 int ret = 0;
00098 if (av_strstart(p, "pgmpu:data:application/vnd.ms.wms-hdr.asfv1;base64,", &p)) {
00099 AVIOContext pb;
00100 RTSPState *rt = s->priv_data;
00101 int len = strlen(p) * 6 / 8;
00102 char *buf = av_mallocz(len);
00103 av_base64_decode(buf, p, len);
00104
00105 if (rtp_asf_fix_header(buf, len) < 0)
00106 av_log(s, AV_LOG_ERROR,
00107 "Failed to fix invalid RTSP-MS/ASF min_pktsize\n");
00108 init_packetizer(&pb, buf, len);
00109 if (rt->asf_ctx) {
00110 av_close_input_file(rt->asf_ctx);
00111 rt->asf_ctx = NULL;
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 av_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
00238 out_len += cur_len;
00239
00240 if (FFMIN(cur_len, len - off) < 0)
00241 return -1;
00242 newmem = av_realloc(asf->buf, out_len);
00243 if (!newmem)
00244 return -1;
00245 asf->buf = newmem;
00246 memcpy(asf->buf + prev_len, buf + off,
00247 FFMIN(cur_len, len - off));
00248 avio_skip(pb, cur_len);
00249 }
00250 }
00251
00252 init_packetizer(pb, asf->buf, out_len);
00253 pb->pos += rt->asf_pb_pos;
00254 pb->eof_reached = 0;
00255 rt->asf_ctx->pb = pb;
00256 }
00257
00258 for (;;) {
00259 int i;
00260
00261 res = av_read_packet(rt->asf_ctx, pkt);
00262 rt->asf_pb_pos = avio_tell(pb);
00263 if (res != 0)
00264 break;
00265 for (i = 0; i < s->nb_streams; i++) {
00266 if (s->streams[i]->id == rt->asf_ctx->streams[pkt->stream_index]->id) {
00267 pkt->stream_index = i;
00268 return 1;
00269 }
00270 }
00271 av_free_packet(pkt);
00272 }
00273
00274 return res == 1 ? -1 : res;
00275 }
00276
00277 static PayloadContext *asfrtp_new_context(void)
00278 {
00279 return av_mallocz(sizeof(PayloadContext));
00280 }
00281
00282 static void asfrtp_free_context(PayloadContext *asf)
00283 {
00284 if (asf->pktbuf) {
00285 uint8_t *p = NULL;
00286 avio_close_dyn_buf(asf->pktbuf, &p);
00287 asf->pktbuf = NULL;
00288 av_free(p);
00289 }
00290 av_freep(&asf->buf);
00291 av_free(asf);
00292 }
00293
00294 #define RTP_ASF_HANDLER(n, s, t) \
00295 RTPDynamicProtocolHandler ff_ms_rtp_ ## n ## _handler = { \
00296 .enc_name = s, \
00297 .codec_type = t, \
00298 .codec_id = CODEC_ID_NONE, \
00299 .parse_sdp_a_line = asfrtp_parse_sdp_line, \
00300 .alloc = asfrtp_new_context, \
00301 .free = asfrtp_free_context, \
00302 .parse_packet = asfrtp_parse_packet, \
00303 }
00304
00305 RTP_ASF_HANDLER(asf_pfv, "x-asf-pf", AVMEDIA_TYPE_VIDEO);
00306 RTP_ASF_HANDLER(asf_pfa, "x-asf-pf", AVMEDIA_TYPE_AUDIO);