1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
/*
* RTP Vorbis Protocol (RFC5215)
* Copyright (c) 2009 Colin McQuillan
*
* 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
*/
/**
* @file libavformat/rtpdec_vorbis.c
* @brief Vorbis / RTP Code (RFC 5215)
* @author Colin McQuillan <m.niloc@gmail.com>
*/
#include "libavutil/base64.h"
#include "libavutil/avstring.h"
#include "libavcodec/bytestream.h"
#include <assert.h>
#include "rtpdec.h"
#include "rtpdec_vorbis.h"
/**
* RTP/Vorbis specific private data.
*/
struct PayloadContext {
unsigned ident; ///< 24-bit stream configuration identifier
};
/**
* Length encoding described in RFC5215 section 3.1.1.
*/
static int get_base128(const uint8_t ** buf, const uint8_t * buf_end)
{
int n = 0;
for (; *buf < buf_end; ++*buf) {
n <<= 7;
n += **buf & 0x7f;
if (!(**buf & 0x80)) {
++*buf;
return n;
}
}
return 0;
}
/**
* Out-of-band headers, described in RFC 5251 section 3.2.1
*/
static unsigned int
parse_packed_headers(const uint8_t * packed_headers,
const uint8_t * packed_headers_end,
AVCodecContext * codec, PayloadContext * vorbis_data)
{
unsigned num_packed, num_headers, length, length1, length2;
uint8_t *ptr;
num_packed = bytestream_get_be32(&packed_headers);
vorbis_data->ident = bytestream_get_be24(&packed_headers);
length = bytestream_get_be16(&packed_headers);
num_headers = get_base128(&packed_headers, packed_headers_end);
length1 = get_base128(&packed_headers, packed_headers_end);
length2 = get_base128(&packed_headers, packed_headers_end);
if (num_packed != 1 || num_headers > 3) {
av_log(codec, AV_LOG_ERROR,
"Unimplemented number of headers: %d packed headers, %d headers\n",
num_packed, num_headers);
return AVERROR_PATCHWELCOME;
}
if (packed_headers_end - packed_headers != length ||
length1 > length || length2 > length - length1) {
av_log(codec, AV_LOG_ERROR,
"Bad packed header lengths (%d,%d,%d,%d)\n", length1,
length2, packed_headers_end - packed_headers, length);
return AVERROR_INVALIDDATA;
}
ptr = codec->extradata = av_mallocz(length + length / 255 + 64);
if (!ptr) {
av_log(codec, AV_LOG_ERROR, "Out of memory");
return AVERROR_NOMEM;
}
*ptr++ = 2;
ptr += av_xiphlacing(ptr, length1);
ptr += av_xiphlacing(ptr, length2);
memcpy(ptr, packed_headers, length);
ptr += length;
codec->extradata_size = ptr - codec->extradata;
return 0;
}
int
ff_vorbis_parse_fmtp_config(AVCodecContext * codec,
void *vorbis_data, char *attr, char *value)
{
int result = 0;
assert(codec->codec_id == CODEC_ID_VORBIS);
assert(vorbis_data);
// The configuration value is a base64 encoded packed header
if (!strcmp(attr, "configuration")) {
uint8_t *decoded_packet = NULL;
int packet_size;
size_t decoded_alloc = strlen(value) / 4 * 3 + 4;
if (decoded_alloc <= INT_MAX) {
decoded_packet = av_malloc(decoded_alloc);
if (decoded_packet) {
packet_size =
av_base64_decode(decoded_packet, value, decoded_alloc);
result = parse_packed_headers
(decoded_packet, decoded_packet + packet_size, codec,
vorbis_data);
} else {
av_log(codec, AV_LOG_ERROR,
"Out of memory while decoding SDP configuration.\n");
result = AVERROR_NOMEM;
}
} else {
av_log(codec, AV_LOG_ERROR, "Packet too large\n");
result = AVERROR_INVALIDDATA;
}
av_free(decoded_packet);
}
return result;
}
static PayloadContext *vorbis_new_context(void)
{
return av_mallocz(sizeof(PayloadContext));
}
static void vorbis_free_context(PayloadContext * data)
{
av_free(data);
}
/**
* Handle payload as described in RFC 5215 section 2.2
*/
static int
vorbis_handle_packet(AVFormatContext * ctx,
PayloadContext * data,
AVStream * st,
AVPacket * pkt,
uint32_t * timestamp,
const uint8_t * buf, int len, int flags)
{
int ident, fragmented, vdt, num_pkts, pkt_len;
if (len < 6) {
av_log(ctx, AV_LOG_ERROR, "Invalid %d byte packet\n", len);
return AVERROR_INVALIDDATA;
}
ident = AV_RB24(buf);
fragmented = buf[3] >> 6;
vdt = (buf[3] >> 4) & 3;
num_pkts = buf[3] & 7;
pkt_len = AV_RB16(buf + 4);
if (pkt_len > len - 6) {
av_log(ctx, AV_LOG_ERROR,
"Invalid packet length %d in %d byte packet\n", pkt_len,
len);
return AVERROR_INVALIDDATA;
}
if (ident != data->ident) {
av_log(ctx, AV_LOG_ERROR,
"Unimplemented Vorbis SDP configuration change detected\n");
return AVERROR_PATCHWELCOME;
}
if (fragmented != 0 || vdt != 0 || num_pkts != 1) {
av_log(ctx, AV_LOG_ERROR,
"Unimplemented RTP Vorbis packet settings (%d,%d,%d)\n",
fragmented, vdt, num_pkts);
return AVERROR_PATCHWELCOME;
}
if (av_new_packet(pkt, pkt_len)) {
av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
return AVERROR_NOMEM;
}
memcpy(pkt->data, buf + 6, pkt_len);
pkt->stream_index = st->index;
return 0;
}
RTPDynamicProtocolHandler ff_vorbis_dynamic_handler = {
.enc_name = "vorbis",
.codec_type = CODEC_TYPE_AUDIO,
.codec_id = CODEC_ID_VORBIS,
.parse_sdp_a_line = NULL,
.open = vorbis_new_context,
.close = vorbis_free_context,
.parse_packet = vorbis_handle_packet
};
|