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_static_pad_template (element_class,
89 &gst_rtp_qcelp_depay_src_template);
90 gst_element_class_add_static_pad_template (element_class,
91 &gst_rtp_qcelp_depay_sink_template);
93 gst_element_class_set_details_simple (element_class, "RTP QCELP depayloader",
94 "Codec/Depayloader/Network/RTP",
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 G_GNUC_UNUSED *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)
149 srccaps = gst_caps_new_simple ("audio/qcelp",
150 "channels", G_TYPE_INT, 1, "rate", G_TYPE_INT, 8000, NULL);
151 res = gst_pad_set_caps (GST_BASE_RTP_DEPAYLOAD_SRCPAD (depayload), srccaps);
152 gst_caps_unref (srccaps);
157 static const gint frame_size[16] = {
158 1, 4, 8, 17, 35, -8, 0, 0,
159 0, 0, 0, 0, 0, 0, 1, 0
162 /* get the frame length, 0 is invalid, negative values are invalid but can be
165 get_frame_len (GstRtpQCELPDepay * depay, guint8 frame_type)
167 if (frame_type >= G_N_ELEMENTS (frame_size))
170 return frame_size[frame_type];
174 count_packets (GstRtpQCELPDepay * depay, guint8 * data, guint size)
181 frame_len = get_frame_len (depay, data[0]);
183 /* 0 is invalid and we throw away the remainder of the frames */
188 frame_len = -frame_len;
190 if (frame_len > size)
201 flush_packets (GstRtpQCELPDepay * depay)
205 GST_DEBUG_OBJECT (depay, "flushing packets");
207 size = depay->packets->len;
209 for (i = 0; i < size; i++) {
212 outbuf = g_ptr_array_index (depay->packets, i);
213 g_ptr_array_index (depay->packets, i) = NULL;
215 gst_base_rtp_depayload_push (GST_BASE_RTP_DEPAYLOAD (depay), outbuf);
218 /* and reset interleaving state */
219 depay->interleaved = FALSE;
224 add_packet (GstRtpQCELPDepay * depay, guint LLL, guint NNN, guint index,
230 /* figure out the position in the array, note that index is never 0 because we
231 * push those packets immediately. */
232 idx = NNN + ((LLL + 1) * (index - 1));
234 GST_DEBUG_OBJECT (depay, "adding packet at index %u", idx);
235 /* free old buffer (should not happen) */
236 old = g_ptr_array_index (depay->packets, idx);
238 gst_buffer_unref (old);
240 /* store new buffer */
241 g_ptr_array_index (depay->packets, idx) = outbuf;
245 create_erasure_buffer (GstRtpQCELPDepay * depay)
249 outbuf = gst_buffer_new_and_alloc (1);
250 GST_BUFFER_DATA (outbuf)[0] = 14;
256 gst_rtp_qcelp_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
258 GstRtpQCELPDepay *depay;
260 GstClockTime timestamp;
261 guint payload_len, offset, index;
265 depay = GST_RTP_QCELP_DEPAY (depayload);
267 payload_len = gst_rtp_buffer_get_payload_len (buf);
272 timestamp = GST_BUFFER_TIMESTAMP (buf);
274 payload = gst_rtp_buffer_get_payload (buf);
281 /* RR = payload[0] >> 6; */
282 LLL = (payload[0] & 0x38) >> 3;
283 NNN = (payload[0] & 0x07);
288 GST_DEBUG_OBJECT (depay, "LLL %u, NNN %u", LLL, NNN);
297 /* we are interleaved */
298 if (!depay->interleaved) {
301 GST_DEBUG_OBJECT (depay, "starting interleaving group");
302 /* bundling is not allowed to change in one interleave group */
303 depay->bundling = count_packets (depay, payload, payload_len);
304 GST_DEBUG_OBJECT (depay, "got bundling of %u", depay->bundling);
305 /* we have one bundle where NNN goes from 0 to L, we don't store the index
306 * 0 frames, so L+1 packets. Each packet has 'bundling - 1' packets */
307 size = (depay->bundling - 1) * (LLL + 1);
308 /* create the array to hold the packets */
309 if (depay->packets == NULL)
310 depay->packets = g_ptr_array_sized_new (size);
311 GST_DEBUG_OBJECT (depay, "created packet array of size %u", size);
312 g_ptr_array_set_size (depay->packets, size);
313 /* we were previously not interleaved, figure out how much space we
314 * need to deinterleave */
315 depay->interleaved = TRUE;
318 /* we are not interleaved */
319 if (depay->interleaved) {
320 GST_DEBUG_OBJECT (depay, "stopping interleaving");
321 /* flush packets if we were previously interleaved */
322 flush_packets (depay);
330 while (payload_len > 0) {
334 frame_len = get_frame_len (depay, payload[0]);
335 GST_DEBUG_OBJECT (depay, "got frame len %d", frame_len);
341 /* need to add an erasure frame but we can recover */
342 frame_len = -frame_len;
348 if (frame_len > payload_len)
352 /* create erasure frame */
353 outbuf = create_erasure_buffer (depay);
355 /* each frame goes into its buffer */
356 outbuf = gst_rtp_buffer_get_payload_subbuffer (buf, offset, frame_len);
359 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
360 GST_BUFFER_DURATION (outbuf) = FRAME_DURATION;
362 if (!depay->interleaved || index == 0) {
363 /* not interleaved or first frame in packet, just push */
364 gst_base_rtp_depayload_push (depayload, outbuf);
367 timestamp += FRAME_DURATION;
369 /* put in interleave buffer */
370 add_packet (depay, LLL, NNN, index, outbuf);
373 timestamp += (FRAME_DURATION * (LLL + 1));
376 payload_len -= frame_len;
377 payload += frame_len;
381 /* discard excess packets */
382 if (depay->bundling > 0 && depay->bundling <= index)
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);
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);
403 GST_ELEMENT_WARNING (depay, STREAM, DECODE,
404 (NULL), ("QCELP RTP payload too small (%d)", payload_len));
409 GST_ELEMENT_WARNING (depay, STREAM, DECODE,
410 (NULL), ("QCELP RTP invalid LLL received (%d)", LLL));
415 GST_ELEMENT_WARNING (depay, STREAM, DECODE,
416 (NULL), ("QCELP RTP invalid NNN received (%d)", NNN));
421 GST_ELEMENT_WARNING (depay, STREAM, DECODE,
422 (NULL), ("QCELP RTP invalid frame received"));
428 gst_rtp_qcelp_depay_plugin_init (GstPlugin * plugin)
430 return gst_element_register (plugin, "rtpqcelpdepay",
431 GST_RANK_SECONDARY, GST_TYPE_RTP_QCELP_DEPAY);