Remove unused variables in _class_init
[platform/upstream/gstreamer.git] / gst / rtp / gstrtpg726depay.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 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., 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 <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <gst/rtp/gstrtpbuffer.h>
31
32 #include "gstrtpg726depay.h"
33
34 #define DEFAULT_BIT_RATE 32000
35 #define SAMPLE_RATE 8000
36 #define LAYOUT_G726 "g726"
37
38 /* elementfactory information */
39 static const GstElementDetails gst_rtp_g726depay_details =
40 GST_ELEMENT_DETAILS ("RTP G.726 depayloader",
41     "Codec/Depayloader/Network",
42     "Extracts G.726 audio from RTP packets",
43     "Axis Communications <dev-gstreamer@axis.com>");
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   PROP_LAST
59 };
60
61 static GstStaticPadTemplate gst_rtp_g726_depay_sink_template =
62     GST_STATIC_PAD_TEMPLATE ("sink",
63     GST_PAD_SINK,
64     GST_PAD_ALWAYS,
65     GST_STATIC_CAPS ("application/x-rtp, "
66         "media = (string) \"audio\", "
67         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
68         "encoding-name = (string) { \"G726\", \"G726-16\", \"G726-24\", \"G726-32\", \"G726-40\", "
69         "\"AAL2-G726-16\", \"AAL2-G726-24\", \"AAL2-G726-32\", \"AAL2-G726-40\" }, "
70         "clock-rate = (int) 8000;")
71     );
72
73 static GstStaticPadTemplate gst_rtp_g726_depay_src_template =
74 GST_STATIC_PAD_TEMPLATE ("src",
75     GST_PAD_SRC,
76     GST_PAD_ALWAYS,
77     GST_STATIC_CAPS ("audio/x-adpcm, "
78         "channels = (int) 1, "
79         "rate = (int) 8000, "
80         "bitrate = (int) { 16000, 24000, 32000, 40000 }, "
81         "layout = (string) \"g726\"")
82     );
83
84 static void gst_rtp_g726_depay_get_property (GObject * object, guint prop_id,
85     GValue * value, GParamSpec * pspec);
86 static void gst_rtp_g726_depay_set_property (GObject * object, guint prop_id,
87     const GValue * value, GParamSpec * pspec);
88
89 static GstBuffer *gst_rtp_g726_depay_process (GstBaseRTPDepayload * depayload,
90     GstBuffer * buf);
91 static gboolean gst_rtp_g726_depay_setcaps (GstBaseRTPDepayload * depayload,
92     GstCaps * caps);
93
94 GST_BOILERPLATE (GstRtpG726Depay, gst_rtp_g726_depay, GstBaseRTPDepayload,
95     GST_TYPE_BASE_RTP_DEPAYLOAD);
96
97 static void
98 gst_rtp_g726_depay_base_init (gpointer klass)
99 {
100   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
101
102   gst_element_class_add_pad_template (element_class,
103       gst_static_pad_template_get (&gst_rtp_g726_depay_src_template));
104   gst_element_class_add_pad_template (element_class,
105       gst_static_pad_template_get (&gst_rtp_g726_depay_sink_template));
106   gst_element_class_set_details (element_class, &gst_rtp_g726depay_details);
107 }
108
109 static void
110 gst_rtp_g726_depay_class_init (GstRtpG726DepayClass * klass)
111 {
112   GObjectClass *gobject_class;
113   GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
114
115   gobject_class = (GObjectClass *) klass;
116   gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
117
118   parent_class = g_type_class_peek_parent (klass);
119
120   gobject_class->set_property = gst_rtp_g726_depay_set_property;
121   gobject_class->get_property = gst_rtp_g726_depay_get_property;
122
123   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_FORCE_AAL2,
124       g_param_spec_boolean ("force-aal2", "Force AAL2",
125           "Force AAL2 decoding for compatibility with bad payloaders",
126           DEFAULT_FORCE_AAL2, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
127
128   gstbasertpdepayload_class->process = gst_rtp_g726_depay_process;
129   gstbasertpdepayload_class->set_caps = gst_rtp_g726_depay_setcaps;
130
131 }
132
133 static void
134 gst_rtp_g726_depay_init (GstRtpG726Depay * rtpG726depay,
135     GstRtpG726DepayClass * klass)
136 {
137   GstBaseRTPDepayload *depayload;
138
139   depayload = GST_BASE_RTP_DEPAYLOAD (rtpG726depay);
140
141   gst_pad_use_fixed_caps (GST_BASE_RTP_DEPAYLOAD_SRCPAD (depayload));
142
143   rtpG726depay->force_aal2 = DEFAULT_FORCE_AAL2;
144 }
145
146 static gboolean
147 gst_rtp_g726_depay_setcaps (GstBaseRTPDepayload * 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   } else {
169     if (g_str_has_prefix (encoding_name, "AAL2-")) {
170       depay->aal2 = TRUE;
171       encoding_name += 5;
172     }
173     if (g_ascii_strcasecmp (encoding_name, "G726-16") == 0) {
174       depay->bitrate = 16000;
175     } else if (g_ascii_strcasecmp (encoding_name, "G726-24") == 0) {
176       depay->bitrate = 24000;
177     } else if (g_ascii_strcasecmp (encoding_name, "G726-32") == 0) {
178       depay->bitrate = 32000;
179     } else if (g_ascii_strcasecmp (encoding_name, "G726-40") == 0) {
180       depay->bitrate = 40000;
181     } else
182       goto unknown_encoding;
183   }
184
185   GST_DEBUG ("RTP G.726 depayloader, bitrate set to %d\n", depay->bitrate);
186
187   srccaps = gst_caps_new_simple ("audio/x-adpcm",
188       "channels", G_TYPE_INT, 1,
189       "rate", G_TYPE_INT, clock_rate,
190       "bitrate", G_TYPE_INT, depay->bitrate,
191       "layout", G_TYPE_STRING, LAYOUT_G726, NULL);
192
193   ret = gst_pad_set_caps (GST_BASE_RTP_DEPAYLOAD_SRCPAD (depayload), srccaps);
194   gst_caps_unref (srccaps);
195
196   return ret;
197
198   /* ERRORS */
199 unknown_encoding:
200   {
201     GST_WARNING ("Could not determine bitrate from encoding-name (%s)",
202         encoding_name);
203     return FALSE;
204   }
205 }
206
207
208 static GstBuffer *
209 gst_rtp_g726_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
210 {
211   GstRtpG726Depay *depay;
212   GstBuffer *outbuf = NULL;
213   gboolean marker;
214
215   depay = GST_RTP_G726_DEPAY (depayload);
216
217   marker = gst_rtp_buffer_get_marker (buf);
218
219   GST_DEBUG ("process : got %d bytes, mark %d ts %u seqn %d",
220       GST_BUFFER_SIZE (buf), marker,
221       gst_rtp_buffer_get_timestamp (buf), gst_rtp_buffer_get_seq (buf));
222
223   if (depay->aal2 || depay->force_aal2) {
224     /* AAL2, we can just copy the bytes */
225     outbuf = gst_rtp_buffer_get_payload_buffer (buf);
226   } else {
227     guint8 *in, *out, tmp;
228     guint len;
229
230     in = gst_rtp_buffer_get_payload (buf);
231     len = gst_rtp_buffer_get_payload_len (buf);
232
233     if (gst_buffer_is_writable (buf)) {
234       outbuf = gst_rtp_buffer_get_payload_buffer (buf);
235     } else {
236       GstBuffer *copy;
237
238       /* copy buffer */
239       copy = gst_buffer_copy (buf);
240       outbuf = gst_rtp_buffer_get_payload_buffer (copy);
241       gst_buffer_unref (copy);
242     }
243     out = GST_BUFFER_DATA (outbuf);
244
245     /* we need to reshuffle the bytes */
246     switch (depay->bitrate) {
247       case 16000:
248       {
249         /*  0               
250          *  0 1 2 3 4 5 6 7 
251          * +-+-+-+-+-+-+-+-+-
252          * |D D|C C|B B|A A| ...
253          * |0 1|0 1|0 1|0 1|
254          * +-+-+-+-+-+-+-+-+-
255          */
256         while (len > 0) {
257           tmp = *in++;
258           *out++ = ((tmp & 0xc0) >> 6) |
259               ((tmp & 0x30) >> 2) | ((tmp & 0x0c) << 2) | ((tmp & 0x03) << 6);
260           len--;
261         }
262         break;
263       }
264       case 24000:
265       {
266         /*  0                   1                   2
267          *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3
268          * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
269          * |C C|B B B|A A A|F|E E E|D D D|C|H H H|G G G|F F| ...
270          * |1 2|0 1 2|0 1 2|2|0 1 2|0 1 2|0|0 1 2|0 1 2|0 1|
271          * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
272          */
273         while (len > 2) {
274           tmp = *in++;
275           *out++ = ((tmp & 0xe0) >> 5) |
276               ((tmp & 0x1c) << 1) | ((tmp & 0x03) << 6);
277           tmp = *in++;
278           *out++ = ((tmp & 0x80) >> 7) |
279               ((tmp & 0x70) >> 3) | ((tmp & 0x0e) << 4) | ((tmp & 0x01) << 7);
280           tmp = *in++;
281           *out++ = ((tmp & 0xc0) >> 6) |
282               ((tmp & 0x38) >> 1) | ((tmp & 0x07) << 5);
283           len -= 3;
284         }
285         break;
286       }
287       case 32000:
288       {
289         /*  0                   1
290          *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
291          * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
292          * |B B B B|A A A A|D D D D|C C C C| ...
293          * |0 1 2 3|0 1 2 3|0 1 2 3|0 1 2 3|
294          * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
295          */
296         while (len > 0) {
297           tmp = *in++;
298           *out++ = ((tmp & 0xf0) >> 4) | ((tmp & 0x0f) << 4);
299           len--;
300         }
301         break;
302       }
303       case 40000:
304       {
305         /*  0                   1                   2                   3                   4
306          *  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
307          * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
308          * |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|
309          * |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|   
310          * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
311          */
312         while (len > 4) {
313           tmp = *in++;
314           *out++ = ((tmp & 0xf8) >> 3) | ((tmp & 0x07) << 5);
315           tmp = *in++;
316           *out++ = ((tmp & 0xc0) >> 6) |
317               ((tmp & 0x3e) << 1) | ((tmp & 0x01) << 7);
318           tmp = *in++;
319           *out++ = ((tmp & 0xf0) >> 4) | ((tmp & 0x0f) << 4);
320           tmp = *in++;
321           *out++ = ((tmp & 0x80) >> 7) |
322               ((tmp & 0x7c) >> 1) | ((tmp & 0x03) << 6);
323           tmp = *in++;
324           *out++ = ((tmp & 0xe0) >> 5) | ((tmp & 0x1f) << 3);
325           len -= 5;
326         }
327         break;
328       }
329     }
330   }
331
332   if (marker) {
333     /* mark start of talkspurt with discont */
334     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
335   }
336
337   return outbuf;
338 }
339
340 static void
341 gst_rtp_g726_depay_set_property (GObject * object, guint prop_id,
342     const GValue * value, GParamSpec * pspec)
343 {
344   GstRtpG726Depay *rtpg726depay;
345
346   rtpg726depay = GST_RTP_G726_DEPAY (object);
347
348   switch (prop_id) {
349     case PROP_FORCE_AAL2:
350       rtpg726depay->force_aal2 = g_value_get_boolean (value);
351       break;
352     default:
353       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
354       break;
355   }
356 }
357
358 static void
359 gst_rtp_g726_depay_get_property (GObject * object, guint prop_id,
360     GValue * value, GParamSpec * pspec)
361 {
362   GstRtpG726Depay *rtpg726depay;
363
364   rtpg726depay = GST_RTP_G726_DEPAY (object);
365
366   switch (prop_id) {
367     case PROP_FORCE_AAL2:
368       g_value_set_boolean (value, rtpg726depay->force_aal2);
369       break;
370     default:
371       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
372       break;
373   }
374 }
375
376 gboolean
377 gst_rtp_g726_depay_plugin_init (GstPlugin * plugin)
378 {
379   return gst_element_register (plugin, "rtpg726depay",
380       GST_RANK_MARGINAL, GST_TYPE_RTP_G726_DEPAY);
381 }