2 * QDesign Music 2 (QDM2) payload for RTP
3 * Copyright (c) 2010 Ronald S. Bultje
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
24 * @brief RTP support for the QDM2 payload (todo: wiki)
25 * @author Ronald S. Bultje <rbultje@ronald.bitfreak.net>
29 #include "libavutil/intreadwrite.h"
30 #include "libavcodec/avcodec.h"
33 #include "rtpdec_formats.h"
35 struct PayloadContext {
36 /** values read from the config header, used as packet headers */
38 int block_type; ///< superblock type, value 2 .. 8
39 int block_size; ///< from extradata, used as pkt length
40 int subpkts_per_block; ///< max. nr. of subpackets to add per output buffer
43 /** Temporary storage for superblock restoring, per packet ID (0x80 total) */
45 uint16_t len[0x80]; ///< how much the temporary buffer is filled
46 uint8_t buf[0x80][0x800]; ///< the temporary storage buffer
48 unsigned int cache; ///< number of data packets that we have cached right now
49 unsigned int n_pkts; ///< number of RTP packets received since last packet output / config
50 uint32_t timestamp; ///< timestamp of next-to-be-returned packet
55 * Parses configuration (basically the codec-specific extradata) from
56 * a RTP config subpacket (starts with 0xff).
58 * Layout of the config subpacket (in bytes):
59 * 1: 0xFF <- config ID
61 * 1: size <- of the current item
62 * 1: item type <- 0 .. 4
63 * size-2: data <- data depends on the item type
66 * Item 0 implies the end of the config subpacket, and has no data.
67 * Item 1 implies a stream configuration without extradata.
68 * Item 2 max. nr. of subpackets per superblock
69 * Item 3 superblock type for the stream
70 * Item 4 implies a stream configuration with extradata (size >= 0x1c).
72 * @return <0 on error, otherwise the number of bytes parsed from the
75 static int qdm2_parse_config(PayloadContext *qdm, AVStream *st,
76 const uint8_t *buf, const uint8_t *end)
78 const uint8_t *p = buf;
80 while (end - p >= 2) {
81 unsigned int item_len = p[0], config_item = p[1];
83 if (item_len < 2 || end - p < item_len || config_item > 4)
84 return AVERROR_INVALIDDATA;
86 switch (config_item) {
87 case 0: /* end of config block */
88 return p - buf + item_len;
89 case 1: /* stream without extradata */
90 /* FIXME: set default qdm->block_size */
92 case 2: /**< subpackets per block */
94 return AVERROR_INVALIDDATA;
95 qdm->subpkts_per_block = p[2];
97 case 3: /* superblock type */
99 return AVERROR_INVALIDDATA;
100 qdm->block_type = AV_RB16(p + 2);
102 case 4: /* stream with extradata */
104 return AVERROR_INVALIDDATA;
105 av_freep(&st->codec->extradata);
106 st->codec->extradata_size = 26 + item_len;
107 if (!(st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE))) {
108 st->codec->extradata_size = 0;
109 return AVERROR(ENOMEM);
111 AV_WB32(st->codec->extradata, 12);
112 memcpy(st->codec->extradata + 4, "frma", 4);
113 memcpy(st->codec->extradata + 8, "QDM2", 4);
114 AV_WB32(st->codec->extradata + 12, 6 + item_len);
115 memcpy(st->codec->extradata + 16, "QDCA", 4);
116 memcpy(st->codec->extradata + 20, p + 2, item_len - 2);
117 AV_WB32(st->codec->extradata + 18 + item_len, 8);
118 AV_WB32(st->codec->extradata + 22 + item_len, 0);
120 qdm->block_size = AV_RB32(p + 26);
127 return AVERROR(EAGAIN); /* not enough data */
131 * Parses a single subpacket. We store this subpacket in an intermediate
132 * buffer (position depends on the ID (byte[0]). When called, at least
133 * 4 bytes are available for reading (see qdm2_parse_packet()).
135 * Layout of a single subpacket (RTP packets commonly contain multiple
136 * such subpackets) - length in bytes:
137 * 1: ordering ID <- 0 .. 0x7F
138 * 1: subpacket type <- 0 .. 0x7F; value & 0x80 means subpacket length = 2 bytes, else 1 byte
139 * 1/2: subpacket length <- length of the data following the flags/length fields
140 * if (subpacket type & 0x7F) == 0x7F
141 * 1: subpacket type, higher bits
142 * size: subpacket data
144 * The subpackets come in randomly, and should be encapsulated into 1
145 * or more superblocks (containing qdm->subpkts_per_block subpackets
146 * each) per RTP packet, in order of ascending "ordering ID", see
147 * qdm2_restore_block().
149 * @return <0 on error, otherwise the number of bytes parsed from the
152 static int qdm2_parse_subpacket(PayloadContext *qdm, AVStream *st,
153 const uint8_t *buf, const uint8_t *end)
155 const uint8_t *p = buf;
156 unsigned int id, len, type, to_copy;
158 /* parse header so we know the size of the header/data */
168 if (end - p < len + (type == 0x7F) || id >= 0x80)
169 return AVERROR_INVALIDDATA;
173 /* copy data into a temporary buffer */
174 to_copy = FFMIN(len + (p - &buf[1]), 0x800 - qdm->len[id]);
175 memcpy(&qdm->buf[id][qdm->len[id]], buf + 1, to_copy);
176 qdm->len[id] += to_copy;
178 return p + len - buf;
182 * Adds a superblock header around a set of subpackets.
184 * @return <0 on error, else 0.
186 static int qdm2_restore_block(PayloadContext *qdm, AVStream *st, AVPacket *pkt)
188 int to_copy, n, res, include_csum;
189 uint8_t *p, *csum_pos = NULL;
191 /* create packet to hold subpkts into a superblock */
192 assert(qdm->cache > 0);
193 for (n = 0; n < 0x80; n++)
198 if ((res = av_new_packet(pkt, qdm->block_size)) < 0)
200 memset(pkt->data, 0, pkt->size);
201 pkt->stream_index = st->index;
204 /* superblock header */
205 if (qdm->len[n] > 0xff) {
206 *p++ = qdm->block_type | 0x80;
207 AV_WB16(p, qdm->len[n]);
210 *p++ = qdm->block_type;
213 if ((include_csum = (qdm->block_type == 2 || qdm->block_type == 4))) {
219 to_copy = FFMIN(qdm->len[n], pkt->size - (p - pkt->data));
220 memcpy(p, qdm->buf[n], to_copy);
223 /* checksum header */
225 unsigned int total = 0;
228 for (q = pkt->data; q < &pkt->data[qdm->block_size]; q++)
230 AV_WB16(csum_pos, (uint16_t) total);
236 /** return 0 on packet, no more left, 1 on packet, -1 on partial packet... */
237 static int qdm2_parse_packet(AVFormatContext *s, PayloadContext *qdm,
238 AVStream *st, AVPacket *pkt,
240 const uint8_t *buf, int len, int flags)
242 int res = AVERROR_INVALIDDATA, n;
243 const uint8_t *end = buf + len, *p = buf;
247 return AVERROR_INVALIDDATA;
249 /* configuration block */
251 if (qdm->n_pkts > 0) {
252 av_log(s, AV_LOG_WARNING,
253 "Out of sequence config - dropping queue\n");
255 memset(qdm->len, 0, sizeof(qdm->len));
258 if ((res = qdm2_parse_config(qdm, st, ++p, end)) < 0)
262 /* We set codec_id to CODEC_ID_NONE initially to
263 * delay decoder initialization since extradata is
264 * carried within the RTP stream, not SDP. Here,
265 * by setting codec_id to CODEC_ID_QDM2, we are signalling
266 * to the decoder that it is OK to initialize. */
267 st->codec->codec_id = CODEC_ID_QDM2;
271 while (end - p >= 4) {
272 if ((res = qdm2_parse_subpacket(qdm, st, p, end)) < 0)
277 qdm->timestamp = *timestamp;
278 if (++qdm->n_pkts < qdm->subpkts_per_block)
279 return AVERROR(EAGAIN);
281 for (n = 0; n < 0x80; n++)
286 /* output the subpackets into freshly created superblock structures */
287 if (!qdm->cache || (res = qdm2_restore_block(qdm, st, pkt)) < 0)
289 if (--qdm->cache == 0)
292 *timestamp = qdm->timestamp;
293 qdm->timestamp = RTP_NOTS_VALUE;
295 return (qdm->cache > 0) ? 1 : 0;
298 static PayloadContext *qdm2_extradata_new(void)
300 return av_mallocz(sizeof(PayloadContext));
303 static void qdm2_extradata_free(PayloadContext *qdm)
308 RTPDynamicProtocolHandler ff_qdm2_dynamic_handler = {