Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.git] / gst / law / mulaw-encode.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-mulawenc
21  *
22  * This element encode 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 #include "mulaw-encode.h"
30 #include "mulaw-conversion.h"
31
32 extern GstStaticPadTemplate mulaw_enc_src_factory;
33 extern GstStaticPadTemplate mulaw_enc_sink_factory;
34
35 /* Stereo signals and args */
36 enum
37 {
38   /* FILL ME */
39   LAST_SIGNAL
40 };
41
42 enum
43 {
44   ARG_0
45 };
46
47 static void gst_mulawenc_class_init (GstMuLawEncClass * klass);
48 static void gst_mulawenc_base_init (GstMuLawEncClass * klass);
49 static void gst_mulawenc_init (GstMuLawEnc * mulawenc);
50
51 static GstFlowReturn gst_mulawenc_chain (GstPad * pad, GstBuffer * buffer);
52
53 static GstElementClass *parent_class = NULL;
54
55 /*static guint gst_stereo_signals[LAST_SIGNAL] = { 0 }; */
56
57 static GstCaps *
58 mulawenc_getcaps (GstPad * pad)
59 {
60   GstMuLawEnc *mulawenc;
61   GstPad *otherpad;
62   GstCaps *othercaps, *result;
63   const GstCaps *templ;
64   const gchar *name;
65   gint i;
66
67   mulawenc = GST_MULAWENC (GST_PAD_PARENT (pad));
68
69   /* figure out the name of the caps we are going to return */
70   if (pad == mulawenc->srcpad) {
71     name = "audio/x-mulaw";
72     otherpad = mulawenc->sinkpad;
73   } else {
74     name = "audio/x-raw-int";
75     otherpad = mulawenc->srcpad;
76   }
77   /* get caps from the peer, this can return NULL when there is no peer */
78   othercaps = gst_pad_peer_get_caps (otherpad);
79
80   /* get the template caps to make sure we return something acceptable */
81   templ = gst_pad_get_pad_template_caps (pad);
82
83   if (othercaps) {
84     /* there was a peer */
85     othercaps = gst_caps_make_writable (othercaps);
86
87     /* go through the caps and remove the fields we don't want */
88     for (i = 0; i < gst_caps_get_size (othercaps); i++) {
89       GstStructure *structure;
90
91       structure = gst_caps_get_structure (othercaps, i);
92
93       /* adjust the name */
94       gst_structure_set_name (structure, name);
95
96       if (pad == mulawenc->srcpad) {
97         /* remove the fields we don't want */
98         gst_structure_remove_fields (structure, "width", "depth", "endianness",
99             "signed", NULL);
100       } else {
101         /* add fixed fields */
102         gst_structure_set (structure, "width", G_TYPE_INT, 16,
103             "depth", G_TYPE_INT, 16,
104             "endianness", G_TYPE_INT, G_BYTE_ORDER,
105             "signed", G_TYPE_BOOLEAN, TRUE, NULL);
106       }
107     }
108     /* filter against the allowed caps of the pad to return our result */
109     result = gst_caps_intersect (othercaps, templ);
110     gst_caps_unref (othercaps);
111   } else {
112     /* there was no peer, return the template caps */
113     result = gst_caps_copy (templ);
114   }
115   return result;
116 }
117
118 static gboolean
119 mulawenc_setcaps (GstPad * pad, GstCaps * caps)
120 {
121   GstMuLawEnc *mulawenc;
122   GstPad *otherpad;
123   GstStructure *structure;
124   GstCaps *base_caps;
125
126   mulawenc = GST_MULAWENC (gst_pad_get_parent (pad));
127
128   structure = gst_caps_get_structure (caps, 0);
129   gst_structure_get_int (structure, "channels", &mulawenc->channels);
130   gst_structure_get_int (structure, "rate", &mulawenc->rate);
131
132   if (pad == mulawenc->sinkpad) {
133     otherpad = mulawenc->srcpad;
134   } else {
135     otherpad = mulawenc->sinkpad;
136   }
137   base_caps = gst_caps_copy (gst_pad_get_pad_template_caps (otherpad));
138
139   structure = gst_caps_get_structure (base_caps, 0);
140   gst_structure_set (structure, "rate", G_TYPE_INT, mulawenc->rate, NULL);
141   gst_structure_set (structure, "channels", G_TYPE_INT, mulawenc->channels,
142       NULL);
143
144   gst_pad_set_caps (otherpad, base_caps);
145
146   gst_object_unref (mulawenc);
147   gst_caps_unref (base_caps);
148
149   return TRUE;
150 }
151
152 GType
153 gst_mulawenc_get_type (void)
154 {
155   static GType mulawenc_type = 0;
156
157   if (!mulawenc_type) {
158     static const GTypeInfo mulawenc_info = {
159       sizeof (GstMuLawEncClass),
160       (GBaseInitFunc) gst_mulawenc_base_init,
161       NULL,
162       (GClassInitFunc) gst_mulawenc_class_init,
163       NULL,
164       NULL,
165       sizeof (GstMuLawEnc),
166       0,
167       (GInstanceInitFunc) gst_mulawenc_init,
168     };
169
170     mulawenc_type =
171         g_type_register_static (GST_TYPE_ELEMENT, "GstMuLawEnc", &mulawenc_info,
172         0);
173   }
174   return mulawenc_type;
175 }
176
177 static void
178 gst_mulawenc_base_init (GstMuLawEncClass * klass)
179 {
180   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
181
182   gst_element_class_add_static_pad_template (element_class,
183       &mulaw_enc_src_factory);
184   gst_element_class_add_static_pad_template (element_class,
185       &mulaw_enc_sink_factory);
186   gst_element_class_set_details_simple (element_class, "Mu Law audio encoder",
187       "Codec/Encoder/Audio",
188       "Convert 16bit PCM to 8bit mu law",
189       "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
190 }
191
192 static void
193 gst_mulawenc_class_init (GstMuLawEncClass * klass)
194 {
195   parent_class = g_type_class_peek_parent (klass);
196 }
197
198 static void
199 gst_mulawenc_init (GstMuLawEnc * mulawenc)
200 {
201   mulawenc->sinkpad =
202       gst_pad_new_from_static_template (&mulaw_enc_sink_factory, "sink");
203   gst_pad_set_setcaps_function (mulawenc->sinkpad, mulawenc_setcaps);
204   gst_pad_set_getcaps_function (mulawenc->sinkpad, mulawenc_getcaps);
205   gst_pad_set_chain_function (mulawenc->sinkpad, gst_mulawenc_chain);
206   gst_element_add_pad (GST_ELEMENT (mulawenc), mulawenc->sinkpad);
207
208   mulawenc->srcpad =
209       gst_pad_new_from_static_template (&mulaw_enc_src_factory, "src");
210   gst_pad_set_setcaps_function (mulawenc->srcpad, mulawenc_setcaps);
211   gst_pad_set_getcaps_function (mulawenc->srcpad, mulawenc_getcaps);
212   gst_pad_use_fixed_caps (mulawenc->srcpad);
213   gst_element_add_pad (GST_ELEMENT (mulawenc), mulawenc->srcpad);
214
215   /* init rest */
216   mulawenc->channels = 0;
217   mulawenc->rate = 0;
218 }
219
220 static GstFlowReturn
221 gst_mulawenc_chain (GstPad * pad, GstBuffer * buffer)
222 {
223   GstMuLawEnc *mulawenc;
224   gint16 *linear_data;
225   guint linear_size;
226   guint8 *mulaw_data;
227   guint mulaw_size;
228   GstBuffer *outbuf;
229   GstFlowReturn ret;
230   GstClockTime timestamp, duration;
231
232   mulawenc = GST_MULAWENC (gst_pad_get_parent (pad));
233
234   if (!mulawenc->rate || !mulawenc->channels)
235     goto not_negotiated;
236
237   linear_data = (gint16 *) GST_BUFFER_DATA (buffer);
238   linear_size = GST_BUFFER_SIZE (buffer);
239
240   mulaw_size = linear_size / 2;
241
242   timestamp = GST_BUFFER_TIMESTAMP (buffer);
243   duration = GST_BUFFER_DURATION (buffer);
244
245   ret = gst_pad_alloc_buffer_and_set_caps (mulawenc->srcpad,
246       GST_BUFFER_OFFSET_NONE, mulaw_size, GST_PAD_CAPS (mulawenc->srcpad),
247       &outbuf);
248   if (ret != GST_FLOW_OK)
249     goto alloc_failed;
250
251   if (duration == -1) {
252     duration = gst_util_uint64_scale_int (mulaw_size,
253         GST_SECOND, mulawenc->rate * mulawenc->channels);
254   }
255
256   if (GST_BUFFER_SIZE (outbuf) < mulaw_size) {
257     /* pad-alloc can suggest a smaller size */
258     gst_buffer_unref (outbuf);
259     outbuf = gst_buffer_new_and_alloc (mulaw_size);
260   }
261
262   mulaw_data = (guint8 *) GST_BUFFER_DATA (outbuf);
263
264   /* copy discont flag */
265   if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT))
266     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
267
268   GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
269   GST_BUFFER_DURATION (outbuf) = duration;
270
271   gst_buffer_set_caps (outbuf, GST_PAD_CAPS (mulawenc->srcpad));
272
273   mulaw_encode (linear_data, mulaw_data, mulaw_size);
274
275   gst_buffer_unref (buffer);
276
277   ret = gst_pad_push (mulawenc->srcpad, outbuf);
278
279 done:
280   gst_object_unref (mulawenc);
281
282   return ret;
283
284 not_negotiated:
285   {
286     GST_DEBUG_OBJECT (mulawenc, "no format negotiated");
287     ret = GST_FLOW_NOT_NEGOTIATED;
288     gst_buffer_unref (buffer);
289     goto done;
290   }
291 alloc_failed:
292   {
293     GST_DEBUG_OBJECT (mulawenc, "pad alloc failed");
294     gst_buffer_unref (buffer);
295     goto done;
296   }
297 }