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.
29 #include <gst/audio/audio.h>
31 #include "mulaw-decode.h"
32 #include "mulaw-conversion.h"
34 extern GstStaticPadTemplate mulaw_dec_src_factory;
35 extern GstStaticPadTemplate mulaw_dec_sink_factory;
37 /* Stereo signals and args */
49 static GstStateChangeReturn
50 gst_mulawdec_change_state (GstElement * element, GstStateChange transition);
52 static gboolean gst_mulawdec_event (GstPad * pad, GstObject * parent,
54 static GstFlowReturn gst_mulawdec_chain (GstPad * pad, GstObject * parent,
57 #define gst_mulawdec_parent_class parent_class
58 G_DEFINE_TYPE (GstMuLawDec, gst_mulawdec, GST_TYPE_ELEMENT);
61 mulawdec_setcaps (GstMuLawDec * mulawdec, GstCaps * caps)
63 GstStructure *structure;
68 structure = gst_caps_get_structure (caps, 0);
69 ret = gst_structure_get_int (structure, "rate", &rate);
70 ret = ret && gst_structure_get_int (structure, "channels", &channels);
74 outcaps = gst_caps_new_simple ("audio/x-raw",
75 "format", G_TYPE_STRING, GST_AUDIO_NE (S16),
76 "rate", G_TYPE_INT, rate, "channels", G_TYPE_INT, channels, NULL);
77 ret = gst_pad_set_caps (mulawdec->srcpad, outcaps);
78 gst_caps_unref (outcaps);
81 GST_DEBUG_OBJECT (mulawdec, "rate=%d, channels=%d", rate, channels);
82 mulawdec->rate = rate;
83 mulawdec->channels = channels;
89 mulawdec_getcaps (GstPad * pad, GstCaps * filter)
91 GstMuLawDec *mulawdec;
93 GstCaps *othercaps, *result;
98 mulawdec = GST_MULAWDEC (GST_PAD_PARENT (pad));
100 /* figure out the name of the caps we are going to return */
101 if (pad == mulawdec->srcpad) {
102 name = "audio/x-raw";
103 otherpad = mulawdec->sinkpad;
105 name = "audio/x-mulaw";
106 otherpad = mulawdec->srcpad;
108 /* get caps from the peer, this can return NULL when there is no peer */
109 othercaps = gst_pad_peer_query_caps (otherpad, NULL);
111 /* get the template caps to make sure we return something acceptable */
112 templ = gst_pad_get_pad_template_caps (pad);
115 /* there was a peer */
116 othercaps = gst_caps_make_writable (othercaps);
118 /* go through the caps and remove the fields we don't want */
119 for (i = 0; i < gst_caps_get_size (othercaps); i++) {
120 GstStructure *structure;
122 structure = gst_caps_get_structure (othercaps, i);
124 /* adjust the name */
125 gst_structure_set_name (structure, name);
127 if (pad == mulawdec->sinkpad) {
128 /* remove the fields we don't want */
129 gst_structure_remove_fields (structure, "format", NULL);
131 /* add fixed fields */
132 gst_structure_set (structure, "format", G_TYPE_STRING,
133 GST_AUDIO_NE (S16), NULL);
136 /* filter against the allowed caps of the pad to return our result */
137 result = gst_caps_intersect (othercaps, templ);
138 gst_caps_unref (othercaps);
140 /* there was no peer, return the template caps */
141 result = gst_caps_copy (templ);
143 if (filter && result) {
146 temp = gst_caps_intersect (result, filter);
147 gst_caps_unref (result);
154 gst_mulawdec_query (GstPad * pad, GstObject * parent, GstQuery * query)
158 switch (GST_QUERY_TYPE (query)) {
161 GstCaps *filter, *caps;
163 gst_query_parse_caps (query, &filter);
164 caps = mulawdec_getcaps (pad, filter);
165 gst_query_set_caps_result (query, caps);
166 gst_caps_unref (caps);
172 res = gst_pad_query_default (pad, parent, query);
179 gst_mulawdec_class_init (GstMuLawDecClass * klass)
181 GstElementClass *element_class = (GstElementClass *) klass;
183 gst_element_class_add_pad_template (element_class,
184 gst_static_pad_template_get (&mulaw_dec_src_factory));
185 gst_element_class_add_pad_template (element_class,
186 gst_static_pad_template_get (&mulaw_dec_sink_factory));
188 gst_element_class_set_details_simple (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>");
193 element_class->change_state = GST_DEBUG_FUNCPTR (gst_mulawdec_change_state);
197 gst_mulawdec_init (GstMuLawDec * mulawdec)
200 gst_pad_new_from_static_template (&mulaw_dec_sink_factory, "sink");
201 gst_pad_set_query_function (mulawdec->sinkpad, gst_mulawdec_query);
202 gst_pad_set_event_function (mulawdec->sinkpad, gst_mulawdec_event);
203 gst_pad_set_chain_function (mulawdec->sinkpad, gst_mulawdec_chain);
204 gst_element_add_pad (GST_ELEMENT (mulawdec), mulawdec->sinkpad);
207 gst_pad_new_from_static_template (&mulaw_dec_src_factory, "src");
208 gst_pad_set_query_function (mulawdec->srcpad, gst_mulawdec_query);
209 gst_element_add_pad (GST_ELEMENT (mulawdec), mulawdec->srcpad);
213 gst_mulawdec_event (GstPad * pad, GstObject * parent, GstEvent * event)
215 GstMuLawDec *mulawdec;
218 mulawdec = GST_MULAWDEC (parent);
220 switch (GST_EVENT_TYPE (event)) {
225 gst_event_parse_caps (event, &caps);
226 mulawdec_setcaps (mulawdec, caps);
227 gst_event_unref (event);
233 res = gst_pad_event_default (pad, parent, event);
240 gst_mulawdec_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
242 GstMuLawDec *mulawdec;
245 gsize mulaw_size, linear_size;
249 mulawdec = GST_MULAWDEC (parent);
251 if (G_UNLIKELY (mulawdec->rate == 0))
254 mulaw_data = gst_buffer_map (buffer, &mulaw_size, NULL, GST_MAP_READ);
256 linear_size = mulaw_size * 2;
258 outbuf = gst_buffer_new_allocate (NULL, linear_size, 0);
259 linear_data = gst_buffer_map (outbuf, NULL, NULL, GST_MAP_WRITE);
261 /* copy discont flag */
262 if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT))
263 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
265 GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buffer);
266 if (GST_BUFFER_DURATION (outbuf) == GST_CLOCK_TIME_NONE)
267 GST_BUFFER_DURATION (outbuf) = gst_util_uint64_scale_int (GST_SECOND,
268 linear_size, 2 * mulawdec->rate * mulawdec->channels);
270 GST_BUFFER_DURATION (outbuf) = GST_BUFFER_DURATION (buffer);
272 mulaw_decode (mulaw_data, linear_data, mulaw_size);
274 gst_buffer_unmap (outbuf, linear_data, -1);
275 gst_buffer_unmap (buffer, mulaw_data, -1);
276 gst_buffer_unref (buffer);
278 ret = gst_pad_push (mulawdec->srcpad, outbuf);
285 GST_WARNING_OBJECT (mulawdec, "no input format set: not-negotiated");
286 gst_buffer_unref (buffer);
287 return GST_FLOW_NOT_NEGOTIATED;
291 static GstStateChangeReturn
292 gst_mulawdec_change_state (GstElement * element, GstStateChange transition)
294 GstStateChangeReturn ret;
295 GstMuLawDec *dec = GST_MULAWDEC (element);
297 switch (transition) {
302 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
303 if (ret != GST_STATE_CHANGE_SUCCESS)
306 switch (transition) {
307 case GST_STATE_CHANGE_PAUSED_TO_READY: