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