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