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 Nokia Corporation <kai.vehmanen@nokia.com>
5 * Copyright (C) 2007,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 <stdlib.h>
28 #include <string.h>
29 #include <gst/rtp/gstrtpbuffer.h>
30
31 #include "gstrtpelements.h"
32 #include "gstrtpg726pay.h"
33
34 GST_DEBUG_CATEGORY_STATIC (rtpg726pay_debug);
35 #define GST_CAT_DEFAULT (rtpg726pay_debug)
36
37 #define DEFAULT_FORCE_AAL2 TRUE
38
39 enum
40 {
41 PROP_0,
42 PROP_FORCE_AAL2
43 };
44
45 static GstStaticPadTemplate gst_rtp_g726_pay_sink_template =
46 GST_STATIC_PAD_TEMPLATE ("sink",
47 GST_PAD_SINK,
48 GST_PAD_ALWAYS,
49 GST_STATIC_CAPS ("audio/x-adpcm, "
50 "channels = (int) 1, "
51 "rate = (int) 8000, "
52 "bitrate = (int) { 16000, 24000, 32000, 40000 }, "
53 "layout = (string) \"g726\"")
54 );
55
56 static GstStaticPadTemplate gst_rtp_g726_pay_src_template =
57 GST_STATIC_PAD_TEMPLATE ("src",
58 GST_PAD_SRC,
59 GST_PAD_ALWAYS,
60 GST_STATIC_CAPS ("application/x-rtp, "
61 "media = (string) \"audio\", "
62 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
63 "clock-rate = (int) 8000, "
64 "encoding-name = (string) { \"G726-16\", \"G726-24\", \"G726-32\", \"G726-40\", "
65 " \"AAL2-G726-16\", \"AAL2-G726-24\", \"AAL2-G726-32\", \"AAL2-G726-40\" } ")
66 );
67
68 static void gst_rtp_g726_pay_get_property (GObject * object, guint prop_id,
69 GValue * value, GParamSpec * pspec);
70 static void gst_rtp_g726_pay_set_property (GObject * object, guint prop_id,
71 const GValue * value, GParamSpec * pspec);
72
73 static gboolean gst_rtp_g726_pay_setcaps (GstRTPBasePayload * payload,
74 GstCaps * caps);
75 static GstFlowReturn gst_rtp_g726_pay_handle_buffer (GstRTPBasePayload *
76 payload, GstBuffer * buffer);
77
78 #define gst_rtp_g726_pay_parent_class parent_class
79 G_DEFINE_TYPE (GstRtpG726Pay, gst_rtp_g726_pay,
80 GST_TYPE_RTP_BASE_AUDIO_PAYLOAD);
81 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtpg726pay, "rtpg726pay",
82 GST_RANK_SECONDARY, GST_TYPE_RTP_G726_PAY, rtp_element_init (plugin));
83
84 static void
gst_rtp_g726_pay_class_init(GstRtpG726PayClass * klass)85 gst_rtp_g726_pay_class_init (GstRtpG726PayClass * klass)
86 {
87 GObjectClass *gobject_class;
88 GstElementClass *gstelement_class;
89 GstRTPBasePayloadClass *gstrtpbasepayload_class;
90
91 gobject_class = (GObjectClass *) klass;
92 gstelement_class = (GstElementClass *) klass;
93 gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
94
95 gobject_class->set_property = gst_rtp_g726_pay_set_property;
96 gobject_class->get_property = gst_rtp_g726_pay_get_property;
97
98 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_FORCE_AAL2,
99 g_param_spec_boolean ("force-aal2", "Force AAL2",
100 "Force AAL2 encoding for compatibility with bad depayloaders",
101 DEFAULT_FORCE_AAL2, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
102
103 gst_element_class_add_static_pad_template (gstelement_class,
104 &gst_rtp_g726_pay_sink_template);
105 gst_element_class_add_static_pad_template (gstelement_class,
106 &gst_rtp_g726_pay_src_template);
107
108 gst_element_class_set_static_metadata (gstelement_class,
109 "RTP G.726 payloader", "Codec/Payloader/Network/RTP",
110 "Payload-encodes G.726 audio into a RTP packet",
111 "Axis Communications <dev-gstreamer@axis.com>");
112
113 gstrtpbasepayload_class->set_caps = gst_rtp_g726_pay_setcaps;
114 gstrtpbasepayload_class->handle_buffer = gst_rtp_g726_pay_handle_buffer;
115
116 GST_DEBUG_CATEGORY_INIT (rtpg726pay_debug, "rtpg726pay", 0,
117 "G.726 RTP Payloader");
118 }
119
120 static void
gst_rtp_g726_pay_init(GstRtpG726Pay * rtpg726pay)121 gst_rtp_g726_pay_init (GstRtpG726Pay * rtpg726pay)
122 {
123 GstRTPBaseAudioPayload *rtpbaseaudiopayload;
124
125 rtpbaseaudiopayload = GST_RTP_BASE_AUDIO_PAYLOAD (rtpg726pay);
126
127 GST_RTP_BASE_PAYLOAD (rtpg726pay)->clock_rate = 8000;
128
129 rtpg726pay->force_aal2 = DEFAULT_FORCE_AAL2;
130
131 /* sample based codec */
132 gst_rtp_base_audio_payload_set_sample_based (rtpbaseaudiopayload);
133 }
134
135 static gboolean
gst_rtp_g726_pay_setcaps(GstRTPBasePayload * payload,GstCaps * caps)136 gst_rtp_g726_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
137 {
138 gchar *encoding_name;
139 GstStructure *structure;
140 GstRTPBaseAudioPayload *rtpbaseaudiopayload;
141 GstRtpG726Pay *pay;
142 GstCaps *peercaps;
143 gboolean res;
144
145 rtpbaseaudiopayload = GST_RTP_BASE_AUDIO_PAYLOAD (payload);
146 pay = GST_RTP_G726_PAY (payload);
147
148 structure = gst_caps_get_structure (caps, 0);
149
150 if (!gst_structure_get_int (structure, "bitrate", &pay->bitrate))
151 pay->bitrate = 32000;
152
153 GST_DEBUG_OBJECT (payload, "using bitrate %d", pay->bitrate);
154
155 pay->aal2 = FALSE;
156
157 /* first see what we can do with the bitrate */
158 switch (pay->bitrate) {
159 case 16000:
160 encoding_name = g_strdup ("G726-16");
161 gst_rtp_base_audio_payload_set_samplebits_options (rtpbaseaudiopayload,
162 2);
163 break;
164 case 24000:
165 encoding_name = g_strdup ("G726-24");
166 gst_rtp_base_audio_payload_set_samplebits_options (rtpbaseaudiopayload,
167 3);
168 break;
169 case 32000:
170 encoding_name = g_strdup ("G726-32");
171 gst_rtp_base_audio_payload_set_samplebits_options (rtpbaseaudiopayload,
172 4);
173 break;
174 case 40000:
175 encoding_name = g_strdup ("G726-40");
176 gst_rtp_base_audio_payload_set_samplebits_options (rtpbaseaudiopayload,
177 5);
178 break;
179 default:
180 goto invalid_bitrate;
181 }
182
183 GST_DEBUG_OBJECT (payload, "selected base encoding %s", encoding_name);
184
185 /* now see if we need to produce AAL2 or not */
186 peercaps = gst_pad_peer_query_caps (payload->srcpad, NULL);
187 if (peercaps) {
188 GstCaps *filter, *intersect;
189 gchar *capsstr;
190
191 GST_DEBUG_OBJECT (payload, "have peercaps %" GST_PTR_FORMAT, peercaps);
192
193 capsstr = g_strdup_printf ("application/x-rtp, "
194 "media = (string) \"audio\", "
195 "clock-rate = (int) 8000, "
196 "encoding-name = (string) %s; "
197 "application/x-rtp, "
198 "media = (string) \"audio\", "
199 "clock-rate = (int) 8000, "
200 "encoding-name = (string) AAL2-%s", encoding_name, encoding_name);
201 filter = gst_caps_from_string (capsstr);
202 g_free (capsstr);
203 g_free (encoding_name);
204
205 /* intersect to filter */
206 intersect = gst_caps_intersect (peercaps, filter);
207 gst_caps_unref (peercaps);
208 gst_caps_unref (filter);
209
210 GST_DEBUG_OBJECT (payload, "intersected to %" GST_PTR_FORMAT, intersect);
211
212 if (!intersect)
213 goto no_format;
214 if (gst_caps_is_empty (intersect)) {
215 gst_caps_unref (intersect);
216 goto no_format;
217 }
218
219 structure = gst_caps_get_structure (intersect, 0);
220
221 /* now see what encoding name we settled on, we need to dup because the
222 * string goes away when we unref the intersection below. */
223 encoding_name =
224 g_strdup (gst_structure_get_string (structure, "encoding-name"));
225
226 /* if we managed to negotiate to AAL2, we definitely are going to do AAL2
227 * encoding. Else we only encode AAL2 when explicitly set by the
228 * property. */
229 if (g_str_has_prefix (encoding_name, "AAL2-"))
230 pay->aal2 = TRUE;
231 else
232 pay->aal2 = pay->force_aal2;
233
234 GST_DEBUG_OBJECT (payload, "final encoding %s, AAL2 %d", encoding_name,
235 pay->aal2);
236
237 gst_caps_unref (intersect);
238 } else {
239 /* downstream can do anything but we prefer the better supported non-AAL2 */
240 pay->aal2 = pay->force_aal2;
241 GST_DEBUG_OBJECT (payload, "no peer caps, AAL2 %d", pay->aal2);
242 }
243
244 gst_rtp_base_payload_set_options (payload, "audio", TRUE, encoding_name,
245 8000);
246 res = gst_rtp_base_payload_set_outcaps (payload, NULL);
247
248 g_free (encoding_name);
249
250 return res;
251
252 /* ERRORS */
253 invalid_bitrate:
254 {
255 GST_ERROR_OBJECT (payload, "invalid bitrate %d specified", pay->bitrate);
256 return FALSE;
257 }
258 no_format:
259 {
260 GST_ERROR_OBJECT (payload, "could not negotiate format");
261 return FALSE;
262 }
263 }
264
265 static GstFlowReturn
gst_rtp_g726_pay_handle_buffer(GstRTPBasePayload * payload,GstBuffer * buffer)266 gst_rtp_g726_pay_handle_buffer (GstRTPBasePayload * payload, GstBuffer * buffer)
267 {
268 GstFlowReturn res;
269 GstRtpG726Pay *pay;
270
271 pay = GST_RTP_G726_PAY (payload);
272
273 if (!pay->aal2) {
274 GstMapInfo map;
275 guint8 *data, tmp;
276 gsize size;
277
278 /* for non AAL2, we need to reshuffle the bytes, we can do this in-place
279 * when the buffer is writable. */
280 buffer = gst_buffer_make_writable (buffer);
281
282 gst_buffer_map (buffer, &map, GST_MAP_READWRITE);
283 data = map.data;
284 size = map.size;
285
286 GST_LOG_OBJECT (pay, "packing %" G_GSIZE_FORMAT " bytes of data", map.size);
287
288 /* we need to reshuffle the bytes, output is of the form:
289 * A B C D .. with the number of bits depending on the bitrate. */
290 switch (pay->bitrate) {
291 case 16000:
292 {
293 /* 0
294 * 0 1 2 3 4 5 6 7
295 * +-+-+-+-+-+-+-+-+-
296 * |D D|C C|B B|A A| ...
297 * |0 1|0 1|0 1|0 1|
298 * +-+-+-+-+-+-+-+-+-
299 */
300 while (size > 0) {
301 tmp = *data;
302 *data++ = ((tmp & 0xc0) >> 6) |
303 ((tmp & 0x30) >> 2) | ((tmp & 0x0c) << 2) | ((tmp & 0x03) << 6);
304 size--;
305 }
306 break;
307 }
308 case 24000:
309 {
310 /* 0 1 2
311 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3
312 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
313 * |C C|B B B|A A A|F|E E E|D D D|C|H H H|G G G|F F| ...
314 * |1 2|0 1 2|0 1 2|2|0 1 2|0 1 2|0|0 1 2|0 1 2|0 1|
315 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
316 */
317 while (size > 2) {
318 tmp = *data;
319 *data++ = ((tmp & 0xc0) >> 6) |
320 ((tmp & 0x38) >> 1) | ((tmp & 0x07) << 5);
321 tmp = *data;
322 *data++ = ((tmp & 0x80) >> 7) |
323 ((tmp & 0x70) >> 3) | ((tmp & 0x0e) << 4) | ((tmp & 0x01) << 7);
324 tmp = *data;
325 *data++ = ((tmp & 0xe0) >> 5) |
326 ((tmp & 0x1c) >> 2) | ((tmp & 0x03) << 6);
327 size -= 3;
328 }
329 break;
330 }
331 case 32000:
332 {
333 /* 0 1
334 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
335 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
336 * |B B B B|A A A A|D D D D|C C C C| ...
337 * |0 1 2 3|0 1 2 3|0 1 2 3|0 1 2 3|
338 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
339 */
340 while (size > 0) {
341 tmp = *data;
342 *data++ = ((tmp & 0xf0) >> 4) | ((tmp & 0x0f) << 4);
343 size--;
344 }
345 break;
346 }
347 case 40000:
348 {
349 /* 0 1 2 3 4
350 * 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
351 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
352 * |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|
353 * |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|
354 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
355 */
356 while (size > 4) {
357 tmp = *data;
358 *data++ = ((tmp & 0xe0) >> 5) | ((tmp & 0x1f) << 3);
359 tmp = *data;
360 *data++ = ((tmp & 0x80) >> 7) |
361 ((tmp & 0x7c) >> 2) | ((tmp & 0x03) << 6);
362 tmp = *data;
363 *data++ = ((tmp & 0xf0) >> 4) | ((tmp & 0x0f) << 4);
364 tmp = *data;
365 *data++ = ((tmp & 0xc0) >> 6) |
366 ((tmp & 0x3e) << 2) | ((tmp & 0x01) << 7);
367 tmp = *data;
368 *data++ = ((tmp & 0xf8) >> 3) | ((tmp & 0x07) << 5);
369 size -= 5;
370 }
371 break;
372 }
373 }
374 gst_buffer_unmap (buffer, &map);
375 }
376
377 res =
378 GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->handle_buffer (payload,
379 buffer);
380
381 return res;
382 }
383
384 static void
gst_rtp_g726_pay_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)385 gst_rtp_g726_pay_set_property (GObject * object, guint prop_id,
386 const GValue * value, GParamSpec * pspec)
387 {
388 GstRtpG726Pay *rtpg726pay;
389
390 rtpg726pay = GST_RTP_G726_PAY (object);
391
392 switch (prop_id) {
393 case PROP_FORCE_AAL2:
394 rtpg726pay->force_aal2 = g_value_get_boolean (value);
395 break;
396 default:
397 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
398 break;
399 }
400 }
401
402 static void
gst_rtp_g726_pay_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)403 gst_rtp_g726_pay_get_property (GObject * object, guint prop_id,
404 GValue * value, GParamSpec * pspec)
405 {
406 GstRtpG726Pay *rtpg726pay;
407
408 rtpg726pay = GST_RTP_G726_PAY (object);
409
410 switch (prop_id) {
411 case PROP_FORCE_AAL2:
412 g_value_set_boolean (value, rtpg726pay->force_aal2);
413 break;
414 default:
415 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
416 break;
417 }
418 }
419