gst-libs/gst/video/video.h: Fix caps template names to be understandable.
[platform/upstream/gst-plugins-good.git] / gst / law / alaw-decode.c
1 /* GStreamer
2  * Copyright (C) 1999 Erik Walthinsen <omega@cse.ogi.edu>
3  * PCM - A-Law conversion
4  *   Copyright (C) 2000 by Abramo Bagnara <abramo@alsa-project.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 #include <gst/gst.h>
26 #include "alaw-decode.h"
27
28 extern GstPadTemplate *alawdec_src_template, *alawdec_sink_template;
29
30 /* elementfactory information */
31 static GstElementDetails alawdec_details = {
32   "A Law to PCM conversion",
33   "Codec/Decoder/Audio",
34   "Convert 8bit A law to 16bit PCM",
35   "Zaheer Merali <zaheer@bellworldwide.net>"
36 };
37
38 /* Stereo signals and args */
39 enum {
40   /* FILL ME */
41   LAST_SIGNAL
42 };
43
44 enum {
45   ARG_0
46 };
47
48 static void             gst_alawdec_class_init          (GstALawDecClass *klass);
49 static void             gst_alawdec_base_init           (GstALawDecClass *klass);
50 static void             gst_alawdec_init                        (GstALawDec *alawdec);
51
52 static void             gst_alawdec_set_property                        (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
53 static void             gst_alawdec_get_property                        (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
54
55 static void             gst_alawdec_chain                       (GstPad *pad, GstData *_data);
56
57
58 static GstElementClass *parent_class = NULL;
59 /*static guint gst_stereo_signals[LAST_SIGNAL] = { 0 }; */
60
61 /*
62  * alaw_to_s16() - Convert an A-law value to 16-bit linear PCM
63  *
64  */
65 static gint alaw_to_s16(guint8 a_val)
66 {
67         gint            t;
68         gint            seg;
69
70         a_val ^= 0x55;
71         t = a_val & 0x7f;
72         if (t < 16)
73                 t = (t << 4) + 8;
74         else {
75                 seg = (t >> 4) & 0x07;
76                 t = ((t & 0x0f) << 4) + 0x108;
77                 t <<= seg -1;
78         }
79         return ((a_val & 0x80) ? t : -t);
80 }
81
82 static GstPadLinkReturn
83 alawdec_link (GstPad *pad, const GstCaps *caps)
84 {
85   GstCaps* tempcaps;
86   gint rate, channels;
87   GstStructure *structure;
88   gboolean ret;
89   
90   GstALawDec* alawdec = GST_ALAWDEC (GST_OBJECT_PARENT (pad));
91   
92   structure = gst_caps_get_structure (caps, 0);
93
94   ret = gst_structure_get_int (structure, "rate", &rate);
95   ret &= gst_structure_get_int (structure, "channels", &channels);
96   if (!ret) return GST_PAD_LINK_REFUSED;
97   
98   tempcaps = gst_caps_new_simple ( "audio/x-raw-int",
99       "depth",    G_TYPE_INT, 16,
100       "width",    G_TYPE_INT, 16,
101       "signed",   G_TYPE_BOOLEAN, TRUE,
102       "endianness",   G_TYPE_INT, G_BYTE_ORDER,
103       "rate",     G_TYPE_INT, rate,
104       "channels", G_TYPE_INT, channels,
105       NULL);
106   
107   return gst_pad_try_set_caps (alawdec->srcpad, tempcaps);
108 }
109
110 GType
111 gst_alawdec_get_type(void) {
112   static GType alawdec_type = 0;
113
114   if (!alawdec_type) {
115     static const GTypeInfo alawdec_info = {
116       sizeof(GstALawDecClass),
117       (GBaseInitFunc)gst_alawdec_base_init,
118       NULL,
119       (GClassInitFunc)gst_alawdec_class_init,
120       NULL,
121       NULL,
122       sizeof(GstALawDec),
123       0,
124       (GInstanceInitFunc)gst_alawdec_init,
125     };
126     alawdec_type = g_type_register_static(GST_TYPE_ELEMENT, "GstALawDec", &alawdec_info, 0);
127   }
128   return alawdec_type;
129 }
130
131 static void
132 gst_alawdec_base_init (GstALawDecClass *klass)
133 {
134   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
135
136   gst_element_class_add_pad_template (element_class, alawdec_src_template);
137   gst_element_class_add_pad_template (element_class, alawdec_sink_template);
138   gst_element_class_set_details (element_class, &alawdec_details);
139 }
140
141 static void
142 gst_alawdec_class_init (GstALawDecClass *klass)
143 {
144   GObjectClass *gobject_class;
145   GstElementClass *gstelement_class;
146
147   gobject_class = (GObjectClass*)klass;
148   gstelement_class = (GstElementClass*)klass;
149
150   parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
151
152   gobject_class->set_property = gst_alawdec_set_property;
153   gobject_class->get_property = gst_alawdec_get_property;
154 }
155
156 static void
157 gst_alawdec_init (GstALawDec *alawdec)
158 {
159   alawdec->sinkpad = gst_pad_new_from_template(alawdec_sink_template,"sink");
160   alawdec->srcpad = gst_pad_new_from_template(alawdec_src_template,"src");
161   gst_pad_set_link_function(alawdec->sinkpad, alawdec_link);
162
163   gst_element_add_pad(GST_ELEMENT(alawdec),alawdec->sinkpad);
164   gst_pad_set_chain_function(alawdec->sinkpad,gst_alawdec_chain);
165   gst_element_add_pad(GST_ELEMENT(alawdec),alawdec->srcpad);
166 }
167
168 static void
169 gst_alawdec_chain (GstPad *pad,GstData *_data)
170 {
171   GstBuffer *buf = GST_BUFFER (_data);
172   GstALawDec *alawdec;
173   gint16 *linear_data;
174   guint8 *alaw_data;
175   GstBuffer* outbuf;
176   gint i;
177
178   g_return_if_fail(pad != NULL);
179   g_return_if_fail(GST_IS_PAD(pad));
180   g_return_if_fail(buf != NULL);
181
182   alawdec = GST_ALAWDEC(GST_OBJECT_PARENT (pad));
183   g_return_if_fail(alawdec != NULL);
184   g_return_if_fail(GST_IS_ALAWDEC(alawdec));
185
186   alaw_data = (guint8 *)GST_BUFFER_DATA(buf);
187   outbuf=gst_buffer_new();
188   GST_BUFFER_DATA(outbuf) = (gchar*)g_new(gint16,GST_BUFFER_SIZE(buf));
189   GST_BUFFER_SIZE(outbuf) = GST_BUFFER_SIZE(buf)*2;
190
191   linear_data = (gint16*)GST_BUFFER_DATA(outbuf);
192   for (i = 0; i < GST_BUFFER_SIZE(buf); i++) {
193     *linear_data = alaw_to_s16 (*alaw_data);
194     linear_data++;
195     alaw_data++;
196   }
197   
198   gst_buffer_unref(buf);
199   gst_pad_push(alawdec->srcpad,GST_DATA (outbuf));
200 }
201
202 static void
203 gst_alawdec_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
204 {
205   GstALawDec *alawdec;
206
207   /* it's not null if we got it, but it might not be ours */
208   g_return_if_fail(GST_IS_ALAWDEC(object));
209   alawdec = GST_ALAWDEC(object);
210
211   switch (prop_id) {
212     default:
213       break;
214   }
215 }
216
217 static void
218 gst_alawdec_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
219 {
220   GstALawDec *alawdec;
221
222   /* it's not null if we got it, but it might not be ours */
223   g_return_if_fail(GST_IS_ALAWDEC(object));
224   alawdec = GST_ALAWDEC(object);
225
226   switch (prop_id) {
227     default:
228       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
229       break;
230   }
231 }
232
233