2 * Copyright (C) 1999 Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) 2005 Edgard Lima <edgard.lima@indt.org.br>
4 * Copyright (C) 2005 Zeeshan Ali <zeenix@gmail.com>
5 * Copyright (C) 2008 Axis Communications <dev-gstreamer@axis.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
30 #include <gst/rtp/gstrtpbuffer.h>
32 #include "gstrtpg726depay.h"
34 GST_DEBUG_CATEGORY_STATIC (rtpg726depay_debug);
35 #define GST_CAT_DEFAULT (rtpg726depay_debug)
37 #define DEFAULT_BIT_RATE 32000
38 #define SAMPLE_RATE 8000
39 #define LAYOUT_G726 "g726"
41 /* RtpG726Depay signals and args */
48 #define DEFAULT_FORCE_AAL2 TRUE
57 static GstStaticPadTemplate gst_rtp_g726_depay_sink_template =
58 GST_STATIC_PAD_TEMPLATE ("sink",
61 GST_STATIC_CAPS ("application/x-rtp, "
62 "media = (string) \"audio\", "
63 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
64 "encoding-name = (string) { \"G726\", \"G726-16\", \"G726-24\", \"G726-32\", \"G726-40\", "
65 "\"AAL2-G726-16\", \"AAL2-G726-24\", \"AAL2-G726-32\", \"AAL2-G726-40\" }, "
66 "clock-rate = (int) 8000;")
69 static GstStaticPadTemplate gst_rtp_g726_depay_src_template =
70 GST_STATIC_PAD_TEMPLATE ("src",
73 GST_STATIC_CAPS ("audio/x-adpcm, "
74 "channels = (int) 1, "
76 "bitrate = (int) { 16000, 24000, 32000, 40000 }, "
77 "layout = (string) \"g726\"")
80 static void gst_rtp_g726_depay_get_property (GObject * object, guint prop_id,
81 GValue * value, GParamSpec * pspec);
82 static void gst_rtp_g726_depay_set_property (GObject * object, guint prop_id,
83 const GValue * value, GParamSpec * pspec);
85 static GstBuffer *gst_rtp_g726_depay_process (GstBaseRTPDepayload * depayload,
87 static gboolean gst_rtp_g726_depay_setcaps (GstBaseRTPDepayload * depayload,
90 GST_BOILERPLATE (GstRtpG726Depay, gst_rtp_g726_depay, GstBaseRTPDepayload,
91 GST_TYPE_BASE_RTP_DEPAYLOAD);
94 gst_rtp_g726_depay_base_init (gpointer klass)
96 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
98 gst_element_class_add_static_pad_template (element_class,
99 &gst_rtp_g726_depay_src_template);
100 gst_element_class_add_static_pad_template (element_class,
101 &gst_rtp_g726_depay_sink_template);
102 gst_element_class_set_details_simple (element_class, "RTP G.726 depayloader",
103 "Codec/Depayloader/Network/RTP",
104 "Extracts G.726 audio from RTP packets",
105 "Axis Communications <dev-gstreamer@axis.com>");
109 gst_rtp_g726_depay_class_init (GstRtpG726DepayClass * klass)
111 GObjectClass *gobject_class;
112 GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
114 gobject_class = (GObjectClass *) klass;
115 gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
117 gobject_class->set_property = gst_rtp_g726_depay_set_property;
118 gobject_class->get_property = gst_rtp_g726_depay_get_property;
120 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_FORCE_AAL2,
121 g_param_spec_boolean ("force-aal2", "Force AAL2",
122 "Force AAL2 decoding for compatibility with bad payloaders",
123 DEFAULT_FORCE_AAL2, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
125 gstbasertpdepayload_class->process = gst_rtp_g726_depay_process;
126 gstbasertpdepayload_class->set_caps = gst_rtp_g726_depay_setcaps;
128 GST_DEBUG_CATEGORY_INIT (rtpg726depay_debug, "rtpg726depay", 0,
129 "G.726 RTP Depayloader");
133 gst_rtp_g726_depay_init (GstRtpG726Depay * rtpG726depay,
134 GstRtpG726DepayClass * klass)
136 GstBaseRTPDepayload *depayload;
138 depayload = GST_BASE_RTP_DEPAYLOAD (rtpG726depay);
140 gst_pad_use_fixed_caps (GST_BASE_RTP_DEPAYLOAD_SRCPAD (depayload));
142 rtpG726depay->force_aal2 = DEFAULT_FORCE_AAL2;
146 gst_rtp_g726_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
149 GstStructure *structure;
152 const gchar *encoding_name;
153 GstRtpG726Depay *depay;
155 depay = GST_RTP_G726_DEPAY (depayload);
157 structure = gst_caps_get_structure (caps, 0);
159 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
160 clock_rate = 8000; /* default */
161 depayload->clock_rate = clock_rate;
164 encoding_name = gst_structure_get_string (structure, "encoding-name");
165 if (encoding_name == NULL || g_ascii_strcasecmp (encoding_name, "G726") == 0) {
166 depay->bitrate = DEFAULT_BIT_RATE;
168 if (g_str_has_prefix (encoding_name, "AAL2-")) {
172 if (g_ascii_strcasecmp (encoding_name, "G726-16") == 0) {
173 depay->bitrate = 16000;
174 } else if (g_ascii_strcasecmp (encoding_name, "G726-24") == 0) {
175 depay->bitrate = 24000;
176 } else if (g_ascii_strcasecmp (encoding_name, "G726-32") == 0) {
177 depay->bitrate = 32000;
178 } else if (g_ascii_strcasecmp (encoding_name, "G726-40") == 0) {
179 depay->bitrate = 40000;
181 goto unknown_encoding;
184 GST_DEBUG ("RTP G.726 depayloader, bitrate set to %d\n", depay->bitrate);
186 srccaps = gst_caps_new_simple ("audio/x-adpcm",
187 "channels", G_TYPE_INT, 1,
188 "rate", G_TYPE_INT, clock_rate,
189 "bitrate", G_TYPE_INT, depay->bitrate,
190 "layout", G_TYPE_STRING, LAYOUT_G726, NULL);
192 ret = gst_pad_set_caps (GST_BASE_RTP_DEPAYLOAD_SRCPAD (depayload), srccaps);
193 gst_caps_unref (srccaps);
200 GST_WARNING ("Could not determine bitrate from encoding-name (%s)",
208 gst_rtp_g726_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
210 GstRtpG726Depay *depay;
211 GstBuffer *outbuf = NULL;
214 depay = GST_RTP_G726_DEPAY (depayload);
216 marker = gst_rtp_buffer_get_marker (buf);
218 GST_DEBUG ("process : got %d bytes, mark %d ts %u seqn %d",
219 GST_BUFFER_SIZE (buf), marker,
220 gst_rtp_buffer_get_timestamp (buf), gst_rtp_buffer_get_seq (buf));
222 if (depay->aal2 || depay->force_aal2) {
223 /* AAL2, we can just copy the bytes */
224 outbuf = gst_rtp_buffer_get_payload_buffer (buf);
228 guint8 *in, *out, tmp;
231 in = gst_rtp_buffer_get_payload (buf);
232 len = gst_rtp_buffer_get_payload_len (buf);
234 if (gst_buffer_is_writable (buf)) {
235 outbuf = gst_rtp_buffer_get_payload_buffer (buf);
240 copy = gst_buffer_copy (buf);
241 outbuf = gst_rtp_buffer_get_payload_buffer (copy);
242 gst_buffer_unref (copy);
248 out = GST_BUFFER_DATA (outbuf);
250 /* we need to reshuffle the bytes, input is always of the form
251 * A B C D ... with the number of bits depending on the bitrate. */
252 switch (depay->bitrate) {
258 * |D D|C C|B B|A A| ...
264 *out++ = ((tmp & 0xc0) >> 6) |
265 ((tmp & 0x30) >> 2) | ((tmp & 0x0c) << 2) | ((tmp & 0x03) << 6);
273 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3
274 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
275 * |C C|B B B|A A A|F|E E E|D D D|C|H H H|G G G|F F| ...
276 * |1 2|0 1 2|0 1 2|2|0 1 2|0 1 2|0|0 1 2|0 1 2|0 1|
277 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
281 *out++ = ((tmp & 0xe0) >> 5) |
282 ((tmp & 0x1c) << 1) | ((tmp & 0x03) << 6);
284 *out++ = ((tmp & 0x80) >> 7) |
285 ((tmp & 0x70) >> 3) | ((tmp & 0x0e) << 4) | ((tmp & 0x01) << 7);
287 *out++ = ((tmp & 0xc0) >> 6) |
288 ((tmp & 0x38) >> 1) | ((tmp & 0x07) << 5);
296 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
297 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
298 * |B B B B|A A A A|D D D D|C C C C| ...
299 * |0 1 2 3|0 1 2 3|0 1 2 3|0 1 2 3|
300 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
304 *out++ = ((tmp & 0xf0) >> 4) | ((tmp & 0x0f) << 4);
312 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
313 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
314 * |B B B|A A A A A|D|C C C C C|B B|E E E E|D D D D|G G|F F F F F|E|H H H H H|G G G|
315 * |2 3 4|0 1 2 3 4|4|0 1 2 3 4|0 1|1 2 3 4|0 1 2 3|3 4|0 1 2 3 4|0|0 1 2 3 4|0 1 2|
316 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
320 *out++ = ((tmp & 0xf8) >> 3) | ((tmp & 0x07) << 5);
322 *out++ = ((tmp & 0xc0) >> 6) |
323 ((tmp & 0x3e) << 1) | ((tmp & 0x01) << 7);
325 *out++ = ((tmp & 0xf0) >> 4) | ((tmp & 0x0f) << 4);
327 *out++ = ((tmp & 0x80) >> 7) |
328 ((tmp & 0x7c) >> 1) | ((tmp & 0x03) << 6);
330 *out++ = ((tmp & 0xe0) >> 5) | ((tmp & 0x1f) << 3);
339 /* mark start of talkspurt with discont */
340 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
350 gst_rtp_g726_depay_set_property (GObject * object, guint prop_id,
351 const GValue * value, GParamSpec * pspec)
353 GstRtpG726Depay *rtpg726depay;
355 rtpg726depay = GST_RTP_G726_DEPAY (object);
358 case PROP_FORCE_AAL2:
359 rtpg726depay->force_aal2 = g_value_get_boolean (value);
362 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
368 gst_rtp_g726_depay_get_property (GObject * object, guint prop_id,
369 GValue * value, GParamSpec * pspec)
371 GstRtpG726Depay *rtpg726depay;
373 rtpg726depay = GST_RTP_G726_DEPAY (object);
376 case PROP_FORCE_AAL2:
377 g_value_set_boolean (value, rtpg726depay->force_aal2);
380 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
386 gst_rtp_g726_depay_plugin_init (GstPlugin * plugin)
388 return gst_element_register (plugin, "rtpg726depay",
389 GST_RANK_SECONDARY, GST_TYPE_RTP_G726_DEPAY);