1 /* GStreamer
2 * Copyright (C) <2010> Wim Taymans <wim.taymans@gmail.com>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #endif
23
24 #include <gst/rtp/gstrtpbuffer.h>
25 #include <gst/audio/audio.h>
26
27 #include <stdlib.h>
28 #include <string.h>
29 #include "gstrtpqcelpdepay.h"
30 #include "gstrtputils.h"
31
32 GST_DEBUG_CATEGORY_STATIC (rtpqcelpdepay_debug);
33 #define GST_CAT_DEFAULT (rtpqcelpdepay_debug)
34
35 /* references:
36 *
37 * RFC 2658 - RTP Payload Format for PureVoice(tm) Audio
38 */
39 #define FRAME_DURATION (20 * GST_MSECOND)
40
41 /* RtpQCELPDepay signals and args */
42 enum
43 {
44 /* FILL ME */
45 LAST_SIGNAL
46 };
47
48 enum
49 {
50 PROP_0
51 };
52
53 static GstStaticPadTemplate gst_rtp_qcelp_depay_sink_template =
54 GST_STATIC_PAD_TEMPLATE ("sink",
55 GST_PAD_SINK,
56 GST_PAD_ALWAYS,
57 GST_STATIC_CAPS ("application/x-rtp, "
58 "media = (string) \"audio\", "
59 "clock-rate = (int) 8000, "
60 "encoding-name = (string) \"QCELP\"; "
61 "application/x-rtp, "
62 "media = (string) \"audio\", "
63 "payload = (int) " GST_RTP_PAYLOAD_QCELP_STRING ", "
64 "clock-rate = (int) 8000")
65 );
66
67 static GstStaticPadTemplate gst_rtp_qcelp_depay_src_template =
68 GST_STATIC_PAD_TEMPLATE ("src",
69 GST_PAD_SRC,
70 GST_PAD_ALWAYS,
71 GST_STATIC_CAPS ("audio/qcelp, " "channels = (int) 1," "rate = (int) 8000")
72 );
73
74 static void gst_rtp_qcelp_depay_finalize (GObject * object);
75
76 static gboolean gst_rtp_qcelp_depay_setcaps (GstRTPBaseDepayload * depayload,
77 GstCaps * caps);
78 static GstBuffer *gst_rtp_qcelp_depay_process (GstRTPBaseDepayload * depayload,
79 GstRTPBuffer * rtp);
80
81 #define gst_rtp_qcelp_depay_parent_class parent_class
82 G_DEFINE_TYPE (GstRtpQCELPDepay, gst_rtp_qcelp_depay,
83 GST_TYPE_RTP_BASE_DEPAYLOAD);
84
85 static void
gst_rtp_qcelp_depay_class_init(GstRtpQCELPDepayClass * klass)86 gst_rtp_qcelp_depay_class_init (GstRtpQCELPDepayClass * klass)
87 {
88 GObjectClass *gobject_class;
89 GstElementClass *gstelement_class;
90 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
91
92 gobject_class = (GObjectClass *) klass;
93 gstelement_class = (GstElementClass *) klass;
94 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
95
96 gobject_class->finalize = gst_rtp_qcelp_depay_finalize;
97
98 gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_qcelp_depay_process;
99 gstrtpbasedepayload_class->set_caps = gst_rtp_qcelp_depay_setcaps;
100
101 gst_element_class_add_static_pad_template (gstelement_class,
102 &gst_rtp_qcelp_depay_src_template);
103 gst_element_class_add_static_pad_template (gstelement_class,
104 &gst_rtp_qcelp_depay_sink_template);
105
106 gst_element_class_set_static_metadata (gstelement_class,
107 "RTP QCELP depayloader", "Codec/Depayloader/Network/RTP",
108 "Extracts QCELP (PureVoice) audio from RTP packets (RFC 2658)",
109 "Wim Taymans <wim.taymans@gmail.com>");
110
111 GST_DEBUG_CATEGORY_INIT (rtpqcelpdepay_debug, "rtpqcelpdepay", 0,
112 "QCELP RTP Depayloader");
113 }
114
115 static void
gst_rtp_qcelp_depay_init(GstRtpQCELPDepay * rtpqcelpdepay)116 gst_rtp_qcelp_depay_init (GstRtpQCELPDepay * rtpqcelpdepay)
117 {
118 }
119
120 static void
gst_rtp_qcelp_depay_finalize(GObject * object)121 gst_rtp_qcelp_depay_finalize (GObject * object)
122 {
123 GstRtpQCELPDepay *depay;
124
125 depay = GST_RTP_QCELP_DEPAY (object);
126
127 if (depay->packets != NULL) {
128 g_ptr_array_foreach (depay->packets, (GFunc) gst_buffer_unref, NULL);
129 g_ptr_array_free (depay->packets, TRUE);
130 depay->packets = NULL;
131 }
132
133 G_OBJECT_CLASS (parent_class)->finalize (object);
134 }
135
136
137 static gboolean
gst_rtp_qcelp_depay_setcaps(GstRTPBaseDepayload * depayload,GstCaps * caps)138 gst_rtp_qcelp_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
139 {
140 GstCaps *srccaps;
141 gboolean res;
142
143 srccaps = gst_caps_new_simple ("audio/qcelp",
144 "channels", G_TYPE_INT, 1, "rate", G_TYPE_INT, 8000, NULL);
145 res = gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload), srccaps);
146 gst_caps_unref (srccaps);
147
148 return res;
149 }
150
151 static const gint frame_size[16] = {
152 1, 4, 8, 17, 35, -8, 0, 0,
153 0, 0, 0, 0, 0, 0, 1, 0
154 };
155
156 /* get the frame length, 0 is invalid, negative values are invalid but can be
157 * recovered from. */
158 static gint
get_frame_len(GstRtpQCELPDepay * depay,guint8 frame_type)159 get_frame_len (GstRtpQCELPDepay * depay, guint8 frame_type)
160 {
161 if (frame_type >= G_N_ELEMENTS (frame_size))
162 return 0;
163
164 return frame_size[frame_type];
165 }
166
167 static guint
count_packets(GstRtpQCELPDepay * depay,guint8 * data,guint size)168 count_packets (GstRtpQCELPDepay * depay, guint8 * data, guint size)
169 {
170 guint count = 0;
171
172 while (size > 0) {
173 gint frame_len;
174
175 frame_len = get_frame_len (depay, data[0]);
176
177 /* 0 is invalid and we throw away the remainder of the frames */
178 if (frame_len == 0)
179 break;
180
181 if (frame_len < 0)
182 frame_len = -frame_len;
183
184 if (frame_len > size)
185 break;
186
187 size -= frame_len;
188 data += frame_len;
189 count++;
190 }
191 return count;
192 }
193
194 static void
flush_packets(GstRtpQCELPDepay * depay)195 flush_packets (GstRtpQCELPDepay * depay)
196 {
197 guint i, size;
198
199 GST_DEBUG_OBJECT (depay, "flushing packets");
200
201 size = depay->packets->len;
202
203 for (i = 0; i < size; i++) {
204 GstBuffer *outbuf;
205
206 outbuf = g_ptr_array_index (depay->packets, i);
207 g_ptr_array_index (depay->packets, i) = NULL;
208
209 gst_rtp_base_depayload_push (GST_RTP_BASE_DEPAYLOAD (depay), outbuf);
210 }
211
212 /* and reset interleaving state */
213 depay->interleaved = FALSE;
214 depay->bundling = 0;
215 }
216
217 static void
add_packet(GstRtpQCELPDepay * depay,guint LLL,guint NNN,guint index,GstBuffer * outbuf)218 add_packet (GstRtpQCELPDepay * depay, guint LLL, guint NNN, guint index,
219 GstBuffer * outbuf)
220 {
221 guint idx;
222 GstBuffer *old;
223
224 /* figure out the position in the array, note that index is never 0 because we
225 * push those packets immediately. */
226 idx = NNN + ((LLL + 1) * (index - 1));
227
228 GST_DEBUG_OBJECT (depay, "adding packet at index %u", idx);
229 /* free old buffer (should not happen) */
230 old = g_ptr_array_index (depay->packets, idx);
231 if (old)
232 gst_buffer_unref (old);
233
234 /* store new buffer */
235 g_ptr_array_index (depay->packets, idx) = outbuf;
236 }
237
238 static GstBuffer *
create_erasure_buffer(GstRtpQCELPDepay * depay)239 create_erasure_buffer (GstRtpQCELPDepay * depay)
240 {
241 GstBuffer *outbuf;
242 GstMapInfo map;
243
244 outbuf = gst_buffer_new_and_alloc (1);
245 gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
246 map.data[0] = 14;
247 gst_buffer_unmap (outbuf, &map);
248
249 return outbuf;
250 }
251
252 static GstBuffer *
gst_rtp_qcelp_depay_process(GstRTPBaseDepayload * depayload,GstRTPBuffer * rtp)253 gst_rtp_qcelp_depay_process (GstRTPBaseDepayload * depayload,
254 GstRTPBuffer * rtp)
255 {
256 GstRtpQCELPDepay *depay;
257 GstBuffer *outbuf;
258 GstClockTime timestamp;
259 guint payload_len, offset, index;
260 guint8 *payload;
261 guint LLL, NNN;
262
263 depay = GST_RTP_QCELP_DEPAY (depayload);
264
265 payload_len = gst_rtp_buffer_get_payload_len (rtp);
266
267 if (payload_len < 2)
268 goto too_small;
269
270 timestamp = GST_BUFFER_PTS (rtp->buffer);
271
272 payload = gst_rtp_buffer_get_payload (rtp);
273
274 /* 0 1 2 3 4 5 6 7
275 * +-+-+-+-+-+-+-+-+
276 * |RR | LLL | NNN |
277 * +-+-+-+-+-+-+-+-+
278 */
279 /* RR = payload[0] >> 6; */
280 LLL = (payload[0] & 0x38) >> 3;
281 NNN = (payload[0] & 0x07);
282
283 payload_len--;
284 payload++;
285
286 GST_DEBUG_OBJECT (depay, "LLL %u, NNN %u", LLL, NNN);
287
288 if (LLL > 5)
289 goto invalid_lll;
290
291 if (NNN > LLL)
292 goto invalid_nnn;
293
294 if (LLL != 0) {
295 /* we are interleaved */
296 if (!depay->interleaved) {
297 guint size;
298
299 GST_DEBUG_OBJECT (depay, "starting interleaving group");
300 /* bundling is not allowed to change in one interleave group */
301 depay->bundling = count_packets (depay, payload, payload_len);
302 GST_DEBUG_OBJECT (depay, "got bundling of %u", depay->bundling);
303 /* we have one bundle where NNN goes from 0 to L, we don't store the index
304 * 0 frames, so L+1 packets. Each packet has 'bundling - 1' packets */
305 size = (depay->bundling - 1) * (LLL + 1);
306 /* create the array to hold the packets */
307 if (depay->packets == NULL)
308 depay->packets = g_ptr_array_sized_new (size);
309 GST_DEBUG_OBJECT (depay, "created packet array of size %u", size);
310 g_ptr_array_set_size (depay->packets, size);
311 /* we were previously not interleaved, figure out how much space we
312 * need to deinterleave */
313 depay->interleaved = TRUE;
314 }
315 } else {
316 /* we are not interleaved */
317 if (depay->interleaved) {
318 GST_DEBUG_OBJECT (depay, "stopping interleaving");
319 /* flush packets if we were previously interleaved */
320 flush_packets (depay);
321 }
322 depay->bundling = 0;
323 }
324
325 index = 0;
326 offset = 1;
327
328 while (payload_len > 0) {
329 gint frame_len;
330 gboolean do_erasure;
331
332 frame_len = get_frame_len (depay, payload[0]);
333 GST_DEBUG_OBJECT (depay, "got frame len %d", frame_len);
334
335 if (frame_len == 0)
336 goto invalid_frame;
337
338 if (frame_len < 0) {
339 /* need to add an erasure frame but we can recover */
340 frame_len = -frame_len;
341 do_erasure = TRUE;
342 } else {
343 do_erasure = FALSE;
344 }
345
346 if (frame_len > payload_len)
347 goto invalid_frame;
348
349 if (do_erasure) {
350 /* create erasure frame */
351 outbuf = create_erasure_buffer (depay);
352 } else {
353 /* each frame goes into its buffer */
354 outbuf = gst_rtp_buffer_get_payload_subbuffer (rtp, offset, frame_len);
355 }
356
357 GST_BUFFER_PTS (outbuf) = timestamp;
358 GST_BUFFER_DURATION (outbuf) = FRAME_DURATION;
359
360 gst_rtp_drop_non_audio_meta (depayload, outbuf);
361
362 if (!depay->interleaved || index == 0) {
363 /* not interleaved or first frame in packet, just push */
364 gst_rtp_base_depayload_push (depayload, outbuf);
365
366 if (timestamp != -1)
367 timestamp += FRAME_DURATION;
368 } else {
369 /* put in interleave buffer */
370 add_packet (depay, LLL, NNN, index, outbuf);
371
372 if (timestamp != -1)
373 timestamp += (FRAME_DURATION * (LLL + 1));
374 }
375
376 payload_len -= frame_len;
377 payload += frame_len;
378 offset += frame_len;
379 index++;
380
381 /* discard excess packets */
382 if (depay->bundling > 0 && depay->bundling <= index)
383 break;
384 }
385 while (index < depay->bundling) {
386 GST_DEBUG_OBJECT (depay, "filling with erasure buffer");
387 /* fill remainder with erasure packets */
388 outbuf = create_erasure_buffer (depay);
389 add_packet (depay, LLL, NNN, index, outbuf);
390 index++;
391 }
392 if (depay->interleaved && LLL == NNN) {
393 GST_DEBUG_OBJECT (depay, "interleave group ended, flushing");
394 /* we have the complete interleave group, flush */
395 flush_packets (depay);
396 }
397
398 return NULL;
399
400 /* ERRORS */
401 too_small:
402 {
403 GST_ELEMENT_WARNING (depay, STREAM, DECODE,
404 (NULL), ("QCELP RTP payload too small (%d)", payload_len));
405 return NULL;
406 }
407 invalid_lll:
408 {
409 GST_ELEMENT_WARNING (depay, STREAM, DECODE,
410 (NULL), ("QCELP RTP invalid LLL received (%d)", LLL));
411 return NULL;
412 }
413 invalid_nnn:
414 {
415 GST_ELEMENT_WARNING (depay, STREAM, DECODE,
416 (NULL), ("QCELP RTP invalid NNN received (%d)", NNN));
417 return NULL;
418 }
419 invalid_frame:
420 {
421 GST_ELEMENT_WARNING (depay, STREAM, DECODE,
422 (NULL), ("QCELP RTP invalid frame received"));
423 return NULL;
424 }
425 }
426
427 gboolean
gst_rtp_qcelp_depay_plugin_init(GstPlugin * plugin)428 gst_rtp_qcelp_depay_plugin_init (GstPlugin * plugin)
429 {
430 return gst_element_register (plugin, "rtpqcelpdepay",
431 GST_RANK_SECONDARY, GST_TYPE_RTP_QCELP_DEPAY);
432 }
433