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 "depth", GST_PROPS_INT (8),
131 "width", GST_PROPS_INT (8),
132 "signed", GST_PROPS_BOOLEAN (FALSE),
133 "rate", GST_PROPS_INT (rate),
134 "channels", GST_PROPS_INT (channels),
137 return gst_pad_try_set_caps (alawenc->srcpad, tempcaps);
141 gst_alawenc_get_type(void) {
142 static GType alawenc_type = 0;
145 static const GTypeInfo alawenc_info = {
146 sizeof(GstALawEncClass), NULL,
148 (GClassInitFunc)gst_alawenc_class_init,
153 (GInstanceInitFunc)gst_alawenc_init,
155 alawenc_type = g_type_register_static(GST_TYPE_ELEMENT, "GstALawEnc", &alawenc_info, 0);
161 gst_alawenc_class_init (GstALawEncClass *klass)
163 GObjectClass *gobject_class;
164 GstElementClass *gstelement_class;
166 gobject_class = (GObjectClass*)klass;
167 gstelement_class = (GstElementClass*)klass;
169 parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
171 gobject_class->set_property = gst_alawenc_set_property;
172 gobject_class->get_property = gst_alawenc_get_property;
176 gst_alawenc_init (GstALawEnc *alawenc)
178 alawenc->sinkpad = gst_pad_new_from_template(alawenc_sink_template,"sink");
179 alawenc->srcpad = gst_pad_new_from_template(alawenc_src_template,"src");
180 gst_pad_set_link_function (alawenc->sinkpad, alawenc_link);
182 gst_element_add_pad(GST_ELEMENT(alawenc),alawenc->sinkpad);
183 gst_pad_set_chain_function(alawenc->sinkpad,gst_alawenc_chain);
184 gst_element_add_pad(GST_ELEMENT(alawenc),alawenc->srcpad);
188 gst_alawenc_chain (GstPad *pad,GstBuffer *buf)
196 g_return_if_fail(pad != NULL);
197 g_return_if_fail(GST_IS_PAD(pad));
198 g_return_if_fail(buf != NULL);
200 alawenc = GST_ALAWENC(GST_OBJECT_PARENT (pad));
201 g_return_if_fail(alawenc != NULL);
202 g_return_if_fail(GST_IS_ALAWENC(alawenc));
204 linear_data = (gint16 *)GST_BUFFER_DATA(buf);
205 outbuf=gst_buffer_new();
206 GST_BUFFER_DATA(outbuf) = (gchar*)g_new(guint8,GST_BUFFER_SIZE(buf)/2);
207 GST_BUFFER_SIZE(outbuf) = GST_BUFFER_SIZE(buf)/2;
210 alaw_data = (guint8*)GST_BUFFER_DATA(outbuf);
211 for (i = 0; i < GST_BUFFER_SIZE(outbuf); i++) {
212 *alaw_data = s16_to_alaw (*linear_data);
216 gst_buffer_unref(buf);
217 gst_pad_push(alawenc->srcpad,outbuf);
221 gst_alawenc_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
225 /* it's not null if we got it, but it might not be ours */
226 g_return_if_fail(GST_IS_ALAWENC(object));
227 alawenc = GST_ALAWENC(object);
236 gst_alawenc_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
240 /* it's not null if we got it, but it might not be ours */
241 g_return_if_fail(GST_IS_ALAWENC(object));
242 alawenc = GST_ALAWENC(object);
246 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);