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 (GstRTPBaseDepayload * depayload,
77 static GstBuffer *gst_rtp_qcelp_depay_process (GstRTPBaseDepayload * depayload,
80 #define gst_rtp_qcelp_depay_parent_class parent_class
81 G_DEFINE_TYPE (GstRtpQCELPDepay, gst_rtp_qcelp_depay,
82 GST_TYPE_RTP_BASE_DEPAYLOAD);
85 gst_rtp_qcelp_depay_class_init (GstRtpQCELPDepayClass * klass)
87 GObjectClass *gobject_class;
88 GstElementClass *gstelement_class;
89 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
91 gobject_class = (GObjectClass *) klass;
92 gstelement_class = (GstElementClass *) klass;
93 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
95 gobject_class->finalize = gst_rtp_qcelp_depay_finalize;
97 gstrtpbasedepayload_class->process = gst_rtp_qcelp_depay_process;
98 gstrtpbasedepayload_class->set_caps = gst_rtp_qcelp_depay_setcaps;
100 gst_element_class_add_pad_template (gstelement_class,
101 gst_static_pad_template_get (&gst_rtp_qcelp_depay_src_template));
102 gst_element_class_add_pad_template (gstelement_class,
103 gst_static_pad_template_get (&gst_rtp_qcelp_depay_sink_template));
105 gst_element_class_set_details_simple (gstelement_class,
106 "RTP QCELP depayloader", "Codec/Depayloader/Network/RTP",
107 "Extracts QCELP (PureVoice) audio from RTP packets (RFC 2658)",
108 "Wim Taymans <wim.taymans@gmail.com>");
110 GST_DEBUG_CATEGORY_INIT (rtpqcelpdepay_debug, "rtpqcelpdepay", 0,
111 "QCELP RTP Depayloader");
115 gst_rtp_qcelp_depay_init (GstRtpQCELPDepay * rtpqcelpdepay)
117 GstRTPBaseDepayload G_GNUC_UNUSED *depayload;
119 depayload = GST_RTP_BASE_DEPAYLOAD (rtpqcelpdepay);
123 gst_rtp_qcelp_depay_finalize (GObject * object)
125 GstRtpQCELPDepay *depay;
127 depay = GST_RTP_QCELP_DEPAY (object);
129 if (depay->packets != NULL) {
130 g_ptr_array_foreach (depay->packets, (GFunc) gst_buffer_unref, NULL);
131 g_ptr_array_free (depay->packets, TRUE);
132 depay->packets = NULL;
135 G_OBJECT_CLASS (parent_class)->finalize (object);
140 gst_rtp_qcelp_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
145 srccaps = gst_caps_new_simple ("audio/qcelp",
146 "channels", G_TYPE_INT, 1, "rate", G_TYPE_INT, 8000, NULL);
147 res = gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload), srccaps);
148 gst_caps_unref (srccaps);
153 static const gint frame_size[16] = {
154 1, 4, 8, 17, 35, -8, 0, 0,
155 0, 0, 0, 0, 0, 0, 1, 0
158 /* get the frame length, 0 is invalid, negative values are invalid but can be
161 get_frame_len (GstRtpQCELPDepay * depay, guint8 frame_type)
163 if (frame_type >= G_N_ELEMENTS (frame_size))
166 return frame_size[frame_type];
170 count_packets (GstRtpQCELPDepay * depay, guint8 * data, guint size)
177 frame_len = get_frame_len (depay, data[0]);
179 /* 0 is invalid and we throw away the remainder of the frames */
184 frame_len = -frame_len;
186 if (frame_len > size)
197 flush_packets (GstRtpQCELPDepay * depay)
201 GST_DEBUG_OBJECT (depay, "flushing packets");
203 size = depay->packets->len;
205 for (i = 0; i < size; i++) {
208 outbuf = g_ptr_array_index (depay->packets, i);
209 g_ptr_array_index (depay->packets, i) = NULL;
211 gst_rtp_base_depayload_push (GST_RTP_BASE_DEPAYLOAD (depay), outbuf);
214 /* and reset interleaving state */
215 depay->interleaved = FALSE;
220 add_packet (GstRtpQCELPDepay * depay, guint LLL, guint NNN, guint index,
226 /* figure out the position in the array, note that index is never 0 because we
227 * push those packets immediately. */
228 idx = NNN + ((LLL + 1) * (index - 1));
230 GST_DEBUG_OBJECT (depay, "adding packet at index %u", idx);
231 /* free old buffer (should not happen) */
232 old = g_ptr_array_index (depay->packets, idx);
234 gst_buffer_unref (old);
236 /* store new buffer */
237 g_ptr_array_index (depay->packets, idx) = outbuf;
241 create_erasure_buffer (GstRtpQCELPDepay * depay)
246 outbuf = gst_buffer_new_and_alloc (1);
247 gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
249 gst_buffer_unmap (outbuf, &map);
255 gst_rtp_qcelp_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
257 GstRtpQCELPDepay *depay;
259 GstClockTime timestamp;
260 guint payload_len, offset, index;
263 GstRTPBuffer rtp = { NULL };
265 depay = GST_RTP_QCELP_DEPAY (depayload);
267 gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
269 payload_len = gst_rtp_buffer_get_payload_len (&rtp);
274 timestamp = GST_BUFFER_TIMESTAMP (buf);
276 payload = gst_rtp_buffer_get_payload (&rtp);
283 /* RR = payload[0] >> 6; */
284 LLL = (payload[0] & 0x38) >> 3;
285 NNN = (payload[0] & 0x07);
290 GST_DEBUG_OBJECT (depay, "LLL %u, NNN %u", LLL, NNN);
299 /* we are interleaved */
300 if (!depay->interleaved) {
303 GST_DEBUG_OBJECT (depay, "starting interleaving group");
304 /* bundling is not allowed to change in one interleave group */
305 depay->bundling = count_packets (depay, payload, payload_len);
306 GST_DEBUG_OBJECT (depay, "got bundling of %u", depay->bundling);
307 /* we have one bundle where NNN goes from 0 to L, we don't store the index
308 * 0 frames, so L+1 packets. Each packet has 'bundling - 1' packets */
309 size = (depay->bundling - 1) * (LLL + 1);
310 /* create the array to hold the packets */
311 if (depay->packets == NULL)
312 depay->packets = g_ptr_array_sized_new (size);
313 GST_DEBUG_OBJECT (depay, "created packet array of size %u", size);
314 g_ptr_array_set_size (depay->packets, size);
315 /* we were previously not interleaved, figure out how much space we
316 * need to deinterleave */
317 depay->interleaved = TRUE;
320 /* we are not interleaved */
321 if (depay->interleaved) {
322 GST_DEBUG_OBJECT (depay, "stopping interleaving");
323 /* flush packets if we were previously interleaved */
324 flush_packets (depay);
332 while (payload_len > 0) {
336 frame_len = get_frame_len (depay, payload[0]);
337 GST_DEBUG_OBJECT (depay, "got frame len %d", frame_len);
343 /* need to add an erasure frame but we can recover */
344 frame_len = -frame_len;
350 if (frame_len > payload_len)
354 /* create erasure frame */
355 outbuf = create_erasure_buffer (depay);
357 /* each frame goes into its buffer */
358 outbuf = gst_rtp_buffer_get_payload_subbuffer (&rtp, offset, frame_len);
361 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
362 GST_BUFFER_DURATION (outbuf) = FRAME_DURATION;
364 if (!depay->interleaved || index == 0) {
365 /* not interleaved or first frame in packet, just push */
366 gst_rtp_base_depayload_push (depayload, outbuf);
369 timestamp += FRAME_DURATION;
371 /* put in interleave buffer */
372 add_packet (depay, LLL, NNN, index, outbuf);
375 timestamp += (FRAME_DURATION * (LLL + 1));
378 payload_len -= frame_len;
379 payload += frame_len;
383 /* discard excess packets */
384 if (depay->bundling > 0 && depay->bundling <= index)
387 while (index < depay->bundling) {
388 GST_DEBUG_OBJECT (depay, "filling with erasure buffer");
389 /* fill remainder with erasure packets */
390 outbuf = create_erasure_buffer (depay);
391 add_packet (depay, LLL, NNN, index, outbuf);
394 if (depay->interleaved && LLL == NNN) {
395 GST_DEBUG_OBJECT (depay, "interleave group ended, flushing");
396 /* we have the complete interleave group, flush */
397 flush_packets (depay);
400 gst_rtp_buffer_unmap (&rtp);
406 GST_ELEMENT_WARNING (depay, STREAM, DECODE,
407 (NULL), ("QCELP RTP payload too small (%d)", payload_len));
408 gst_rtp_buffer_unmap (&rtp);
413 GST_ELEMENT_WARNING (depay, STREAM, DECODE,
414 (NULL), ("QCELP RTP invalid LLL received (%d)", LLL));
415 gst_rtp_buffer_unmap (&rtp);
420 GST_ELEMENT_WARNING (depay, STREAM, DECODE,
421 (NULL), ("QCELP RTP invalid NNN received (%d)", NNN));
422 gst_rtp_buffer_unmap (&rtp);
427 GST_ELEMENT_WARNING (depay, STREAM, DECODE,
428 (NULL), ("QCELP RTP invalid frame received"));
429 gst_rtp_buffer_unmap (&rtp);
435 gst_rtp_qcelp_depay_plugin_init (GstPlugin * plugin)
437 return gst_element_register (plugin, "rtpqcelpdepay",
438 GST_RANK_SECONDARY, GST_TYPE_RTP_QCELP_DEPAY);