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