Browse code

Add a Vorbis payload parser. Implemented by Colin McQuillan as a GSoC qualification task, see "RTP/Vorbis payload implementation (GSoC qual task)" thread on mailinglist.

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

Ronald S. Bultje authored on 2009/04/15 00:01:46
Showing 6 changed files
... ...
@@ -8,6 +8,7 @@ version <next>:
8 8
 - PCX encoder
9 9
 - RTP packetization of H.263
10 10
 - RTP packetization of AMR
11
+- RTP depacketization of Vorbis
11 12
 
12 13
 
13 14
 
... ...
@@ -196,7 +196,7 @@ OBJS-$(CONFIG_RTP_MUXER)                 += rtp.o         \
196 196
                                             rtpenc_h264.o \
197 197
                                             avc.o
198 198
 OBJS-$(CONFIG_RTSP_DEMUXER)              += rdt.o rtsp.o
199
-OBJS-$(CONFIG_SDP_DEMUXER)               += rtsp.o rtp.o rtpdec.o rtp_h264.o
199
+OBJS-$(CONFIG_SDP_DEMUXER)               += rtsp.o rtp.o rtpdec.o rtp_h264.o rtp_vorbis.o
200 200
 OBJS-$(CONFIG_SEGAFILM_DEMUXER)          += segafilm.o
201 201
 OBJS-$(CONFIG_SHORTEN_DEMUXER)           += raw.o id3v2.o
202 202
 OBJS-$(CONFIG_SIFF_DEMUXER)              += siff.o
203 203
new file mode 100644
... ...
@@ -0,0 +1,218 @@
0
+/*
1
+ * RTP Vorbis Protocol (RFC5215)
2
+ * Copyright (c) 2009 Colin McQuillan
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+/**
22
+ * @file libavformat/rtp_vorbis.c
23
+ * @brief Vorbis / RTP Code (RFC 5215)
24
+ * @author Colin McQuillan <m.niloc@gmail.com>
25
+ */
26
+
27
+#include "libavutil/base64.h"
28
+#include "libavutil/avstring.h"
29
+#include "libavcodec/bytestream.h"
30
+
31
+#include <assert.h>
32
+
33
+#include "rtpdec.h"
34
+#include "rtp_vorbis.h"
35
+
36
+/**
37
+ * RTP/Vorbis specific private data.
38
+ */
39
+struct PayloadContext {
40
+    unsigned ident;             ///< 24-bit stream configuration identifier
41
+};
42
+
43
+/**
44
+ * Length encoding described in RFC5215 section 3.1.1.
45
+ */
46
+static int get_base128(const uint8_t ** buf, const uint8_t * buf_end)
47
+{
48
+    int n = 0;
49
+    for (; *buf < buf_end; ++*buf) {
50
+        n <<= 7;
51
+        n += **buf & 0x7f;
52
+        if (!(**buf & 0x80)) {
53
+            ++*buf;
54
+            return n;
55
+        }
56
+    }
57
+    return 0;
58
+}
59
+
60
+/**
61
+ * Out-of-band headers, described in RFC 5251 section 3.2.1
62
+ */
63
+static unsigned int
64
+parse_packed_headers(const uint8_t * packed_headers,
65
+                     const uint8_t * packed_headers_end,
66
+                     AVCodecContext * codec, PayloadContext * vorbis_data)
67
+{
68
+    unsigned num_packed, num_headers, length, length1, length2;
69
+    uint8_t *ptr;
70
+
71
+    num_packed         = bytestream_get_be32(&packed_headers);
72
+    vorbis_data->ident = bytestream_get_be24(&packed_headers);
73
+    length             = bytestream_get_be16(&packed_headers);
74
+    num_headers        = get_base128(&packed_headers, packed_headers_end);
75
+    length1            = get_base128(&packed_headers, packed_headers_end);
76
+    length2            = get_base128(&packed_headers, packed_headers_end);
77
+
78
+    if (num_packed != 1 || num_headers > 3) {
79
+        av_log(codec, AV_LOG_ERROR,
80
+               "Unimplemented number of headers: %d packed headers, %d headers\n",
81
+               num_packed, num_headers);
82
+        return AVERROR_PATCHWELCOME;
83
+    }
84
+
85
+    if (packed_headers_end - packed_headers != length ||
86
+        length1 > length || length2 > length - length1) {
87
+        av_log(codec, AV_LOG_ERROR,
88
+               "Bad packed header lengths (%d,%d,%d,%d)\n", length1,
89
+               length2, packed_headers_end - packed_headers, length);
90
+        return AVERROR_INVALIDDATA;
91
+    }
92
+
93
+    ptr = codec->extradata = av_mallocz(length + length / 255 + 64);
94
+    if (!ptr) {
95
+        av_log(codec, AV_LOG_ERROR, "Out of memory");
96
+        return AVERROR_NOMEM;
97
+    }
98
+    *ptr++ = 2;
99
+    ptr += av_xiphlacing(ptr, length1);
100
+    ptr += av_xiphlacing(ptr, length2);
101
+    memcpy(ptr, packed_headers, length);
102
+    ptr += length;
103
+    codec->extradata_size = ptr - codec->extradata;
104
+
105
+    return 0;
106
+}
107
+
108
+int
109
+ff_vorbis_parse_fmtp_config(AVCodecContext * codec,
110
+                            void *vorbis_data, char *attr, char *value)
111
+{
112
+    int result = 0;
113
+    assert(codec->codec_id == CODEC_ID_VORBIS);
114
+    assert(vorbis_data);
115
+
116
+    // The configuration value is a base64 encoded packed header
117
+    if (!strcmp(attr, "configuration")) {
118
+        uint8_t *decoded_packet = NULL;
119
+        int packet_size;
120
+        size_t decoded_alloc = strlen(value) / 4 * 3 + 4;
121
+
122
+        if (decoded_alloc <= INT_MAX) {
123
+            decoded_packet = av_malloc(decoded_alloc);
124
+            if (decoded_packet) {
125
+                packet_size =
126
+                    av_base64_decode(decoded_packet, value, decoded_alloc);
127
+
128
+                result = parse_packed_headers
129
+                    (decoded_packet, decoded_packet + packet_size, codec,
130
+                     vorbis_data);
131
+            } else {
132
+                av_log(codec, AV_LOG_ERROR,
133
+                       "Out of memory while decoding SDP configuration.\n");
134
+                result = AVERROR_NOMEM;
135
+            }
136
+        } else {
137
+            av_log(codec, AV_LOG_ERROR, "Packet too large\n");
138
+            result = AVERROR_INVALIDDATA;
139
+        }
140
+        av_free(decoded_packet);
141
+    }
142
+    return result;
143
+}
144
+
145
+static PayloadContext *vorbis_new_extradata(void)
146
+{
147
+    return av_mallocz(sizeof(PayloadContext));
148
+}
149
+
150
+static void vorbis_free_extradata(PayloadContext * data)
151
+{
152
+    av_free(data);
153
+}
154
+
155
+/**
156
+ * Handle payload as described in RFC 5215 section 2.2
157
+ */
158
+static int
159
+vorbis_handle_packet(AVFormatContext * ctx,
160
+                     PayloadContext * data,
161
+                     AVStream * st,
162
+                     AVPacket * pkt,
163
+                     uint32_t * timestamp,
164
+                     const uint8_t * buf, int len, int flags)
165
+{
166
+    int ident, fragmented, vdt, num_pkts, pkt_len;
167
+
168
+    if (len < 6) {
169
+        av_log(ctx, AV_LOG_ERROR, "Invalid %d byte packet\n", len);
170
+        return AVERROR_INVALIDDATA;
171
+    }
172
+
173
+    ident = AV_RB24(buf);
174
+    fragmented = buf[3] >> 6;
175
+    vdt = (buf[3] >> 4) & 3;
176
+    num_pkts = buf[3] & 7;
177
+    pkt_len = AV_RB16(buf + 4);
178
+
179
+    if (pkt_len > len - 6) {
180
+        av_log(ctx, AV_LOG_ERROR,
181
+               "Invalid packet length %d in %d byte packet\n", pkt_len,
182
+               len);
183
+        return AVERROR_INVALIDDATA;
184
+    }
185
+
186
+    if (ident != data->ident) {
187
+        av_log(ctx, AV_LOG_ERROR,
188
+               "Unimplemented Vorbis SDP configuration change detected\n");
189
+        return AVERROR_PATCHWELCOME;
190
+    }
191
+
192
+    if (fragmented != 0 || vdt != 0 || num_pkts != 1) {
193
+        av_log(ctx, AV_LOG_ERROR,
194
+               "Unimplemented RTP Vorbis packet settings (%d,%d,%d)\n",
195
+               fragmented, vdt, num_pkts);
196
+        return AVERROR_PATCHWELCOME;
197
+    }
198
+
199
+    if (av_new_packet(pkt, pkt_len)) {
200
+        av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
201
+        return AVERROR_NOMEM;
202
+    }
203
+
204
+    memcpy(pkt->data, buf + 6, pkt_len);
205
+    pkt->stream_index = st->index;
206
+    return 0;
207
+}
208
+
209
+RTPDynamicProtocolHandler ff_vorbis_dynamic_handler = {
210
+    "vorbis",
211
+    CODEC_TYPE_AUDIO,
212
+    CODEC_ID_VORBIS,
213
+    NULL,
214
+    vorbis_new_extradata,
215
+    vorbis_free_extradata,
216
+    vorbis_handle_packet
217
+};
0 218
new file mode 100644
... ...
@@ -0,0 +1,45 @@
0
+/*
1
+ * RTP Vorbis Protocol (RFC 5215)
2
+ * Copyright (c) 2009 Colin McQuillan
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#ifndef AVFORMAT_RTP_VORBIS_H
22
+#define AVFORMAT_RTP_VORBIS_H
23
+
24
+#include "libavcodec/avcodec.h"
25
+#include "rtpdec.h"
26
+
27
+/**
28
+ * Handle a Vorbis-specific FMTP parameter
29
+ *
30
+ * @param codec The context of the codec
31
+ * @param ctx Private Vorbis RTP context
32
+ * @param attr Format-specific parameter name
33
+ * @param value Format-specific paremeter value
34
+ */
35
+int
36
+ff_vorbis_parse_fmtp_config(AVCodecContext * codec,
37
+                            void *ctx, char *attr, char *value);
38
+
39
+/**
40
+ * Vorbis RTP callbacks.
41
+ */
42
+extern RTPDynamicProtocolHandler ff_vorbis_dynamic_handler;
43
+
44
+#endif /* AVFORMAT_RTP_VORBIS_H */
... ...
@@ -32,6 +32,7 @@
32 32
 #include "rtpdec.h"
33 33
 #include "rtp_asf.h"
34 34
 #include "rtp_h264.h"
35
+#include "rtp_vorbis.h"
35 36
 
36 37
 //#define DEBUG
37 38
 
... ...
@@ -61,6 +62,7 @@ void av_register_rtp_dynamic_payload_handlers(void)
61 61
     ff_register_dynamic_payload_handler(&mp4v_es_handler);
62 62
     ff_register_dynamic_payload_handler(&mpeg4_generic_handler);
63 63
     ff_register_dynamic_payload_handler(&ff_h264_dynamic_handler);
64
+    ff_register_dynamic_payload_handler(&ff_vorbis_dynamic_handler);
64 65
 
65 66
     ff_register_dynamic_payload_handler(&ff_ms_rtp_asf_pfv_handler);
66 67
     ff_register_dynamic_payload_handler(&ff_ms_rtp_asf_pfa_handler);
... ...
@@ -37,6 +37,7 @@
37 37
 #include "rtpdec.h"
38 38
 #include "rdt.h"
39 39
 #include "rtp_asf.h"
40
+#include "rtp_vorbis.h"
40 41
 
41 42
 //#define DEBUG
42 43
 //#define DEBUG_RTP_TCP
... ...
@@ -196,7 +197,8 @@ static int hex_to_data(uint8_t *data, const char *p)
196 196
     return len;
197 197
 }
198 198
 
199
-static void sdp_parse_fmtp_config(AVCodecContext *codec, char *attr, char *value)
199
+static void sdp_parse_fmtp_config(AVCodecContext * codec, void *ctx,
200
+                                  char *attr, char *value)
200 201
 {
201 202
     switch (codec->codec_id) {
202 203
         case CODEC_ID_MPEG4:
... ...
@@ -213,6 +215,9 @@ static void sdp_parse_fmtp_config(AVCodecContext *codec, char *attr, char *value
213 213
                 hex_to_data(codec->extradata, value);
214 214
             }
215 215
             break;
216
+        case CODEC_ID_VORBIS:
217
+            ff_vorbis_parse_fmtp_config(codec, ctx, attr, value);
218
+            break;
216 219
         default:
217 220
             break;
218 221
     }
... ...
@@ -274,7 +279,8 @@ static void sdp_parse_fmtp(AVStream *st, const char *p)
274 274
     while(rtsp_next_attr_and_value(&p, attr, sizeof(attr), value, sizeof(value)))
275 275
     {
276 276
         /* grab the codec extra_data from the config parameter of the fmtp line */
277
-        sdp_parse_fmtp_config(codec, attr, value);
277
+        sdp_parse_fmtp_config(codec, rtsp_st->dynamic_protocol_context,
278
+                              attr, value);
278 279
         /* Looking for a known attribute */
279 280
         for (i = 0; attr_names[i].str; ++i) {
280 281
             if (!strcasecmp(attr, attr_names[i].str)) {