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