Merge remote-tracking branch 'origin/master' into 0.11
[platform/upstream/gst-plugins-good.git] / gst / law / mulaw-decode.c
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., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19 /**
20  * SECTION:element-mulawdec
21  *
22  * This element decodes mulaw audio. Mulaw coding is also known as G.711.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28 #include <gst/gst.h>
29
30 #include "mulaw-decode.h"
31 #include "mulaw-conversion.h"
32
33 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
34 #define INT_FORMAT "S16LE"
35 #else
36 #define INT_FORMAT "S16BE"
37 #endif
38
39 extern GstStaticPadTemplate mulaw_dec_src_factory;
40 extern GstStaticPadTemplate mulaw_dec_sink_factory;
41
42 /* Stereo signals and args */
43 enum
44 {
45   /* FILL ME */
46   LAST_SIGNAL
47 };
48
49 enum
50 {
51   ARG_0
52 };
53
54 static GstStateChangeReturn
55 gst_mulawdec_change_state (GstElement * element, GstStateChange transition);
56
57 static gboolean gst_mulawdec_event (GstPad * pad, GstObject * parent,
58     GstEvent * event);
59 static GstFlowReturn gst_mulawdec_chain (GstPad * pad, GstObject * parent,
60     GstBuffer * buffer);
61
62 #define gst_mulawdec_parent_class parent_class
63 G_DEFINE_TYPE (GstMuLawDec, gst_mulawdec, GST_TYPE_ELEMENT);
64
65 static gboolean
66 mulawdec_setcaps (GstMuLawDec * mulawdec, GstCaps * caps)
67 {
68   GstStructure *structure;
69   int rate, channels;
70   gboolean ret;
71   GstCaps *outcaps;
72
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);
76   if (!ret)
77     return FALSE;
78
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);
84
85   if (ret) {
86     GST_DEBUG_OBJECT (mulawdec, "rate=%d, channels=%d", rate, channels);
87     mulawdec->rate = rate;
88     mulawdec->channels = channels;
89   }
90   return ret;
91 }
92
93 static GstCaps *
94 mulawdec_getcaps (GstPad * pad, GstCaps * filter)
95 {
96   GstMuLawDec *mulawdec;
97   GstPad *otherpad;
98   GstCaps *othercaps, *result;
99   const GstCaps *templ;
100   const gchar *name;
101   gint i;
102
103   mulawdec = GST_MULAWDEC (GST_PAD_PARENT (pad));
104
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;
109   } else {
110     name = "audio/x-mulaw";
111     otherpad = mulawdec->srcpad;
112   }
113   /* get caps from the peer, this can return NULL when there is no peer */
114   othercaps = gst_pad_peer_query_caps (otherpad, filter);
115
116   /* get the template caps to make sure we return something acceptable */
117   templ = gst_pad_get_pad_template_caps (pad);
118
119   if (othercaps) {
120     /* there was a peer */
121     othercaps = gst_caps_make_writable (othercaps);
122
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;
126
127       structure = gst_caps_get_structure (othercaps, i);
128
129       /* adjust the name */
130       gst_structure_set_name (structure, name);
131
132       if (pad == mulawdec->sinkpad) {
133         /* remove the fields we don't want */
134         gst_structure_remove_fields (structure, "format", NULL);
135       } else {
136         /* add fixed fields */
137         gst_structure_set (structure, "format", G_TYPE_STRING, INT_FORMAT,
138             NULL);
139       }
140     }
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);
144   } else {
145     /* there was no peer, return the template caps */
146     result = gst_caps_copy (templ);
147   }
148   return result;
149 }
150
151 static gboolean
152 gst_mulawdec_query (GstPad * pad, GstObject * parent, GstQuery * query)
153 {
154   gboolean res;
155
156   switch (GST_QUERY_TYPE (query)) {
157     case GST_QUERY_CAPS:
158     {
159       GstCaps *filter, *caps;
160
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);
165
166       res = TRUE;
167       break;
168     }
169     default:
170       res = gst_pad_query_default (pad, parent, query);
171       break;
172   }
173   return res;
174 }
175
176 static void
177 gst_mulawdec_class_init (GstMuLawDecClass * klass)
178 {
179   GstElementClass *element_class = (GstElementClass *) klass;
180
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));
185
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>");
190
191   element_class->change_state = GST_DEBUG_FUNCPTR (gst_mulawdec_change_state);
192 }
193
194 static void
195 gst_mulawdec_init (GstMuLawDec * mulawdec)
196 {
197   mulawdec->sinkpad =
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);
203
204   mulawdec->srcpad =
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);
208 }
209
210 static gboolean
211 gst_mulawdec_event (GstPad * pad, GstObject * parent, GstEvent * event)
212 {
213   GstMuLawDec *mulawdec;
214   gboolean res;
215
216   mulawdec = GST_MULAWDEC (parent);
217
218   switch (GST_EVENT_TYPE (event)) {
219     case GST_EVENT_CAPS:
220     {
221       GstCaps *caps;
222
223       gst_event_parse_caps (event, &caps);
224       mulawdec_setcaps (mulawdec, caps);
225       gst_event_unref (event);
226
227       res = TRUE;
228       break;
229     }
230     default:
231       res = gst_pad_event_default (pad, parent, event);
232       break;
233   }
234   return res;
235 }
236
237 static GstFlowReturn
238 gst_mulawdec_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
239 {
240   GstMuLawDec *mulawdec;
241   gint16 *linear_data;
242   guint8 *mulaw_data;
243   gsize mulaw_size, linear_size;
244   GstBuffer *outbuf;
245   GstFlowReturn ret;
246
247   mulawdec = GST_MULAWDEC (parent);
248
249   if (G_UNLIKELY (mulawdec->rate == 0))
250     goto not_negotiated;
251
252   mulaw_data = gst_buffer_map (buffer, &mulaw_size, NULL, GST_MAP_READ);
253
254   linear_size = mulaw_size * 2;
255
256   outbuf = gst_buffer_new_allocate (NULL, linear_size, 0);
257   linear_data = gst_buffer_map (outbuf, NULL, NULL, GST_MAP_WRITE);
258
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);
262
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);
267   else
268     GST_BUFFER_DURATION (outbuf) = GST_BUFFER_DURATION (buffer);
269
270   mulaw_decode (mulaw_data, linear_data, mulaw_size);
271
272   gst_buffer_unmap (outbuf, linear_data, -1);
273   gst_buffer_unmap (buffer, mulaw_data, -1);
274   gst_buffer_unref (buffer);
275
276   ret = gst_pad_push (mulawdec->srcpad, outbuf);
277
278   return ret;
279
280   /* ERRORS */
281 not_negotiated:
282   {
283     GST_WARNING_OBJECT (mulawdec, "no input format set: not-negotiated");
284     gst_buffer_unref (buffer);
285     return GST_FLOW_NOT_NEGOTIATED;
286   }
287 }
288
289 static GstStateChangeReturn
290 gst_mulawdec_change_state (GstElement * element, GstStateChange transition)
291 {
292   GstStateChangeReturn ret;
293   GstMuLawDec *dec = GST_MULAWDEC (element);
294
295   switch (transition) {
296     default:
297       break;
298   }
299
300   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
301   if (ret != GST_STATE_CHANGE_SUCCESS)
302     return ret;
303
304   switch (transition) {
305     case GST_STATE_CHANGE_PAUSED_TO_READY:
306       dec->rate = 0;
307       dec->channels = 0;
308       break;
309     default:
310       break;
311   }
312
313   return ret;
314 }