2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
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.
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-mulawdec
22 * This element decodes mulaw audio. Mulaw coding is also known as G.711.
30 #include "mulaw-decode.h"
31 #include "mulaw-conversion.h"
33 extern GstStaticPadTemplate mulaw_dec_src_factory;
34 extern GstStaticPadTemplate mulaw_dec_sink_factory;
36 static gboolean gst_mulawdec_set_format (GstAudioDecoder * dec, GstCaps * caps);
37 static GstFlowReturn gst_mulawdec_handle_frame (GstAudioDecoder * dec,
41 /* Stereo signals and args */
53 #define gst_mulawdec_parent_class parent_class
54 G_DEFINE_TYPE (GstMuLawDec, gst_mulawdec, GST_TYPE_AUDIO_DECODER);
57 gst_mulawdec_set_format (GstAudioDecoder * dec, GstCaps * caps)
59 GstMuLawDec *mulawdec = GST_MULAWDEC (dec);
60 GstStructure *structure;
64 structure = gst_caps_get_structure (caps, 0);
66 GST_ERROR ("failed to get structure from caps");
67 goto error_failed_get_structure;
70 if (!gst_structure_get_int (structure, "rate", &rate)) {
71 GST_ERROR ("failed to find field rate in input caps");
72 goto error_failed_find_rate;
75 if (!gst_structure_get_int (structure, "channels", &channels)) {
76 GST_ERROR ("failed to find field channels in input caps");
77 goto error_failed_find_channel;
80 gst_audio_info_init (&info);
81 gst_audio_info_set_format (&info, GST_AUDIO_FORMAT_S16, rate, channels, NULL);
83 GST_DEBUG_OBJECT (mulawdec, "rate=%d, channels=%d", rate, channels);
85 return gst_audio_decoder_set_output_format (dec, &info);
87 error_failed_find_channel:
88 error_failed_find_rate:
89 error_failed_get_structure:
94 gst_mulawdec_handle_frame (GstAudioDecoder * dec, GstBuffer * buffer)
96 GstMapInfo inmap, outmap;
99 gsize mulaw_size, linear_size;
106 if (!gst_buffer_map (buffer, &inmap, GST_MAP_READ)) {
107 GST_ERROR ("failed to map input buffer");
108 goto error_failed_map_input_buffer;
111 mulaw_data = inmap.data;
112 mulaw_size = inmap.size;
114 linear_size = mulaw_size * 2;
116 outbuf = gst_audio_decoder_allocate_output_buffer (dec, linear_size);
117 if (!gst_buffer_map (outbuf, &outmap, GST_MAP_WRITE)) {
118 GST_ERROR ("failed to map input buffer");
119 goto error_failed_map_output_buffer;
122 linear_data = (gint16 *) outmap.data;
124 mulaw_decode (mulaw_data, linear_data, mulaw_size);
126 gst_buffer_unmap (outbuf, &outmap);
127 gst_buffer_unmap (buffer, &inmap);
129 return gst_audio_decoder_finish_frame (dec, outbuf, -1);
131 error_failed_map_output_buffer:
132 gst_buffer_unref (outbuf);
134 error_failed_map_input_buffer:
135 return GST_FLOW_ERROR;
139 gst_mulawdec_start (GstAudioDecoder * dec)
141 gst_audio_decoder_set_estimate_rate (dec, TRUE);
147 gst_mulawdec_class_init (GstMuLawDecClass * klass)
149 GstElementClass *element_class = (GstElementClass *) klass;
150 GstAudioDecoderClass *audiodec_class = GST_AUDIO_DECODER_CLASS (klass);
152 gst_element_class_add_pad_template (element_class,
153 gst_static_pad_template_get (&mulaw_dec_src_factory));
154 gst_element_class_add_pad_template (element_class,
155 gst_static_pad_template_get (&mulaw_dec_sink_factory));
158 audiodec_class->start = GST_DEBUG_FUNCPTR (gst_mulawdec_start);
159 audiodec_class->set_format = GST_DEBUG_FUNCPTR (gst_mulawdec_set_format);
160 audiodec_class->handle_frame = GST_DEBUG_FUNCPTR (gst_mulawdec_handle_frame);
162 gst_element_class_set_static_metadata (element_class, "Mu Law audio decoder",
163 "Codec/Decoder/Audio",
164 "Convert 8bit mu law to 16bit PCM",
165 "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
169 gst_mulawdec_init (GstMuLawDec * mulawdec)
171 gst_audio_decoder_set_needs_format (GST_AUDIO_DECODER (mulawdec), TRUE);
172 gst_audio_decoder_set_use_default_pad_acceptcaps (GST_AUDIO_DECODER_CAST
174 GST_PAD_SET_ACCEPT_TEMPLATE (GST_AUDIO_DECODER_SINK_PAD (mulawdec));