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, GstEvent * event);
48 static GstFlowReturn gst_mulawenc_chain (GstPad * pad, GstBuffer * buffer);
49
50 #define gst_mulawenc_parent_class parent_class
51 G_DEFINE_TYPE (GstMuLawEnc, gst_mulawenc, GST_TYPE_ELEMENT);
52
53 /*static guint gst_stereo_signals[LAST_SIGNAL] = { 0 }; */
54
55 static GstCaps *
56 mulawenc_getcaps (GstPad * pad, GstCaps * filter)
57 {
58   GstMuLawEnc *mulawenc;
59   GstPad *otherpad;
60   GstCaps *othercaps, *result;
61   const GstCaps *templ;
62   const gchar *name;
63   gint i;
64
65   mulawenc = GST_MULAWENC (GST_PAD_PARENT (pad));
66
67   /* figure out the name of the caps we are going to return */
68   if (pad == mulawenc->srcpad) {
69     name = "audio/x-mulaw";
70     otherpad = mulawenc->sinkpad;
71   } else {
72     name = "audio/x-raw-int";
73     otherpad = mulawenc->srcpad;
74   }
75   /* get caps from the peer, this can return NULL when there is no peer */
76   othercaps = gst_pad_peer_get_caps (otherpad, filter);
77
78   /* get the template caps to make sure we return something acceptable */
79   templ = gst_pad_get_pad_template_caps (pad);
80
81   if (othercaps) {
82     /* there was a peer */
83     othercaps = gst_caps_make_writable (othercaps);
84
85     /* go through the caps and remove the fields we don't want */
86     for (i = 0; i < gst_caps_get_size (othercaps); i++) {
87       GstStructure *structure;
88
89       structure = gst_caps_get_structure (othercaps, i);
90
91       /* adjust the name */
92       gst_structure_set_name (structure, name);
93
94       if (pad == mulawenc->srcpad) {
95         /* remove the fields we don't want */
96         gst_structure_remove_fields (structure, "width", "depth", "endianness",
97             "signed", NULL);
98       } else {
99         /* add fixed fields */
100         gst_structure_set (structure, "width", G_TYPE_INT, 16,
101             "depth", G_TYPE_INT, 16,
102             "endianness", G_TYPE_INT, G_BYTE_ORDER,
103             "signed", G_TYPE_BOOLEAN, TRUE, NULL);
104       }
105     }
106     /* filter against the allowed caps of the pad to return our result */
107     result = gst_caps_intersect (othercaps, templ);
108     gst_caps_unref (othercaps);
109   } else {
110     /* there was no peer, return the template caps */
111     result = gst_caps_copy (templ);
112   }
113   return result;
114 }
115
116 static gboolean
117 mulawenc_setcaps (GstMuLawEnc * mulawenc, GstCaps * caps)
118 {
119   GstStructure *structure;
120   GstCaps *base_caps;
121
122   structure = gst_caps_get_structure (caps, 0);
123   gst_structure_get_int (structure, "channels", &mulawenc->channels);
124   gst_structure_get_int (structure, "rate", &mulawenc->rate);
125
126   base_caps = gst_caps_copy (gst_pad_get_pad_template_caps (mulawenc->srcpad));
127
128   structure = gst_caps_get_structure (base_caps, 0);
129   gst_structure_set (structure, "rate", G_TYPE_INT, mulawenc->rate, NULL);
130   gst_structure_set (structure, "channels", G_TYPE_INT, mulawenc->channels,
131       NULL);
132
133   gst_pad_set_caps (mulawenc->srcpad, base_caps);
134
135   gst_caps_unref (base_caps);
136
137   return TRUE;
138 }
139
140 static void
141 gst_mulawenc_class_init (GstMuLawEncClass * klass)
142 {
143   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
144
145   gst_element_class_add_pad_template (element_class,
146       gst_static_pad_template_get (&mulaw_enc_src_factory));
147   gst_element_class_add_pad_template (element_class,
148       gst_static_pad_template_get (&mulaw_enc_sink_factory));
149
150   gst_element_class_set_details_simple (element_class, "Mu Law audio encoder",
151       "Codec/Encoder/Audio",
152       "Convert 16bit PCM to 8bit mu law",
153       "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
154 }
155
156 static void
157 gst_mulawenc_init (GstMuLawEnc * mulawenc)
158 {
159   mulawenc->sinkpad =
160       gst_pad_new_from_static_template (&mulaw_enc_sink_factory, "sink");
161   gst_pad_set_getcaps_function (mulawenc->sinkpad, mulawenc_getcaps);
162   gst_pad_set_event_function (mulawenc->sinkpad, gst_mulawenc_event);
163   gst_pad_set_chain_function (mulawenc->sinkpad, gst_mulawenc_chain);
164   gst_element_add_pad (GST_ELEMENT (mulawenc), mulawenc->sinkpad);
165
166   mulawenc->srcpad =
167       gst_pad_new_from_static_template (&mulaw_enc_src_factory, "src");
168   gst_pad_set_getcaps_function (mulawenc->srcpad, mulawenc_getcaps);
169   gst_pad_use_fixed_caps (mulawenc->srcpad);
170   gst_element_add_pad (GST_ELEMENT (mulawenc), mulawenc->srcpad);
171
172   /* init rest */
173   mulawenc->channels = 0;
174   mulawenc->rate = 0;
175 }
176
177 static gboolean
178 gst_mulawenc_event (GstPad * pad, GstEvent * event)
179 {
180   GstMuLawEnc *mulawenc;
181   gboolean res;
182
183   mulawenc = GST_MULAWENC (GST_PAD_PARENT (pad));
184
185   switch (GST_EVENT_TYPE (event)) {
186     case GST_EVENT_CAPS:
187     {
188       GstCaps *caps;
189
190       gst_event_parse_caps (event, &caps);
191       mulawenc_setcaps (mulawenc, caps);
192       gst_event_unref (event);
193
194       res = TRUE;
195       break;
196     }
197     default:
198       res = gst_pad_event_default (pad, event);
199       break;
200   }
201   return res;
202 }
203
204 static GstFlowReturn
205 gst_mulawenc_chain (GstPad * pad, GstBuffer * buffer)
206 {
207   GstMuLawEnc *mulawenc;
208   gint16 *linear_data;
209   gsize linear_size;
210   guint8 *mulaw_data;
211   guint mulaw_size;
212   GstBuffer *outbuf;
213   GstFlowReturn ret;
214   GstClockTime timestamp, duration;
215
216   mulawenc = GST_MULAWENC (gst_pad_get_parent (pad));
217
218   if (!mulawenc->rate || !mulawenc->channels)
219     goto not_negotiated;
220
221   linear_data = gst_buffer_map (buffer, &linear_size, NULL, GST_MAP_READ);
222
223   mulaw_size = linear_size / 2;
224
225   timestamp = GST_BUFFER_TIMESTAMP (buffer);
226   duration = GST_BUFFER_DURATION (buffer);
227
228   outbuf = gst_buffer_new_allocate (NULL, mulaw_size, 0);
229
230   if (duration == -1) {
231     duration = gst_util_uint64_scale_int (mulaw_size,
232         GST_SECOND, mulawenc->rate * mulawenc->channels);
233   }
234
235   mulaw_data = gst_buffer_map (outbuf, NULL, NULL, GST_MAP_WRITE);
236
237   /* copy discont flag */
238   if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT))
239     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
240
241   GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
242   GST_BUFFER_DURATION (outbuf) = duration;
243
244   mulaw_encode (linear_data, mulaw_data, mulaw_size);
245
246   gst_buffer_unmap (outbuf, mulaw_data, -1);
247   gst_buffer_unmap (buffer, linear_data, -1);
248   gst_buffer_unref (buffer);
249
250   ret = gst_pad_push (mulawenc->srcpad, outbuf);
251
252 done:
253   gst_object_unref (mulawenc);
254
255   return ret;
256
257 not_negotiated:
258   {
259     GST_DEBUG_OBJECT (mulawenc, "no format negotiated");
260     ret = GST_FLOW_NOT_NEGOTIATED;
261     gst_buffer_unref (buffer);
262     goto done;
263   }
264 }