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