Merge branch 'master' into 0.11
[platform/upstream/gst-plugins-good.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 gboolean gst_mulawenc_event (GstPad * pad, GstObject * parent,
48     GstEvent * event);
49 static GstFlowReturn gst_mulawenc_chain (GstPad * pad, GstObject * parent,
50     GstBuffer * buffer);
51
52 #define gst_mulawenc_parent_class parent_class
53 G_DEFINE_TYPE (GstMuLawEnc, gst_mulawenc, GST_TYPE_ELEMENT);
54
55 /*static guint gst_stereo_signals[LAST_SIGNAL] = { 0 }; */
56
57 static GstCaps *
58 mulawenc_getcaps (GstPad * pad, GstCaps * filter)
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_query_caps (otherpad, filter);
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 gst_mulawenc_query (GstPad * pad, GstObject * parent, GstQuery * query)
120 {
121   gboolean res;
122
123   switch (GST_QUERY_TYPE (query)) {
124     case GST_QUERY_CAPS:
125     {
126       GstCaps *filter, *caps;
127
128       gst_query_parse_caps (query, &filter);
129       caps = mulawenc_getcaps (pad, filter);
130       gst_query_set_caps_result (query, caps);
131       gst_caps_unref (caps);
132
133       res = TRUE;
134       break;
135     }
136     default:
137       res = gst_pad_query_default (pad, parent, query);
138       break;
139   }
140   return res;
141 }
142
143
144 static gboolean
145 mulawenc_setcaps (GstMuLawEnc * mulawenc, GstCaps * caps)
146 {
147   GstStructure *structure;
148   GstCaps *base_caps;
149
150   structure = gst_caps_get_structure (caps, 0);
151   gst_structure_get_int (structure, "channels", &mulawenc->channels);
152   gst_structure_get_int (structure, "rate", &mulawenc->rate);
153
154   base_caps = gst_caps_copy (gst_pad_get_pad_template_caps (mulawenc->srcpad));
155
156   structure = gst_caps_get_structure (base_caps, 0);
157   gst_structure_set (structure, "rate", G_TYPE_INT, mulawenc->rate, NULL);
158   gst_structure_set (structure, "channels", G_TYPE_INT, mulawenc->channels,
159       NULL);
160
161   gst_pad_set_caps (mulawenc->srcpad, base_caps);
162
163   gst_caps_unref (base_caps);
164
165   return TRUE;
166 }
167
168 static void
169 gst_mulawenc_class_init (GstMuLawEncClass * klass)
170 {
171   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
172
173   gst_element_class_add_pad_template (element_class,
174       gst_static_pad_template_get (&mulaw_enc_src_factory));
175   gst_element_class_add_pad_template (element_class,
176       gst_static_pad_template_get (&mulaw_enc_sink_factory));
177
178   gst_element_class_set_details_simple (element_class, "Mu Law audio encoder",
179       "Codec/Encoder/Audio",
180       "Convert 16bit PCM to 8bit mu law",
181       "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
182 }
183
184 static void
185 gst_mulawenc_init (GstMuLawEnc * mulawenc)
186 {
187   mulawenc->sinkpad =
188       gst_pad_new_from_static_template (&mulaw_enc_sink_factory, "sink");
189   gst_pad_set_query_function (mulawenc->sinkpad, gst_mulawenc_query);
190   gst_pad_set_event_function (mulawenc->sinkpad, gst_mulawenc_event);
191   gst_pad_set_chain_function (mulawenc->sinkpad, gst_mulawenc_chain);
192   gst_element_add_pad (GST_ELEMENT (mulawenc), mulawenc->sinkpad);
193
194   mulawenc->srcpad =
195       gst_pad_new_from_static_template (&mulaw_enc_src_factory, "src");
196   gst_pad_set_query_function (mulawenc->srcpad, gst_mulawenc_query);
197   gst_element_add_pad (GST_ELEMENT (mulawenc), mulawenc->srcpad);
198
199   /* init rest */
200   mulawenc->channels = 0;
201   mulawenc->rate = 0;
202 }
203
204 static gboolean
205 gst_mulawenc_event (GstPad * pad, GstObject * parent, GstEvent * event)
206 {
207   GstMuLawEnc *mulawenc;
208   gboolean res;
209
210   mulawenc = GST_MULAWENC (parent);
211
212   switch (GST_EVENT_TYPE (event)) {
213     case GST_EVENT_CAPS:
214     {
215       GstCaps *caps;
216
217       gst_event_parse_caps (event, &caps);
218       mulawenc_setcaps (mulawenc, caps);
219       gst_event_unref (event);
220
221       res = TRUE;
222       break;
223     }
224     default:
225       res = gst_pad_event_default (pad, parent, event);
226       break;
227   }
228   return res;
229 }
230
231 static GstFlowReturn
232 gst_mulawenc_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
233 {
234   GstMuLawEnc *mulawenc;
235   gint16 *linear_data;
236   gsize linear_size;
237   guint8 *mulaw_data;
238   guint mulaw_size;
239   GstBuffer *outbuf;
240   GstFlowReturn ret;
241   GstClockTime timestamp, duration;
242
243   mulawenc = GST_MULAWENC (parent);
244
245   if (!mulawenc->rate || !mulawenc->channels)
246     goto not_negotiated;
247
248   linear_data = gst_buffer_map (buffer, &linear_size, NULL, GST_MAP_READ);
249
250   mulaw_size = linear_size / 2;
251
252   timestamp = GST_BUFFER_TIMESTAMP (buffer);
253   duration = GST_BUFFER_DURATION (buffer);
254
255   outbuf = gst_buffer_new_allocate (NULL, mulaw_size, 0);
256
257   if (duration == -1) {
258     duration = gst_util_uint64_scale_int (mulaw_size,
259         GST_SECOND, mulawenc->rate * mulawenc->channels);
260   }
261
262   mulaw_data = gst_buffer_map (outbuf, NULL, NULL, GST_MAP_WRITE);
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   mulaw_encode (linear_data, mulaw_data, mulaw_size);
272
273   gst_buffer_unmap (outbuf, mulaw_data, -1);
274   gst_buffer_unmap (buffer, linear_data, -1);
275   gst_buffer_unref (buffer);
276
277   ret = gst_pad_push (mulawenc->srcpad, outbuf);
278
279 done:
280
281   return ret;
282
283 not_negotiated:
284   {
285     GST_DEBUG_OBJECT (mulawenc, "no format negotiated");
286     ret = GST_FLOW_NOT_NEGOTIATED;
287     gst_buffer_unref (buffer);
288     goto done;
289   }
290 }