2 * Copyright (C) 1999 Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) 2005 Edgard Lima <edgard.lima@gmail.com>
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., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
30 #include <gst/rtp/gstrtpbuffer.h>
31 #include <gst/audio/audio.h>
33 #include "gstrtpg726depay.h"
34 #include "gstrtputils.h"
36 GST_DEBUG_CATEGORY_STATIC (rtpg726depay_debug);
37 #define GST_CAT_DEFAULT (rtpg726depay_debug)
39 #define DEFAULT_BIT_RATE 32000
40 #define DEFAULT_BLOCK_ALIGN 4
41 #define SAMPLE_RATE 8000
42 #define LAYOUT_G726 "g726"
44 /* RtpG726Depay signals and args */
51 #define DEFAULT_FORCE_AAL2 TRUE
59 static GstStaticPadTemplate gst_rtp_g726_depay_sink_template =
60 GST_STATIC_PAD_TEMPLATE ("sink",
63 GST_STATIC_CAPS ("application/x-rtp, "
64 "media = (string) \"audio\", "
65 "encoding-name = (string) { \"G726\", \"G726-16\", \"G726-24\", \"G726-32\", \"G726-40\", "
66 "\"AAL2-G726-16\", \"AAL2-G726-24\", \"AAL2-G726-32\", \"AAL2-G726-40\" }, "
67 "clock-rate = (int) 8000;")
70 static GstStaticPadTemplate gst_rtp_g726_depay_src_template =
71 GST_STATIC_PAD_TEMPLATE ("src",
74 GST_STATIC_CAPS ("audio/x-adpcm, "
75 "channels = (int) 1, "
77 "bitrate = (int) { 16000, 24000, 32000, 40000 }, "
78 "block_align = (int) { 2, 3, 4, 5 }, " "layout = (string) \"g726\"")
81 static void gst_rtp_g726_depay_get_property (GObject * object, guint prop_id,
82 GValue * value, GParamSpec * pspec);
83 static void gst_rtp_g726_depay_set_property (GObject * object, guint prop_id,
84 const GValue * value, GParamSpec * pspec);
86 static GstBuffer *gst_rtp_g726_depay_process (GstRTPBaseDepayload * depayload,
88 static gboolean gst_rtp_g726_depay_setcaps (GstRTPBaseDepayload * depayload,
91 #define gst_rtp_g726_depay_parent_class parent_class
92 G_DEFINE_TYPE (GstRtpG726Depay, gst_rtp_g726_depay,
93 GST_TYPE_RTP_BASE_DEPAYLOAD);
96 gst_rtp_g726_depay_class_init (GstRtpG726DepayClass * klass)
98 GObjectClass *gobject_class;
99 GstElementClass *gstelement_class;
100 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
102 GST_DEBUG_CATEGORY_INIT (rtpg726depay_debug, "rtpg726depay", 0,
103 "G.726 RTP Depayloader");
105 gobject_class = (GObjectClass *) klass;
106 gstelement_class = (GstElementClass *) klass;
107 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
109 gobject_class->set_property = gst_rtp_g726_depay_set_property;
110 gobject_class->get_property = gst_rtp_g726_depay_get_property;
112 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_FORCE_AAL2,
113 g_param_spec_boolean ("force-aal2", "Force AAL2",
114 "Force AAL2 decoding for compatibility with bad payloaders",
115 DEFAULT_FORCE_AAL2, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
117 gst_element_class_add_static_pad_template (gstelement_class,
118 &gst_rtp_g726_depay_src_template);
119 gst_element_class_add_static_pad_template (gstelement_class,
120 &gst_rtp_g726_depay_sink_template);
122 gst_element_class_set_static_metadata (gstelement_class,
123 "RTP G.726 depayloader", "Codec/Depayloader/Network/RTP",
124 "Extracts G.726 audio from RTP packets",
125 "Axis Communications <dev-gstreamer@axis.com>");
127 gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_g726_depay_process;
128 gstrtpbasedepayload_class->set_caps = gst_rtp_g726_depay_setcaps;
132 gst_rtp_g726_depay_init (GstRtpG726Depay * rtpG726depay)
134 GstRTPBaseDepayload *depayload;
136 depayload = GST_RTP_BASE_DEPAYLOAD (rtpG726depay);
138 gst_pad_use_fixed_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload));
140 rtpG726depay->force_aal2 = DEFAULT_FORCE_AAL2;
144 gst_rtp_g726_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
147 GstStructure *structure;
150 const gchar *encoding_name;
151 GstRtpG726Depay *depay;
153 depay = GST_RTP_G726_DEPAY (depayload);
155 structure = gst_caps_get_structure (caps, 0);
157 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
158 clock_rate = 8000; /* default */
159 depayload->clock_rate = clock_rate;
162 encoding_name = gst_structure_get_string (structure, "encoding-name");
163 if (encoding_name == NULL || g_ascii_strcasecmp (encoding_name, "G726") == 0) {
164 depay->bitrate = DEFAULT_BIT_RATE;
165 depay->block_align = DEFAULT_BLOCK_ALIGN;
167 if (g_str_has_prefix (encoding_name, "AAL2-")) {
171 if (g_ascii_strcasecmp (encoding_name, "G726-16") == 0) {
172 depay->bitrate = 16000;
173 depay->block_align = 2;
174 } else if (g_ascii_strcasecmp (encoding_name, "G726-24") == 0) {
175 depay->bitrate = 24000;
176 depay->block_align = 3;
177 } else if (g_ascii_strcasecmp (encoding_name, "G726-32") == 0) {
178 depay->bitrate = 32000;
179 depay->block_align = 4;
180 } else if (g_ascii_strcasecmp (encoding_name, "G726-40") == 0) {
181 depay->bitrate = 40000;
182 depay->block_align = 5;
184 goto unknown_encoding;
187 GST_DEBUG ("RTP G.726 depayloader, bitrate set to %d\n", depay->bitrate);
189 srccaps = gst_caps_new_simple ("audio/x-adpcm",
190 "channels", G_TYPE_INT, 1,
191 "rate", G_TYPE_INT, clock_rate,
192 "bitrate", G_TYPE_INT, depay->bitrate,
193 "block_align", G_TYPE_INT, depay->block_align,
194 "layout", G_TYPE_STRING, LAYOUT_G726, NULL);
196 ret = gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload), srccaps);
197 gst_caps_unref (srccaps);
204 GST_WARNING ("Could not determine bitrate from encoding-name (%s)",
212 gst_rtp_g726_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
214 GstRtpG726Depay *depay;
215 GstBuffer *outbuf = NULL;
218 depay = GST_RTP_G726_DEPAY (depayload);
220 marker = gst_rtp_buffer_get_marker (rtp);
222 GST_DEBUG ("process : got %" G_GSIZE_FORMAT " bytes, mark %d ts %u seqn %d",
223 gst_buffer_get_size (rtp->buffer), marker,
224 gst_rtp_buffer_get_timestamp (rtp), gst_rtp_buffer_get_seq (rtp));
226 if (depay->aal2 || depay->force_aal2) {
227 /* AAL2, we can just copy the bytes */
228 outbuf = gst_rtp_buffer_get_payload_buffer (rtp);
231 gst_rtp_drop_non_audio_meta (depay, outbuf);
233 guint8 *in, *out, tmp;
237 in = gst_rtp_buffer_get_payload (rtp);
238 len = gst_rtp_buffer_get_payload_len (rtp);
240 outbuf = gst_rtp_buffer_get_payload_buffer (rtp);
243 outbuf = gst_buffer_make_writable (outbuf);
245 gst_rtp_drop_non_audio_meta (depay, outbuf);
247 gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
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);
336 gst_buffer_unmap (outbuf, &map);
340 /* mark start of talkspurt with RESYNC */
341 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_RESYNC);
353 gst_rtp_g726_depay_set_property (GObject * object, guint prop_id,
354 const GValue * value, GParamSpec * pspec)
356 GstRtpG726Depay *rtpg726depay;
358 rtpg726depay = GST_RTP_G726_DEPAY (object);
361 case PROP_FORCE_AAL2:
362 rtpg726depay->force_aal2 = g_value_get_boolean (value);
365 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
371 gst_rtp_g726_depay_get_property (GObject * object, guint prop_id,
372 GValue * value, GParamSpec * pspec)
374 GstRtpG726Depay *rtpg726depay;
376 rtpg726depay = GST_RTP_G726_DEPAY (object);
379 case PROP_FORCE_AAL2:
380 g_value_set_boolean (value, rtpg726depay->force_aal2);
383 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
389 gst_rtp_g726_depay_plugin_init (GstPlugin * plugin)
391 return gst_element_register (plugin, "rtpg726depay",
392 GST_RANK_SECONDARY, GST_TYPE_RTP_G726_DEPAY);