1 /* GStreamer A-Law to PCM conversion
2  * Copyright (C) 2000 by Abramo Bagnara <abramo@alsa-project.org>
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.1 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., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 /**
20  * SECTION:element-alawdec
21  *
22  * This element decodes alaw audio. Alaw coding is also known as G.711.
23  */
24 
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28 
29 #include "alaw-decode.h"
30 
31 extern GstStaticPadTemplate alaw_dec_src_factory;
32 extern GstStaticPadTemplate alaw_dec_sink_factory;
33 
34 GST_DEBUG_CATEGORY_STATIC (alaw_dec_debug);
35 #define GST_CAT_DEFAULT alaw_dec_debug
36 
37 static gboolean gst_alaw_dec_set_format (GstAudioDecoder * dec, GstCaps * caps);
38 static GstFlowReturn gst_alaw_dec_handle_frame (GstAudioDecoder * dec,
39     GstBuffer * buffer);
40 
41 #define gst_alaw_dec_parent_class parent_class
42 G_DEFINE_TYPE (GstALawDec, gst_alaw_dec, GST_TYPE_AUDIO_DECODER);
43 
44 /* some day we might have defines in gstconfig.h that tell us about the
45  * desired cpu/memory/binary size trade-offs */
46 #define GST_ALAW_DEC_USE_TABLE
47 
48 #ifdef GST_ALAW_DEC_USE_TABLE
49 
50 static const gint alaw_to_s16_table[256] = {
51   -5504, -5248, -6016, -5760, -4480, -4224, -4992, -4736,
52   -7552, -7296, -8064, -7808, -6528, -6272, -7040, -6784,
53   -2752, -2624, -3008, -2880, -2240, -2112, -2496, -2368,
54   -3776, -3648, -4032, -3904, -3264, -3136, -3520, -3392,
55   -22016, -20992, -24064, -23040, -17920, -16896, -19968, -18944,
56   -30208, -29184, -32256, -31232, -26112, -25088, -28160, -27136,
57   -11008, -10496, -12032, -11520, -8960, -8448, -9984, -9472,
58   -15104, -14592, -16128, -15616, -13056, -12544, -14080, -13568,
59   -344, -328, -376, -360, -280, -264, -312, -296,
60   -472, -456, -504, -488, -408, -392, -440, -424,
61   -88, -72, -120, -104, -24, -8, -56, -40,
62   -216, -200, -248, -232, -152, -136, -184, -168,
63   -1376, -1312, -1504, -1440, -1120, -1056, -1248, -1184,
64   -1888, -1824, -2016, -1952, -1632, -1568, -1760, -1696,
65   -688, -656, -752, -720, -560, -528, -624, -592,
66   -944, -912, -1008, -976, -816, -784, -880, -848,
67   5504, 5248, 6016, 5760, 4480, 4224, 4992, 4736,
68   7552, 7296, 8064, 7808, 6528, 6272, 7040, 6784,
69   2752, 2624, 3008, 2880, 2240, 2112, 2496, 2368,
70   3776, 3648, 4032, 3904, 3264, 3136, 3520, 3392,
71   22016, 20992, 24064, 23040, 17920, 16896, 19968, 18944,
72   30208, 29184, 32256, 31232, 26112, 25088, 28160, 27136,
73   11008, 10496, 12032, 11520, 8960, 8448, 9984, 9472,
74   15104, 14592, 16128, 15616, 13056, 12544, 14080, 13568,
75   344, 328, 376, 360, 280, 264, 312, 296,
76   472, 456, 504, 488, 408, 392, 440, 424,
77   88, 72, 120, 104, 24, 8, 56, 40,
78   216, 200, 248, 232, 152, 136, 184, 168,
79   1376, 1312, 1504, 1440, 1120, 1056, 1248, 1184,
80   1888, 1824, 2016, 1952, 1632, 1568, 1760, 1696,
81   688, 656, 752, 720, 560, 528, 624, 592,
82   944, 912, 1008, 976, 816, 784, 880, 848
83 };
84 
85 static inline gint
alaw_to_s16(guint8 a_val)86 alaw_to_s16 (guint8 a_val)
87 {
88   return alaw_to_s16_table[a_val];
89 }
90 
91 #else /* GST_ALAW_DEC_USE_TABLE */
92 
93 static inline gint
alaw_to_s16(guint8 a_val)94 alaw_to_s16 (guint8 a_val)
95 {
96   gint t;
97   gint seg;
98 
99   a_val ^= 0x55;
100   t = a_val & 0x7f;
101   if (t < 16)
102     t = (t << 4) + 8;
103   else {
104     seg = (t >> 4) & 0x07;
105     t = ((t & 0x0f) << 4) + 0x108;
106     t <<= seg - 1;
107   }
108   return ((a_val & 0x80) ? t : -t);
109 }
110 
111 #endif /* GST_ALAW_DEC_USE_TABLE */
112 
113 static gboolean
gst_alaw_dec_set_format(GstAudioDecoder * dec,GstCaps * caps)114 gst_alaw_dec_set_format (GstAudioDecoder * dec, GstCaps * caps)
115 {
116   GstALawDec *alawdec = GST_ALAW_DEC (dec);
117   GstStructure *structure;
118   int rate, channels;
119   GstAudioInfo info;
120 
121   structure = gst_caps_get_structure (caps, 0);
122   if (!structure) {
123     GST_ERROR_OBJECT (dec, "failed to get structure from caps");
124     return FALSE;
125   }
126 
127   if (!gst_structure_get_int (structure, "rate", &rate)) {
128     GST_ERROR_OBJECT (dec, "failed to find field rate in input caps");
129     return FALSE;
130   }
131 
132   if (!gst_structure_get_int (structure, "channels", &channels)) {
133     GST_ERROR_OBJECT (dec, "failed to find field channels in input caps");
134     return FALSE;
135   }
136 
137   gst_audio_info_init (&info);
138   gst_audio_info_set_format (&info, GST_AUDIO_FORMAT_S16, rate, channels, NULL);
139 
140   GST_DEBUG_OBJECT (alawdec, "rate=%d, channels=%d", rate, channels);
141 
142   return gst_audio_decoder_set_output_format (dec, &info);
143 }
144 
145 static GstFlowReturn
gst_alaw_dec_handle_frame(GstAudioDecoder * dec,GstBuffer * buffer)146 gst_alaw_dec_handle_frame (GstAudioDecoder * dec, GstBuffer * buffer)
147 {
148   GstMapInfo inmap, outmap;
149   gint16 *linear_data;
150   guint8 *alaw_data;
151   gsize alaw_size, linear_size;
152   GstBuffer *outbuf;
153   gint i;
154 
155   if (!buffer) {
156     return GST_FLOW_OK;
157   }
158 
159   if (!gst_buffer_map (buffer, &inmap, GST_MAP_READ)) {
160     GST_ERROR_OBJECT (dec, "failed to map input buffer");
161     goto error_failed_map_input_buffer;
162   }
163 
164   alaw_data = inmap.data;
165   alaw_size = inmap.size;
166 
167   linear_size = alaw_size * 2;
168 
169   outbuf = gst_audio_decoder_allocate_output_buffer (dec, linear_size);
170   if (!gst_buffer_map (outbuf, &outmap, GST_MAP_WRITE)) {
171     GST_ERROR_OBJECT (dec, "failed to map input buffer");
172     goto error_failed_map_output_buffer;
173   }
174 
175   linear_data = (gint16 *) outmap.data;
176   for (i = 0; i < alaw_size; i++) {
177     linear_data[i] = alaw_to_s16 (alaw_data[i]);
178   }
179 
180   gst_buffer_unmap (outbuf, &outmap);
181   gst_buffer_unmap (buffer, &inmap);
182 
183   return gst_audio_decoder_finish_frame (dec, outbuf, -1);
184 
185 error_failed_map_output_buffer:
186   gst_buffer_unref (outbuf);
187   gst_buffer_unmap (buffer, &inmap);
188 
189 error_failed_map_input_buffer:
190   return GST_FLOW_ERROR;
191 }
192 
193 static gboolean
gst_alaw_dec_start(GstAudioDecoder * dec)194 gst_alaw_dec_start (GstAudioDecoder * dec)
195 {
196   gst_audio_decoder_set_estimate_rate (dec, TRUE);
197 
198   return TRUE;
199 }
200 
201 static void
gst_alaw_dec_class_init(GstALawDecClass * klass)202 gst_alaw_dec_class_init (GstALawDecClass * klass)
203 {
204   GstElementClass *element_class = (GstElementClass *) klass;
205   GstAudioDecoderClass *audiodec_class = GST_AUDIO_DECODER_CLASS (klass);
206 
207   gst_element_class_add_static_pad_template (element_class,
208       &alaw_dec_src_factory);
209   gst_element_class_add_static_pad_template (element_class,
210       &alaw_dec_sink_factory);
211 
212   audiodec_class->start = GST_DEBUG_FUNCPTR (gst_alaw_dec_start);
213   audiodec_class->set_format = GST_DEBUG_FUNCPTR (gst_alaw_dec_set_format);
214   audiodec_class->handle_frame = GST_DEBUG_FUNCPTR (gst_alaw_dec_handle_frame);
215 
216   gst_element_class_set_static_metadata (element_class, "A Law audio decoder",
217       "Codec/Decoder/Audio",
218       "Convert 8bit A law to 16bit PCM",
219       "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
220 
221   GST_DEBUG_CATEGORY_INIT (alaw_dec_debug, "alawdec", 0, "A Law audio decoder");
222 }
223 
224 static void
gst_alaw_dec_init(GstALawDec * alawdec)225 gst_alaw_dec_init (GstALawDec * alawdec)
226 {
227   gst_audio_decoder_set_needs_format (GST_AUDIO_DECODER (alawdec), TRUE);
228   gst_audio_decoder_set_use_default_pad_acceptcaps (GST_AUDIO_DECODER_CAST
229       (alawdec), TRUE);
230   GST_PAD_SET_ACCEPT_TEMPLATE (GST_AUDIO_DECODER_SINK_PAD (alawdec));
231 }
232