1 /* GStreamer
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>
6 *
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.
11 *
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.
16 *
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.
21 */
22
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <gst/rtp/gstrtpbuffer.h>
31 #include <gst/audio/audio.h>
32
33 #include "gstrtpelements.h"
34 #include "gstrtpg726depay.h"
35 #include "gstrtputils.h"
36
37 GST_DEBUG_CATEGORY_STATIC (rtpg726depay_debug);
38 #define GST_CAT_DEFAULT (rtpg726depay_debug)
39
40 #define DEFAULT_BIT_RATE 32000
41 #define DEFAULT_BLOCK_ALIGN 4
42 #define SAMPLE_RATE 8000
43 #define LAYOUT_G726 "g726"
44
45 /* RtpG726Depay signals and args */
46 enum
47 {
48 /* FILL ME */
49 LAST_SIGNAL
50 };
51
52 #define DEFAULT_FORCE_AAL2 TRUE
53
54 enum
55 {
56 PROP_0,
57 PROP_FORCE_AAL2
58 };
59
60 static GstStaticPadTemplate gst_rtp_g726_depay_sink_template =
61 GST_STATIC_PAD_TEMPLATE ("sink",
62 GST_PAD_SINK,
63 GST_PAD_ALWAYS,
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;")
69 );
70
71 static GstStaticPadTemplate gst_rtp_g726_depay_src_template =
72 GST_STATIC_PAD_TEMPLATE ("src",
73 GST_PAD_SRC,
74 GST_PAD_ALWAYS,
75 GST_STATIC_CAPS ("audio/x-adpcm, "
76 "channels = (int) 1, "
77 "rate = (int) 8000, "
78 "bitrate = (int) { 16000, 24000, 32000, 40000 }, "
79 "block_align = (int) { 2, 3, 4, 5 }, " "layout = (string) \"g726\"")
80 );
81
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);
86
87 static GstBuffer *gst_rtp_g726_depay_process (GstRTPBaseDepayload * depayload,
88 GstRTPBuffer * rtp);
89 static gboolean gst_rtp_g726_depay_setcaps (GstRTPBaseDepayload * depayload,
90 GstCaps * caps);
91
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));
97
98 static void
gst_rtp_g726_depay_class_init(GstRtpG726DepayClass * klass)99 gst_rtp_g726_depay_class_init (GstRtpG726DepayClass * klass)
100 {
101 GObjectClass *gobject_class;
102 GstElementClass *gstelement_class;
103 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
104
105 GST_DEBUG_CATEGORY_INIT (rtpg726depay_debug, "rtpg726depay", 0,
106 "G.726 RTP Depayloader");
107
108 gobject_class = (GObjectClass *) klass;
109 gstelement_class = (GstElementClass *) klass;
110 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
111
112 gobject_class->set_property = gst_rtp_g726_depay_set_property;
113 gobject_class->get_property = gst_rtp_g726_depay_get_property;
114
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));
119
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);
124
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>");
129
130 gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_g726_depay_process;
131 gstrtpbasedepayload_class->set_caps = gst_rtp_g726_depay_setcaps;
132 }
133
134 static void
gst_rtp_g726_depay_init(GstRtpG726Depay * rtpG726depay)135 gst_rtp_g726_depay_init (GstRtpG726Depay * rtpG726depay)
136 {
137 GstRTPBaseDepayload *depayload;
138
139 depayload = GST_RTP_BASE_DEPAYLOAD (rtpG726depay);
140
141 gst_pad_use_fixed_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload));
142
143 rtpG726depay->force_aal2 = DEFAULT_FORCE_AAL2;
144 }
145
146 static gboolean
gst_rtp_g726_depay_setcaps(GstRTPBaseDepayload * depayload,GstCaps * caps)147 gst_rtp_g726_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
148 {
149 GstCaps *srccaps;
150 GstStructure *structure;
151 gboolean ret;
152 gint clock_rate;
153 const gchar *encoding_name;
154 GstRtpG726Depay *depay;
155
156 depay = GST_RTP_G726_DEPAY (depayload);
157
158 structure = gst_caps_get_structure (caps, 0);
159
160 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
161 clock_rate = 8000; /* default */
162 depayload->clock_rate = clock_rate;
163
164 depay->aal2 = FALSE;
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;
169 } else {
170 if (g_str_has_prefix (encoding_name, "AAL2-")) {
171 depay->aal2 = TRUE;
172 encoding_name += 5;
173 }
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;
186 } else
187 goto unknown_encoding;
188 }
189
190 GST_DEBUG ("RTP G.726 depayloader, bitrate set to %d\n", depay->bitrate);
191
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);
198
199 ret = gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload), srccaps);
200 gst_caps_unref (srccaps);
201
202 return ret;
203
204 /* ERRORS */
205 unknown_encoding:
206 {
207 GST_WARNING ("Could not determine bitrate from encoding-name (%s)",
208 encoding_name);
209 return FALSE;
210 }
211 }
212
213
214 static GstBuffer *
gst_rtp_g726_depay_process(GstRTPBaseDepayload * depayload,GstRTPBuffer * rtp)215 gst_rtp_g726_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
216 {
217 GstRtpG726Depay *depay;
218 GstBuffer *outbuf = NULL;
219 gboolean marker;
220
221 depay = GST_RTP_G726_DEPAY (depayload);
222
223 marker = gst_rtp_buffer_get_marker (rtp);
224
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));
228
229 if (depay->aal2 || depay->force_aal2) {
230 /* AAL2, we can just copy the bytes */
231 outbuf = gst_rtp_buffer_get_payload_buffer (rtp);
232 if (!outbuf)
233 goto bad_len;
234 gst_rtp_drop_non_audio_meta (depay, outbuf);
235 } else {
236 guint8 *in, *out, tmp;
237 guint len;
238 GstMapInfo map;
239
240 in = gst_rtp_buffer_get_payload (rtp);
241 len = gst_rtp_buffer_get_payload_len (rtp);
242
243 outbuf = gst_rtp_buffer_get_payload_buffer (rtp);
244 if (!outbuf)
245 goto bad_len;
246 outbuf = gst_buffer_make_writable (outbuf);
247
248 gst_rtp_drop_non_audio_meta (depay, outbuf);
249
250 gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
251 out = map.data;
252
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) {
256 case 16000:
257 {
258 /* 0
259 * 0 1 2 3 4 5 6 7
260 * +-+-+-+-+-+-+-+-+-
261 * |D D|C C|B B|A A| ...
262 * |0 1|0 1|0 1|0 1|
263 * +-+-+-+-+-+-+-+-+-
264 */
265 while (len > 0) {
266 tmp = *in++;
267 *out++ = ((tmp & 0xc0) >> 6) |
268 ((tmp & 0x30) >> 2) | ((tmp & 0x0c) << 2) | ((tmp & 0x03) << 6);
269 len--;
270 }
271 break;
272 }
273 case 24000:
274 {
275 /* 0 1 2
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 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
281 */
282 while (len > 2) {
283 tmp = *in++;
284 *out++ = ((tmp & 0xe0) >> 5) |
285 ((tmp & 0x1c) << 1) | ((tmp & 0x03) << 6);
286 tmp = *in++;
287 *out++ = ((tmp & 0x80) >> 7) |
288 ((tmp & 0x70) >> 3) | ((tmp & 0x0e) << 3) | ((tmp & 0x01) << 7);
289 tmp = *in++;
290 *out++ = ((tmp & 0xc0) >> 6) |
291 ((tmp & 0x38) >> 1) | ((tmp & 0x07) << 5);
292 len -= 3;
293 }
294 break;
295 }
296 case 32000:
297 {
298 /* 0 1
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 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
304 */
305 while (len > 0) {
306 tmp = *in++;
307 *out++ = ((tmp & 0xf0) >> 4) | ((tmp & 0x0f) << 4);
308 len--;
309 }
310 break;
311 }
312 case 40000:
313 {
314 /* 0 1 2 3 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 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
320 */
321 while (len > 4) {
322 tmp = *in++;
323 *out++ = ((tmp & 0xf8) >> 3) | ((tmp & 0x07) << 5);
324 tmp = *in++;
325 *out++ = ((tmp & 0xc0) >> 6) |
326 ((tmp & 0x3e) << 1) | ((tmp & 0x01) << 7);
327 tmp = *in++;
328 *out++ = ((tmp & 0xf0) >> 4) | ((tmp & 0x0f) << 4);
329 tmp = *in++;
330 *out++ = ((tmp & 0x80) >> 7) |
331 ((tmp & 0x7c) >> 1) | ((tmp & 0x03) << 6);
332 tmp = *in++;
333 *out++ = ((tmp & 0xe0) >> 5) | ((tmp & 0x1f) << 3);
334 len -= 5;
335 }
336 break;
337 }
338 }
339 gst_buffer_unmap (outbuf, &map);
340 }
341
342 if (marker) {
343 /* mark start of talkspurt with RESYNC */
344 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_RESYNC);
345 }
346
347 return outbuf;
348
349 bad_len:
350 {
351 return NULL;
352 }
353 }
354
355 static void
gst_rtp_g726_depay_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)356 gst_rtp_g726_depay_set_property (GObject * object, guint prop_id,
357 const GValue * value, GParamSpec * pspec)
358 {
359 GstRtpG726Depay *rtpg726depay;
360
361 rtpg726depay = GST_RTP_G726_DEPAY (object);
362
363 switch (prop_id) {
364 case PROP_FORCE_AAL2:
365 rtpg726depay->force_aal2 = g_value_get_boolean (value);
366 break;
367 default:
368 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
369 break;
370 }
371 }
372
373 static void
gst_rtp_g726_depay_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)374 gst_rtp_g726_depay_get_property (GObject * object, guint prop_id,
375 GValue * value, GParamSpec * pspec)
376 {
377 GstRtpG726Depay *rtpg726depay;
378
379 rtpg726depay = GST_RTP_G726_DEPAY (object);
380
381 switch (prop_id) {
382 case PROP_FORCE_AAL2:
383 g_value_set_boolean (value, rtpg726depay->force_aal2);
384 break;
385 default:
386 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
387 break;
388 }
389 }
390