_peer_get_caps() -> _peer_query_caps()
[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
40 static gboolean gst_alaw_dec_event (GstPad * pad, GstEvent * event);
41 static GstFlowReturn gst_alaw_dec_chain (GstPad * pad, GstBuffer * buffer);
42
43 #define gst_alaw_dec_parent_class parent_class
44 G_DEFINE_TYPE (GstALawDec, gst_alaw_dec, GST_TYPE_ELEMENT);
45
46 /* some day we might have defines in gstconfig.h that tell us about the
47  * desired cpu/memory/binary size trade-offs */
48 #define GST_ALAW_DEC_USE_TABLE
49
50 #ifdef GST_ALAW_DEC_USE_TABLE
51
52 static const gint alaw_to_s16_table[256] = {
53   -5504, -5248, -6016, -5760, -4480, -4224, -4992, -4736,
54   -7552, -7296, -8064, -7808, -6528, -6272, -7040, -6784,
55   -2752, -2624, -3008, -2880, -2240, -2112, -2496, -2368,
56   -3776, -3648, -4032, -3904, -3264, -3136, -3520, -3392,
57   -22016, -20992, -24064, -23040, -17920, -16896, -19968, -18944,
58   -30208, -29184, -32256, -31232, -26112, -25088, -28160, -27136,
59   -11008, -10496, -12032, -11520, -8960, -8448, -9984, -9472,
60   -15104, -14592, -16128, -15616, -13056, -12544, -14080, -13568,
61   -344, -328, -376, -360, -280, -264, -312, -296,
62   -472, -456, -504, -488, -408, -392, -440, -424,
63   -88, -72, -120, -104, -24, -8, -56, -40,
64   -216, -200, -248, -232, -152, -136, -184, -168,
65   -1376, -1312, -1504, -1440, -1120, -1056, -1248, -1184,
66   -1888, -1824, -2016, -1952, -1632, -1568, -1760, -1696,
67   -688, -656, -752, -720, -560, -528, -624, -592,
68   -944, -912, -1008, -976, -816, -784, -880, -848,
69   5504, 5248, 6016, 5760, 4480, 4224, 4992, 4736,
70   7552, 7296, 8064, 7808, 6528, 6272, 7040, 6784,
71   2752, 2624, 3008, 2880, 2240, 2112, 2496, 2368,
72   3776, 3648, 4032, 3904, 3264, 3136, 3520, 3392,
73   22016, 20992, 24064, 23040, 17920, 16896, 19968, 18944,
74   30208, 29184, 32256, 31232, 26112, 25088, 28160, 27136,
75   11008, 10496, 12032, 11520, 8960, 8448, 9984, 9472,
76   15104, 14592, 16128, 15616, 13056, 12544, 14080, 13568,
77   344, 328, 376, 360, 280, 264, 312, 296,
78   472, 456, 504, 488, 408, 392, 440, 424,
79   88, 72, 120, 104, 24, 8, 56, 40,
80   216, 200, 248, 232, 152, 136, 184, 168,
81   1376, 1312, 1504, 1440, 1120, 1056, 1248, 1184,
82   1888, 1824, 2016, 1952, 1632, 1568, 1760, 1696,
83   688, 656, 752, 720, 560, 528, 624, 592,
84   944, 912, 1008, 976, 816, 784, 880, 848
85 };
86
87 static inline gint
88 alaw_to_s16 (guint8 a_val)
89 {
90   return alaw_to_s16_table[a_val];
91 }
92
93 #else /* GST_ALAW_DEC_USE_TABLE */
94
95 static inline gint
96 alaw_to_s16 (guint8 a_val)
97 {
98   gint t;
99   gint seg;
100
101   a_val ^= 0x55;
102   t = a_val & 0x7f;
103   if (t < 16)
104     t = (t << 4) + 8;
105   else {
106     seg = (t >> 4) & 0x07;
107     t = ((t & 0x0f) << 4) + 0x108;
108     t <<= seg - 1;
109   }
110   return ((a_val & 0x80) ? t : -t);
111 }
112
113 #endif /* GST_ALAW_DEC_USE_TABLE */
114
115 static gboolean
116 gst_alaw_dec_setcaps (GstALawDec * alawdec, GstCaps * caps)
117 {
118   GstStructure *structure;
119   int rate, channels;
120   gboolean ret;
121   GstCaps *outcaps;
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, GstCaps * filter)
150 {
151   GstALawDec *alawdec;
152   GstPad *otherpad;
153   GstCaps *othercaps, *result;
154   const GstCaps *templ;
155   const gchar *name;
156   gint i;
157
158   alawdec = GST_ALAW_DEC (GST_PAD_PARENT (pad));
159
160   /* figure out the name of the caps we are going to return */
161   if (pad == alawdec->srcpad) {
162     name = "audio/x-raw-int";
163     otherpad = alawdec->sinkpad;
164   } else {
165     name = "audio/x-alaw";
166     otherpad = alawdec->srcpad;
167   }
168   /* get caps from the peer, this can return NULL when there is no peer */
169   othercaps = gst_pad_peer_query_caps (otherpad, filter);
170
171   /* get the template caps to make sure we return something acceptable */
172   templ = gst_pad_get_pad_template_caps (pad);
173
174   if (othercaps) {
175     /* there was a peer */
176     othercaps = gst_caps_make_writable (othercaps);
177
178     /* go through the caps and remove the fields we don't want */
179     for (i = 0; i < gst_caps_get_size (othercaps); i++) {
180       GstStructure *structure;
181
182       structure = gst_caps_get_structure (othercaps, i);
183
184       /* adjust the name */
185       gst_structure_set_name (structure, name);
186
187       if (pad == alawdec->sinkpad) {
188         /* remove the fields we don't want */
189         gst_structure_remove_fields (structure, "width", "depth", "endianness",
190             "signed", NULL);
191       } else {
192         /* add fixed fields */
193         gst_structure_set (structure, "width", G_TYPE_INT, 16,
194             "depth", G_TYPE_INT, 16,
195             "endianness", G_TYPE_INT, G_BYTE_ORDER,
196             "signed", G_TYPE_BOOLEAN, TRUE, NULL);
197       }
198     }
199     /* filter against the allowed caps of the pad to return our result */
200     result = gst_caps_intersect (othercaps, templ);
201     gst_caps_unref (othercaps);
202   } else {
203     /* there was no peer, return the template caps */
204     result = gst_caps_copy (templ);
205   }
206
207   return result;
208 }
209
210 static gboolean
211 gst_alaw_dec_query (GstPad * pad, GstQuery * query)
212 {
213   gboolean res;
214
215   switch (GST_QUERY_TYPE (query)) {
216     case GST_QUERY_CAPS:
217     {
218       GstCaps *filter, *caps;
219
220       gst_query_parse_caps (query, &filter);
221       caps = gst_alaw_dec_getcaps (pad, filter);
222       gst_query_set_caps_result (query, caps);
223       gst_caps_unref (caps);
224
225       res = TRUE;
226       break;
227     }
228     default:
229       res = gst_pad_query_default (pad, query);
230       break;
231   }
232   return res;
233 }
234
235 static void
236 gst_alaw_dec_class_init (GstALawDecClass * klass)
237 {
238   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
239
240   gst_element_class_add_pad_template (element_class,
241       gst_static_pad_template_get (&alaw_dec_src_factory));
242   gst_element_class_add_pad_template (element_class,
243       gst_static_pad_template_get (&alaw_dec_sink_factory));
244
245   gst_element_class_set_details_simple (element_class, "A Law audio decoder",
246       "Codec/Decoder/Audio", "Convert 8bit A law to 16bit PCM",
247       "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
248
249   element_class->change_state = GST_DEBUG_FUNCPTR (gst_alaw_dec_change_state);
250
251   GST_DEBUG_CATEGORY_INIT (alaw_dec_debug, "alawdec", 0, "A Law audio decoder");
252 }
253
254 static void
255 gst_alaw_dec_init (GstALawDec * alawdec)
256 {
257   alawdec->sinkpad =
258       gst_pad_new_from_static_template (&alaw_dec_sink_factory, "sink");
259   gst_pad_set_query_function (alawdec->sinkpad,
260       GST_DEBUG_FUNCPTR (gst_alaw_dec_query));
261   gst_pad_set_event_function (alawdec->sinkpad,
262       GST_DEBUG_FUNCPTR (gst_alaw_dec_event));
263   gst_pad_set_chain_function (alawdec->sinkpad,
264       GST_DEBUG_FUNCPTR (gst_alaw_dec_chain));
265   gst_element_add_pad (GST_ELEMENT (alawdec), alawdec->sinkpad);
266
267   alawdec->srcpad =
268       gst_pad_new_from_static_template (&alaw_dec_src_factory, "src");
269   gst_pad_use_fixed_caps (alawdec->srcpad);
270   gst_pad_set_query_function (alawdec->srcpad,
271       GST_DEBUG_FUNCPTR (gst_alaw_dec_query));
272   gst_element_add_pad (GST_ELEMENT (alawdec), alawdec->srcpad);
273 }
274
275 static gboolean
276 gst_alaw_dec_event (GstPad * pad, GstEvent * event)
277 {
278   GstALawDec *alawdec;
279   gboolean res;
280
281   alawdec = GST_ALAW_DEC (GST_PAD_PARENT (pad));
282
283   switch (GST_EVENT_TYPE (event)) {
284     case GST_EVENT_CAPS:
285     {
286       GstCaps *caps;
287
288       gst_event_parse_caps (event, &caps);
289       gst_alaw_dec_setcaps (alawdec, caps);
290       gst_event_unref (event);
291
292       res = TRUE;
293       break;
294     }
295     default:
296       res = gst_pad_event_default (pad, event);
297       break;
298   }
299   return res;
300 }
301
302 static GstFlowReturn
303 gst_alaw_dec_chain (GstPad * pad, GstBuffer * buffer)
304 {
305   GstALawDec *alawdec;
306   gint16 *linear_data;
307   guint8 *alaw_data;
308   gsize alaw_size;
309   GstBuffer *outbuf;
310   gint i;
311   GstFlowReturn ret;
312
313   alawdec = GST_ALAW_DEC (GST_PAD_PARENT (pad));
314
315   if (G_UNLIKELY (alawdec->rate == 0))
316     goto not_negotiated;
317
318   GST_LOG_OBJECT (alawdec, "buffer with ts=%" GST_TIME_FORMAT,
319       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)));
320
321   alaw_data = gst_buffer_map (buffer, &alaw_size, NULL, GST_MAP_READ);
322
323   outbuf = gst_buffer_new_allocate (NULL, alaw_size, 0);
324
325   linear_data = gst_buffer_map (outbuf, NULL, NULL, GST_MAP_WRITE);
326
327   /* copy discont flag */
328   if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT))
329     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
330
331   GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buffer);
332   GST_BUFFER_DURATION (outbuf) = GST_BUFFER_DURATION (buffer);
333
334   for (i = 0; i < alaw_size; i++) {
335     linear_data[i] = alaw_to_s16 (alaw_data[i]);
336   }
337
338   gst_buffer_unmap (outbuf, linear_data, -1);
339   gst_buffer_unmap (buffer, alaw_data, -1);
340   gst_buffer_unref (buffer);
341
342   ret = gst_pad_push (alawdec->srcpad, outbuf);
343
344   return ret;
345
346 not_negotiated:
347   {
348     gst_buffer_unref (buffer);
349     GST_WARNING_OBJECT (alawdec, "no input format set: not-negotiated");
350     return GST_FLOW_NOT_NEGOTIATED;
351   }
352 }
353
354 static GstStateChangeReturn
355 gst_alaw_dec_change_state (GstElement * element, GstStateChange transition)
356 {
357   GstStateChangeReturn ret;
358   GstALawDec *dec = GST_ALAW_DEC (element);
359
360   switch (transition) {
361     default:
362       break;
363   }
364
365   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
366   if (ret != GST_STATE_CHANGE_SUCCESS)
367     return ret;
368
369   switch (transition) {
370     case GST_STATE_CHANGE_PAUSED_TO_READY:
371       dec->rate = 0;
372       dec->channels = 0;
373       break;
374     default:
375       break;
376   }
377
378   return ret;
379 }