1 /* GStreamer A-Law to PCM conversion
2 * Copyright (C) 2000 by Abramo Bagnara <abramo@alsa-project.org>
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.
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.
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.
20 * SECTION:element-alawdec
22 * This element decodes alaw audio. Alaw coding is also known as G.711.
29 #include "alaw-decode.h"
31 extern GstStaticPadTemplate alaw_dec_src_factory;
32 extern GstStaticPadTemplate alaw_dec_sink_factory;
34 GST_DEBUG_CATEGORY_STATIC (alaw_dec_debug);
35 #define GST_CAT_DEFAULT alaw_dec_debug
37 static gboolean gst_alaw_dec_set_format (GstAudioDecoder * dec, GstCaps * caps);
38 static GstFlowReturn gst_alaw_dec_handle_frame (GstAudioDecoder * dec,
41 #define gst_alaw_dec_parent_class parent_class
42 G_DEFINE_TYPE (GstALawDec, gst_alaw_dec, GST_TYPE_AUDIO_DECODER);
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
48 #ifdef GST_ALAW_DEC_USE_TABLE
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
86 alaw_to_s16 (guint8 a_val)
88 return alaw_to_s16_table[a_val];
91 #else /* GST_ALAW_DEC_USE_TABLE */
94 alaw_to_s16 (guint8 a_val)
104 seg = (t >> 4) & 0x07;
105 t = ((t & 0x0f) << 4) + 0x108;
108 return ((a_val & 0x80) ? t : -t);
111 #endif /* GST_ALAW_DEC_USE_TABLE */
114 gst_alaw_dec_set_format (GstAudioDecoder * dec, GstCaps * caps)
116 GstALawDec *alawdec = GST_ALAW_DEC (dec);
117 GstStructure *structure;
121 structure = gst_caps_get_structure (caps, 0);
123 GST_ERROR_OBJECT (dec, "failed to get structure from caps");
127 if (!gst_structure_get_int (structure, "rate", &rate)) {
128 GST_ERROR_OBJECT (dec, "failed to find field rate in input caps");
132 if (!gst_structure_get_int (structure, "channels", &channels)) {
133 GST_ERROR_OBJECT (dec, "failed to find field channels in input caps");
137 gst_audio_info_init (&info);
138 gst_audio_info_set_format (&info, GST_AUDIO_FORMAT_S16, rate, channels, NULL);
140 GST_DEBUG_OBJECT (alawdec, "rate=%d, channels=%d", rate, channels);
142 return gst_audio_decoder_set_output_format (dec, &info);
146 gst_alaw_dec_handle_frame (GstAudioDecoder * dec, GstBuffer * buffer)
148 GstMapInfo inmap, outmap;
151 gsize alaw_size, linear_size;
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;
164 alaw_data = inmap.data;
165 alaw_size = inmap.size;
167 linear_size = alaw_size * 2;
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;
175 linear_data = (gint16 *) outmap.data;
176 for (i = 0; i < alaw_size; i++) {
177 linear_data[i] = alaw_to_s16 (alaw_data[i]);
180 gst_buffer_unmap (outbuf, &outmap);
181 gst_buffer_unmap (buffer, &inmap);
183 return gst_audio_decoder_finish_frame (dec, outbuf, -1);
185 error_failed_map_output_buffer:
186 gst_buffer_unref (outbuf);
188 error_failed_map_input_buffer:
189 return GST_FLOW_ERROR;
193 gst_alaw_dec_start (GstAudioDecoder * dec)
195 gst_audio_decoder_set_estimate_rate (dec, TRUE);
201 gst_alaw_dec_class_init (GstALawDecClass * klass)
203 GstElementClass *element_class = (GstElementClass *) klass;
204 GstAudioDecoderClass *audiodec_class = GST_AUDIO_DECODER_CLASS (klass);
206 gst_element_class_add_pad_template (element_class,
207 gst_static_pad_template_get (&alaw_dec_src_factory));
208 gst_element_class_add_pad_template (element_class,
209 gst_static_pad_template_get (&alaw_dec_sink_factory));
211 audiodec_class->start = GST_DEBUG_FUNCPTR (gst_alaw_dec_start);
212 audiodec_class->set_format = GST_DEBUG_FUNCPTR (gst_alaw_dec_set_format);
213 audiodec_class->handle_frame = GST_DEBUG_FUNCPTR (gst_alaw_dec_handle_frame);
215 gst_element_class_set_static_metadata (element_class, "A Law audio decoder",
216 "Codec/Decoder/Audio",
217 "Convert 8bit A law to 16bit PCM",
218 "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
220 GST_DEBUG_CATEGORY_INIT (alaw_dec_debug, "alawdec", 0, "A Law audio decoder");
224 gst_alaw_dec_init (GstALawDec * alawdec)
226 gst_audio_decoder_set_needs_format (GST_AUDIO_DECODER (alawdec), TRUE);