g726pay: fix compilation
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpg726pay.c
1 /* GStreamer
2  * Copyright (C) 1999 Erik Walthinsen <omega@cse.ogi.edu>
3  * Copyright (C) 2005 Edgard Lima <edgard.lima@indt.org.br>
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., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, 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   PROP_LAST
43 };
44
45 static const GstElementDetails gst_rtp_g726_pay_details =
46 GST_ELEMENT_DETAILS ("RTP G.726 payloader",
47     "Codec/Payloader/Network",
48     "Payload-encodes G.726 audio into a RTP packet",
49     "Axis Communications <dev-gstreamer@axis.com>");
50
51 static GstStaticPadTemplate gst_rtp_g726_pay_sink_template =
52 GST_STATIC_PAD_TEMPLATE ("sink",
53     GST_PAD_SINK,
54     GST_PAD_ALWAYS,
55     GST_STATIC_CAPS ("audio/x-adpcm, "
56         "channels = (int) 1, "
57         "rate = (int) 8000, "
58         "bitrate = (int) { 16000, 24000, 32000, 40000 }, "
59         "layout = (string) \"g726\"")
60     );
61
62 static GstStaticPadTemplate gst_rtp_g726_pay_src_template =
63 GST_STATIC_PAD_TEMPLATE ("src",
64     GST_PAD_SRC,
65     GST_PAD_ALWAYS,
66     GST_STATIC_CAPS ("application/x-rtp, "
67         "media = (string) \"audio\", "
68         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
69         "clock-rate = (int) 8000, "
70         "encoding-name = (string) { \"G726-16\", \"G726-24\", \"G726-32\", \"G726-40\", "
71         "    \"AAL2-G726-16\", \"AAL2-G726-24\", \"AAL2-G726-32\", \"AAL2-G726-40\" } ")
72     );
73
74 static void gst_rtp_g726_pay_get_property (GObject * object, guint prop_id,
75     GValue * value, GParamSpec * pspec);
76 static void gst_rtp_g726_pay_set_property (GObject * object, guint prop_id,
77     const GValue * value, GParamSpec * pspec);
78
79 static gboolean gst_rtp_g726_pay_setcaps (GstBaseRTPPayload * payload,
80     GstCaps * caps);
81 static GstFlowReturn gst_rtp_g726_pay_handle_buffer (GstBaseRTPPayload *
82     payload, GstBuffer * buffer);
83
84 GST_BOILERPLATE (GstRtpG726Pay, gst_rtp_g726_pay, GstBaseRTPAudioPayload,
85     GST_TYPE_BASE_RTP_AUDIO_PAYLOAD);
86
87 static void
88 gst_rtp_g726_pay_base_init (gpointer klass)
89 {
90   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
91
92   gst_element_class_add_pad_template (element_class,
93       gst_static_pad_template_get (&gst_rtp_g726_pay_sink_template));
94   gst_element_class_add_pad_template (element_class,
95       gst_static_pad_template_get (&gst_rtp_g726_pay_src_template));
96   gst_element_class_set_details (element_class, &gst_rtp_g726_pay_details);
97 }
98
99 static void
100 gst_rtp_g726_pay_class_init (GstRtpG726PayClass * klass)
101 {
102   GObjectClass *gobject_class;
103   GstBaseRTPPayloadClass *gstbasertppayload_class;
104
105   gobject_class = (GObjectClass *) klass;
106   gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
107
108   gobject_class->set_property = gst_rtp_g726_pay_set_property;
109   gobject_class->get_property = gst_rtp_g726_pay_get_property;
110
111   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_FORCE_AAL2,
112       g_param_spec_boolean ("force-aal2", "Force AAL2",
113           "Force AAL2 encoding for compatibility with bad depayloaders",
114           DEFAULT_FORCE_AAL2, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
115
116   gstbasertppayload_class->set_caps = gst_rtp_g726_pay_setcaps;
117   gstbasertppayload_class->handle_buffer = gst_rtp_g726_pay_handle_buffer;
118
119   GST_DEBUG_CATEGORY_INIT (rtpg726pay_debug, "rtpg726pay", 0,
120       "G.726 RTP Payloader");
121 }
122
123 static void
124 gst_rtp_g726_pay_init (GstRtpG726Pay * rtpg726pay, GstRtpG726PayClass * klass)
125 {
126   GstBaseRTPAudioPayload *basertpaudiopayload;
127
128   basertpaudiopayload = GST_BASE_RTP_AUDIO_PAYLOAD (rtpg726pay);
129
130   GST_BASE_RTP_PAYLOAD (rtpg726pay)->clock_rate = 8000;
131
132   rtpg726pay->force_aal2 = DEFAULT_FORCE_AAL2;
133
134   /* sample based codec */
135   gst_base_rtp_audio_payload_set_sample_based (basertpaudiopayload);
136 }
137
138 static gboolean
139 gst_rtp_g726_pay_setcaps (GstBaseRTPPayload * payload, GstCaps * caps)
140 {
141   gchar *encoding_name;
142   GstStructure *structure;
143   GstBaseRTPAudioPayload *basertpaudiopayload;
144   GstRtpG726Pay *pay;
145   GstCaps *peercaps;
146
147   basertpaudiopayload = GST_BASE_RTP_AUDIO_PAYLOAD (payload);
148   pay = GST_RTP_G726_PAY (payload);
149
150   structure = gst_caps_get_structure (caps, 0);
151
152   if (!gst_structure_get_int (structure, "bitrate", &pay->bitrate))
153     pay->bitrate = 32000;
154
155   GST_DEBUG_OBJECT (payload, "using bitrate %d", pay->bitrate);
156
157   pay->aal2 = FALSE;
158
159   /* first see what we can do with the bitrate */
160   switch (pay->bitrate) {
161     case 16000:
162       encoding_name = g_strdup ("G726-16");
163       gst_base_rtp_audio_payload_set_samplebits_options (basertpaudiopayload,
164           2);
165       break;
166     case 24000:
167       encoding_name = g_strdup ("G726-24");
168       gst_base_rtp_audio_payload_set_samplebits_options (basertpaudiopayload,
169           3);
170       break;
171     case 32000:
172       encoding_name = g_strdup ("G726-32");
173       gst_base_rtp_audio_payload_set_samplebits_options (basertpaudiopayload,
174           4);
175       break;
176     case 40000:
177       encoding_name = g_strdup ("G726-40");
178       gst_base_rtp_audio_payload_set_samplebits_options (basertpaudiopayload,
179           5);
180       break;
181     default:
182       goto invalid_bitrate;
183   }
184
185   GST_DEBUG_OBJECT (payload, "selected base encoding %s", encoding_name);
186
187   /* now see if we need to produce AAL2 or not */
188   peercaps = gst_pad_peer_get_caps (payload->srcpad);
189   if (peercaps) {
190     GstCaps *filter, *intersect;
191     gchar *capsstr;
192
193     GST_DEBUG_OBJECT (payload, "have peercaps %" GST_PTR_FORMAT, peercaps);
194
195     capsstr = g_strdup_printf ("application/x-rtp, "
196         "media = (string) \"audio\", "
197         "clock-rate = (int) 8000, "
198         "encoding-name = (string) %s; "
199         "application/x-rtp, "
200         "media = (string) \"audio\", "
201         "clock-rate = (int) 8000, "
202         "encoding-name = (string) AAL2-%s", encoding_name, encoding_name);
203     filter = gst_caps_from_string (capsstr);
204     g_free (capsstr);
205
206     /* intersect to filter */
207     intersect = gst_caps_intersect (peercaps, filter);
208     gst_caps_unref (peercaps);
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 definatly 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_basertppayload_set_options (payload, "audio", TRUE, encoding_name, 8000);
245   gst_basertppayload_set_outcaps (payload, NULL);
246
247   g_free (encoding_name);
248
249   return TRUE;
250
251   /* ERRORS */
252 invalid_bitrate:
253   {
254     GST_ERROR_OBJECT (payload, "invalid bitrate %d specified", pay->bitrate);
255     return FALSE;
256   }
257 no_format:
258   {
259     GST_ERROR_OBJECT (payload, "could not negotiate format");
260     return FALSE;
261   }
262 }
263
264 static GstFlowReturn
265 gst_rtp_g726_pay_handle_buffer (GstBaseRTPPayload * payload, GstBuffer * buffer)
266 {
267   GstFlowReturn res;
268   GstRtpG726Pay *pay;
269
270   pay = GST_RTP_G726_PAY (payload);
271
272   if (!pay->aal2) {
273     guint8 *data, tmp;
274     guint len;
275
276     /* for non AAL2, we need to reshuffle the bytes, we can do this in-place
277      * when the buffer is writable. */
278     buffer = gst_buffer_make_writable (buffer);
279
280     data = GST_BUFFER_DATA (buffer);
281     len = GST_BUFFER_SIZE (buffer);
282
283     GST_LOG_OBJECT (pay, "packing %u bytes of data", len);
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 (len > 0) {
298           tmp = *data;
299           *data++ = ((tmp & 0xc0) >> 6) |
300               ((tmp & 0x30) >> 2) | ((tmp & 0x0c) << 2) | ((tmp & 0x03) << 6);
301           len--;
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 (len > 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           len -= 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 (len > 0) {
338           tmp = *data;
339           *data++ = ((tmp & 0xf0) >> 4) | ((tmp & 0x0f) << 4);
340           len--;
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 (len > 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           len -= 5;
367         }
368         break;
369       }
370     }
371   }
372
373   res =
374       GST_BASE_RTP_PAYLOAD_CLASS (parent_class)->handle_buffer (payload,
375       buffer);
376
377   return res;
378 }
379
380 static void
381 gst_rtp_g726_pay_set_property (GObject * object, guint prop_id,
382     const GValue * value, GParamSpec * pspec)
383 {
384   GstRtpG726Pay *rtpg726pay;
385
386   rtpg726pay = GST_RTP_G726_PAY (object);
387
388   switch (prop_id) {
389     case PROP_FORCE_AAL2:
390       rtpg726pay->force_aal2 = g_value_get_boolean (value);
391       break;
392     default:
393       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
394       break;
395   }
396 }
397
398 static void
399 gst_rtp_g726_pay_get_property (GObject * object, guint prop_id,
400     GValue * value, GParamSpec * pspec)
401 {
402   GstRtpG726Pay *rtpg726pay;
403
404   rtpg726pay = GST_RTP_G726_PAY (object);
405
406   switch (prop_id) {
407     case PROP_FORCE_AAL2:
408       g_value_set_boolean (value, rtpg726pay->force_aal2);
409       break;
410     default:
411       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
412       break;
413   }
414 }
415
416 gboolean
417 gst_rtp_g726_pay_plugin_init (GstPlugin * plugin)
418 {
419   return gst_element_register (plugin, "rtpg726pay",
420       GST_RANK_NONE, GST_TYPE_RTP_G726_PAY);
421 }