rtpdec_asf.c 9.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * Microsoft RTP/ASF support.
 * Copyright (c) 2008 Ronald S. Bultje
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
23
 * @file
24 25 26 27
 * @brief Microsoft RTP/ASF support
 * @author Ronald S. Bultje <rbultje@ronald.bitfreak.net>
 */

28 29 30
#include "libavutil/base64.h"
#include "libavutil/avstring.h"
#include "libavutil/intreadwrite.h"
31
#include "rtp.h"
32
#include "rtpdec_formats.h"
33 34
#include "rtsp.h"
#include "asf.h"
35
#include "avio_internal.h"
36
#include "internal.h"
37

38 39 40 41 42 43 44
/**
 * From MSDN 2.2.1.4, we learn that ASF data packets over RTP should not
 * contain any padding. Unfortunately, the header min/max_pktsize are not
 * updated (thus making min_pktsize invalid). Here, we "fix" these faulty
 * min_pktsize values in the ASF file header.
 * @return 0 on success, <0 on failure (currently -1).
 */
45
static int rtp_asf_fix_header(uint8_t *buf, int len)
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
{
    uint8_t *p = buf, *end = buf + len;

    if (len < sizeof(ff_asf_guid) * 2 + 22 ||
        memcmp(p, ff_asf_header, sizeof(ff_asf_guid))) {
        return -1;
    }
    p += sizeof(ff_asf_guid) + 14;
    do {
        uint64_t chunksize = AV_RL64(p + sizeof(ff_asf_guid));
        if (memcmp(p, ff_asf_file_header, sizeof(ff_asf_guid))) {
            if (chunksize > end - p)
                return -1;
            p += chunksize;
            continue;
        }

        /* skip most of the file header, to min_pktsize */
        p += 6 * 8 + 3 * 4 + sizeof(ff_asf_guid) * 2;
        if (p + 8 <= end && AV_RL32(p) == AV_RL32(p + 4)) {
            /* and set that to zero */
            AV_WL32(p, 0);
            return 0;
        }
        break;
    } while (end - p >= sizeof(ff_asf_guid) + 8);

    return -1;
}

/**
77
 * The following code is basically a buffered AVIOContext,
78 79 80 81
 * with the added benefit of returning -EAGAIN (instead of 0)
 * on packet boundaries, such that the ASF demuxer can return
 * safely and resume business at the next packet.
 */
82
static int packetizer_read(void *opaque, uint8_t *buf, int buf_size)
83 84 85 86
{
    return AVERROR(EAGAIN);
}

87
static void init_packetizer(AVIOContext *pb, uint8_t *buf, int len)
88
{
89
    ffio_init_context(pb, buf, len, 0, NULL, packetizer_read, NULL, NULL);
90 91 92 93 94 95

    /* this "fills" the buffer with its current content */
    pb->pos     = len;
    pb->buf_end = buf + len;
}

96
int ff_wms_parse_sdp_a_line(AVFormatContext *s, const char *p)
97
{
98
    int ret = 0;
99
    if (av_strstart(p, "pgmpu:data:application/vnd.ms.wms-hdr.asfv1;base64,", &p)) {
100
        AVIOContext pb;
101
        RTSPState *rt = s->priv_data;
102
        AVDictionary *opts = NULL;
103 104 105 106
        int len = strlen(p) * 6 / 8;
        char *buf = av_mallocz(len);
        av_base64_decode(buf, p, len);

107 108 109 110
        if (rtp_asf_fix_header(buf, len) < 0)
            av_log(s, AV_LOG_ERROR,
                   "Failed to fix invalid RTSP-MS/ASF min_pktsize\n");
        init_packetizer(&pb, buf, len);
111
        if (rt->asf_ctx) {
112
            avformat_close_input(&rt->asf_ctx);
113
        }
114 115 116
        if (!(rt->asf_ctx = avformat_alloc_context()))
            return AVERROR(ENOMEM);
        rt->asf_ctx->pb      = &pb;
117 118 119
        av_dict_set(&opts, "no_resync_search", "1", 0);
        ret = avformat_open_input(&rt->asf_ctx, "", &ff_asf_demuxer, &opts);
        av_dict_free(&opts);
120 121
        if (ret < 0)
            return ret;
122
        av_dict_copy(&s->metadata, rt->asf_ctx->metadata, 0);
123
        rt->asf_pb_pos = avio_tell(&pb);
124 125 126
        av_free(buf);
        rt->asf_ctx->pb = NULL;
    }
127
    return ret;
128
}
129

130 131
static int asfrtp_parse_sdp_line(AVFormatContext *s, int stream_index,
                                 PayloadContext *asf, const char *line)
132
{
133 134
    if (stream_index < 0)
        return 0;
135 136 137 138 139 140 141 142 143 144 145 146 147 148
    if (av_strstart(line, "stream:", &line)) {
        RTSPState *rt = s->priv_data;

        s->streams[stream_index]->id = strtol(line, NULL, 10);

        if (rt->asf_ctx) {
            int i;

            for (i = 0; i < rt->asf_ctx->nb_streams; i++) {
                if (s->streams[stream_index]->id == rt->asf_ctx->streams[i]->id) {
                    *s->streams[stream_index]->codec =
                        *rt->asf_ctx->streams[i]->codec;
                    rt->asf_ctx->streams[i]->codec->extradata_size = 0;
                    rt->asf_ctx->streams[i]->codec->extradata = NULL;
149
                    avpriv_set_pts_info(s->streams[stream_index], 32, 1, 1000);
150 151 152 153 154 155 156 157
                }
           }
        }
    }

    return 0;
}

158
struct PayloadContext {
159
    AVIOContext *pktbuf, pb;
Eli Friedman's avatar
Eli Friedman committed
160
    uint8_t *buf;
161 162 163 164 165 166 167
};

/**
 * @return 0 when a packet was written into /p pkt, and no more data is left;
 *         1 when a packet was written into /p pkt, and more packets might be left;
 *        <0 when not enough data was provided to return a full packet, or on error.
 */
168 169 170 171
static int asfrtp_parse_packet(AVFormatContext *s, PayloadContext *asf,
                               AVStream *st, AVPacket *pkt,
                               uint32_t *timestamp,
                               const uint8_t *buf, int len, int flags)
172
{
173
    AVIOContext *pb = &asf->pb;
174 175 176 177 178 179 180
    int res, mflags, len_off;
    RTSPState *rt = s->priv_data;

    if (!rt->asf_ctx)
        return -1;

    if (len > 0) {
181
        int off, out_len = 0;
182 183 184 185

        if (len < 4)
            return -1;

186 187
        av_freep(&asf->buf);

188
        ffio_init_context(pb, buf, len, 0, NULL, NULL, NULL, NULL);
189

190 191
        while (avio_tell(pb) + 4 < len) {
            int start_off = avio_tell(pb);
192

193
            mflags = avio_r8(pb);
194 195
            if (mflags & 0x80)
                flags |= RTP_FLAG_KEY;
196
            len_off = avio_rb24(pb);
197
            if (mflags & 0x20)   /**< relative timestamp */
198
                avio_skip(pb, 4);
199
            if (mflags & 0x10)   /**< has duration */
200
                avio_skip(pb, 4);
201
            if (mflags & 0x8)    /**< has location ID */
202
                avio_skip(pb, 4);
203
            off = avio_tell(pb);
204 205 206 207 208 209 210 211

            if (!(mflags & 0x40)) {
                /**
                 * If 0x40 is not set, the len_off field specifies an offset
                 * of this packet's payload data in the complete (reassembled)
                 * ASF packet. This is used to spread one ASF packet over
                 * multiple RTP packets.
                 */
212
                if (asf->pktbuf && len_off != avio_tell(asf->pktbuf)) {
213
                    uint8_t *p;
214
                    avio_close_dyn_buf(asf->pktbuf, &p);
215 216 217 218
                    asf->pktbuf = NULL;
                    av_free(p);
                }
                if (!len_off && !asf->pktbuf &&
219
                    (res = avio_open_dyn_buf(&asf->pktbuf)) < 0)
220 221 222 223
                    return res;
                if (!asf->pktbuf)
                    return AVERROR(EIO);

224
                avio_write(asf->pktbuf, buf + off, len - off);
225
                avio_skip(pb, len - off);
226 227
                if (!(flags & RTP_FLAG_MARKER))
                    return -1;
228
                out_len     = avio_close_dyn_buf(asf->pktbuf, &asf->buf);
229
                asf->pktbuf = NULL;
230 231 232 233 234 235 236 237 238 239 240
            } else {
                /**
                 * If 0x40 is set, the len_off field specifies the length of
                 * the next ASF packet that can be read from this payload
                 * data alone. This is commonly the same as the payload size,
                 * but could be less in case of packet splitting (i.e.
                 * multiple ASF packets in one RTP packet).
                 */

                int cur_len = start_off + len_off - off;
                int prev_len = out_len;
241
                void *newmem;
242
                out_len += cur_len;
243
                if (FFMIN(cur_len, len - off) < 0)
244
                    return -1;
245 246
                newmem = av_realloc(asf->buf, out_len);
                if (!newmem)
247
                    return -1;
248
                asf->buf = newmem;
249 250
                memcpy(asf->buf + prev_len, buf + off,
                       FFMIN(cur_len, len - off));
251
                avio_skip(pb, cur_len);
252 253 254 255 256 257 258 259 260 261 262 263
            }
        }

        init_packetizer(pb, asf->buf, out_len);
        pb->pos += rt->asf_pb_pos;
        pb->eof_reached = 0;
        rt->asf_ctx->pb = pb;
    }

    for (;;) {
        int i;

264
        res = ff_read_packet(rt->asf_ctx, pkt);
265
        rt->asf_pb_pos = avio_tell(pb);
266 267 268 269 270 271 272 273 274 275 276 277 278 279
        if (res != 0)
            break;
        for (i = 0; i < s->nb_streams; i++) {
            if (s->streams[i]->id == rt->asf_ctx->streams[pkt->stream_index]->id) {
                pkt->stream_index = i;
                return 1; // FIXME: return 0 if last packet
            }
        }
        av_free_packet(pkt);
    }

    return res == 1 ? -1 : res;
}

280
static PayloadContext *asfrtp_new_context(void)
281 282 283 284
{
    return av_mallocz(sizeof(PayloadContext));
}

285
static void asfrtp_free_context(PayloadContext *asf)
286 287 288
{
    if (asf->pktbuf) {
        uint8_t *p = NULL;
289
        avio_close_dyn_buf(asf->pktbuf, &p);
290 291 292 293 294 295 296
        asf->pktbuf = NULL;
        av_free(p);
    }
    av_freep(&asf->buf);
    av_free(asf);
}

297 298
#define RTP_ASF_HANDLER(n, s, t) \
RTPDynamicProtocolHandler ff_ms_rtp_ ## n ## _handler = { \
299 300
    .enc_name         = s, \
    .codec_type       = t, \
301
    .codec_id         = AV_CODEC_ID_NONE, \
302
    .parse_sdp_a_line = asfrtp_parse_sdp_line, \
303 304
    .alloc            = asfrtp_new_context, \
    .free             = asfrtp_free_context, \
305
    .parse_packet     = asfrtp_parse_packet,   \
306
}
307

308 309
RTP_ASF_HANDLER(asf_pfv, "x-asf-pf",  AVMEDIA_TYPE_VIDEO);
RTP_ASF_HANDLER(asf_pfa, "x-asf-pf",  AVMEDIA_TYPE_AUDIO);