2 * Copyright (C) <2010> Wim Taymans <wim.taymans@gmail.com>
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.
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.
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.
24 #include <gst/rtp/gstrtpbuffer.h>
28 #include "gstrtpqcelpdepay.h"
30 GST_DEBUG_CATEGORY_STATIC (rtpqcelpdepay_debug);
31 #define GST_CAT_DEFAULT (rtpqcelpdepay_debug)
35 * RFC 2658 - RTP Payload Format for PureVoice(tm) Audio
37 #define FRAME_DURATION (20 * GST_MSECOND)
39 /* RtpQCELPDepay signals and args */
51 static GstStaticPadTemplate gst_rtp_qcelp_depay_sink_template =
52 GST_STATIC_PAD_TEMPLATE ("sink",
55 GST_STATIC_CAPS ("application/x-rtp, "
56 "media = (string) \"audio\", "
57 "clock-rate = (int) 8000, "
58 "encoding-name = (string) \"QCELP\"; "
60 "media = (string) \"audio\", "
61 "payload = (int) " GST_RTP_PAYLOAD_QCELP_STRING ", "
62 "clock-rate = (int) 8000")
65 static GstStaticPadTemplate gst_rtp_qcelp_depay_src_template =
66 GST_STATIC_PAD_TEMPLATE ("src",
69 GST_STATIC_CAPS ("audio/qcelp, " "channels = (int) 1," "rate = (int) 8000")
72 static void gst_rtp_qcelp_depay_finalize (GObject * object);
74 static gboolean gst_rtp_qcelp_depay_setcaps (GstRTPBaseDepayload * depayload,
76 static GstBuffer *gst_rtp_qcelp_depay_process (GstRTPBaseDepayload * depayload,
79 #define gst_rtp_qcelp_depay_parent_class parent_class
80 G_DEFINE_TYPE (GstRtpQCELPDepay, gst_rtp_qcelp_depay,
81 GST_TYPE_RTP_BASE_DEPAYLOAD);
84 gst_rtp_qcelp_depay_class_init (GstRtpQCELPDepayClass * klass)
86 GObjectClass *gobject_class;
87 GstElementClass *gstelement_class;
88 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
90 gobject_class = (GObjectClass *) klass;
91 gstelement_class = (GstElementClass *) klass;
92 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
94 gobject_class->finalize = gst_rtp_qcelp_depay_finalize;
96 gstrtpbasedepayload_class->process = gst_rtp_qcelp_depay_process;
97 gstrtpbasedepayload_class->set_caps = gst_rtp_qcelp_depay_setcaps;
99 gst_element_class_add_pad_template (gstelement_class,
100 gst_static_pad_template_get (&gst_rtp_qcelp_depay_src_template));
101 gst_element_class_add_pad_template (gstelement_class,
102 gst_static_pad_template_get (&gst_rtp_qcelp_depay_sink_template));
104 gst_element_class_set_static_metadata (gstelement_class,
105 "RTP QCELP depayloader", "Codec/Depayloader/Network/RTP",
106 "Extracts QCELP (PureVoice) audio from RTP packets (RFC 2658)",
107 "Wim Taymans <wim.taymans@gmail.com>");
109 GST_DEBUG_CATEGORY_INIT (rtpqcelpdepay_debug, "rtpqcelpdepay", 0,
110 "QCELP RTP Depayloader");
114 gst_rtp_qcelp_depay_init (GstRtpQCELPDepay * rtpqcelpdepay)
119 gst_rtp_qcelp_depay_finalize (GObject * object)
121 GstRtpQCELPDepay *depay;
123 depay = GST_RTP_QCELP_DEPAY (object);
125 if (depay->packets != NULL) {
126 g_ptr_array_foreach (depay->packets, (GFunc) gst_buffer_unref, NULL);
127 g_ptr_array_free (depay->packets, TRUE);
128 depay->packets = NULL;
131 G_OBJECT_CLASS (parent_class)->finalize (object);
136 gst_rtp_qcelp_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
141 srccaps = gst_caps_new_simple ("audio/qcelp",
142 "channels", G_TYPE_INT, 1, "rate", G_TYPE_INT, 8000, NULL);
143 res = gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload), srccaps);
144 gst_caps_unref (srccaps);
149 static const gint frame_size[16] = {
150 1, 4, 8, 17, 35, -8, 0, 0,
151 0, 0, 0, 0, 0, 0, 1, 0
154 /* get the frame length, 0 is invalid, negative values are invalid but can be
157 get_frame_len (GstRtpQCELPDepay * depay, guint8 frame_type)
159 if (frame_type >= G_N_ELEMENTS (frame_size))
162 return frame_size[frame_type];
166 count_packets (GstRtpQCELPDepay * depay, guint8 * data, guint size)
173 frame_len = get_frame_len (depay, data[0]);
175 /* 0 is invalid and we throw away the remainder of the frames */
180 frame_len = -frame_len;
182 if (frame_len > size)
193 flush_packets (GstRtpQCELPDepay * depay)
197 GST_DEBUG_OBJECT (depay, "flushing packets");
199 size = depay->packets->len;
201 for (i = 0; i < size; i++) {
204 outbuf = g_ptr_array_index (depay->packets, i);
205 g_ptr_array_index (depay->packets, i) = NULL;
207 gst_rtp_base_depayload_push (GST_RTP_BASE_DEPAYLOAD (depay), outbuf);
210 /* and reset interleaving state */
211 depay->interleaved = FALSE;
216 add_packet (GstRtpQCELPDepay * depay, guint LLL, guint NNN, guint index,
222 /* figure out the position in the array, note that index is never 0 because we
223 * push those packets immediately. */
224 idx = NNN + ((LLL + 1) * (index - 1));
226 GST_DEBUG_OBJECT (depay, "adding packet at index %u", idx);
227 /* free old buffer (should not happen) */
228 old = g_ptr_array_index (depay->packets, idx);
230 gst_buffer_unref (old);
232 /* store new buffer */
233 g_ptr_array_index (depay->packets, idx) = outbuf;
237 create_erasure_buffer (GstRtpQCELPDepay * depay)
242 outbuf = gst_buffer_new_and_alloc (1);
243 gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
245 gst_buffer_unmap (outbuf, &map);
251 gst_rtp_qcelp_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
253 GstRtpQCELPDepay *depay;
255 GstClockTime timestamp;
256 guint payload_len, offset, index;
259 GstRTPBuffer rtp = { NULL };
261 depay = GST_RTP_QCELP_DEPAY (depayload);
263 gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
265 payload_len = gst_rtp_buffer_get_payload_len (&rtp);
270 timestamp = GST_BUFFER_TIMESTAMP (buf);
272 payload = gst_rtp_buffer_get_payload (&rtp);
279 /* RR = payload[0] >> 6; */
280 LLL = (payload[0] & 0x38) >> 3;
281 NNN = (payload[0] & 0x07);
286 GST_DEBUG_OBJECT (depay, "LLL %u, NNN %u", LLL, NNN);
295 /* we are interleaved */
296 if (!depay->interleaved) {
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;
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);
328 while (payload_len > 0) {
332 frame_len = get_frame_len (depay, payload[0]);
333 GST_DEBUG_OBJECT (depay, "got frame len %d", frame_len);
339 /* need to add an erasure frame but we can recover */
340 frame_len = -frame_len;
346 if (frame_len > payload_len)
350 /* create erasure frame */
351 outbuf = create_erasure_buffer (depay);
353 /* each frame goes into its buffer */
354 outbuf = gst_rtp_buffer_get_payload_subbuffer (&rtp, offset, frame_len);
357 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
358 GST_BUFFER_DURATION (outbuf) = FRAME_DURATION;
360 if (!depay->interleaved || index == 0) {
361 /* not interleaved or first frame in packet, just push */
362 gst_rtp_base_depayload_push (depayload, outbuf);
365 timestamp += FRAME_DURATION;
367 /* put in interleave buffer */
368 add_packet (depay, LLL, NNN, index, outbuf);
371 timestamp += (FRAME_DURATION * (LLL + 1));
374 payload_len -= frame_len;
375 payload += frame_len;
379 /* discard excess packets */
380 if (depay->bundling > 0 && depay->bundling <= index)
383 while (index < depay->bundling) {
384 GST_DEBUG_OBJECT (depay, "filling with erasure buffer");
385 /* fill remainder with erasure packets */
386 outbuf = create_erasure_buffer (depay);
387 add_packet (depay, LLL, NNN, index, outbuf);
390 if (depay->interleaved && LLL == NNN) {
391 GST_DEBUG_OBJECT (depay, "interleave group ended, flushing");
392 /* we have the complete interleave group, flush */
393 flush_packets (depay);
396 gst_rtp_buffer_unmap (&rtp);
402 GST_ELEMENT_WARNING (depay, STREAM, DECODE,
403 (NULL), ("QCELP RTP payload too small (%d)", payload_len));
404 gst_rtp_buffer_unmap (&rtp);
409 GST_ELEMENT_WARNING (depay, STREAM, DECODE,
410 (NULL), ("QCELP RTP invalid LLL received (%d)", LLL));
411 gst_rtp_buffer_unmap (&rtp);
416 GST_ELEMENT_WARNING (depay, STREAM, DECODE,
417 (NULL), ("QCELP RTP invalid NNN received (%d)", NNN));
418 gst_rtp_buffer_unmap (&rtp);
423 GST_ELEMENT_WARNING (depay, STREAM, DECODE,
424 (NULL), ("QCELP RTP invalid frame received"));
425 gst_rtp_buffer_unmap (&rtp);
431 gst_rtp_qcelp_depay_plugin_init (GstPlugin * plugin)
433 return gst_element_register (plugin, "rtpqcelpdepay",
434 GST_RANK_SECONDARY, GST_TYPE_RTP_QCELP_DEPAY);