Browse code

Implement RTSP-MS/ASF packet parsing - this completes RTSP-MS support. See discussion in "[PATCH] RTSP-MS 14/15: ASF packet parsing" thread on mailinglist.

Originally committed as revision 19516 to svn://svn.ffmpeg.org/ffmpeg/trunk

Ronald S. Bultje authored on 2009/07/27 23:00:10
Showing 3 changed files
... ...
@@ -28,6 +28,7 @@ version <next>:
28 28
 - DivX (XSUB) subtitle encoder
29 29
 - nonfree libamr support for AMR-NB/WB decoding/encoding removed
30 30
 - Experimental AAC encoder
31
+- RTP depacketization of ASF and RTSP from WMS servers
31 32
 
32 33
 
33 34
 
... ...
@@ -27,11 +27,73 @@
27 27
 
28 28
 #include <libavutil/base64.h>
29 29
 #include <libavutil/avstring.h>
30
+#include <libavutil/intreadwrite.h>
30 31
 #include "rtp.h"
31 32
 #include "rtp_asf.h"
32 33
 #include "rtsp.h"
33 34
 #include "asf.h"
34 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
44
+rtp_asf_fix_header(uint8_t *buf, int len)
45
+{
46
+    uint8_t *p = buf, *end = buf + len;
47
+
48
+    if (len < sizeof(ff_asf_guid) * 2 + 22 ||
49
+        memcmp(p, ff_asf_header, sizeof(ff_asf_guid))) {
50
+        return -1;
51
+    }
52
+    p += sizeof(ff_asf_guid) + 14;
53
+    do {
54
+        uint64_t chunksize = AV_RL64(p + sizeof(ff_asf_guid));
55
+        if (memcmp(p, ff_asf_file_header, sizeof(ff_asf_guid))) {
56
+            if (chunksize > end - p)
57
+                return -1;
58
+            p += chunksize;
59
+            continue;
60
+        }
61
+
62
+        /* skip most of the file header, to min_pktsize */
63
+        p += 6 * 8 + 3 * 4 + sizeof(ff_asf_guid) * 2;
64
+        if (p + 8 <= end && AV_RL32(p) == AV_RL32(p + 4)) {
65
+            /* and set that to zero */
66
+            AV_WL32(p, 0);
67
+            return 0;
68
+        }
69
+        break;
70
+    } while (end - p >= sizeof(ff_asf_guid) + 8);
71
+
72
+    return -1;
73
+}
74
+
75
+/**
76
+ * The following code is basically a buffered ByteIOContext,
77
+ * with the added benefit of returning -EAGAIN (instead of 0)
78
+ * on packet boundaries, such that the ASF demuxer can return
79
+ * safely and resume business at the next packet.
80
+ */
81
+static int
82
+packetizer_read(void *opaque, uint8_t *buf, int buf_size)
83
+{
84
+    return AVERROR(EAGAIN);
85
+}
86
+
87
+static void
88
+init_packetizer(ByteIOContext *pb, uint8_t *buf, int len)
89
+{
90
+    init_put_byte(pb, buf, len, 0, NULL, packetizer_read, NULL, NULL);
91
+
92
+    /* this "fills" the buffer with its current content */
93
+    pb->pos     = len;
94
+    pb->buf_end = buf + len;
95
+}
96
+
35 97
 void ff_wms_parse_sdp_a_line(AVFormatContext *s, const char *p)
36 98
 {
37 99
     if (av_strstart(p, "pgmpu:data:application/vnd.ms.wms-hdr.asfv1;base64,", &p)) {
... ...
@@ -41,12 +103,16 @@ void ff_wms_parse_sdp_a_line(AVFormatContext *s, const char *p)
41 41
         char *buf = av_mallocz(len);
42 42
         av_base64_decode(buf, p, len);
43 43
 
44
-        init_put_byte(&pb, buf, len, 0, NULL, NULL, NULL, NULL);
44
+        if (rtp_asf_fix_header(buf, len) < 0)
45
+            av_log(s, AV_LOG_ERROR,
46
+                   "Failed to fix invalid RTSP-MS/ASF min_pktsize\n");
47
+        init_packetizer(&pb, buf, len);
45 48
         if (rt->asf_ctx) {
46 49
             av_close_input_stream(rt->asf_ctx);
47 50
             rt->asf_ctx = NULL;
48 51
         }
49 52
         av_open_input_stream(&rt->asf_ctx, &pb, "", &asf_demuxer, NULL);
53
+        rt->asf_pb_pos = url_ftell(&pb);
50 54
         av_free(buf);
51 55
         rt->asf_ctx->pb = NULL;
52 56
     }
... ...
@@ -79,12 +145,142 @@ asfrtp_parse_sdp_line (AVFormatContext *s, int stream_index,
79 79
     return 0;
80 80
 }
81 81
 
82
+struct PayloadContext {
83
+    ByteIOContext *pktbuf, pb;
84
+    char *buf;
85
+};
86
+
87
+/**
88
+ * @return 0 when a packet was written into /p pkt, and no more data is left;
89
+ *         1 when a packet was written into /p pkt, and more packets might be left;
90
+ *        <0 when not enough data was provided to return a full packet, or on error.
91
+ */
92
+static int
93
+asfrtp_parse_packet (AVFormatContext *s, PayloadContext *asf, AVStream *st,
94
+                     AVPacket *pkt, uint32_t *timestamp,
95
+                     const uint8_t *buf, int len, int flags)
96
+{
97
+    ByteIOContext *pb = &asf->pb;
98
+    int res, mflags, len_off;
99
+    RTSPState *rt = s->priv_data;
100
+
101
+    if (!rt->asf_ctx)
102
+        return -1;
103
+
104
+    if (len > 0) {
105
+        int off, out_len;
106
+
107
+        if (len < 4)
108
+            return -1;
109
+
110
+        init_put_byte(pb, buf, len, 0, NULL, NULL, NULL, NULL);
111
+        mflags = get_byte(pb);
112
+        if (mflags & 0x80)
113
+            flags |= RTP_FLAG_KEY;
114
+        len_off = get_be24(pb);
115
+        if (mflags & 0x20)   /**< relative timestamp */
116
+            url_fskip(pb, 4);
117
+        if (mflags & 0x10)   /**< has duration */
118
+            url_fskip(pb, 4);
119
+        if (mflags & 0x8)    /**< has location ID */
120
+            url_fskip(pb, 4);
121
+        off = url_ftell(pb);
122
+
123
+        av_freep(&asf->buf);
124
+        if (!(mflags & 0x40)) {
125
+            /**
126
+             * If 0x40 is not set, the len_off field specifies an offset of this
127
+             * packet's payload data in the complete (reassembled) ASF packet.
128
+             * This is used to spread one ASF packet over multiple RTP packets.
129
+             */
130
+            if (asf->pktbuf && len_off != url_ftell(asf->pktbuf)) {
131
+                uint8_t *p;
132
+                url_close_dyn_buf(asf->pktbuf, &p);
133
+                asf->pktbuf = NULL;
134
+                av_free(p);
135
+            }
136
+            if (!len_off && !asf->pktbuf &&
137
+                !(res = url_open_dyn_packet_buf(&asf->pktbuf, rt->asf_ctx->packet_size)))
138
+                return res;
139
+            if (!asf->pktbuf)
140
+                return AVERROR(EIO);
141
+
142
+            put_buffer(asf->pktbuf, buf + off, len - off);
143
+            if (!(flags & RTP_FLAG_MARKER))
144
+                return -1;
145
+            out_len     = url_close_dyn_buf(asf->pktbuf, &asf->buf);
146
+            asf->pktbuf = NULL;
147
+        } else {
148
+            /**
149
+             * If 0x40 is set, the len_off field specifies the length of the
150
+             * next ASF packet that can be read from this payload data alone.
151
+             * This is commonly the same as the payload size, but could be
152
+             * less in case of packet splitting (i.e. multiple ASF packets in
153
+             * one RTP packet).
154
+             */
155
+            if (len_off != len) {
156
+                av_log_missing_feature(s,
157
+                    "RTSP-MS packet splitting", 1);
158
+                return -1;
159
+            }
160
+            asf->buf = av_malloc(len - off);
161
+            out_len  = len - off;
162
+            memcpy(asf->buf, buf + off, len - off);
163
+        }
164
+
165
+        init_packetizer(pb, asf->buf, out_len);
166
+        pb->pos += rt->asf_pb_pos;
167
+        pb->eof_reached = 0;
168
+        rt->asf_ctx->pb = pb;
169
+    }
170
+
171
+    for (;;) {
172
+        int i;
173
+
174
+        res = av_read_packet(rt->asf_ctx, pkt);
175
+        rt->asf_pb_pos = url_ftell(pb);
176
+        if (res != 0)
177
+            break;
178
+        for (i = 0; i < s->nb_streams; i++) {
179
+            if (s->streams[i]->id == rt->asf_ctx->streams[pkt->stream_index]->id) {
180
+                pkt->stream_index = i;
181
+                return 1; // FIXME: return 0 if last packet
182
+            }
183
+        }
184
+        av_free_packet(pkt);
185
+    }
186
+
187
+    return res == 1 ? -1 : res;
188
+}
189
+
190
+static PayloadContext *
191
+asfrtp_new_context (void)
192
+{
193
+    return av_mallocz(sizeof(PayloadContext));
194
+}
195
+
196
+static void
197
+asfrtp_free_context (PayloadContext *asf)
198
+{
199
+    if (asf->pktbuf) {
200
+        uint8_t *p = NULL;
201
+        url_close_dyn_buf(asf->pktbuf, &p);
202
+        asf->pktbuf = NULL;
203
+        av_free(p);
204
+    }
205
+    av_freep(&asf->buf);
206
+    av_free(asf);
207
+}
208
+
82 209
 #define RTP_ASF_HANDLER(n, s, t) \
83 210
 RTPDynamicProtocolHandler ff_ms_rtp_ ## n ## _handler = { \
84 211
     s, \
85 212
     t, \
86 213
     CODEC_ID_NONE, \
87 214
     asfrtp_parse_sdp_line, \
215
+    asfrtp_new_context, \
216
+    asfrtp_free_context, \
217
+    asfrtp_parse_packet,   \
88 218
 };
89 219
 
90 220
 RTP_ASF_HANDLER(asf_pfv, "x-asf-pf",  CODEC_TYPE_VIDEO);
... ...
@@ -249,6 +249,10 @@ typedef struct RTSPState {
249 249
     //@{
250 250
     /** ASF demuxer context for the embedded ASF stream from WMS servers */
251 251
     AVFormatContext *asf_ctx;
252
+
253
+    /** cache for position of the asf demuxer, since we load a new
254
+     * data packet in the bytecontext for each incoming RTSP packet. */
255
+    uint64_t asf_pb_pos;
252 256
     //@}
253 257
 } RTSPState;
254 258