725dc83736eb5b07f2bf4dbf13195121c82574c4
[platform/upstream/gstreamer.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   /* FILL ME */
40   LAST_SIGNAL
41 };
42
43 enum {
44   ARG_0
45 };
46
47 static void             gst_mulawdec_class_init         (GstMuLawDecClass *klass);
48 static void             gst_mulawdec_base_init          (GstMuLawDecClass *klass);
49 static void             gst_mulawdec_init                       (GstMuLawDec *mulawdec);
50
51 static void             gst_mulawdec_set_property                       (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
52 static void             gst_mulawdec_get_property                       (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
53
54 static void             gst_mulawdec_chain                      (GstPad *pad, GstData *_data);
55
56
57 static GstElementClass *parent_class = NULL;
58 /*static guint gst_stereo_signals[LAST_SIGNAL] = { 0 };*/
59
60
61 static GstPadLinkReturn
62 mulawdec_link (GstPad *pad, GstCaps *caps)
63 {
64   GstCaps* tempcaps;
65   gint rate, channels;
66   
67   GstMuLawDec* mulawdec = GST_MULAWDEC (GST_OBJECT_PARENT (pad));
68   
69   if (!GST_CAPS_IS_FIXED (caps))
70     return GST_PAD_LINK_DELAYED;  
71   
72   if (!gst_caps_get (caps, "rate", &rate,
73                            "channels", &channels,
74                            NULL))
75     return GST_PAD_LINK_DELAYED;
76   
77   tempcaps = GST_CAPS_NEW (
78               "sinesrc_src_caps",
79               "audio/x-raw-int",
80           "depth",    GST_PROPS_INT (16),
81           "width",    GST_PROPS_INT (16),
82           "signed",   GST_PROPS_BOOLEAN (TRUE),
83           "endianness",    GST_PROPS_INT (G_BYTE_ORDER),
84           "rate",     GST_PROPS_INT (rate),
85           "channels", GST_PROPS_INT (channels),
86         NULL);
87   
88   return gst_pad_try_set_caps (mulawdec->srcpad, tempcaps);
89 }
90
91 GType
92 gst_mulawdec_get_type(void) {
93   static GType mulawdec_type = 0;
94
95   if (!mulawdec_type) {
96     static const GTypeInfo mulawdec_info = {
97       sizeof(GstMuLawDecClass),
98       (GBaseInitFunc)gst_mulawdec_base_init,
99       NULL,
100       (GClassInitFunc)gst_mulawdec_class_init,
101       NULL,
102       NULL,
103       sizeof(GstMuLawDec),
104       0,
105       (GInstanceInitFunc)gst_mulawdec_init,
106     };
107     mulawdec_type = g_type_register_static(GST_TYPE_ELEMENT, "GstMuLawDec", &mulawdec_info, 0);
108   }
109   return mulawdec_type;
110 }
111
112 static void
113 gst_mulawdec_base_init (GstMuLawDecClass *klass)
114 {
115   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
116
117   gst_element_class_add_pad_template (element_class, mulawdec_src_template);
118   gst_element_class_add_pad_template (element_class, mulawdec_sink_template);
119   gst_element_class_set_details (element_class, &mulawdec_details);
120 }
121
122 static void
123 gst_mulawdec_class_init (GstMuLawDecClass *klass)
124 {
125   GObjectClass *gobject_class;
126   GstElementClass *gstelement_class;
127
128   gobject_class = (GObjectClass*)klass;
129   gstelement_class = (GstElementClass*)klass;
130
131   parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
132
133   gobject_class->set_property = gst_mulawdec_set_property;
134   gobject_class->get_property = gst_mulawdec_get_property;
135 }
136
137 static void
138 gst_mulawdec_init (GstMuLawDec *mulawdec)
139 {
140   mulawdec->sinkpad = gst_pad_new_from_template(mulawdec_sink_template,"sink");
141   mulawdec->srcpad = gst_pad_new_from_template(mulawdec_src_template,"src");
142   gst_pad_set_link_function(mulawdec->sinkpad, mulawdec_link);
143
144   gst_element_add_pad(GST_ELEMENT(mulawdec),mulawdec->sinkpad);
145   gst_pad_set_chain_function(mulawdec->sinkpad,gst_mulawdec_chain);
146   gst_element_add_pad(GST_ELEMENT(mulawdec),mulawdec->srcpad);
147 }
148
149 static void
150 gst_mulawdec_chain (GstPad *pad,GstData *_data)
151 {
152   GstBuffer *buf = GST_BUFFER (_data);
153   GstMuLawDec *mulawdec;
154   gint16 *linear_data;
155   guint8 *mulaw_data;
156   GstBuffer* outbuf;
157
158   g_return_if_fail(pad != NULL);
159   g_return_if_fail(GST_IS_PAD(pad));
160   g_return_if_fail(buf != NULL);
161
162   mulawdec = GST_MULAWDEC(GST_OBJECT_PARENT (pad));
163   g_return_if_fail(mulawdec != NULL);
164   g_return_if_fail(GST_IS_MULAWDEC(mulawdec));
165
166   mulaw_data = (guint8 *)GST_BUFFER_DATA(buf);
167   outbuf=gst_buffer_new();
168   GST_BUFFER_DATA(outbuf) = (gchar*)g_new(gint16,GST_BUFFER_SIZE(buf));
169   GST_BUFFER_SIZE(outbuf) = GST_BUFFER_SIZE(buf)*2;
170
171   linear_data = (gint16*)GST_BUFFER_DATA(outbuf);
172   mulaw_decode(mulaw_data,linear_data,GST_BUFFER_SIZE(buf));
173
174   gst_buffer_unref(buf);
175   gst_pad_push(mulawdec->srcpad,GST_DATA (outbuf));
176 }
177
178 static void
179 gst_mulawdec_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
180 {
181   GstMuLawDec *mulawdec;
182
183   /* it's not null if we got it, but it might not be ours */
184   g_return_if_fail(GST_IS_MULAWDEC(object));
185   mulawdec = GST_MULAWDEC(object);
186
187   switch (prop_id) {
188     default:
189       break;
190   }
191 }
192
193 static void
194 gst_mulawdec_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
195 {
196   GstMuLawDec *mulawdec;
197
198   /* it's not null if we got it, but it might not be ours */
199   g_return_if_fail(GST_IS_MULAWDEC(object));
200   mulawdec = GST_MULAWDEC(object);
201
202   switch (prop_id) {
203     default:
204       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
205       break;
206   }
207 }