1 /* GStreamer
2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
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 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-mulawdec
21 * @title: mulawdec
22 *
23 * This element decodes mulaw audio. Mulaw coding is also known as G.711.
24 */
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29 #include <gst/gst.h>
30
31 #include "mulaw-decode.h"
32 #include "mulaw-conversion.h"
33
34 extern GstStaticPadTemplate mulaw_dec_src_factory;
35 extern GstStaticPadTemplate mulaw_dec_sink_factory;
36
37 static gboolean gst_mulawdec_set_format (GstAudioDecoder * dec, GstCaps * caps);
38 static GstFlowReturn gst_mulawdec_handle_frame (GstAudioDecoder * dec,
39 GstBuffer * buffer);
40
41
42 /* Stereo signals and args */
43 enum
44 {
45 /* FILL ME */
46 LAST_SIGNAL
47 };
48
49 enum
50 {
51 PROP_0
52 };
53
54 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
55 #define INT_FORMAT "S16LE"
56 #else
57 #define INT_FORMAT "S16BE"
58 #endif
59
60 GstStaticPadTemplate mulaw_dec_src_factory = GST_STATIC_PAD_TEMPLATE ("src",
61 GST_PAD_SRC,
62 GST_PAD_ALWAYS,
63 GST_STATIC_CAPS ("audio/x-raw, "
64 "format = (string) " INT_FORMAT ", "
65 "layout = (string) interleaved, "
66 "rate = (int) [ 8000, 192000 ], " "channels = (int) [ 1, 2 ]")
67 );
68
69 GstStaticPadTemplate mulaw_dec_sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
70 GST_PAD_SINK,
71 GST_PAD_ALWAYS,
72 GST_STATIC_CAPS ("audio/x-mulaw, "
73 "rate = [ 8000 , 192000 ], " "channels = [ 1 , 2 ]")
74 );
75
76 #define gst_mulawdec_parent_class parent_class
77 G_DEFINE_TYPE (GstMuLawDec, gst_mulawdec, GST_TYPE_AUDIO_DECODER);
78 GST_ELEMENT_REGISTER_DEFINE (mulawdec, "mulawdec", GST_RANK_PRIMARY,
79 GST_TYPE_MULAWDEC);
80
81 static gboolean
gst_mulawdec_set_format(GstAudioDecoder * dec,GstCaps * caps)82 gst_mulawdec_set_format (GstAudioDecoder * dec, GstCaps * caps)
83 {
84 GstMuLawDec *mulawdec = GST_MULAWDEC (dec);
85 GstStructure *structure;
86 int rate, channels;
87 GstAudioInfo info;
88
89 structure = gst_caps_get_structure (caps, 0);
90 if (!structure) {
91 GST_ERROR ("failed to get structure from caps");
92 goto error_failed_get_structure;
93 }
94
95 if (!gst_structure_get_int (structure, "rate", &rate)) {
96 GST_ERROR ("failed to find field rate in input caps");
97 goto error_failed_find_rate;
98 }
99
100 if (!gst_structure_get_int (structure, "channels", &channels)) {
101 GST_ERROR ("failed to find field channels in input caps");
102 goto error_failed_find_channel;
103 }
104
105 gst_audio_info_init (&info);
106 gst_audio_info_set_format (&info, GST_AUDIO_FORMAT_S16, rate, channels, NULL);
107
108 GST_DEBUG_OBJECT (mulawdec, "rate=%d, channels=%d", rate, channels);
109
110 return gst_audio_decoder_set_output_format (dec, &info);
111
112 error_failed_find_channel:
113 error_failed_find_rate:
114 error_failed_get_structure:
115 return FALSE;
116 }
117
118 static GstFlowReturn
gst_mulawdec_handle_frame(GstAudioDecoder * dec,GstBuffer * buffer)119 gst_mulawdec_handle_frame (GstAudioDecoder * dec, GstBuffer * buffer)
120 {
121 GstMapInfo inmap, outmap;
122 gint16 *linear_data;
123 guint8 *mulaw_data;
124 gsize mulaw_size, linear_size;
125 GstBuffer *outbuf;
126
127 if (!buffer) {
128 return GST_FLOW_OK;
129 }
130
131 if (!gst_buffer_map (buffer, &inmap, GST_MAP_READ)) {
132 GST_ERROR ("failed to map input buffer");
133 goto error_failed_map_input_buffer;
134 }
135
136 mulaw_data = inmap.data;
137 mulaw_size = inmap.size;
138
139 linear_size = mulaw_size * 2;
140
141 outbuf = gst_audio_decoder_allocate_output_buffer (dec, linear_size);
142 if (!gst_buffer_map (outbuf, &outmap, GST_MAP_WRITE)) {
143 GST_ERROR ("failed to map input buffer");
144 goto error_failed_map_output_buffer;
145 }
146
147 linear_data = (gint16 *) outmap.data;
148
149 mulaw_decode (mulaw_data, linear_data, mulaw_size);
150
151 gst_buffer_unmap (outbuf, &outmap);
152 gst_buffer_unmap (buffer, &inmap);
153
154 return gst_audio_decoder_finish_frame (dec, outbuf, -1);
155
156 error_failed_map_output_buffer:
157 gst_buffer_unref (outbuf);
158 gst_buffer_unmap (buffer, &inmap);
159
160 error_failed_map_input_buffer:
161 return GST_FLOW_ERROR;
162 }
163
164 static gboolean
gst_mulawdec_start(GstAudioDecoder * dec)165 gst_mulawdec_start (GstAudioDecoder * dec)
166 {
167 gst_audio_decoder_set_estimate_rate (dec, TRUE);
168
169 return TRUE;
170 }
171
172 static void
gst_mulawdec_class_init(GstMuLawDecClass * klass)173 gst_mulawdec_class_init (GstMuLawDecClass * klass)
174 {
175 GstElementClass *element_class = (GstElementClass *) klass;
176 GstAudioDecoderClass *audiodec_class = GST_AUDIO_DECODER_CLASS (klass);
177
178 gst_element_class_add_static_pad_template (element_class,
179 &mulaw_dec_src_factory);
180 gst_element_class_add_static_pad_template (element_class,
181 &mulaw_dec_sink_factory);
182
183
184 audiodec_class->start = GST_DEBUG_FUNCPTR (gst_mulawdec_start);
185 audiodec_class->set_format = GST_DEBUG_FUNCPTR (gst_mulawdec_set_format);
186 audiodec_class->handle_frame = GST_DEBUG_FUNCPTR (gst_mulawdec_handle_frame);
187
188 gst_element_class_set_static_metadata (element_class, "Mu Law audio decoder",
189 "Codec/Decoder/Audio",
190 "Convert 8bit mu law to 16bit PCM",
191 "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
192 }
193
194 static void
gst_mulawdec_init(GstMuLawDec * mulawdec)195 gst_mulawdec_init (GstMuLawDec * mulawdec)
196 {
197 gst_audio_decoder_set_needs_format (GST_AUDIO_DECODER (mulawdec), TRUE);
198 gst_audio_decoder_set_use_default_pad_acceptcaps (GST_AUDIO_DECODER_CAST
199 (mulawdec), TRUE);
200 GST_PAD_SET_ACCEPT_TEMPLATE (GST_AUDIO_DECODER_SINK_PAD (mulawdec));
201 }
202