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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, 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 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
34 #define INT_FORMAT "S16LE"
36 #define INT_FORMAT "S16BE"
39 extern GstStaticPadTemplate mulaw_dec_src_factory;
40 extern GstStaticPadTemplate mulaw_dec_sink_factory;
42 /* Stereo signals and args */
54 static GstStateChangeReturn
55 gst_mulawdec_change_state (GstElement * element, GstStateChange transition);
57 static gboolean gst_mulawdec_event (GstPad * pad, GstObject * parent,
59 static GstFlowReturn gst_mulawdec_chain (GstPad * pad, GstObject * parent,
62 #define gst_mulawdec_parent_class parent_class
63 G_DEFINE_TYPE (GstMuLawDec, gst_mulawdec, GST_TYPE_ELEMENT);
66 mulawdec_setcaps (GstMuLawDec * mulawdec, GstCaps * caps)
68 GstStructure *structure;
73 structure = gst_caps_get_structure (caps, 0);
74 ret = gst_structure_get_int (structure, "rate", &rate);
75 ret = ret && gst_structure_get_int (structure, "channels", &channels);
79 outcaps = gst_caps_new_simple ("audio/x-raw",
80 "format", G_TYPE_STRING, INT_FORMAT,
81 "rate", G_TYPE_INT, rate, "channels", G_TYPE_INT, channels, NULL);
82 ret = gst_pad_set_caps (mulawdec->srcpad, outcaps);
83 gst_caps_unref (outcaps);
86 GST_DEBUG_OBJECT (mulawdec, "rate=%d, channels=%d", rate, channels);
87 mulawdec->rate = rate;
88 mulawdec->channels = channels;
94 mulawdec_getcaps (GstPad * pad, GstCaps * filter)
96 GstMuLawDec *mulawdec;
98 GstCaps *othercaps, *result;
103 mulawdec = GST_MULAWDEC (GST_PAD_PARENT (pad));
105 /* figure out the name of the caps we are going to return */
106 if (pad == mulawdec->srcpad) {
107 name = "audio/x-raw";
108 otherpad = mulawdec->sinkpad;
110 name = "audio/x-mulaw";
111 otherpad = mulawdec->srcpad;
113 /* get caps from the peer, this can return NULL when there is no peer */
114 othercaps = gst_pad_peer_query_caps (otherpad, filter);
116 /* get the template caps to make sure we return something acceptable */
117 templ = gst_pad_get_pad_template_caps (pad);
120 /* there was a peer */
121 othercaps = gst_caps_make_writable (othercaps);
123 /* go through the caps and remove the fields we don't want */
124 for (i = 0; i < gst_caps_get_size (othercaps); i++) {
125 GstStructure *structure;
127 structure = gst_caps_get_structure (othercaps, i);
129 /* adjust the name */
130 gst_structure_set_name (structure, name);
132 if (pad == mulawdec->sinkpad) {
133 /* remove the fields we don't want */
134 gst_structure_remove_fields (structure, "format", NULL);
136 /* add fixed fields */
137 gst_structure_set (structure, "format", G_TYPE_STRING, INT_FORMAT,
141 /* filter against the allowed caps of the pad to return our result */
142 result = gst_caps_intersect (othercaps, templ);
143 gst_caps_unref (othercaps);
145 /* there was no peer, return the template caps */
146 result = gst_caps_copy (templ);
152 gst_mulawdec_query (GstPad * pad, GstObject * parent, GstQuery * query)
156 switch (GST_QUERY_TYPE (query)) {
159 GstCaps *filter, *caps;
161 gst_query_parse_caps (query, &filter);
162 caps = mulawdec_getcaps (pad, filter);
163 gst_query_set_caps_result (query, caps);
164 gst_caps_unref (caps);
170 res = gst_pad_query_default (pad, parent, query);
177 gst_mulawdec_class_init (GstMuLawDecClass * klass)
179 GstElementClass *element_class = (GstElementClass *) klass;
181 gst_element_class_add_pad_template (element_class,
182 gst_static_pad_template_get (&mulaw_dec_src_factory));
183 gst_element_class_add_pad_template (element_class,
184 gst_static_pad_template_get (&mulaw_dec_sink_factory));
186 gst_element_class_set_details_simple (element_class, "Mu Law audio decoder",
187 "Codec/Decoder/Audio",
188 "Convert 8bit mu law to 16bit PCM",
189 "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
191 element_class->change_state = GST_DEBUG_FUNCPTR (gst_mulawdec_change_state);
195 gst_mulawdec_init (GstMuLawDec * mulawdec)
198 gst_pad_new_from_static_template (&mulaw_dec_sink_factory, "sink");
199 gst_pad_set_query_function (mulawdec->sinkpad, gst_mulawdec_query);
200 gst_pad_set_event_function (mulawdec->sinkpad, gst_mulawdec_event);
201 gst_pad_set_chain_function (mulawdec->sinkpad, gst_mulawdec_chain);
202 gst_element_add_pad (GST_ELEMENT (mulawdec), mulawdec->sinkpad);
205 gst_pad_new_from_static_template (&mulaw_dec_src_factory, "src");
206 gst_pad_set_query_function (mulawdec->srcpad, gst_mulawdec_query);
207 gst_element_add_pad (GST_ELEMENT (mulawdec), mulawdec->srcpad);
211 gst_mulawdec_event (GstPad * pad, GstObject * parent, GstEvent * event)
213 GstMuLawDec *mulawdec;
216 mulawdec = GST_MULAWDEC (parent);
218 switch (GST_EVENT_TYPE (event)) {
223 gst_event_parse_caps (event, &caps);
224 mulawdec_setcaps (mulawdec, caps);
225 gst_event_unref (event);
231 res = gst_pad_event_default (pad, parent, event);
238 gst_mulawdec_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
240 GstMuLawDec *mulawdec;
243 gsize mulaw_size, linear_size;
247 mulawdec = GST_MULAWDEC (parent);
249 if (G_UNLIKELY (mulawdec->rate == 0))
252 mulaw_data = gst_buffer_map (buffer, &mulaw_size, NULL, GST_MAP_READ);
254 linear_size = mulaw_size * 2;
256 outbuf = gst_buffer_new_allocate (NULL, linear_size, 0);
257 linear_data = gst_buffer_map (outbuf, NULL, NULL, GST_MAP_WRITE);
259 /* copy discont flag */
260 if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT))
261 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
263 GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buffer);
264 if (GST_BUFFER_DURATION (outbuf) == GST_CLOCK_TIME_NONE)
265 GST_BUFFER_DURATION (outbuf) = gst_util_uint64_scale_int (GST_SECOND,
266 linear_size, 2 * mulawdec->rate * mulawdec->channels);
268 GST_BUFFER_DURATION (outbuf) = GST_BUFFER_DURATION (buffer);
270 mulaw_decode (mulaw_data, linear_data, mulaw_size);
272 gst_buffer_unmap (outbuf, linear_data, -1);
273 gst_buffer_unmap (buffer, mulaw_data, -1);
274 gst_buffer_unref (buffer);
276 ret = gst_pad_push (mulawdec->srcpad, outbuf);
283 GST_WARNING_OBJECT (mulawdec, "no input format set: not-negotiated");
284 gst_buffer_unref (buffer);
285 return GST_FLOW_NOT_NEGOTIATED;
289 static GstStateChangeReturn
290 gst_mulawdec_change_state (GstElement * element, GstStateChange transition)
292 GstStateChangeReturn ret;
293 GstMuLawDec *dec = GST_MULAWDEC (element);
295 switch (transition) {
300 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
301 if (ret != GST_STATE_CHANGE_SUCCESS)
304 switch (transition) {
305 case GST_STATE_CHANGE_PAUSED_TO_READY: