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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, 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 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
58 "clock-rate = (int) 8000, "
59 "encoding-name = (string) \"QCELP\"; "
61 "media = (string) \"audio\", "
62 "payload = (int) " GST_RTP_PAYLOAD_QCELP_STRING ", "
63 "clock-rate = (int) 8000")
66 static GstStaticPadTemplate gst_rtp_qcelp_depay_src_template =
67 GST_STATIC_PAD_TEMPLATE ("src",
70 GST_STATIC_CAPS ("audio/qcelp, " "channels = (int) 1," "rate = (int) 8000")
73 static void gst_rtp_qcelp_depay_finalize (GObject * object);
75 static gboolean gst_rtp_qcelp_depay_setcaps (GstBaseRTPDepayload * depayload,
77 static GstBuffer *gst_rtp_qcelp_depay_process (GstBaseRTPDepayload * depayload,
80 GST_BOILERPLATE (GstRtpQCELPDepay, gst_rtp_qcelp_depay, GstBaseRTPDepayload,
81 GST_TYPE_BASE_RTP_DEPAYLOAD);
84 gst_rtp_qcelp_depay_base_init (gpointer klass)
86 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
88 gst_element_class_add_pad_template (element_class,
89 gst_static_pad_template_get (&gst_rtp_qcelp_depay_src_template));
90 gst_element_class_add_pad_template (element_class,
91 gst_static_pad_template_get (&gst_rtp_qcelp_depay_sink_template));
93 gst_element_class_set_details_simple (element_class, "RTP QCELP depayloader",
94 "Codec/Depayloader/Network",
95 "Extracts QCELP (PureVoice) audio from RTP packets (RFC 2658)",
96 "Wim Taymans <wim.taymans@gmail.com>");
100 gst_rtp_qcelp_depay_class_init (GstRtpQCELPDepayClass * klass)
102 GObjectClass *gobject_class;
103 GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
105 gobject_class = (GObjectClass *) klass;
106 gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
108 gobject_class->finalize = gst_rtp_qcelp_depay_finalize;
110 gstbasertpdepayload_class->process = gst_rtp_qcelp_depay_process;
111 gstbasertpdepayload_class->set_caps = gst_rtp_qcelp_depay_setcaps;
113 GST_DEBUG_CATEGORY_INIT (rtpqcelpdepay_debug, "rtpqcelpdepay", 0,
114 "QCELP RTP Depayloader");
118 gst_rtp_qcelp_depay_init (GstRtpQCELPDepay * rtpqcelpdepay,
119 GstRtpQCELPDepayClass * klass)
121 GstBaseRTPDepayload *depayload;
123 depayload = GST_BASE_RTP_DEPAYLOAD (rtpqcelpdepay);
127 gst_rtp_qcelp_depay_finalize (GObject * object)
129 GstRtpQCELPDepay *depay;
131 depay = GST_RTP_QCELP_DEPAY (object);
133 if (depay->packets != NULL) {
134 g_ptr_array_foreach (depay->packets, (GFunc) gst_buffer_unref, NULL);
135 g_ptr_array_free (depay->packets, TRUE);
136 depay->packets = NULL;
139 G_OBJECT_CLASS (parent_class)->finalize (object);
144 gst_rtp_qcelp_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
147 GstRtpQCELPDepay *rtpqcelpdepay;
150 rtpqcelpdepay = GST_RTP_QCELP_DEPAY (depayload);
152 srccaps = gst_caps_new_simple ("audio/qcelp",
153 "channels", G_TYPE_INT, 1, "rate", G_TYPE_INT, 8000, NULL);
154 res = gst_pad_set_caps (GST_BASE_RTP_DEPAYLOAD_SRCPAD (depayload), srccaps);
155 gst_caps_unref (srccaps);
160 static const gint frame_size[16] = {
161 1, 4, 8, 17, 35, -8, 0, 0,
162 0, 0, 0, 0, 0, 0, 1, 0
165 /* get the frame length, 0 is invalid, negative values are invalid but can be
168 get_frame_len (GstRtpQCELPDepay * depay, guint8 frame_type)
173 return frame_size[frame_type];
177 count_packets (GstRtpQCELPDepay * depay, guint8 * data, guint size)
184 frame_len = get_frame_len (depay, data[0]);
186 /* 0 is invalid and we throw away the remainder of the frames */
191 frame_len = -frame_len;
193 if (frame_len > size)
204 flush_packets (GstRtpQCELPDepay * depay)
208 GST_DEBUG_OBJECT (depay, "flushing packets");
210 size = depay->packets->len;
212 for (i = 0; i < size; i++) {
215 outbuf = g_ptr_array_index (depay->packets, i);
216 g_ptr_array_index (depay->packets, i) = NULL;
218 gst_base_rtp_depayload_push (GST_BASE_RTP_DEPAYLOAD (depay), outbuf);
221 /* and reset interleaving state */
222 depay->interleaved = FALSE;
227 add_packet (GstRtpQCELPDepay * depay, guint LLL, guint NNN, guint index,
233 /* figure out the position in the array, note that index is never 0 because we
234 * push those packets immediately. */
235 idx = NNN + ((LLL + 1) * (index - 1));
237 GST_DEBUG_OBJECT (depay, "adding packet at index %u", idx);
238 /* free old buffer (should not happen) */
239 old = g_ptr_array_index (depay->packets, idx);
241 gst_buffer_unref (old);
243 /* store new buffer */
244 g_ptr_array_index (depay->packets, idx) = outbuf;
248 create_erasure_buffer (GstRtpQCELPDepay * depay)
252 outbuf = gst_buffer_new_and_alloc (1);
253 GST_BUFFER_DATA (outbuf)[0] = 14;
259 gst_rtp_qcelp_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
261 GstRtpQCELPDepay *depay;
263 GstClockTime timestamp;
264 guint payload_len, offset, index;
268 depay = GST_RTP_QCELP_DEPAY (depayload);
270 payload_len = gst_rtp_buffer_get_payload_len (buf);
275 timestamp = GST_BUFFER_TIMESTAMP (buf);
277 payload = gst_rtp_buffer_get_payload (buf);
284 RR = payload[0] >> 6;
285 LLL = (payload[0] & 0x38) >> 3;
286 NNN = (payload[0] & 0x07);
291 GST_DEBUG_OBJECT (depay, "LLL %u, NNN %u", LLL, NNN);
300 /* we are interleaved */
301 if (!depay->interleaved) {
304 GST_DEBUG_OBJECT (depay, "starting interleaving group");
305 /* bundling is not allowed to change in one interleave group */
306 depay->bundling = count_packets (depay, payload, payload_len);
307 GST_DEBUG_OBJECT (depay, "got bundling of %u", depay->bundling);
308 /* we have one bundle where NNN goes from 0 to L, we don't store the index
309 * 0 frames, so L+1 packets. Each packet has 'bundling - 1' packets */
310 size = (depay->bundling - 1) * (LLL + 1);
311 /* create the array to hold the packets */
312 if (depay->packets == NULL)
313 depay->packets = g_ptr_array_sized_new (size);
314 GST_DEBUG_OBJECT (depay, "created packet array of size %u", size);
315 g_ptr_array_set_size (depay->packets, size);
316 /* we were previously not interleaved, figure out how much space we
317 * need to deinterleave */
318 depay->interleaved = TRUE;
321 /* we are not interleaved */
322 if (depay->interleaved) {
323 GST_DEBUG_OBJECT (depay, "stopping interleaving");
324 /* flush packets if we were previously interleaved */
325 flush_packets (depay);
333 while (payload_len > 0) {
337 frame_len = get_frame_len (depay, payload[0]);
338 GST_DEBUG_OBJECT (depay, "got frame len %d", frame_len);
344 /* need to add an erasure frame but we can recover */
345 frame_len = -frame_len;
351 if (frame_len > payload_len)
355 /* create erasure frame */
356 outbuf = create_erasure_buffer (depay);
358 /* each frame goes into its buffer */
359 outbuf = gst_rtp_buffer_get_payload_subbuffer (buf, offset, frame_len);
362 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
363 GST_BUFFER_DURATION (outbuf) = FRAME_DURATION;
365 if (!depay->interleaved || index == 0) {
366 /* not interleaved or first frame in packet, just push */
367 gst_base_rtp_depayload_push (depayload, outbuf);
370 timestamp += FRAME_DURATION;
372 /* put in interleave buffer */
373 add_packet (depay, LLL, NNN, index, outbuf);
376 timestamp += (FRAME_DURATION * (LLL + 1));
379 payload_len -= frame_len;
380 payload += frame_len;
384 /* discard excess packets */
385 if (depay->bundling > 0 && depay->bundling <= index)
388 while (index < depay->bundling) {
389 GST_DEBUG_OBJECT (depay, "filling with erasure buffer");
390 /* fill remainder with erasure packets */
391 outbuf = create_erasure_buffer (depay);
392 add_packet (depay, LLL, NNN, index, outbuf);
395 if (depay->interleaved && LLL == NNN) {
396 GST_DEBUG_OBJECT (depay, "interleave group ended, flushing");
397 /* we have the complete interleave group, flush */
398 flush_packets (depay);
406 GST_ELEMENT_WARNING (depay, STREAM, DECODE,
407 (NULL), ("QCELP RTP payload too small (%d)", payload_len));
412 GST_ELEMENT_WARNING (depay, STREAM, DECODE,
413 (NULL), ("QCELP RTP invalid LLL received (%d)", LLL));
418 GST_ELEMENT_WARNING (depay, STREAM, DECODE,
419 (NULL), ("QCELP RTP invalid NNN received (%d)", NNN));
424 GST_ELEMENT_WARNING (depay, STREAM, DECODE,
425 (NULL), ("QCELP RTP invalid frame received"));
431 gst_rtp_qcelp_depay_plugin_init (GstPlugin * plugin)
433 return gst_element_register (plugin, "rtpqcelpdepay",
434 GST_RANK_MARGINAL, GST_TYPE_RTP_QCELP_DEPAY);