alaw/mulaw: Implement _getcaps function for alaw/mulaw decoders
[platform/upstream/gst-plugins-good.git] / gst / law / alaw-decode.c
1 /* GStreamer A-Law to PCM conversion
2  * Copyright (C) 2000 by Abramo Bagnara <abramo@alsa-project.org>
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.1 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-alawdec
21  *
22  * This element decodes alaw audio. Alaw coding is also known as G.711.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include "alaw-decode.h"
30
31 extern GstStaticPadTemplate alaw_dec_src_factory;
32 extern GstStaticPadTemplate alaw_dec_sink_factory;
33
34 GST_DEBUG_CATEGORY_STATIC (alaw_dec_debug);
35 #define GST_CAT_DEFAULT alaw_dec_debug
36
37 static GstStateChangeReturn
38 gst_alaw_dec_change_state (GstElement * element, GstStateChange transition);
39 static GstFlowReturn gst_alaw_dec_chain (GstPad * pad, GstBuffer * buffer);
40
41 GST_BOILERPLATE (GstALawDec, gst_alaw_dec, GstElement, GST_TYPE_ELEMENT);
42
43 /* some day we might have defines in gstconfig.h that tell us about the
44  * desired cpu/memory/binary size trade-offs */
45 #define GST_ALAW_DEC_USE_TABLE
46
47 #ifdef GST_ALAW_DEC_USE_TABLE
48
49 static const gint alaw_to_s16_table[256] = {
50   -5504, -5248, -6016, -5760, -4480, -4224, -4992, -4736,
51   -7552, -7296, -8064, -7808, -6528, -6272, -7040, -6784,
52   -2752, -2624, -3008, -2880, -2240, -2112, -2496, -2368,
53   -3776, -3648, -4032, -3904, -3264, -3136, -3520, -3392,
54   -22016, -20992, -24064, -23040, -17920, -16896, -19968, -18944,
55   -30208, -29184, -32256, -31232, -26112, -25088, -28160, -27136,
56   -11008, -10496, -12032, -11520, -8960, -8448, -9984, -9472,
57   -15104, -14592, -16128, -15616, -13056, -12544, -14080, -13568,
58   -344, -328, -376, -360, -280, -264, -312, -296,
59   -472, -456, -504, -488, -408, -392, -440, -424,
60   -88, -72, -120, -104, -24, -8, -56, -40,
61   -216, -200, -248, -232, -152, -136, -184, -168,
62   -1376, -1312, -1504, -1440, -1120, -1056, -1248, -1184,
63   -1888, -1824, -2016, -1952, -1632, -1568, -1760, -1696,
64   -688, -656, -752, -720, -560, -528, -624, -592,
65   -944, -912, -1008, -976, -816, -784, -880, -848,
66   5504, 5248, 6016, 5760, 4480, 4224, 4992, 4736,
67   7552, 7296, 8064, 7808, 6528, 6272, 7040, 6784,
68   2752, 2624, 3008, 2880, 2240, 2112, 2496, 2368,
69   3776, 3648, 4032, 3904, 3264, 3136, 3520, 3392,
70   22016, 20992, 24064, 23040, 17920, 16896, 19968, 18944,
71   30208, 29184, 32256, 31232, 26112, 25088, 28160, 27136,
72   11008, 10496, 12032, 11520, 8960, 8448, 9984, 9472,
73   15104, 14592, 16128, 15616, 13056, 12544, 14080, 13568,
74   344, 328, 376, 360, 280, 264, 312, 296,
75   472, 456, 504, 488, 408, 392, 440, 424,
76   88, 72, 120, 104, 24, 8, 56, 40,
77   216, 200, 248, 232, 152, 136, 184, 168,
78   1376, 1312, 1504, 1440, 1120, 1056, 1248, 1184,
79   1888, 1824, 2016, 1952, 1632, 1568, 1760, 1696,
80   688, 656, 752, 720, 560, 528, 624, 592,
81   944, 912, 1008, 976, 816, 784, 880, 848
82 };
83
84 static inline gint
85 alaw_to_s16 (guint8 a_val)
86 {
87   return alaw_to_s16_table[a_val];
88 }
89
90 #else /* GST_ALAW_DEC_USE_TABLE */
91
92 static inline gint
93 alaw_to_s16 (guint8 a_val)
94 {
95   gint t;
96   gint seg;
97
98   a_val ^= 0x55;
99   t = a_val & 0x7f;
100   if (t < 16)
101     t = (t << 4) + 8;
102   else {
103     seg = (t >> 4) & 0x07;
104     t = ((t & 0x0f) << 4) + 0x108;
105     t <<= seg - 1;
106   }
107   return ((a_val & 0x80) ? t : -t);
108 }
109
110 #endif /* GST_ALAW_DEC_USE_TABLE */
111
112 static gboolean
113 gst_alaw_dec_sink_setcaps (GstPad * pad, GstCaps * caps)
114 {
115   GstALawDec *alawdec;
116   GstStructure *structure;
117   int rate, channels;
118   gboolean ret;
119   GstCaps *outcaps;
120
121   alawdec = GST_ALAW_DEC (GST_PAD_PARENT (pad));
122
123   structure = gst_caps_get_structure (caps, 0);
124
125   ret = gst_structure_get_int (structure, "rate", &rate);
126   ret &= gst_structure_get_int (structure, "channels", &channels);
127   if (!ret)
128     return FALSE;
129
130   outcaps = gst_caps_new_simple ("audio/x-raw-int",
131       "width", G_TYPE_INT, 16,
132       "depth", G_TYPE_INT, 16,
133       "endianness", G_TYPE_INT, G_BYTE_ORDER,
134       "signed", G_TYPE_BOOLEAN, TRUE,
135       "rate", G_TYPE_INT, rate, "channels", G_TYPE_INT, channels, NULL);
136
137   ret = gst_pad_set_caps (alawdec->srcpad, outcaps);
138   gst_caps_unref (outcaps);
139
140   if (ret) {
141     GST_DEBUG_OBJECT (alawdec, "rate=%d, channels=%d", rate, channels);
142     alawdec->rate = rate;
143     alawdec->channels = channels;
144   }
145   return ret;
146 }
147
148 static GstCaps *
149 gst_alaw_dec_getcaps (GstPad * pad)
150 {
151   GstALawDec *alawdec;
152   GstPad *otherpad;
153   GstCaps *base_caps, *othercaps;
154
155   alawdec = GST_ALAW_DEC (GST_PAD_PARENT (pad));
156
157   base_caps = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
158
159   if (pad == alawdec->srcpad) {
160     otherpad = alawdec->sinkpad;
161   } else {
162     otherpad = alawdec->srcpad;
163   }
164   othercaps = gst_pad_peer_get_caps (otherpad);
165   if (othercaps) {
166     GstStructure *structure;
167     const GValue *orate, *ochans;
168     const GValue *rate, *chans;
169     GValue irate = { 0 };
170     GValue ichans = { 0 };
171
172     if (gst_caps_is_empty (othercaps) || gst_caps_is_any (othercaps))
173       goto done;
174
175     structure = gst_caps_get_structure (othercaps, 0);
176     orate = gst_structure_get_value (structure, "rate");
177     ochans = gst_structure_get_value (structure, "channels");
178
179     structure = gst_caps_get_structure (base_caps, 0);
180     rate = gst_structure_get_value (structure, "rate");
181     chans = gst_structure_get_value (structure, "channels");
182
183     if (orate) {
184       gst_value_intersect (&irate, orate, rate);
185       gst_structure_set_value (structure, "rate", &irate);
186     }
187
188     if (ochans) {
189       gst_value_intersect (&ichans, ochans, chans);
190       gst_structure_set_value (structure, "channels", &ichans);
191     }
192
193   done:
194     gst_caps_unref (othercaps);
195   }
196   return base_caps;
197 }
198
199 static void
200 gst_alaw_dec_base_init (gpointer klass)
201 {
202   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
203
204   gst_element_class_add_pad_template (element_class,
205       gst_static_pad_template_get (&alaw_dec_src_factory));
206   gst_element_class_add_pad_template (element_class,
207       gst_static_pad_template_get (&alaw_dec_sink_factory));
208
209   gst_element_class_set_details_simple (element_class, "A Law audio decoder",
210       "Codec/Decoder/Audio", "Convert 8bit A law to 16bit PCM",
211       "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
212
213   GST_DEBUG_CATEGORY_INIT (alaw_dec_debug, "alawdec", 0, "A Law audio decoder");
214 }
215
216 static void
217 gst_alaw_dec_class_init (GstALawDecClass * klass)
218 {
219   GstElementClass *element_class = (GstElementClass *) klass;
220
221   element_class->change_state = GST_DEBUG_FUNCPTR (gst_alaw_dec_change_state);
222 }
223
224 static void
225 gst_alaw_dec_init (GstALawDec * alawdec, GstALawDecClass * klass)
226 {
227   alawdec->sinkpad =
228       gst_pad_new_from_static_template (&alaw_dec_sink_factory, "sink");
229   gst_pad_set_setcaps_function (alawdec->sinkpad,
230       GST_DEBUG_FUNCPTR (gst_alaw_dec_sink_setcaps));
231   gst_pad_set_getcaps_function (alawdec->sinkpad,
232       GST_DEBUG_FUNCPTR (gst_alaw_dec_getcaps));
233   gst_pad_set_chain_function (alawdec->sinkpad,
234       GST_DEBUG_FUNCPTR (gst_alaw_dec_chain));
235   gst_element_add_pad (GST_ELEMENT (alawdec), alawdec->sinkpad);
236
237   alawdec->srcpad =
238       gst_pad_new_from_static_template (&alaw_dec_src_factory, "src");
239   gst_pad_use_fixed_caps (alawdec->srcpad);
240   gst_pad_set_getcaps_function (alawdec->srcpad,
241       GST_DEBUG_FUNCPTR (gst_alaw_dec_getcaps));
242   gst_element_add_pad (GST_ELEMENT (alawdec), alawdec->srcpad);
243 }
244
245 static GstFlowReturn
246 gst_alaw_dec_chain (GstPad * pad, GstBuffer * buffer)
247 {
248   GstALawDec *alawdec;
249   gint16 *linear_data;
250   guint8 *alaw_data;
251   guint alaw_size;
252   GstBuffer *outbuf;
253   gint i;
254   GstFlowReturn ret;
255
256   alawdec = GST_ALAW_DEC (GST_PAD_PARENT (pad));
257
258   if (G_UNLIKELY (alawdec->rate == 0))
259     goto not_negotiated;
260
261   GST_LOG_OBJECT (alawdec, "buffer with ts=%" GST_TIME_FORMAT,
262       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)));
263
264   alaw_data = GST_BUFFER_DATA (buffer);
265   alaw_size = GST_BUFFER_SIZE (buffer);
266
267   ret =
268       gst_pad_alloc_buffer_and_set_caps (alawdec->srcpad,
269       GST_BUFFER_OFFSET_NONE, alaw_size * 2, GST_PAD_CAPS (alawdec->srcpad),
270       &outbuf);
271   if (ret != GST_FLOW_OK)
272     goto alloc_failed;
273
274   linear_data = (gint16 *) GST_BUFFER_DATA (outbuf);
275
276   /* copy discont flag */
277   if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT))
278     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
279
280   GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buffer);
281   GST_BUFFER_DURATION (outbuf) = GST_BUFFER_DURATION (buffer);
282   gst_buffer_set_caps (outbuf, GST_PAD_CAPS (alawdec->srcpad));
283
284   for (i = 0; i < alaw_size; i++) {
285     linear_data[i] = alaw_to_s16 (alaw_data[i]);
286   }
287   gst_buffer_unref (buffer);
288
289   ret = gst_pad_push (alawdec->srcpad, outbuf);
290
291   return ret;
292
293 not_negotiated:
294   {
295     gst_buffer_unref (buffer);
296     GST_ERROR_OBJECT (alawdec, "no format negotiated");
297     ret = GST_FLOW_NOT_NEGOTIATED;
298     return ret;
299   }
300 alloc_failed:
301   {
302     gst_buffer_unref (buffer);
303     GST_ERROR_OBJECT (alawdec, "pad alloc failed");
304     return ret;
305   }
306 }
307
308 static GstStateChangeReturn
309 gst_alaw_dec_change_state (GstElement * element, GstStateChange transition)
310 {
311   GstStateChangeReturn ret;
312   GstALawDec *dec = GST_ALAW_DEC (element);
313
314   switch (transition) {
315     default:
316       break;
317   }
318
319   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
320   if (ret != GST_STATE_CHANGE_SUCCESS)
321     return ret;
322
323   switch (transition) {
324     case GST_STATE_CHANGE_PAUSED_TO_READY:
325       dec->rate = 0;
326       dec->channels = 0;
327       break;
328     default:
329       break;
330   }
331
332   return ret;
333 }