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