annotate ffmpeg/libavformat/rtpdec_asf.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents f445c3017523
children
rev   line source
yading@11 1 /*
yading@11 2 * Microsoft RTP/ASF support.
yading@11 3 * Copyright (c) 2008 Ronald S. Bultje
yading@11 4 *
yading@11 5 * This file is part of FFmpeg.
yading@11 6 *
yading@11 7 * FFmpeg is free software; you can redistribute it and/or
yading@11 8 * modify it under the terms of the GNU Lesser General Public
yading@11 9 * License as published by the Free Software Foundation; either
yading@11 10 * version 2.1 of the License, or (at your option) any later version.
yading@11 11 *
yading@11 12 * FFmpeg is distributed in the hope that it will be useful,
yading@11 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@11 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@11 15 * Lesser General Public License for more details.
yading@11 16 *
yading@11 17 * You should have received a copy of the GNU Lesser General Public
yading@11 18 * License along with FFmpeg; if not, write to the Free Software
yading@11 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@11 20 */
yading@11 21
yading@11 22 /**
yading@11 23 * @file
yading@11 24 * @brief Microsoft RTP/ASF support
yading@11 25 * @author Ronald S. Bultje <rbultje@ronald.bitfreak.net>
yading@11 26 */
yading@11 27
yading@11 28 #include "libavutil/base64.h"
yading@11 29 #include "libavutil/avstring.h"
yading@11 30 #include "libavutil/intreadwrite.h"
yading@11 31 #include "rtp.h"
yading@11 32 #include "rtpdec_formats.h"
yading@11 33 #include "rtsp.h"
yading@11 34 #include "asf.h"
yading@11 35 #include "avio_internal.h"
yading@11 36 #include "internal.h"
yading@11 37
yading@11 38 /**
yading@11 39 * From MSDN 2.2.1.4, we learn that ASF data packets over RTP should not
yading@11 40 * contain any padding. Unfortunately, the header min/max_pktsize are not
yading@11 41 * updated (thus making min_pktsize invalid). Here, we "fix" these faulty
yading@11 42 * min_pktsize values in the ASF file header.
yading@11 43 * @return 0 on success, <0 on failure (currently -1).
yading@11 44 */
yading@11 45 static int rtp_asf_fix_header(uint8_t *buf, int len)
yading@11 46 {
yading@11 47 uint8_t *p = buf, *end = buf + len;
yading@11 48
yading@11 49 if (len < sizeof(ff_asf_guid) * 2 + 22 ||
yading@11 50 memcmp(p, ff_asf_header, sizeof(ff_asf_guid))) {
yading@11 51 return -1;
yading@11 52 }
yading@11 53 p += sizeof(ff_asf_guid) + 14;
yading@11 54 do {
yading@11 55 uint64_t chunksize = AV_RL64(p + sizeof(ff_asf_guid));
yading@11 56 if (memcmp(p, ff_asf_file_header, sizeof(ff_asf_guid))) {
yading@11 57 if (chunksize > end - p)
yading@11 58 return -1;
yading@11 59 p += chunksize;
yading@11 60 continue;
yading@11 61 }
yading@11 62
yading@11 63 /* skip most of the file header, to min_pktsize */
yading@11 64 p += 6 * 8 + 3 * 4 + sizeof(ff_asf_guid) * 2;
yading@11 65 if (p + 8 <= end && AV_RL32(p) == AV_RL32(p + 4)) {
yading@11 66 /* and set that to zero */
yading@11 67 AV_WL32(p, 0);
yading@11 68 return 0;
yading@11 69 }
yading@11 70 break;
yading@11 71 } while (end - p >= sizeof(ff_asf_guid) + 8);
yading@11 72
yading@11 73 return -1;
yading@11 74 }
yading@11 75
yading@11 76 /**
yading@11 77 * The following code is basically a buffered AVIOContext,
yading@11 78 * with the added benefit of returning -EAGAIN (instead of 0)
yading@11 79 * on packet boundaries, such that the ASF demuxer can return
yading@11 80 * safely and resume business at the next packet.
yading@11 81 */
yading@11 82 static int packetizer_read(void *opaque, uint8_t *buf, int buf_size)
yading@11 83 {
yading@11 84 return AVERROR(EAGAIN);
yading@11 85 }
yading@11 86
yading@11 87 static void init_packetizer(AVIOContext *pb, uint8_t *buf, int len)
yading@11 88 {
yading@11 89 ffio_init_context(pb, buf, len, 0, NULL, packetizer_read, NULL, NULL);
yading@11 90
yading@11 91 /* this "fills" the buffer with its current content */
yading@11 92 pb->pos = len;
yading@11 93 pb->buf_end = buf + len;
yading@11 94 }
yading@11 95
yading@11 96 int ff_wms_parse_sdp_a_line(AVFormatContext *s, const char *p)
yading@11 97 {
yading@11 98 int ret = 0;
yading@11 99 if (av_strstart(p, "pgmpu:data:application/vnd.ms.wms-hdr.asfv1;base64,", &p)) {
yading@11 100 AVIOContext pb;
yading@11 101 RTSPState *rt = s->priv_data;
yading@11 102 AVDictionary *opts = NULL;
yading@11 103 int len = strlen(p) * 6 / 8;
yading@11 104 char *buf = av_mallocz(len);
yading@11 105 av_base64_decode(buf, p, len);
yading@11 106
yading@11 107 if (rtp_asf_fix_header(buf, len) < 0)
yading@11 108 av_log(s, AV_LOG_ERROR,
yading@11 109 "Failed to fix invalid RTSP-MS/ASF min_pktsize\n");
yading@11 110 init_packetizer(&pb, buf, len);
yading@11 111 if (rt->asf_ctx) {
yading@11 112 avformat_close_input(&rt->asf_ctx);
yading@11 113 }
yading@11 114 if (!(rt->asf_ctx = avformat_alloc_context()))
yading@11 115 return AVERROR(ENOMEM);
yading@11 116 rt->asf_ctx->pb = &pb;
yading@11 117 av_dict_set(&opts, "no_resync_search", "1", 0);
yading@11 118 ret = avformat_open_input(&rt->asf_ctx, "", &ff_asf_demuxer, &opts);
yading@11 119 av_dict_free(&opts);
yading@11 120 if (ret < 0)
yading@11 121 return ret;
yading@11 122 av_dict_copy(&s->metadata, rt->asf_ctx->metadata, 0);
yading@11 123 rt->asf_pb_pos = avio_tell(&pb);
yading@11 124 av_free(buf);
yading@11 125 rt->asf_ctx->pb = NULL;
yading@11 126 }
yading@11 127 return ret;
yading@11 128 }
yading@11 129
yading@11 130 static int asfrtp_parse_sdp_line(AVFormatContext *s, int stream_index,
yading@11 131 PayloadContext *asf, const char *line)
yading@11 132 {
yading@11 133 if (stream_index < 0)
yading@11 134 return 0;
yading@11 135 if (av_strstart(line, "stream:", &line)) {
yading@11 136 RTSPState *rt = s->priv_data;
yading@11 137
yading@11 138 s->streams[stream_index]->id = strtol(line, NULL, 10);
yading@11 139
yading@11 140 if (rt->asf_ctx) {
yading@11 141 int i;
yading@11 142
yading@11 143 for (i = 0; i < rt->asf_ctx->nb_streams; i++) {
yading@11 144 if (s->streams[stream_index]->id == rt->asf_ctx->streams[i]->id) {
yading@11 145 *s->streams[stream_index]->codec =
yading@11 146 *rt->asf_ctx->streams[i]->codec;
yading@11 147 rt->asf_ctx->streams[i]->codec->extradata_size = 0;
yading@11 148 rt->asf_ctx->streams[i]->codec->extradata = NULL;
yading@11 149 avpriv_set_pts_info(s->streams[stream_index], 32, 1, 1000);
yading@11 150 }
yading@11 151 }
yading@11 152 }
yading@11 153 }
yading@11 154
yading@11 155 return 0;
yading@11 156 }
yading@11 157
yading@11 158 struct PayloadContext {
yading@11 159 AVIOContext *pktbuf, pb;
yading@11 160 uint8_t *buf;
yading@11 161 };
yading@11 162
yading@11 163 /**
yading@11 164 * @return 0 when a packet was written into /p pkt, and no more data is left;
yading@11 165 * 1 when a packet was written into /p pkt, and more packets might be left;
yading@11 166 * <0 when not enough data was provided to return a full packet, or on error.
yading@11 167 */
yading@11 168 static int asfrtp_parse_packet(AVFormatContext *s, PayloadContext *asf,
yading@11 169 AVStream *st, AVPacket *pkt,
yading@11 170 uint32_t *timestamp,
yading@11 171 const uint8_t *buf, int len, uint16_t seq,
yading@11 172 int flags)
yading@11 173 {
yading@11 174 AVIOContext *pb = &asf->pb;
yading@11 175 int res, mflags, len_off;
yading@11 176 RTSPState *rt = s->priv_data;
yading@11 177
yading@11 178 if (!rt->asf_ctx)
yading@11 179 return -1;
yading@11 180
yading@11 181 if (len > 0) {
yading@11 182 int off, out_len = 0;
yading@11 183
yading@11 184 if (len < 4)
yading@11 185 return -1;
yading@11 186
yading@11 187 av_freep(&asf->buf);
yading@11 188
yading@11 189 ffio_init_context(pb, buf, len, 0, NULL, NULL, NULL, NULL);
yading@11 190
yading@11 191 while (avio_tell(pb) + 4 < len) {
yading@11 192 int start_off = avio_tell(pb);
yading@11 193
yading@11 194 mflags = avio_r8(pb);
yading@11 195 if (mflags & 0x80)
yading@11 196 flags |= RTP_FLAG_KEY;
yading@11 197 len_off = avio_rb24(pb);
yading@11 198 if (mflags & 0x20) /**< relative timestamp */
yading@11 199 avio_skip(pb, 4);
yading@11 200 if (mflags & 0x10) /**< has duration */
yading@11 201 avio_skip(pb, 4);
yading@11 202 if (mflags & 0x8) /**< has location ID */
yading@11 203 avio_skip(pb, 4);
yading@11 204 off = avio_tell(pb);
yading@11 205
yading@11 206 if (!(mflags & 0x40)) {
yading@11 207 /**
yading@11 208 * If 0x40 is not set, the len_off field specifies an offset
yading@11 209 * of this packet's payload data in the complete (reassembled)
yading@11 210 * ASF packet. This is used to spread one ASF packet over
yading@11 211 * multiple RTP packets.
yading@11 212 */
yading@11 213 if (asf->pktbuf && len_off != avio_tell(asf->pktbuf)) {
yading@11 214 uint8_t *p;
yading@11 215 avio_close_dyn_buf(asf->pktbuf, &p);
yading@11 216 asf->pktbuf = NULL;
yading@11 217 av_free(p);
yading@11 218 }
yading@11 219 if (!len_off && !asf->pktbuf &&
yading@11 220 (res = avio_open_dyn_buf(&asf->pktbuf)) < 0)
yading@11 221 return res;
yading@11 222 if (!asf->pktbuf)
yading@11 223 return AVERROR(EIO);
yading@11 224
yading@11 225 avio_write(asf->pktbuf, buf + off, len - off);
yading@11 226 avio_skip(pb, len - off);
yading@11 227 if (!(flags & RTP_FLAG_MARKER))
yading@11 228 return -1;
yading@11 229 out_len = avio_close_dyn_buf(asf->pktbuf, &asf->buf);
yading@11 230 asf->pktbuf = NULL;
yading@11 231 } else {
yading@11 232 /**
yading@11 233 * If 0x40 is set, the len_off field specifies the length of
yading@11 234 * the next ASF packet that can be read from this payload
yading@11 235 * data alone. This is commonly the same as the payload size,
yading@11 236 * but could be less in case of packet splitting (i.e.
yading@11 237 * multiple ASF packets in one RTP packet).
yading@11 238 */
yading@11 239
yading@11 240 int cur_len = start_off + len_off - off;
yading@11 241 int prev_len = out_len;
yading@11 242 void *newmem;
yading@11 243 out_len += cur_len;
yading@11 244 if (FFMIN(cur_len, len - off) < 0)
yading@11 245 return -1;
yading@11 246 newmem = av_realloc(asf->buf, out_len);
yading@11 247 if (!newmem)
yading@11 248 return -1;
yading@11 249 asf->buf = newmem;
yading@11 250 memcpy(asf->buf + prev_len, buf + off,
yading@11 251 FFMIN(cur_len, len - off));
yading@11 252 avio_skip(pb, cur_len);
yading@11 253 }
yading@11 254 }
yading@11 255
yading@11 256 init_packetizer(pb, asf->buf, out_len);
yading@11 257 pb->pos += rt->asf_pb_pos;
yading@11 258 pb->eof_reached = 0;
yading@11 259 rt->asf_ctx->pb = pb;
yading@11 260 }
yading@11 261
yading@11 262 for (;;) {
yading@11 263 int i;
yading@11 264
yading@11 265 res = ff_read_packet(rt->asf_ctx, pkt);
yading@11 266 rt->asf_pb_pos = avio_tell(pb);
yading@11 267 if (res != 0)
yading@11 268 break;
yading@11 269 for (i = 0; i < s->nb_streams; i++) {
yading@11 270 if (s->streams[i]->id == rt->asf_ctx->streams[pkt->stream_index]->id) {
yading@11 271 pkt->stream_index = i;
yading@11 272 return 1; // FIXME: return 0 if last packet
yading@11 273 }
yading@11 274 }
yading@11 275 av_free_packet(pkt);
yading@11 276 }
yading@11 277
yading@11 278 return res == 1 ? -1 : res;
yading@11 279 }
yading@11 280
yading@11 281 static PayloadContext *asfrtp_new_context(void)
yading@11 282 {
yading@11 283 return av_mallocz(sizeof(PayloadContext));
yading@11 284 }
yading@11 285
yading@11 286 static void asfrtp_free_context(PayloadContext *asf)
yading@11 287 {
yading@11 288 if (asf->pktbuf) {
yading@11 289 uint8_t *p = NULL;
yading@11 290 avio_close_dyn_buf(asf->pktbuf, &p);
yading@11 291 asf->pktbuf = NULL;
yading@11 292 av_free(p);
yading@11 293 }
yading@11 294 av_freep(&asf->buf);
yading@11 295 av_free(asf);
yading@11 296 }
yading@11 297
yading@11 298 #define RTP_ASF_HANDLER(n, s, t) \
yading@11 299 RTPDynamicProtocolHandler ff_ms_rtp_ ## n ## _handler = { \
yading@11 300 .enc_name = s, \
yading@11 301 .codec_type = t, \
yading@11 302 .codec_id = AV_CODEC_ID_NONE, \
yading@11 303 .parse_sdp_a_line = asfrtp_parse_sdp_line, \
yading@11 304 .alloc = asfrtp_new_context, \
yading@11 305 .free = asfrtp_free_context, \
yading@11 306 .parse_packet = asfrtp_parse_packet, \
yading@11 307 }
yading@11 308
yading@11 309 RTP_ASF_HANDLER(asf_pfv, "x-asf-pf", AVMEDIA_TYPE_VIDEO);
yading@11 310 RTP_ASF_HANDLER(asf_pfa, "x-asf-pf", AVMEDIA_TYPE_AUDIO);