law: port to 0.11
[platform/upstream/gstreamer.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 "S16_LE"
35 #else
36 #define INT_FORMAT "S16_BE"
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, GstEvent * event);
58 static GstFlowReturn gst_mulawdec_chain (GstPad * pad, GstBuffer * buffer);
59
60 #define gst_mulawdec_parent_class parent_class
61 G_DEFINE_TYPE (GstMuLawDec, gst_mulawdec, GST_TYPE_ELEMENT);
62
63 static gboolean
64 mulawdec_setcaps (GstMuLawDec * mulawdec, GstCaps * caps)
65 {
66   GstStructure *structure;
67   int rate, channels;
68   gboolean ret;
69   GstCaps *outcaps;
70
71   structure = gst_caps_get_structure (caps, 0);
72   ret = gst_structure_get_int (structure, "rate", &rate);
73   ret = ret && gst_structure_get_int (structure, "channels", &channels);
74   if (!ret)
75     return FALSE;
76
77   outcaps = gst_caps_new_simple ("audio/x-raw",
78       "format", G_TYPE_STRING, INT_FORMAT,
79       "rate", G_TYPE_INT, rate, "channels", G_TYPE_INT, channels, NULL);
80   ret = gst_pad_set_caps (mulawdec->srcpad, outcaps);
81   gst_caps_unref (outcaps);
82
83   if (ret) {
84     GST_DEBUG_OBJECT (mulawdec, "rate=%d, channels=%d", rate, channels);
85     mulawdec->rate = rate;
86     mulawdec->channels = channels;
87   }
88   return ret;
89 }
90
91 static GstCaps *
92 mulawdec_getcaps (GstPad * pad, GstCaps * filter)
93 {
94   GstMuLawDec *mulawdec;
95   GstPad *otherpad;
96   GstCaps *othercaps, *result;
97   const GstCaps *templ;
98   const gchar *name;
99   gint i;
100
101   mulawdec = GST_MULAWDEC (GST_PAD_PARENT (pad));
102
103   /* figure out the name of the caps we are going to return */
104   if (pad == mulawdec->srcpad) {
105     name = "audio/x-raw";
106     otherpad = mulawdec->sinkpad;
107   } else {
108     name = "audio/x-mulaw";
109     otherpad = mulawdec->srcpad;
110   }
111   /* get caps from the peer, this can return NULL when there is no peer */
112   othercaps = gst_pad_peer_get_caps (otherpad, filter);
113
114   /* get the template caps to make sure we return something acceptable */
115   templ = gst_pad_get_pad_template_caps (pad);
116
117   if (othercaps) {
118     /* there was a peer */
119     othercaps = gst_caps_make_writable (othercaps);
120
121     /* go through the caps and remove the fields we don't want */
122     for (i = 0; i < gst_caps_get_size (othercaps); i++) {
123       GstStructure *structure;
124
125       structure = gst_caps_get_structure (othercaps, i);
126
127       /* adjust the name */
128       gst_structure_set_name (structure, name);
129
130       if (pad == mulawdec->sinkpad) {
131         /* remove the fields we don't want */
132         gst_structure_remove_fields (structure, "format", NULL);
133       } else {
134         /* add fixed fields */
135         gst_structure_set (structure, "format", G_TYPE_STRING, INT_FORMAT,
136             NULL);
137       }
138     }
139     /* filter against the allowed caps of the pad to return our result */
140     result = gst_caps_intersect (othercaps, templ);
141     gst_caps_unref (othercaps);
142   } else {
143     /* there was no peer, return the template caps */
144     result = gst_caps_copy (templ);
145   }
146   return result;
147 }
148
149 static void
150 gst_mulawdec_class_init (GstMuLawDecClass * klass)
151 {
152   GstElementClass *element_class = (GstElementClass *) klass;
153
154   gst_element_class_add_pad_template (element_class,
155       gst_static_pad_template_get (&mulaw_dec_src_factory));
156   gst_element_class_add_pad_template (element_class,
157       gst_static_pad_template_get (&mulaw_dec_sink_factory));
158
159   gst_element_class_set_details_simple (element_class, "Mu Law audio decoder",
160       "Codec/Decoder/Audio",
161       "Convert 8bit mu law to 16bit PCM",
162       "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
163
164   element_class->change_state = GST_DEBUG_FUNCPTR (gst_mulawdec_change_state);
165 }
166
167 static void
168 gst_mulawdec_init (GstMuLawDec * mulawdec)
169 {
170   mulawdec->sinkpad =
171       gst_pad_new_from_static_template (&mulaw_dec_sink_factory, "sink");
172   gst_pad_set_getcaps_function (mulawdec->sinkpad, mulawdec_getcaps);
173   gst_pad_set_event_function (mulawdec->sinkpad, gst_mulawdec_event);
174   gst_pad_set_chain_function (mulawdec->sinkpad, gst_mulawdec_chain);
175   gst_element_add_pad (GST_ELEMENT (mulawdec), mulawdec->sinkpad);
176
177   mulawdec->srcpad =
178       gst_pad_new_from_static_template (&mulaw_dec_src_factory, "src");
179   gst_pad_use_fixed_caps (mulawdec->srcpad);
180   gst_pad_set_getcaps_function (mulawdec->srcpad, mulawdec_getcaps);
181   gst_element_add_pad (GST_ELEMENT (mulawdec), mulawdec->srcpad);
182 }
183
184 static gboolean
185 gst_mulawdec_event (GstPad * pad, GstEvent * event)
186 {
187   GstMuLawDec *mulawdec;
188   gboolean res;
189
190   mulawdec = GST_MULAWDEC (GST_PAD_PARENT (pad));
191
192   switch (GST_EVENT_TYPE (event)) {
193     case GST_EVENT_CAPS:
194     {
195       GstCaps *caps;
196
197       gst_event_parse_caps (event, &caps);
198       mulawdec_setcaps (mulawdec, caps);
199       gst_event_unref (event);
200
201       res = TRUE;
202       break;
203     }
204     default:
205       res = gst_pad_event_default (pad, event);
206       break;
207   }
208   return res;
209 }
210
211 static GstFlowReturn
212 gst_mulawdec_chain (GstPad * pad, GstBuffer * buffer)
213 {
214   GstMuLawDec *mulawdec;
215   gint16 *linear_data;
216   guint8 *mulaw_data;
217   gsize mulaw_size, linear_size;
218   GstBuffer *outbuf;
219   GstFlowReturn ret;
220
221   mulawdec = GST_MULAWDEC (GST_PAD_PARENT (pad));
222
223   if (G_UNLIKELY (mulawdec->rate == 0))
224     goto not_negotiated;
225
226   mulaw_data = gst_buffer_map (buffer, &mulaw_size, NULL, GST_MAP_READ);
227
228   linear_size = mulaw_size * 2;
229
230   outbuf = gst_buffer_new_allocate (NULL, linear_size, 0);
231   linear_data = gst_buffer_map (outbuf, NULL, NULL, GST_MAP_WRITE);
232
233   /* copy discont flag */
234   if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT))
235     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
236
237   GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buffer);
238   if (GST_BUFFER_DURATION (outbuf) == GST_CLOCK_TIME_NONE)
239     GST_BUFFER_DURATION (outbuf) = gst_util_uint64_scale_int (GST_SECOND,
240         linear_size, 2 * mulawdec->rate * mulawdec->channels);
241   else
242     GST_BUFFER_DURATION (outbuf) = GST_BUFFER_DURATION (buffer);
243
244   mulaw_decode (mulaw_data, linear_data, mulaw_size);
245
246   gst_buffer_unmap (outbuf, linear_data, -1);
247   gst_buffer_unmap (buffer, mulaw_data, -1);
248   gst_buffer_unref (buffer);
249
250   ret = gst_pad_push (mulawdec->srcpad, outbuf);
251
252   return ret;
253
254   /* ERRORS */
255 not_negotiated:
256   {
257     GST_WARNING_OBJECT (mulawdec, "no input format set: not-negotiated");
258     gst_buffer_unref (buffer);
259     return GST_FLOW_NOT_NEGOTIATED;
260   }
261 }
262
263 static GstStateChangeReturn
264 gst_mulawdec_change_state (GstElement * element, GstStateChange transition)
265 {
266   GstStateChangeReturn ret;
267   GstMuLawDec *dec = GST_MULAWDEC (element);
268
269   switch (transition) {
270     default:
271       break;
272   }
273
274   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
275   if (ret != GST_STATE_CHANGE_SUCCESS)
276     return ret;
277
278   switch (transition) {
279     case GST_STATE_CHANGE_PAUSED_TO_READY:
280       dec->rate = 0;
281       dec->channels = 0;
282       break;
283     default:
284       break;
285   }
286
287   return ret;
288 }