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