cosmetics: line wrap and vertical alignment
[FFMpeg-mirror/lagarith.git] / libavformat / rtp_vorbis.c
blobd9bdf4909d03233e39d6f9328f9e303a9c5b022c
1 /*
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
22 /**
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"
32 #include <assert.h>
34 #include "rtpdec.h"
35 #include "rtp_vorbis.h"
37 /**
38 * RTP/Vorbis specific private data.
40 struct PayloadContext {
41 unsigned ident; ///< 24-bit stream configuration identifier
44 /**
45 * Length encoding described in RFC5215 section 3.1.1.
47 static int get_base128(const uint8_t ** buf, const uint8_t * buf_end)
49 int n = 0;
50 for (; *buf < buf_end; ++*buf) {
51 n <<= 7;
52 n += **buf & 0x7f;
53 if (!(**buf & 0x80)) {
54 ++*buf;
55 return n;
58 return 0;
61 /**
62 * Out-of-band headers, described in RFC 5251 section 3.2.1
64 static unsigned int
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;
70 uint8_t *ptr;
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);
95 if (!ptr) {
96 av_log(codec, AV_LOG_ERROR, "Out of memory");
97 return AVERROR_NOMEM;
99 *ptr++ = 2;
100 ptr += av_xiphlacing(ptr, length1);
101 ptr += av_xiphlacing(ptr, length2);
102 memcpy(ptr, packed_headers, length);
103 ptr += length;
104 codec->extradata_size = ptr - codec->extradata;
106 return 0;
110 ff_vorbis_parse_fmtp_config(AVCodecContext * codec,
111 void *vorbis_data, char *attr, char *value)
113 int result = 0;
114 assert(codec->codec_id == CODEC_ID_VORBIS);
115 assert(vorbis_data);
117 // The configuration value is a base64 encoded packed header
118 if (!strcmp(attr, "configuration")) {
119 uint8_t *decoded_packet = NULL;
120 int packet_size;
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) {
126 packet_size =
127 av_base64_decode(decoded_packet, value, decoded_alloc);
129 result = parse_packed_headers
130 (decoded_packet, decoded_packet + packet_size, codec,
131 vorbis_data);
132 } else {
133 av_log(codec, AV_LOG_ERROR,
134 "Out of memory while decoding SDP configuration.\n");
135 result = AVERROR_NOMEM;
137 } else {
138 av_log(codec, AV_LOG_ERROR, "Packet too large\n");
139 result = AVERROR_INVALIDDATA;
141 av_free(decoded_packet);
143 return result;
146 static PayloadContext *vorbis_new_extradata(void)
148 return av_mallocz(sizeof(PayloadContext));
151 static void vorbis_free_extradata(PayloadContext * data)
153 av_free(data);
157 * Handle payload as described in RFC 5215 section 2.2
159 static int
160 vorbis_handle_packet(AVFormatContext * ctx,
161 PayloadContext * data,
162 AVStream * st,
163 AVPacket * pkt,
164 uint32_t * timestamp,
165 const uint8_t * buf, int len, int flags)
167 int ident, fragmented, vdt, num_pkts, pkt_len;
169 if (len < 6) {
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,
183 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;
207 return 0;
210 RTPDynamicProtocolHandler ff_vorbis_dynamic_handler = {
211 "vorbis",
212 CODEC_TYPE_AUDIO,
213 CODEC_ID_VORBIS,
214 NULL,
215 vorbis_new_extradata,
216 vorbis_free_extradata,
217 vorbis_handle_packet