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 "gstrtpelements.h"
34 #include "gstrtpg726depay.h"
35 #include "gstrtputils.h"
37 GST_DEBUG_CATEGORY_STATIC (rtpg726depay_debug);
38 #define GST_CAT_DEFAULT (rtpg726depay_debug)
40 #define DEFAULT_BIT_RATE 32000
41 #define DEFAULT_BLOCK_ALIGN 4
42 #define SAMPLE_RATE 8000
43 #define LAYOUT_G726 "g726"
45 /* RtpG726Depay signals and args */
52 #define DEFAULT_FORCE_AAL2 TRUE
60 static GstStaticPadTemplate gst_rtp_g726_depay_sink_template =
61 GST_STATIC_PAD_TEMPLATE ("sink",
64 GST_STATIC_CAPS ("application/x-rtp, "
65 "media = (string) \"audio\", "
66 "encoding-name = (string) { \"G726\", \"G726-16\", \"G726-24\", \"G726-32\", \"G726-40\", "
67 "\"AAL2-G726-16\", \"AAL2-G726-24\", \"AAL2-G726-32\", \"AAL2-G726-40\" }, "
68 "clock-rate = (int) 8000;")
71 static GstStaticPadTemplate gst_rtp_g726_depay_src_template =
72 GST_STATIC_PAD_TEMPLATE ("src",
75 GST_STATIC_CAPS ("audio/x-adpcm, "
76 "channels = (int) 1, "
78 "bitrate = (int) { 16000, 24000, 32000, 40000 }, "
79 "block_align = (int) { 2, 3, 4, 5 }, " "layout = (string) \"g726\"")
82 static void gst_rtp_g726_depay_get_property (GObject * object, guint prop_id,
83 GValue * value, GParamSpec * pspec);
84 static void gst_rtp_g726_depay_set_property (GObject * object, guint prop_id,
85 const GValue * value, GParamSpec * pspec);
87 static GstBuffer *gst_rtp_g726_depay_process (GstRTPBaseDepayload * depayload,
89 static gboolean gst_rtp_g726_depay_setcaps (GstRTPBaseDepayload * depayload,
92 #define gst_rtp_g726_depay_parent_class parent_class
93 G_DEFINE_TYPE (GstRtpG726Depay, gst_rtp_g726_depay,
94 GST_TYPE_RTP_BASE_DEPAYLOAD);
95 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtpg726depay, "rtpg726depay",
96 GST_RANK_SECONDARY, GST_TYPE_RTP_G726_DEPAY, rtp_element_init (plugin));
99 gst_rtp_g726_depay_class_init (GstRtpG726DepayClass * klass)
101 GObjectClass *gobject_class;
102 GstElementClass *gstelement_class;
103 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
105 GST_DEBUG_CATEGORY_INIT (rtpg726depay_debug, "rtpg726depay", 0,
106 "G.726 RTP Depayloader");
108 gobject_class = (GObjectClass *) klass;
109 gstelement_class = (GstElementClass *) klass;
110 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
112 gobject_class->set_property = gst_rtp_g726_depay_set_property;
113 gobject_class->get_property = gst_rtp_g726_depay_get_property;
115 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_FORCE_AAL2,
116 g_param_spec_boolean ("force-aal2", "Force AAL2",
117 "Force AAL2 decoding for compatibility with bad payloaders",
118 DEFAULT_FORCE_AAL2, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
120 gst_element_class_add_static_pad_template (gstelement_class,
121 &gst_rtp_g726_depay_src_template);
122 gst_element_class_add_static_pad_template (gstelement_class,
123 &gst_rtp_g726_depay_sink_template);
125 gst_element_class_set_static_metadata (gstelement_class,
126 "RTP G.726 depayloader", "Codec/Depayloader/Network/RTP",
127 "Extracts G.726 audio from RTP packets",
128 "Axis Communications <dev-gstreamer@axis.com>");
130 gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_g726_depay_process;
131 gstrtpbasedepayload_class->set_caps = gst_rtp_g726_depay_setcaps;
135 gst_rtp_g726_depay_init (GstRtpG726Depay * rtpG726depay)
137 GstRTPBaseDepayload *depayload;
139 depayload = GST_RTP_BASE_DEPAYLOAD (rtpG726depay);
141 gst_pad_use_fixed_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload));
143 rtpG726depay->force_aal2 = DEFAULT_FORCE_AAL2;
147 gst_rtp_g726_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
150 GstStructure *structure;
153 const gchar *encoding_name;
154 GstRtpG726Depay *depay;
156 depay = GST_RTP_G726_DEPAY (depayload);
158 structure = gst_caps_get_structure (caps, 0);
160 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
161 clock_rate = 8000; /* default */
162 depayload->clock_rate = clock_rate;
165 encoding_name = gst_structure_get_string (structure, "encoding-name");
166 if (encoding_name == NULL || g_ascii_strcasecmp (encoding_name, "G726") == 0) {
167 depay->bitrate = DEFAULT_BIT_RATE;
168 depay->block_align = DEFAULT_BLOCK_ALIGN;
170 if (g_str_has_prefix (encoding_name, "AAL2-")) {
174 if (g_ascii_strcasecmp (encoding_name, "G726-16") == 0) {
175 depay->bitrate = 16000;
176 depay->block_align = 2;
177 } else if (g_ascii_strcasecmp (encoding_name, "G726-24") == 0) {
178 depay->bitrate = 24000;
179 depay->block_align = 3;
180 } else if (g_ascii_strcasecmp (encoding_name, "G726-32") == 0) {
181 depay->bitrate = 32000;
182 depay->block_align = 4;
183 } else if (g_ascii_strcasecmp (encoding_name, "G726-40") == 0) {
184 depay->bitrate = 40000;
185 depay->block_align = 5;
187 goto unknown_encoding;
190 GST_DEBUG ("RTP G.726 depayloader, bitrate set to %d\n", depay->bitrate);
192 srccaps = gst_caps_new_simple ("audio/x-adpcm",
193 "channels", G_TYPE_INT, 1,
194 "rate", G_TYPE_INT, clock_rate,
195 "bitrate", G_TYPE_INT, depay->bitrate,
196 "block_align", G_TYPE_INT, depay->block_align,
197 "layout", G_TYPE_STRING, LAYOUT_G726, NULL);
199 ret = gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload), srccaps);
200 gst_caps_unref (srccaps);
207 GST_WARNING ("Could not determine bitrate from encoding-name (%s)",
215 gst_rtp_g726_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
217 GstRtpG726Depay *depay;
218 GstBuffer *outbuf = NULL;
221 depay = GST_RTP_G726_DEPAY (depayload);
223 marker = gst_rtp_buffer_get_marker (rtp);
225 GST_DEBUG ("process : got %" G_GSIZE_FORMAT " bytes, mark %d ts %u seqn %d",
226 gst_buffer_get_size (rtp->buffer), marker,
227 gst_rtp_buffer_get_timestamp (rtp), gst_rtp_buffer_get_seq (rtp));
229 if (depay->aal2 || depay->force_aal2) {
230 /* AAL2, we can just copy the bytes */
231 outbuf = gst_rtp_buffer_get_payload_buffer (rtp);
234 gst_rtp_drop_non_audio_meta (depay, outbuf);
236 guint8 *in, *out, tmp;
240 in = gst_rtp_buffer_get_payload (rtp);
241 len = gst_rtp_buffer_get_payload_len (rtp);
243 outbuf = gst_rtp_buffer_get_payload_buffer (rtp);
246 outbuf = gst_buffer_make_writable (outbuf);
248 gst_rtp_drop_non_audio_meta (depay, outbuf);
250 gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
253 /* we need to reshuffle the bytes, input is always of the form
254 * A B C D ... with the number of bits depending on the bitrate. */
255 switch (depay->bitrate) {
261 * |D D|C C|B B|A A| ...
267 *out++ = ((tmp & 0xc0) >> 6) |
268 ((tmp & 0x30) >> 2) | ((tmp & 0x0c) << 2) | ((tmp & 0x03) << 6);
276 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3
277 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
278 * |C C|B B B|A A A|F|E E E|D D D|C|H H H|G G G|F F| ...
279 * |1 2|0 1 2|0 1 2|2|0 1 2|0 1 2|0|0 1 2|0 1 2|0 1|
280 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
284 *out++ = ((tmp & 0xe0) >> 5) |
285 ((tmp & 0x1c) << 1) | ((tmp & 0x03) << 6);
287 *out++ = ((tmp & 0x80) >> 7) |
288 ((tmp & 0x70) >> 3) | ((tmp & 0x0e) << 3) | ((tmp & 0x01) << 7);
290 *out++ = ((tmp & 0xc0) >> 6) |
291 ((tmp & 0x38) >> 1) | ((tmp & 0x07) << 5);
299 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
300 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
301 * |B B B B|A A A A|D D D D|C C C C| ...
302 * |0 1 2 3|0 1 2 3|0 1 2 3|0 1 2 3|
303 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
307 *out++ = ((tmp & 0xf0) >> 4) | ((tmp & 0x0f) << 4);
315 * 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
316 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
317 * |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|
318 * |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|
319 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
323 *out++ = ((tmp & 0xf8) >> 3) | ((tmp & 0x07) << 5);
325 *out++ = ((tmp & 0xc0) >> 6) |
326 ((tmp & 0x3e) << 1) | ((tmp & 0x01) << 7);
328 *out++ = ((tmp & 0xf0) >> 4) | ((tmp & 0x0f) << 4);
330 *out++ = ((tmp & 0x80) >> 7) |
331 ((tmp & 0x7c) >> 1) | ((tmp & 0x03) << 6);
333 *out++ = ((tmp & 0xe0) >> 5) | ((tmp & 0x1f) << 3);
339 gst_buffer_unmap (outbuf, &map);
343 /* mark start of talkspurt with RESYNC */
344 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_RESYNC);
356 gst_rtp_g726_depay_set_property (GObject * object, guint prop_id,
357 const GValue * value, GParamSpec * pspec)
359 GstRtpG726Depay *rtpg726depay;
361 rtpg726depay = GST_RTP_G726_DEPAY (object);
364 case PROP_FORCE_AAL2:
365 rtpg726depay->force_aal2 = g_value_get_boolean (value);
368 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
374 gst_rtp_g726_depay_get_property (GObject * object, guint prop_id,
375 GValue * value, GParamSpec * pspec)
377 GstRtpG726Depay *rtpg726depay;
379 rtpg726depay = GST_RTP_G726_DEPAY (object);
382 case PROP_FORCE_AAL2:
383 g_value_set_boolean (value, rtpg726depay->force_aal2);
386 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);