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 "gstrtpelements.h"
30 #include "gstrtpqcelpdepay.h"
31 #include "gstrtputils.h"
33 GST_DEBUG_CATEGORY_STATIC (rtpqcelpdepay_debug);
34 #define GST_CAT_DEFAULT (rtpqcelpdepay_debug)
38 * RFC 2658 - RTP Payload Format for PureVoice(tm) Audio
40 #define FRAME_DURATION (20 * GST_MSECOND)
42 /* RtpQCELPDepay signals and args */
54 static GstStaticPadTemplate gst_rtp_qcelp_depay_sink_template =
55 GST_STATIC_PAD_TEMPLATE ("sink",
58 GST_STATIC_CAPS ("application/x-rtp, "
59 "media = (string) \"audio\", "
60 "clock-rate = (int) 8000, "
61 "encoding-name = (string) \"QCELP\"; "
63 "media = (string) \"audio\", "
64 "payload = (int) " GST_RTP_PAYLOAD_QCELP_STRING ", "
65 "clock-rate = (int) 8000")
68 static GstStaticPadTemplate gst_rtp_qcelp_depay_src_template =
69 GST_STATIC_PAD_TEMPLATE ("src",
72 GST_STATIC_CAPS ("audio/qcelp, " "channels = (int) 1," "rate = (int) 8000")
75 static void gst_rtp_qcelp_depay_finalize (GObject * object);
77 static gboolean gst_rtp_qcelp_depay_setcaps (GstRTPBaseDepayload * depayload,
79 static GstBuffer *gst_rtp_qcelp_depay_process (GstRTPBaseDepayload * depayload,
82 #define gst_rtp_qcelp_depay_parent_class parent_class
83 G_DEFINE_TYPE (GstRtpQCELPDepay, gst_rtp_qcelp_depay,
84 GST_TYPE_RTP_BASE_DEPAYLOAD);
85 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtpqcelpdepay, "rtpqcelpdepay",
86 GST_RANK_SECONDARY, GST_TYPE_RTP_QCELP_DEPAY, rtp_element_init (plugin));
89 gst_rtp_qcelp_depay_class_init (GstRtpQCELPDepayClass * klass)
91 GObjectClass *gobject_class;
92 GstElementClass *gstelement_class;
93 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
95 gobject_class = (GObjectClass *) klass;
96 gstelement_class = (GstElementClass *) klass;
97 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
99 gobject_class->finalize = gst_rtp_qcelp_depay_finalize;
101 gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_qcelp_depay_process;
102 gstrtpbasedepayload_class->set_caps = gst_rtp_qcelp_depay_setcaps;
104 gst_element_class_add_static_pad_template (gstelement_class,
105 &gst_rtp_qcelp_depay_src_template);
106 gst_element_class_add_static_pad_template (gstelement_class,
107 &gst_rtp_qcelp_depay_sink_template);
109 gst_element_class_set_static_metadata (gstelement_class,
110 "RTP QCELP depayloader", "Codec/Depayloader/Network/RTP",
111 "Extracts QCELP (PureVoice) audio from RTP packets (RFC 2658)",
112 "Wim Taymans <wim.taymans@gmail.com>");
114 GST_DEBUG_CATEGORY_INIT (rtpqcelpdepay_debug, "rtpqcelpdepay", 0,
115 "QCELP RTP Depayloader");
119 gst_rtp_qcelp_depay_init (GstRtpQCELPDepay * rtpqcelpdepay)
124 gst_rtp_qcelp_depay_finalize (GObject * object)
126 GstRtpQCELPDepay *depay;
128 depay = GST_RTP_QCELP_DEPAY (object);
130 if (depay->packets != NULL) {
131 g_ptr_array_foreach (depay->packets, (GFunc) gst_buffer_unref, NULL);
132 g_ptr_array_free (depay->packets, TRUE);
133 depay->packets = NULL;
136 G_OBJECT_CLASS (parent_class)->finalize (object);
141 gst_rtp_qcelp_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
146 srccaps = gst_caps_new_simple ("audio/qcelp",
147 "channels", G_TYPE_INT, 1, "rate", G_TYPE_INT, 8000, NULL);
148 res = gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload), srccaps);
149 gst_caps_unref (srccaps);
154 static const gint frame_size[16] = {
155 1, 4, 8, 17, 35, -8, 0, 0,
156 0, 0, 0, 0, 0, 0, 1, 0
159 /* get the frame length, 0 is invalid, negative values are invalid but can be
162 get_frame_len (GstRtpQCELPDepay * depay, guint8 frame_type)
164 if (frame_type >= G_N_ELEMENTS (frame_size))
167 return frame_size[frame_type];
171 count_packets (GstRtpQCELPDepay * depay, guint8 * data, guint size)
178 frame_len = get_frame_len (depay, data[0]);
180 /* 0 is invalid and we throw away the remainder of the frames */
185 frame_len = -frame_len;
187 if (frame_len > size)
198 flush_packets (GstRtpQCELPDepay * depay)
202 GST_DEBUG_OBJECT (depay, "flushing packets");
204 size = depay->packets->len;
206 for (i = 0; i < size; i++) {
209 outbuf = g_ptr_array_index (depay->packets, i);
210 g_ptr_array_index (depay->packets, i) = NULL;
212 gst_rtp_base_depayload_push (GST_RTP_BASE_DEPAYLOAD (depay), outbuf);
215 /* and reset interleaving state */
216 depay->interleaved = FALSE;
221 add_packet (GstRtpQCELPDepay * depay, guint LLL, guint NNN, guint index,
227 /* figure out the position in the array, note that index is never 0 because we
228 * push those packets immediately. */
229 idx = NNN + ((LLL + 1) * (index - 1));
231 GST_DEBUG_OBJECT (depay, "adding packet at index %u", idx);
232 /* free old buffer (should not happen) */
233 old = g_ptr_array_index (depay->packets, idx);
235 gst_buffer_unref (old);
237 /* store new buffer */
238 g_ptr_array_index (depay->packets, idx) = outbuf;
242 create_erasure_buffer (GstRtpQCELPDepay * depay)
247 outbuf = gst_buffer_new_and_alloc (1);
248 gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
250 gst_buffer_unmap (outbuf, &map);
256 gst_rtp_qcelp_depay_process (GstRTPBaseDepayload * depayload,
259 GstRtpQCELPDepay *depay;
261 GstClockTime timestamp;
262 guint payload_len, offset, index;
266 depay = GST_RTP_QCELP_DEPAY (depayload);
268 payload_len = gst_rtp_buffer_get_payload_len (rtp);
273 timestamp = GST_BUFFER_PTS (rtp->buffer);
275 payload = gst_rtp_buffer_get_payload (rtp);
282 /* RR = payload[0] >> 6; */
283 LLL = (payload[0] & 0x38) >> 3;
284 NNN = (payload[0] & 0x07);
289 GST_DEBUG_OBJECT (depay, "LLL %u, NNN %u", LLL, NNN);
298 /* we are interleaved */
299 if (!depay->interleaved) {
302 GST_DEBUG_OBJECT (depay, "starting interleaving group");
303 /* bundling is not allowed to change in one interleave group */
304 depay->bundling = count_packets (depay, payload, payload_len);
305 GST_DEBUG_OBJECT (depay, "got bundling of %u", depay->bundling);
306 /* we have one bundle where NNN goes from 0 to L, we don't store the index
307 * 0 frames, so L+1 packets. Each packet has 'bundling - 1' packets */
308 size = (depay->bundling - 1) * (LLL + 1);
309 /* create the array to hold the packets */
310 if (depay->packets == NULL)
311 depay->packets = g_ptr_array_sized_new (size);
312 GST_DEBUG_OBJECT (depay, "created packet array of size %u", size);
313 g_ptr_array_set_size (depay->packets, size);
314 /* we were previously not interleaved, figure out how much space we
315 * need to deinterleave */
316 depay->interleaved = TRUE;
319 /* we are not interleaved */
320 if (depay->interleaved) {
321 GST_DEBUG_OBJECT (depay, "stopping interleaving");
322 /* flush packets if we were previously interleaved */
323 flush_packets (depay);
331 while (payload_len > 0) {
335 frame_len = get_frame_len (depay, payload[0]);
336 GST_DEBUG_OBJECT (depay, "got frame len %d", frame_len);
342 /* need to add an erasure frame but we can recover */
343 frame_len = -frame_len;
349 if (frame_len > payload_len)
353 /* create erasure frame */
354 outbuf = create_erasure_buffer (depay);
356 /* each frame goes into its buffer */
357 outbuf = gst_rtp_buffer_get_payload_subbuffer (rtp, offset, frame_len);
360 GST_BUFFER_PTS (outbuf) = timestamp;
361 GST_BUFFER_DURATION (outbuf) = FRAME_DURATION;
363 gst_rtp_drop_non_audio_meta (depayload, outbuf);
365 if (!depay->interleaved || index == 0) {
366 /* not interleaved or first frame in packet, just push */
367 gst_rtp_base_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"));