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>
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.
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.
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.
23 #include "alaw-encode.h"
25 extern GstPadTemplate *alawenc_src_template, *alawenc_sink_template;
28 /* Stereo signals and args */
38 static void gst_alawenc_class_init (GstALawEncClass *klass);
39 static void gst_alawenc_init (GstALawEnc *alawenc);
41 static void gst_alawenc_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
42 static void gst_alawenc_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
44 static void gst_alawenc_chain (GstPad *pad, GstBuffer *buf);
47 * s16_to_alaw() - Convert a 16-bit linear PCM value to 8-bit A-law
49 * s16_to_alaw() accepts an 16-bit integer and encodes it as A-law data.
51 * Linear Input Code Compressed Code
52 * ------------------------ ---------------
53 * 0000000wxyza 000wxyz
54 * 0000001wxyza 001wxyz
55 * 000001wxyzab 010wxyz
56 * 00001wxyzabc 011wxyz
57 * 0001wxyzabcd 100wxyz
58 * 001wxyzabcde 101wxyz
59 * 01wxyzabcdef 110wxyz
60 * 1wxyzabcdefg 111wxyz
62 * For further information see John C. Bellamy's Digital Telephony, 1982,
63 * John Wiley & Sons, pps 98-111 and 472-476.
66 static inline gint val_seg(gint val)
83 static guint8 s16_to_alaw(gint pcm_val)
101 /* Convert the scaled magnitude to segment number. */
102 seg = val_seg(pcm_val);
103 aval = (seg << 4) | ((pcm_val >> (seg + 3)) & 0x0f);
108 static GstElementClass *parent_class = NULL;
109 /*static guint gst_stereo_signals[LAST_SIGNAL] = { 0 }; */
111 static GstPadLinkReturn
112 alawenc_link (GstPad *pad, GstCaps *caps)
117 GstALawEnc* alawenc = GST_ALAWENC (GST_OBJECT_PARENT (pad));
119 if (!GST_CAPS_IS_FIXED (caps))
120 return GST_PAD_LINK_DELAYED;
122 if (!gst_caps_get (caps, "rate", &rate,
123 "channels", &channels,
125 return GST_PAD_LINK_DELAYED;
127 tempcaps = GST_CAPS_NEW (
130 "format", GST_PROPS_STRING ("int"),
131 "law", GST_PROPS_INT (2),
132 "depth", GST_PROPS_INT (8),
133 "width", GST_PROPS_INT (8),
134 "signed", GST_PROPS_BOOLEAN (FALSE),
135 "rate", GST_PROPS_INT (rate),
136 "channels", GST_PROPS_INT (channels),
139 return gst_pad_try_set_caps (alawenc->srcpad, tempcaps);
143 gst_alawenc_get_type(void) {
144 static GType alawenc_type = 0;
147 static const GTypeInfo alawenc_info = {
148 sizeof(GstALawEncClass), NULL,
150 (GClassInitFunc)gst_alawenc_class_init,
155 (GInstanceInitFunc)gst_alawenc_init,
157 alawenc_type = g_type_register_static(GST_TYPE_ELEMENT, "GstALawEnc", &alawenc_info, 0);
163 gst_alawenc_class_init (GstALawEncClass *klass)
165 GObjectClass *gobject_class;
166 GstElementClass *gstelement_class;
168 gobject_class = (GObjectClass*)klass;
169 gstelement_class = (GstElementClass*)klass;
171 parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
173 gobject_class->set_property = gst_alawenc_set_property;
174 gobject_class->get_property = gst_alawenc_get_property;
178 gst_alawenc_init (GstALawEnc *alawenc)
180 alawenc->sinkpad = gst_pad_new_from_template(alawenc_sink_template,"sink");
181 alawenc->srcpad = gst_pad_new_from_template(alawenc_src_template,"src");
182 gst_pad_set_link_function (alawenc->sinkpad, alawenc_link);
184 gst_element_add_pad(GST_ELEMENT(alawenc),alawenc->sinkpad);
185 gst_pad_set_chain_function(alawenc->sinkpad,gst_alawenc_chain);
186 gst_element_add_pad(GST_ELEMENT(alawenc),alawenc->srcpad);
190 gst_alawenc_chain (GstPad *pad,GstBuffer *buf)
198 g_return_if_fail(pad != NULL);
199 g_return_if_fail(GST_IS_PAD(pad));
200 g_return_if_fail(buf != NULL);
202 alawenc = GST_ALAWENC(GST_OBJECT_PARENT (pad));
203 g_return_if_fail(alawenc != NULL);
204 g_return_if_fail(GST_IS_ALAWENC(alawenc));
206 linear_data = (gint16 *)GST_BUFFER_DATA(buf);
207 outbuf=gst_buffer_new();
208 GST_BUFFER_DATA(outbuf) = (gchar*)g_new(guint8,GST_BUFFER_SIZE(buf)/2);
209 GST_BUFFER_SIZE(outbuf) = GST_BUFFER_SIZE(buf)/2;
212 alaw_data = (guint8*)GST_BUFFER_DATA(outbuf);
213 for (i = 0; i < GST_BUFFER_SIZE(outbuf); i++) {
214 *alaw_data = s16_to_alaw (*linear_data);
218 gst_buffer_unref(buf);
219 gst_pad_push(alawenc->srcpad,outbuf);
223 gst_alawenc_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
227 /* it's not null if we got it, but it might not be ours */
228 g_return_if_fail(GST_IS_ALAWENC(object));
229 alawenc = GST_ALAWENC(object);
238 gst_alawenc_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
242 /* it's not null if we got it, but it might not be ours */
243 g_return_if_fail(GST_IS_ALAWENC(object));
244 alawenc = GST_ALAWENC(object);
248 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);