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