Libav 0.7.1
|
00001 /* 00002 * Microsoft RTP/ASF support. 00003 * Copyright (c) 2008 Ronald S. Bultje 00004 * 00005 * This file is part of Libav. 00006 * 00007 * Libav is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * Libav is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with Libav; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 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 /* skip most of the file header, to min_pktsize */ 00063 p += 6 * 8 + 3 * 4 + sizeof(ff_asf_guid) * 2; 00064 if (p + 8 <= end && AV_RL32(p) == AV_RL32(p + 4)) { 00065 /* and set that to zero */ 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 /* this "fills" the buffer with its current content */ 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 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; // FIXME: return 0 if last packet 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);