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>
25 #include <gst/audio/audio.h>
29 #include "gstrtpqcelpdepay.h"
30 #include "gstrtputils.h"
32 GST_DEBUG_CATEGORY_STATIC (rtpqcelpdepay_debug);
33 #define GST_CAT_DEFAULT (rtpqcelpdepay_debug)
37 * RFC 2658 - RTP Payload Format for PureVoice(tm) Audio
39 #define FRAME_DURATION (20 * GST_MSECOND)
41 /* RtpQCELPDepay signals and args */
53 static GstStaticPadTemplate gst_rtp_qcelp_depay_sink_template =
54 GST_STATIC_PAD_TEMPLATE ("sink",
57 GST_STATIC_CAPS ("application/x-rtp, "
58 "media = (string) \"audio\", "
59 "clock-rate = (int) 8000, "
60 "encoding-name = (string) \"QCELP\"; "
62 "media = (string) \"audio\", "
63 "payload = (int) " GST_RTP_PAYLOAD_QCELP_STRING ", "
64 "clock-rate = (int) 8000")
67 static GstStaticPadTemplate gst_rtp_qcelp_depay_src_template =
68 GST_STATIC_PAD_TEMPLATE ("src",
71 GST_STATIC_CAPS ("audio/qcelp, " "channels = (int) 1," "rate = (int) 8000")
74 static void gst_rtp_qcelp_depay_finalize (GObject * object);
76 static gboolean gst_rtp_qcelp_depay_setcaps (GstRTPBaseDepayload * depayload,
78 static GstBuffer *gst_rtp_qcelp_depay_process (GstRTPBaseDepayload * depayload,
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);
86 gst_rtp_qcelp_depay_class_init (GstRtpQCELPDepayClass * klass)
88 GObjectClass *gobject_class;
89 GstElementClass *gstelement_class;
90 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
92 gobject_class = (GObjectClass *) klass;
93 gstelement_class = (GstElementClass *) klass;
94 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
96 gobject_class->finalize = gst_rtp_qcelp_depay_finalize;
98 gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_qcelp_depay_process;
99 gstrtpbasedepayload_class->set_caps = gst_rtp_qcelp_depay_setcaps;
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);
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>");
111 GST_DEBUG_CATEGORY_INIT (rtpqcelpdepay_debug, "rtpqcelpdepay", 0,
112 "QCELP RTP Depayloader");
116 gst_rtp_qcelp_depay_init (GstRtpQCELPDepay * rtpqcelpdepay)
121 gst_rtp_qcelp_depay_finalize (GObject * object)
123 GstRtpQCELPDepay *depay;
125 depay = GST_RTP_QCELP_DEPAY (object);
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;
133 G_OBJECT_CLASS (parent_class)->finalize (object);
138 gst_rtp_qcelp_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
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);
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
156 /* get the frame length, 0 is invalid, negative values are invalid but can be
159 get_frame_len (GstRtpQCELPDepay * depay, guint8 frame_type)
161 if (frame_type >= G_N_ELEMENTS (frame_size))
164 return frame_size[frame_type];
168 count_packets (GstRtpQCELPDepay * depay, guint8 * data, guint size)
175 frame_len = get_frame_len (depay, data[0]);
177 /* 0 is invalid and we throw away the remainder of the frames */
182 frame_len = -frame_len;
184 if (frame_len > size)
195 flush_packets (GstRtpQCELPDepay * depay)
199 GST_DEBUG_OBJECT (depay, "flushing packets");
201 size = depay->packets->len;
203 for (i = 0; i < size; i++) {
206 outbuf = g_ptr_array_index (depay->packets, i);
207 g_ptr_array_index (depay->packets, i) = NULL;
209 gst_rtp_base_depayload_push (GST_RTP_BASE_DEPAYLOAD (depay), outbuf);
212 /* and reset interleaving state */
213 depay->interleaved = FALSE;
218 add_packet (GstRtpQCELPDepay * depay, guint LLL, guint NNN, guint index,
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));
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);
232 gst_buffer_unref (old);
234 /* store new buffer */
235 g_ptr_array_index (depay->packets, idx) = outbuf;
239 create_erasure_buffer (GstRtpQCELPDepay * depay)
244 outbuf = gst_buffer_new_and_alloc (1);
245 gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
247 gst_buffer_unmap (outbuf, &map);
253 gst_rtp_qcelp_depay_process (GstRTPBaseDepayload * depayload,
256 GstRtpQCELPDepay *depay;
258 GstClockTime timestamp;
259 guint payload_len, offset, index;
263 depay = GST_RTP_QCELP_DEPAY (depayload);
265 payload_len = gst_rtp_buffer_get_payload_len (rtp);
270 timestamp = GST_BUFFER_PTS (rtp->buffer);
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_PTS (outbuf) = timestamp;
358 GST_BUFFER_DURATION (outbuf) = FRAME_DURATION;
360 gst_rtp_drop_meta (GST_ELEMENT_CAST (depayload), outbuf,
361 g_quark_from_static_string (GST_META_TAG_AUDIO_STR));
363 if (!depay->interleaved || index == 0) {
364 /* not interleaved or first frame in packet, just push */
365 gst_rtp_base_depayload_push (depayload, outbuf);
368 timestamp += FRAME_DURATION;
370 /* put in interleave buffer */
371 add_packet (depay, LLL, NNN, index, outbuf);
374 timestamp += (FRAME_DURATION * (LLL + 1));
377 payload_len -= frame_len;
378 payload += frame_len;
382 /* discard excess packets */
383 if (depay->bundling > 0 && depay->bundling <= index)
386 while (index < depay->bundling) {
387 GST_DEBUG_OBJECT (depay, "filling with erasure buffer");
388 /* fill remainder with erasure packets */
389 outbuf = create_erasure_buffer (depay);
390 add_packet (depay, LLL, NNN, index, outbuf);
393 if (depay->interleaved && LLL == NNN) {
394 GST_DEBUG_OBJECT (depay, "interleave group ended, flushing");
395 /* we have the complete interleave group, flush */
396 flush_packets (depay);
404 GST_ELEMENT_WARNING (depay, STREAM, DECODE,
405 (NULL), ("QCELP RTP payload too small (%d)", payload_len));
410 GST_ELEMENT_WARNING (depay, STREAM, DECODE,
411 (NULL), ("QCELP RTP invalid LLL received (%d)", LLL));
416 GST_ELEMENT_WARNING (depay, STREAM, DECODE,
417 (NULL), ("QCELP RTP invalid NNN received (%d)", NNN));
422 GST_ELEMENT_WARNING (depay, STREAM, DECODE,
423 (NULL), ("QCELP RTP invalid frame received"));
429 gst_rtp_qcelp_depay_plugin_init (GstPlugin * plugin)
431 return gst_element_register (plugin, "rtpqcelpdepay",
432 GST_RANK_SECONDARY, GST_TYPE_RTP_QCELP_DEPAY);