law: fix negotiation
[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 #include <gst/audio/audio.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 /* Stereo signals and args */
38 enum
39 {
40   /* FILL ME */
41   LAST_SIGNAL
42 };
43
44 enum
45 {
46   ARG_0
47 };
48
49 static GstStateChangeReturn
50 gst_mulawdec_change_state (GstElement * element, GstStateChange transition);
51
52 static gboolean gst_mulawdec_event (GstPad * pad, GstObject * parent,
53     GstEvent * event);
54 static GstFlowReturn gst_mulawdec_chain (GstPad * pad, GstObject * parent,
55     GstBuffer * buffer);
56
57 #define gst_mulawdec_parent_class parent_class
58 G_DEFINE_TYPE (GstMuLawDec, gst_mulawdec, GST_TYPE_ELEMENT);
59
60 static gboolean
61 mulawdec_setcaps (GstMuLawDec * mulawdec, GstCaps * caps)
62 {
63   GstStructure *structure;
64   int rate, channels;
65   gboolean ret;
66   GstCaps *outcaps;
67
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);
71   if (!ret)
72     return FALSE;
73
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);
79
80   if (ret) {
81     GST_DEBUG_OBJECT (mulawdec, "rate=%d, channels=%d", rate, channels);
82     mulawdec->rate = rate;
83     mulawdec->channels = channels;
84   }
85   return ret;
86 }
87
88 static GstCaps *
89 mulawdec_getcaps (GstPad * pad, GstCaps * filter)
90 {
91   GstMuLawDec *mulawdec;
92   GstPad *otherpad;
93   GstCaps *othercaps, *result;
94   const GstCaps *templ;
95   const gchar *name;
96   gint i;
97
98   mulawdec = GST_MULAWDEC (GST_PAD_PARENT (pad));
99
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;
104   } else {
105     name = "audio/x-mulaw";
106     otherpad = mulawdec->srcpad;
107   }
108   /* get caps from the peer, this can return NULL when there is no peer */
109   othercaps = gst_pad_peer_query_caps (otherpad, NULL);
110
111   /* get the template caps to make sure we return something acceptable */
112   templ = gst_pad_get_pad_template_caps (pad);
113
114   if (othercaps) {
115     /* there was a peer */
116     othercaps = gst_caps_make_writable (othercaps);
117
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;
121
122       structure = gst_caps_get_structure (othercaps, i);
123
124       /* adjust the name */
125       gst_structure_set_name (structure, name);
126
127       if (pad == mulawdec->sinkpad) {
128         /* remove the fields we don't want */
129         gst_structure_remove_fields (structure, "format", NULL);
130       } else {
131         /* add fixed fields */
132         gst_structure_set (structure, "format", G_TYPE_STRING,
133             GST_AUDIO_NE (S16), NULL);
134       }
135     }
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);
139   } else {
140     /* there was no peer, return the template caps */
141     result = gst_caps_copy (templ);
142   }
143   if (filter && result) {
144     GstCaps *temp;
145
146     temp = gst_caps_intersect (result, filter);
147     gst_caps_unref (result);
148     result = temp;
149   }
150   return result;
151 }
152
153 static gboolean
154 gst_mulawdec_query (GstPad * pad, GstObject * parent, GstQuery * query)
155 {
156   gboolean res;
157
158   switch (GST_QUERY_TYPE (query)) {
159     case GST_QUERY_CAPS:
160     {
161       GstCaps *filter, *caps;
162
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);
167
168       res = TRUE;
169       break;
170     }
171     default:
172       res = gst_pad_query_default (pad, parent, query);
173       break;
174   }
175   return res;
176 }
177
178 static void
179 gst_mulawdec_class_init (GstMuLawDecClass * klass)
180 {
181   GstElementClass *element_class = (GstElementClass *) klass;
182
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));
187
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>");
192
193   element_class->change_state = GST_DEBUG_FUNCPTR (gst_mulawdec_change_state);
194 }
195
196 static void
197 gst_mulawdec_init (GstMuLawDec * mulawdec)
198 {
199   mulawdec->sinkpad =
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);
205
206   mulawdec->srcpad =
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);
210 }
211
212 static gboolean
213 gst_mulawdec_event (GstPad * pad, GstObject * parent, GstEvent * event)
214 {
215   GstMuLawDec *mulawdec;
216   gboolean res;
217
218   mulawdec = GST_MULAWDEC (parent);
219
220   switch (GST_EVENT_TYPE (event)) {
221     case GST_EVENT_CAPS:
222     {
223       GstCaps *caps;
224
225       gst_event_parse_caps (event, &caps);
226       mulawdec_setcaps (mulawdec, caps);
227       gst_event_unref (event);
228
229       res = TRUE;
230       break;
231     }
232     default:
233       res = gst_pad_event_default (pad, parent, event);
234       break;
235   }
236   return res;
237 }
238
239 static GstFlowReturn
240 gst_mulawdec_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
241 {
242   GstMuLawDec *mulawdec;
243   gint16 *linear_data;
244   guint8 *mulaw_data;
245   gsize mulaw_size, linear_size;
246   GstBuffer *outbuf;
247   GstFlowReturn ret;
248
249   mulawdec = GST_MULAWDEC (parent);
250
251   if (G_UNLIKELY (mulawdec->rate == 0))
252     goto not_negotiated;
253
254   mulaw_data = gst_buffer_map (buffer, &mulaw_size, NULL, GST_MAP_READ);
255
256   linear_size = mulaw_size * 2;
257
258   outbuf = gst_buffer_new_allocate (NULL, linear_size, 0);
259   linear_data = gst_buffer_map (outbuf, NULL, NULL, GST_MAP_WRITE);
260
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);
264
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);
269   else
270     GST_BUFFER_DURATION (outbuf) = GST_BUFFER_DURATION (buffer);
271
272   mulaw_decode (mulaw_data, linear_data, mulaw_size);
273
274   gst_buffer_unmap (outbuf, linear_data, -1);
275   gst_buffer_unmap (buffer, mulaw_data, -1);
276   gst_buffer_unref (buffer);
277
278   ret = gst_pad_push (mulawdec->srcpad, outbuf);
279
280   return ret;
281
282   /* ERRORS */
283 not_negotiated:
284   {
285     GST_WARNING_OBJECT (mulawdec, "no input format set: not-negotiated");
286     gst_buffer_unref (buffer);
287     return GST_FLOW_NOT_NEGOTIATED;
288   }
289 }
290
291 static GstStateChangeReturn
292 gst_mulawdec_change_state (GstElement * element, GstStateChange transition)
293 {
294   GstStateChangeReturn ret;
295   GstMuLawDec *dec = GST_MULAWDEC (element);
296
297   switch (transition) {
298     default:
299       break;
300   }
301
302   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
303   if (ret != GST_STATE_CHANGE_SUCCESS)
304     return ret;
305
306   switch (transition) {
307     case GST_STATE_CHANGE_PAUSED_TO_READY:
308       dec->rate = 0;
309       dec->channels = 0;
310       break;
311     default:
312       break;
313   }
314
315   return ret;
316 }