Statistics
| Branch: | Revision:

ffmpeg / libavformat / rtpdec_asf.c @ f65919af

History | View | Annotate | Download (8.9 KB)

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

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

    
28
#include <libavutil/base64.h>
29
#include <libavutil/avstring.h>
30
#include <libavutil/intreadwrite.h>
31
#include "rtp.h"
32
#include "rtpdec_asf.h"
33
#include "rtsp.h"
34
#include "asf.h"
35

    
36
/**
37
 * From MSDN 2.2.1.4, we learn that ASF data packets over RTP should not
38
 * contain any padding. Unfortunately, the header min/max_pktsize are not
39
 * updated (thus making min_pktsize invalid). Here, we "fix" these faulty
40
 * min_pktsize values in the ASF file header.
41
 * @return 0 on success, <0 on failure (currently -1).
42
 */
43
static int rtp_asf_fix_header(uint8_t *buf, int len)
44
{
45
    uint8_t *p = buf, *end = buf + len;
46

    
47
    if (len < sizeof(ff_asf_guid) * 2 + 22 ||
48
        memcmp(p, ff_asf_header, sizeof(ff_asf_guid))) {
49
        return -1;
50
    }
51
    p += sizeof(ff_asf_guid) + 14;
52
    do {
53
        uint64_t chunksize = AV_RL64(p + sizeof(ff_asf_guid));
54
        if (memcmp(p, ff_asf_file_header, sizeof(ff_asf_guid))) {
55
            if (chunksize > end - p)
56
                return -1;
57
            p += chunksize;
58
            continue;
59
        }
60

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

    
71
    return -1;
72
}
73

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

    
85
static void init_packetizer(ByteIOContext *pb, uint8_t *buf, int len)
86
{
87
    init_put_byte(pb, buf, len, 0, NULL, packetizer_read, NULL, NULL);
88

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

    
94
void ff_wms_parse_sdp_a_line(AVFormatContext *s, const char *p)
95
{
96
    if (av_strstart(p, "pgmpu:data:application/vnd.ms.wms-hdr.asfv1;base64,", &p)) {
97
        ByteIOContext pb;
98
        RTSPState *rt = s->priv_data;
99
        int len = strlen(p) * 6 / 8;
100
        char *buf = av_mallocz(len);
101
        av_base64_decode(buf, p, len);
102

    
103
        if (rtp_asf_fix_header(buf, len) < 0)
104
            av_log(s, AV_LOG_ERROR,
105
                   "Failed to fix invalid RTSP-MS/ASF min_pktsize\n");
106
        init_packetizer(&pb, buf, len);
107
        if (rt->asf_ctx) {
108
            av_close_input_stream(rt->asf_ctx);
109
            rt->asf_ctx = NULL;
110
        }
111
        av_open_input_stream(&rt->asf_ctx, &pb, "", &asf_demuxer, NULL);
112
        rt->asf_pb_pos = url_ftell(&pb);
113
        av_free(buf);
114
        rt->asf_ctx->pb = NULL;
115
    }
116
}
117

    
118
static int asfrtp_parse_sdp_line(AVFormatContext *s, int stream_index,
119
                                 PayloadContext *asf, const char *line)
120
{
121
    if (av_strstart(line, "stream:", &line)) {
122
        RTSPState *rt = s->priv_data;
123

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

    
126
        if (rt->asf_ctx) {
127
            int i;
128

    
129
            for (i = 0; i < rt->asf_ctx->nb_streams; i++) {
130
                if (s->streams[stream_index]->id == rt->asf_ctx->streams[i]->id) {
131
                    *s->streams[stream_index]->codec =
132
                        *rt->asf_ctx->streams[i]->codec;
133
                    rt->asf_ctx->streams[i]->codec->extradata_size = 0;
134
                    rt->asf_ctx->streams[i]->codec->extradata = NULL;
135
                    av_set_pts_info(s->streams[stream_index], 32, 1, 1000);
136
                }
137
           }
138
        }
139
    }
140

    
141
    return 0;
142
}
143

    
144
struct PayloadContext {
145
    ByteIOContext *pktbuf, pb;
146
    char *buf;
147
};
148

    
149
/**
150
 * @return 0 when a packet was written into /p pkt, and no more data is left;
151
 *         1 when a packet was written into /p pkt, and more packets might be left;
152
 *        <0 when not enough data was provided to return a full packet, or on error.
153
 */
154
static int asfrtp_parse_packet(AVFormatContext *s, PayloadContext *asf,
155
                               AVStream *st, AVPacket *pkt,
156
                               uint32_t *timestamp,
157
                               const uint8_t *buf, int len, int flags)
158
{
159
    ByteIOContext *pb = &asf->pb;
160
    int res, mflags, len_off;
161
    RTSPState *rt = s->priv_data;
162

    
163
    if (!rt->asf_ctx)
164
        return -1;
165

    
166
    if (len > 0) {
167
        int off, out_len;
168

    
169
        if (len < 4)
170
            return -1;
171

    
172
        init_put_byte(pb, buf, len, 0, NULL, NULL, NULL, NULL);
173
        mflags = get_byte(pb);
174
        if (mflags & 0x80)
175
            flags |= RTP_FLAG_KEY;
176
        len_off = get_be24(pb);
177
        if (mflags & 0x20)   /**< relative timestamp */
178
            url_fskip(pb, 4);
179
        if (mflags & 0x10)   /**< has duration */
180
            url_fskip(pb, 4);
181
        if (mflags & 0x8)    /**< has location ID */
182
            url_fskip(pb, 4);
183
        off = url_ftell(pb);
184

    
185
        av_freep(&asf->buf);
186
        if (!(mflags & 0x40)) {
187
            /**
188
             * If 0x40 is not set, the len_off field specifies an offset of this
189
             * packet's payload data in the complete (reassembled) ASF packet.
190
             * This is used to spread one ASF packet over multiple RTP packets.
191
             */
192
            if (asf->pktbuf && len_off != url_ftell(asf->pktbuf)) {
193
                uint8_t *p;
194
                url_close_dyn_buf(asf->pktbuf, &p);
195
                asf->pktbuf = NULL;
196
                av_free(p);
197
            }
198
            if (!len_off && !asf->pktbuf &&
199
                (res = url_open_dyn_buf(&asf->pktbuf)) < 0)
200
                return res;
201
            if (!asf->pktbuf)
202
                return AVERROR(EIO);
203

    
204
            put_buffer(asf->pktbuf, buf + off, len - off);
205
            if (!(flags & RTP_FLAG_MARKER))
206
                return -1;
207
            out_len     = url_close_dyn_buf(asf->pktbuf, &asf->buf);
208
            asf->pktbuf = NULL;
209
        } else {
210
            /**
211
             * If 0x40 is set, the len_off field specifies the length of the
212
             * next ASF packet that can be read from this payload data alone.
213
             * This is commonly the same as the payload size, but could be
214
             * less in case of packet splitting (i.e. multiple ASF packets in
215
             * one RTP packet).
216
             */
217
            if (len_off != len) {
218
                av_log_missing_feature(s,
219
                    "RTSP-MS packet splitting", 1);
220
                return -1;
221
            }
222
            asf->buf = av_malloc(len - off);
223
            out_len  = len - off;
224
            memcpy(asf->buf, buf + off, len - off);
225
        }
226

    
227
        init_packetizer(pb, asf->buf, out_len);
228
        pb->pos += rt->asf_pb_pos;
229
        pb->eof_reached = 0;
230
        rt->asf_ctx->pb = pb;
231
    }
232

    
233
    for (;;) {
234
        int i;
235

    
236
        res = av_read_packet(rt->asf_ctx, pkt);
237
        rt->asf_pb_pos = url_ftell(pb);
238
        if (res != 0)
239
            break;
240
        for (i = 0; i < s->nb_streams; i++) {
241
            if (s->streams[i]->id == rt->asf_ctx->streams[pkt->stream_index]->id) {
242
                pkt->stream_index = i;
243
                return 1; // FIXME: return 0 if last packet
244
            }
245
        }
246
        av_free_packet(pkt);
247
    }
248

    
249
    return res == 1 ? -1 : res;
250
}
251

    
252
static PayloadContext *asfrtp_new_context(void)
253
{
254
    return av_mallocz(sizeof(PayloadContext));
255
}
256

    
257
static void asfrtp_free_context(PayloadContext *asf)
258
{
259
    if (asf->pktbuf) {
260
        uint8_t *p = NULL;
261
        url_close_dyn_buf(asf->pktbuf, &p);
262
        asf->pktbuf = NULL;
263
        av_free(p);
264
    }
265
    av_freep(&asf->buf);
266
    av_free(asf);
267
}
268

    
269
#define RTP_ASF_HANDLER(n, s, t) \
270
RTPDynamicProtocolHandler ff_ms_rtp_ ## n ## _handler = { \
271
    .enc_name         = s, \
272
    .codec_type       = t, \
273
    .codec_id         = CODEC_ID_NONE, \
274
    .parse_sdp_a_line = asfrtp_parse_sdp_line, \
275
    .open             = asfrtp_new_context, \
276
    .close            = asfrtp_free_context, \
277
    .parse_packet     = asfrtp_parse_packet,   \
278
};
279

    
280
RTP_ASF_HANDLER(asf_pfv, "x-asf-pf",  CODEC_TYPE_VIDEO);
281
RTP_ASF_HANDLER(asf_pfa, "x-asf-pf",  CODEC_TYPE_AUDIO);