Patch by Josh Allmann, joshua dot allmann at gmail, original code
by Ronald S Bultje.
Originally committed as revision 24236 to svn://svn.ffmpeg.org/ffmpeg/trunk
... | ... |
@@ -35,6 +35,7 @@ |
35 | 35 |
#include "rtpdec_h263.h" |
36 | 36 |
#include "rtpdec_h264.h" |
37 | 37 |
#include "rtpdec_mpeg4.h" |
38 |
+#include "rtpdec_qdm2.h" |
|
38 | 39 |
#include "rtpdec_svq3.h" |
39 | 40 |
#include "rtpdec_xiph.h" |
40 | 41 |
|
... | ... |
@@ -69,6 +70,7 @@ void av_register_rtp_dynamic_payload_handlers(void) |
69 | 69 |
ff_register_dynamic_payload_handler(&ff_h264_dynamic_handler); |
70 | 70 |
ff_register_dynamic_payload_handler(&ff_vorbis_dynamic_handler); |
71 | 71 |
ff_register_dynamic_payload_handler(&ff_theora_dynamic_handler); |
72 |
+ ff_register_dynamic_payload_handler(&ff_qdm2_dynamic_handler); |
|
72 | 73 |
ff_register_dynamic_payload_handler(&ff_svq3_dynamic_handler); |
73 | 74 |
|
74 | 75 |
ff_register_dynamic_payload_handler(&ff_ms_rtp_asf_pfv_handler); |
75 | 76 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,316 @@ |
0 |
+/* |
|
1 |
+ * QDesign Music 2 (QDM2) payload for RTP |
|
2 |
+ * Copyright (c) 2010 Ronald S. Bultje |
|
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 |
|
23 |
+ * @brief RTP support for the QDM2 payload (todo: wiki) |
|
24 |
+ * @author Ronald S. Bultje <rbultje@ronald.bitfreak.net> |
|
25 |
+ */ |
|
26 |
+ |
|
27 |
+#include <string.h> |
|
28 |
+#include "libavutil/intreadwrite.h" |
|
29 |
+#include "libavcodec/avcodec.h" |
|
30 |
+#include "rtp.h" |
|
31 |
+#include "rtpdec.h" |
|
32 |
+#include "rtpdec_qdm2.h" |
|
33 |
+ |
|
34 |
+struct PayloadContext { |
|
35 |
+ /** values read from the config header, used as packet headers */ |
|
36 |
+ //@{ |
|
37 |
+ int block_type; ///< superblock type, value 2 .. 8 |
|
38 |
+ int block_size; ///< from extradata, used as pkt length |
|
39 |
+ int subpkts_per_block; ///< max. nr. of subpackets to add per output buffer |
|
40 |
+ //@} |
|
41 |
+ |
|
42 |
+ /** Temporary storage for superblock restoring, per packet ID (0x80 total) */ |
|
43 |
+ //@{ |
|
44 |
+ uint16_t len[0x80]; ///< how much the temporary buffer is filled |
|
45 |
+ uint8_t buf[0x80][0x800]; ///< the temporary storage buffer |
|
46 |
+ |
|
47 |
+ unsigned int cache; ///< number of data packets that we have cached right now |
|
48 |
+ unsigned int n_pkts; ///< number of RTP packets received since last packet output / config |
|
49 |
+ uint32_t timestamp; ///< timestamp of next-to-be-returned packet |
|
50 |
+ //@} |
|
51 |
+}; |
|
52 |
+ |
|
53 |
+/** |
|
54 |
+ * Parses configuration (basically the codec-specific extradata) from |
|
55 |
+ * a RTP config subpacket (starts with 0xff). |
|
56 |
+ * |
|
57 |
+ * Layout of the config subpacket (in bytes): |
|
58 |
+ * 1: 0xFF <- config ID |
|
59 |
+ * then an array { |
|
60 |
+ * 1: size <- of the current item |
|
61 |
+ * 1: item type <- 0 .. 4 |
|
62 |
+ * size-2: data <- data depends on the item type |
|
63 |
+ * } |
|
64 |
+ * |
|
65 |
+ * Item 0 implies the end of the config subpacket, and has no data. |
|
66 |
+ * Item 1 implies a stream configuration without extradata. |
|
67 |
+ * Item 2 max. nr. of subpackets per superblock |
|
68 |
+ * Item 3 superblock type for the stream |
|
69 |
+ * Item 4 implies a stream configuration with extradata (size >= 0x1c). |
|
70 |
+ * |
|
71 |
+ * @return <0 on error, otherwise the number of bytes parsed from the |
|
72 |
+ * input buffer. |
|
73 |
+ */ |
|
74 |
+static int qdm2_parse_config(PayloadContext *qdm, AVStream *st, |
|
75 |
+ const uint8_t *buf, const uint8_t *end) |
|
76 |
+{ |
|
77 |
+ const uint8_t *p = buf; |
|
78 |
+ |
|
79 |
+ while (end - p >= 2) { |
|
80 |
+ unsigned int item_len = p[0], config_item = p[1]; |
|
81 |
+ |
|
82 |
+ if (item_len < 2 || end - p < item_len || config_item > 4) |
|
83 |
+ return AVERROR_INVALIDDATA; |
|
84 |
+ |
|
85 |
+ switch (config_item) { |
|
86 |
+ case 0: /* end of config block */ |
|
87 |
+ return p - buf + item_len; |
|
88 |
+ case 1: /* stream without extradata */ |
|
89 |
+ /* FIXME: set default qdm->block_size */ |
|
90 |
+ break; |
|
91 |
+ case 2: /**< subpackets per block */ |
|
92 |
+ if (item_len < 3) |
|
93 |
+ return AVERROR_INVALIDDATA; |
|
94 |
+ qdm->subpkts_per_block = p[2]; |
|
95 |
+ break; |
|
96 |
+ case 3: /* superblock type */ |
|
97 |
+ if (item_len < 4) |
|
98 |
+ return AVERROR_INVALIDDATA; |
|
99 |
+ qdm->block_type = AV_RB16(p + 2); |
|
100 |
+ break; |
|
101 |
+ case 4: /* stream with extradata */ |
|
102 |
+ if (item_len < 30) |
|
103 |
+ return AVERROR_INVALIDDATA; |
|
104 |
+ av_freep(&st->codec->extradata); |
|
105 |
+ st->codec->extradata_size = 26 + item_len; |
|
106 |
+ if (!(st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE))) { |
|
107 |
+ st->codec->extradata_size = 0; |
|
108 |
+ return AVERROR(ENOMEM); |
|
109 |
+ } |
|
110 |
+ AV_WB32(st->codec->extradata, 12); |
|
111 |
+ memcpy(st->codec->extradata + 4, "frma", 4); |
|
112 |
+ memcpy(st->codec->extradata + 8, "QDM2", 4); |
|
113 |
+ AV_WB32(st->codec->extradata + 12, 6 + item_len); |
|
114 |
+ memcpy(st->codec->extradata + 16, "QDCA", 4); |
|
115 |
+ memcpy(st->codec->extradata + 20, p + 2, item_len - 2); |
|
116 |
+ AV_WB32(st->codec->extradata + 18 + item_len, 8); |
|
117 |
+ AV_WB32(st->codec->extradata + 22 + item_len, 0); |
|
118 |
+ |
|
119 |
+ qdm->block_size = AV_RB32(p + 26); |
|
120 |
+ break; |
|
121 |
+ } |
|
122 |
+ |
|
123 |
+ p += item_len; |
|
124 |
+ } |
|
125 |
+ |
|
126 |
+ return AVERROR(EAGAIN); /* not enough data */ |
|
127 |
+} |
|
128 |
+ |
|
129 |
+/** |
|
130 |
+ * Parses a single subpacket. We store this subpacket in an intermediate |
|
131 |
+ * buffer (position depends on the ID (byte[0]). When called, at least |
|
132 |
+ * 4 bytes are available for reading (see qdm2_parse_packet()). |
|
133 |
+ * |
|
134 |
+ * Layout of a single subpacket (RTP packets commonly contain multiple |
|
135 |
+ * such subpackets) - length in bytes: |
|
136 |
+ * 1: ordering ID <- 0 .. 0x7F |
|
137 |
+ * 1: subpacket type <- 0 .. 0x7F; value & 0x80 means subpacket length = 2 bytes, else 1 byte |
|
138 |
+ * 1/2: subpacket length <- length of the data following the flags/length fields |
|
139 |
+ * if (subpacket type & 0x7F) == 0x7F |
|
140 |
+ * 1: subpacket type, higher bits |
|
141 |
+ * size: subpacket data |
|
142 |
+ * |
|
143 |
+ * The subpackets come in randomly, and should be encapsulated into 1 |
|
144 |
+ * or more superblocks (containing qdm->subpkts_per_block subpackets |
|
145 |
+ * each) per RTP packet, in order of ascending "ordering ID", see |
|
146 |
+ * qdm2_restore_block(). |
|
147 |
+ * |
|
148 |
+ * @return <0 on error, otherwise the number of bytes parsed from the |
|
149 |
+ * input buffer. |
|
150 |
+ */ |
|
151 |
+static int qdm2_parse_subpacket(PayloadContext *qdm, AVStream *st, |
|
152 |
+ const uint8_t *buf, const uint8_t *end) |
|
153 |
+{ |
|
154 |
+ const uint8_t *p = buf; |
|
155 |
+ unsigned int id, len, type, to_copy; |
|
156 |
+ |
|
157 |
+ /* parse header so we know the size of the header/data */ |
|
158 |
+ id = *p++; |
|
159 |
+ type = *p++; |
|
160 |
+ if (type & 0x80) { |
|
161 |
+ len = AV_RB16(p); |
|
162 |
+ p += 2; |
|
163 |
+ type &= 0x7F; |
|
164 |
+ } else |
|
165 |
+ len = *p++; |
|
166 |
+ |
|
167 |
+ if (end - p < len + (type == 0x7F) || id >= 0x80) |
|
168 |
+ return AVERROR_INVALIDDATA; |
|
169 |
+ if (type == 0x7F) |
|
170 |
+ type |= *p++ << 8; |
|
171 |
+ |
|
172 |
+ /* copy data into a temporary buffer */ |
|
173 |
+ to_copy = FFMIN(len + (p - &buf[1]), 0x800 - qdm->len[id]); |
|
174 |
+ memcpy(&qdm->buf[id][qdm->len[id]], buf + 1, to_copy); |
|
175 |
+ qdm->len[id] += to_copy; |
|
176 |
+ |
|
177 |
+ return p + len - buf; |
|
178 |
+} |
|
179 |
+ |
|
180 |
+/** |
|
181 |
+ * Adds a superblock header around a set of subpackets. |
|
182 |
+ * |
|
183 |
+ * @return <0 on error, else 0. |
|
184 |
+ */ |
|
185 |
+static int qdm2_restore_block(PayloadContext *qdm, AVStream *st, AVPacket *pkt) |
|
186 |
+{ |
|
187 |
+ int to_copy, n, res, include_csum; |
|
188 |
+ uint8_t *p, *csum_pos = NULL; |
|
189 |
+ |
|
190 |
+ /* create packet to hold subpkts into a superblock */ |
|
191 |
+ assert(qdm->cache > 0); |
|
192 |
+ for (n = 0; n < 0x80; n++) |
|
193 |
+ if (qdm->len[n] > 0) |
|
194 |
+ break; |
|
195 |
+ assert(n < 0x80); |
|
196 |
+ |
|
197 |
+ if ((res = av_new_packet(pkt, qdm->block_size)) < 0) |
|
198 |
+ return res; |
|
199 |
+ memset(pkt->data, 0, pkt->size); |
|
200 |
+ pkt->stream_index = st->index; |
|
201 |
+ p = pkt->data; |
|
202 |
+ |
|
203 |
+ /* superblock header */ |
|
204 |
+ if (qdm->len[n] > 0xff) { |
|
205 |
+ *p++ = qdm->block_type | 0x80; |
|
206 |
+ AV_WB16(p, qdm->len[n]); |
|
207 |
+ p += 2; |
|
208 |
+ } else { |
|
209 |
+ *p++ = qdm->block_type; |
|
210 |
+ *p++ = qdm->len[n]; |
|
211 |
+ } |
|
212 |
+ if ((include_csum = (qdm->block_type == 2 || qdm->block_type == 4))) { |
|
213 |
+ csum_pos = p; |
|
214 |
+ p += 2; |
|
215 |
+ } |
|
216 |
+ |
|
217 |
+ /* subpacket data */ |
|
218 |
+ to_copy = FFMIN(qdm->len[n], pkt->size - (p - pkt->data)); |
|
219 |
+ memcpy(p, qdm->buf[n], to_copy); |
|
220 |
+ qdm->len[n] = 0; |
|
221 |
+ |
|
222 |
+ /* checksum header */ |
|
223 |
+ if (include_csum) { |
|
224 |
+ unsigned int total = 0; |
|
225 |
+ uint8_t *q; |
|
226 |
+ |
|
227 |
+ for (q = pkt->data; q < &pkt->data[qdm->block_size]; q++) |
|
228 |
+ total += *q; |
|
229 |
+ AV_WB16(csum_pos, (uint16_t) total); |
|
230 |
+ } |
|
231 |
+ |
|
232 |
+ return 0; |
|
233 |
+} |
|
234 |
+ |
|
235 |
+/** return 0 on packet, no more left, 1 on packet, -1 on partial packet... */ |
|
236 |
+static int qdm2_parse_packet(AVFormatContext *s, PayloadContext *qdm, |
|
237 |
+ AVStream *st, AVPacket *pkt, |
|
238 |
+ uint32_t *timestamp, |
|
239 |
+ const uint8_t *buf, int len, int flags) |
|
240 |
+{ |
|
241 |
+ int res = AVERROR_INVALIDDATA, n; |
|
242 |
+ const uint8_t *end = buf + len, *p = buf; |
|
243 |
+ |
|
244 |
+ if (len > 0) { |
|
245 |
+ if (len < 2) |
|
246 |
+ return AVERROR_INVALIDDATA; |
|
247 |
+ |
|
248 |
+ /* configuration block */ |
|
249 |
+ if (*p == 0xff) { |
|
250 |
+ if (qdm->n_pkts > 0) { |
|
251 |
+ av_log(s, AV_LOG_WARNING, |
|
252 |
+ "Out of sequence config - dropping queue\n"); |
|
253 |
+ qdm->n_pkts = 0; |
|
254 |
+ memset(qdm->len, 0, sizeof(qdm->len)); |
|
255 |
+ } |
|
256 |
+ |
|
257 |
+ if ((res = qdm2_parse_config(qdm, st, ++p, end)) < 0) |
|
258 |
+ return res; |
|
259 |
+ p += res; |
|
260 |
+ |
|
261 |
+ /* We set codec_id to CODEC_ID_NONE initially to |
|
262 |
+ * delay decoder initialization since extradata is |
|
263 |
+ * carried within the RTP stream, not SDP. Here, |
|
264 |
+ * by setting codec_id to CODEC_ID_QDM2, we are signalling |
|
265 |
+ * to the decoder that it is OK to initialize. */ |
|
266 |
+ st->codec->codec_id = CODEC_ID_QDM2; |
|
267 |
+ } |
|
268 |
+ |
|
269 |
+ /* subpackets */ |
|
270 |
+ while (end - p >= 4) { |
|
271 |
+ if ((res = qdm2_parse_subpacket(qdm, st, p, end)) < 0) |
|
272 |
+ return res; |
|
273 |
+ p += res; |
|
274 |
+ } |
|
275 |
+ |
|
276 |
+ qdm->timestamp = *timestamp; |
|
277 |
+ if (++qdm->n_pkts < qdm->subpkts_per_block) |
|
278 |
+ return AVERROR(EAGAIN); |
|
279 |
+ qdm->cache = 0; |
|
280 |
+ for (n = 0; n < 0x80; n++) |
|
281 |
+ if (qdm->len[n] > 0) |
|
282 |
+ qdm->cache++; |
|
283 |
+ } |
|
284 |
+ |
|
285 |
+ /* output the subpackets into freshly created superblock structures */ |
|
286 |
+ if (!qdm->cache || (res = qdm2_restore_block(qdm, st, pkt)) < 0) |
|
287 |
+ return res; |
|
288 |
+ if (--qdm->cache == 0) |
|
289 |
+ qdm->n_pkts = 0; |
|
290 |
+ |
|
291 |
+ *timestamp = qdm->timestamp; |
|
292 |
+ qdm->timestamp = RTP_NOTS_VALUE; |
|
293 |
+ |
|
294 |
+ return (qdm->cache > 0) ? 1 : 0; |
|
295 |
+} |
|
296 |
+ |
|
297 |
+static PayloadContext *qdm2_extradata_new(void) |
|
298 |
+{ |
|
299 |
+ return av_mallocz(sizeof(PayloadContext)); |
|
300 |
+} |
|
301 |
+ |
|
302 |
+static void qdm2_extradata_free(PayloadContext *qdm) |
|
303 |
+{ |
|
304 |
+ av_free(qdm); |
|
305 |
+} |
|
306 |
+ |
|
307 |
+RTPDynamicProtocolHandler ff_qdm2_dynamic_handler = { |
|
308 |
+ "X-QDM", |
|
309 |
+ CODEC_TYPE_AUDIO, |
|
310 |
+ CODEC_ID_NONE, |
|
311 |
+ NULL, |
|
312 |
+ qdm2_extradata_new, |
|
313 |
+ qdm2_extradata_free, |
|
314 |
+ qdm2_parse_packet, |
|
315 |
+}; |
0 | 316 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,32 @@ |
0 |
+/* |
|
1 |
+ * QDesign Music 2 (QDM2) payload for RTP |
|
2 |
+ * Copyright (c) 2010 Ronald S. Bultje |
|
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_RTPDEC_QDM2_H |
|
22 |
+#define AVFORMAT_RTPDEC_QDM2_H |
|
23 |
+ |
|
24 |
+#include "rtpdec.h" |
|
25 |
+ |
|
26 |
+/** |
|
27 |
+ * QMD2 RTP callbacks. |
|
28 |
+ */ |
|
29 |
+extern RTPDynamicProtocolHandler ff_qdm2_dynamic_handler; |
|
30 |
+ |
|
31 |
+#endif /* AVFORMAT_RTPDEC_QDM2_H */ |