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