gst-indent
[platform/upstream/gst-plugins-good.git] / gst / law / mulaw-encode.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-encode.h"
25 #include "mulaw-conversion.h"
26
27 extern GstPadTemplate *mulawenc_src_template, *mulawenc_sink_template;
28
29 /* elementfactory information */
30 static GstElementDetails mulawenc_details = {
31   "PCM to Mu Law conversion",
32   "Codec/Encoder/Audio",
33   "Convert 16bit PCM to 8bit mu law",
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_mulawenc_class_init (GstMuLawEncClass * klass);
50 static void gst_mulawenc_base_init (GstMuLawEncClass * klass);
51 static void gst_mulawenc_init (GstMuLawEnc * mulawenc);
52
53 static void gst_mulawenc_set_property (GObject * object, guint prop_id,
54     const GValue * value, GParamSpec * pspec);
55 static void gst_mulawenc_get_property (GObject * object, guint prop_id,
56     GValue * value, GParamSpec * pspec);
57
58 static void gst_mulawenc_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 static GstPadLinkReturn
66 mulawenc_link (GstPad * pad, const GstCaps * caps)
67 {
68   GstCaps *tempcaps;
69   gint rate, channels;
70   GstStructure *structure;
71   gboolean ret;
72
73   GstMuLawEnc *mulawenc = GST_MULAWENC (GST_OBJECT_PARENT (pad));
74
75   structure = gst_caps_get_structure (caps, 0);
76   ret = gst_structure_get_int (structure, "rate", &rate);
77   ret = gst_structure_get_int (structure, "channels", &channels);
78   if (!ret)
79     return GST_PAD_LINK_REFUSED;
80
81   tempcaps = gst_caps_new_simple ("audio/x-mulaw",
82       "depth", G_TYPE_INT, 8,
83       "width", G_TYPE_INT, 8,
84       "signed", G_TYPE_BOOLEAN, FALSE,
85       "rate", G_TYPE_INT, rate, "channels", G_TYPE_INT, channels, NULL);
86
87   return gst_pad_try_set_caps (mulawenc->srcpad, tempcaps);
88 }
89
90 GType
91 gst_mulawenc_get_type (void)
92 {
93   static GType mulawenc_type = 0;
94
95   if (!mulawenc_type) {
96     static const GTypeInfo mulawenc_info = {
97       sizeof (GstMuLawEncClass),
98       (GBaseInitFunc) gst_mulawenc_base_init,
99       NULL,
100       (GClassInitFunc) gst_mulawenc_class_init,
101       NULL,
102       NULL,
103       sizeof (GstMuLawEnc),
104       0,
105       (GInstanceInitFunc) gst_mulawenc_init,
106     };
107     mulawenc_type =
108         g_type_register_static (GST_TYPE_ELEMENT, "GstMuLawEnc", &mulawenc_info,
109         0);
110   }
111   return mulawenc_type;
112 }
113
114 static void
115 gst_mulawenc_base_init (GstMuLawEncClass * klass)
116 {
117   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
118
119   gst_element_class_add_pad_template (element_class, mulawenc_src_template);
120   gst_element_class_add_pad_template (element_class, mulawenc_sink_template);
121   gst_element_class_set_details (element_class, &mulawenc_details);
122 }
123
124 static void
125 gst_mulawenc_class_init (GstMuLawEncClass * klass)
126 {
127   GObjectClass *gobject_class;
128   GstElementClass *gstelement_class;
129
130   gobject_class = (GObjectClass *) klass;
131   gstelement_class = (GstElementClass *) klass;
132
133   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
134
135   gobject_class->set_property = gst_mulawenc_set_property;
136   gobject_class->get_property = gst_mulawenc_get_property;
137 }
138
139 static void
140 gst_mulawenc_init (GstMuLawEnc * mulawenc)
141 {
142   mulawenc->sinkpad =
143       gst_pad_new_from_template (mulawenc_sink_template, "sink");
144   mulawenc->srcpad = gst_pad_new_from_template (mulawenc_src_template, "src");
145   gst_pad_set_link_function (mulawenc->sinkpad, mulawenc_link);
146
147   gst_element_add_pad (GST_ELEMENT (mulawenc), mulawenc->sinkpad);
148   gst_pad_set_chain_function (mulawenc->sinkpad, gst_mulawenc_chain);
149   gst_element_add_pad (GST_ELEMENT (mulawenc), mulawenc->srcpad);
150 }
151
152 static void
153 gst_mulawenc_chain (GstPad * pad, GstData * _data)
154 {
155   GstBuffer *buf = GST_BUFFER (_data);
156   GstMuLawEnc *mulawenc;
157   gint16 *linear_data;
158   guint8 *mulaw_data;
159   GstBuffer *outbuf;
160
161   g_return_if_fail (pad != NULL);
162   g_return_if_fail (GST_IS_PAD (pad));
163   g_return_if_fail (buf != NULL);
164
165   mulawenc = GST_MULAWENC (GST_OBJECT_PARENT (pad));
166   g_return_if_fail (mulawenc != NULL);
167   g_return_if_fail (GST_IS_MULAWENC (mulawenc));
168
169   linear_data = (gint16 *) GST_BUFFER_DATA (buf);
170   outbuf = gst_buffer_new ();
171   GST_BUFFER_DATA (outbuf) =
172       (gchar *) g_new (gint16, GST_BUFFER_SIZE (buf) / 4);
173   GST_BUFFER_SIZE (outbuf) = GST_BUFFER_SIZE (buf) / 2;
174
175   mulaw_data = (gint8 *) GST_BUFFER_DATA (outbuf);
176   mulaw_encode (linear_data, mulaw_data, GST_BUFFER_SIZE (outbuf));
177
178   gst_buffer_unref (buf);
179   gst_pad_push (mulawenc->srcpad, GST_DATA (outbuf));
180 }
181
182 static void
183 gst_mulawenc_set_property (GObject * object, guint prop_id,
184     const GValue * value, GParamSpec * pspec)
185 {
186   GstMuLawEnc *mulawenc;
187
188   /* it's not null if we got it, but it might not be ours */
189   g_return_if_fail (GST_IS_MULAWENC (object));
190   mulawenc = GST_MULAWENC (object);
191
192   switch (prop_id) {
193     default:
194       break;
195   }
196 }
197
198 static void
199 gst_mulawenc_get_property (GObject * object, guint prop_id, GValue * value,
200     GParamSpec * pspec)
201 {
202   GstMuLawEnc *mulawenc;
203
204   /* it's not null if we got it, but it might not be ours */
205   g_return_if_fail (GST_IS_MULAWENC (object));
206   mulawenc = GST_MULAWENC (object);
207
208   switch (prop_id) {
209     default:
210       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
211       break;
212   }
213 }