2 * RTP Vorbis Protocol (RFC5215)
3 * Copyright (c) 2009 Colin McQuillan
5 * This file is part of FFmpeg.
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.
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.
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
23 * @file libavformat/rtp_vorbis.c
24 * @brief Vorbis / RTP Code (RFC 5215)
25 * @author Colin McQuillan <m.niloc@gmail.com>
28 #include "libavutil/base64.h"
29 #include "libavutil/avstring.h"
30 #include "libavcodec/bytestream.h"
35 #include "rtp_vorbis.h"
38 * RTP/Vorbis specific private data.
40 struct PayloadContext
{
41 unsigned ident
; ///< 24-bit stream configuration identifier
45 * Length encoding described in RFC5215 section 3.1.1.
47 static int get_base128(const uint8_t ** buf
, const uint8_t * buf_end
)
50 for (; *buf
< buf_end
; ++*buf
) {
53 if (!(**buf
& 0x80)) {
62 * Out-of-band headers, described in RFC 5251 section 3.2.1
65 parse_packed_headers(const uint8_t * packed_headers
,
66 const uint8_t * packed_headers_end
,
67 AVCodecContext
* codec
, PayloadContext
* vorbis_data
)
69 unsigned num_packed
, num_headers
, length
, length1
, length2
;
72 num_packed
= bytestream_get_be32(&packed_headers
);
73 vorbis_data
->ident
= bytestream_get_be24(&packed_headers
);
74 length
= bytestream_get_be16(&packed_headers
);
75 num_headers
= get_base128(&packed_headers
, packed_headers_end
);
76 length1
= get_base128(&packed_headers
, packed_headers_end
);
77 length2
= get_base128(&packed_headers
, packed_headers_end
);
79 if (num_packed
!= 1 || num_headers
> 3) {
80 av_log(codec
, AV_LOG_ERROR
,
81 "Unimplemented number of headers: %d packed headers, %d headers\n",
82 num_packed
, num_headers
);
83 return AVERROR_PATCHWELCOME
;
86 if (packed_headers_end
- packed_headers
!= length
||
87 length1
> length
|| length2
> length
- length1
) {
88 av_log(codec
, AV_LOG_ERROR
,
89 "Bad packed header lengths (%d,%d,%d,%d)\n", length1
,
90 length2
, packed_headers_end
- packed_headers
, length
);
91 return AVERROR_INVALIDDATA
;
94 ptr
= codec
->extradata
= av_mallocz(length
+ length
/ 255 + 64);
96 av_log(codec
, AV_LOG_ERROR
, "Out of memory");
100 ptr
+= av_xiphlacing(ptr
, length1
);
101 ptr
+= av_xiphlacing(ptr
, length2
);
102 memcpy(ptr
, packed_headers
, length
);
104 codec
->extradata_size
= ptr
- codec
->extradata
;
110 ff_vorbis_parse_fmtp_config(AVCodecContext
* codec
,
111 void *vorbis_data
, char *attr
, char *value
)
114 assert(codec
->codec_id
== CODEC_ID_VORBIS
);
117 // The configuration value is a base64 encoded packed header
118 if (!strcmp(attr
, "configuration")) {
119 uint8_t *decoded_packet
= NULL
;
121 size_t decoded_alloc
= strlen(value
) / 4 * 3 + 4;
123 if (decoded_alloc
<= INT_MAX
) {
124 decoded_packet
= av_malloc(decoded_alloc
);
125 if (decoded_packet
) {
127 av_base64_decode(decoded_packet
, value
, decoded_alloc
);
129 result
= parse_packed_headers
130 (decoded_packet
, decoded_packet
+ packet_size
, codec
,
133 av_log(codec
, AV_LOG_ERROR
,
134 "Out of memory while decoding SDP configuration.\n");
135 result
= AVERROR_NOMEM
;
138 av_log(codec
, AV_LOG_ERROR
, "Packet too large\n");
139 result
= AVERROR_INVALIDDATA
;
141 av_free(decoded_packet
);
146 static PayloadContext
*vorbis_new_context(void)
148 return av_mallocz(sizeof(PayloadContext
));
151 static void vorbis_free_context(PayloadContext
* data
)
157 * Handle payload as described in RFC 5215 section 2.2
160 vorbis_handle_packet(AVFormatContext
* ctx
,
161 PayloadContext
* data
,
164 uint32_t * timestamp
,
165 const uint8_t * buf
, int len
, int flags
)
167 int ident
, fragmented
, vdt
, num_pkts
, pkt_len
;
170 av_log(ctx
, AV_LOG_ERROR
, "Invalid %d byte packet\n", len
);
171 return AVERROR_INVALIDDATA
;
174 ident
= AV_RB24(buf
);
175 fragmented
= buf
[3] >> 6;
176 vdt
= (buf
[3] >> 4) & 3;
177 num_pkts
= buf
[3] & 7;
178 pkt_len
= AV_RB16(buf
+ 4);
180 if (pkt_len
> len
- 6) {
181 av_log(ctx
, AV_LOG_ERROR
,
182 "Invalid packet length %d in %d byte packet\n", pkt_len
,
184 return AVERROR_INVALIDDATA
;
187 if (ident
!= data
->ident
) {
188 av_log(ctx
, AV_LOG_ERROR
,
189 "Unimplemented Vorbis SDP configuration change detected\n");
190 return AVERROR_PATCHWELCOME
;
193 if (fragmented
!= 0 || vdt
!= 0 || num_pkts
!= 1) {
194 av_log(ctx
, AV_LOG_ERROR
,
195 "Unimplemented RTP Vorbis packet settings (%d,%d,%d)\n",
196 fragmented
, vdt
, num_pkts
);
197 return AVERROR_PATCHWELCOME
;
200 if (av_new_packet(pkt
, pkt_len
)) {
201 av_log(ctx
, AV_LOG_ERROR
, "Out of memory.\n");
202 return AVERROR_NOMEM
;
205 memcpy(pkt
->data
, buf
+ 6, pkt_len
);
206 pkt
->stream_index
= st
->index
;
210 RTPDynamicProtocolHandler ff_vorbis_dynamic_handler
= {
211 .enc_name
= "vorbis",
212 .codec_type
= CODEC_TYPE_AUDIO
,
213 .codec_id
= CODEC_ID_VORBIS
,
214 .parse_sdp_a_line
= NULL
,
215 .open
= vorbis_new_context
,
216 .close
= vorbis_free_context
,
217 .parse_packet
= vorbis_handle_packet