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