2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
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.
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.
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.
20 * SECTION:element-mulawenc
22 * This element encode mulaw audio. Mulaw coding is also known as G.711.
29 #include "mulaw-encode.h"
30 #include "mulaw-conversion.h"
32 extern GstStaticPadTemplate mulaw_enc_src_factory;
33 extern GstStaticPadTemplate mulaw_enc_sink_factory;
35 /* Stereo signals and args */
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);
51 static GstFlowReturn gst_mulawenc_chain (GstPad * pad, GstBuffer * buffer);
53 static GstElementClass *parent_class = NULL;
55 /*static guint gst_stereo_signals[LAST_SIGNAL] = { 0 }; */
58 mulawenc_getcaps (GstPad * pad)
60 GstMuLawEnc *mulawenc;
62 GstCaps *othercaps, *result;
67 mulawenc = GST_MULAWENC (GST_PAD_PARENT (pad));
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;
74 name = "audio/x-raw-int";
75 otherpad = mulawenc->srcpad;
77 /* get caps from the peer, this can return NULL when there is no peer */
78 othercaps = gst_pad_peer_get_caps (otherpad);
80 /* get the template caps to make sure we return something acceptable */
81 templ = gst_pad_get_pad_template_caps (pad);
84 /* there was a peer */
85 othercaps = gst_caps_make_writable (othercaps);
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;
91 structure = gst_caps_get_structure (othercaps, i);
94 gst_structure_set_name (structure, name);
96 if (pad == mulawenc->srcpad) {
97 /* remove the fields we don't want */
98 gst_structure_remove_fields (structure, "width", "depth", "endianness",
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);
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);
112 /* there was no peer, return the template caps */
113 result = gst_caps_copy (templ);
119 mulawenc_setcaps (GstPad * pad, GstCaps * caps)
121 GstMuLawEnc *mulawenc;
123 GstStructure *structure;
126 mulawenc = GST_MULAWENC (gst_pad_get_parent (pad));
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);
132 if (pad == mulawenc->sinkpad) {
133 otherpad = mulawenc->srcpad;
135 otherpad = mulawenc->sinkpad;
137 base_caps = gst_caps_copy (gst_pad_get_pad_template_caps (otherpad));
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,
144 gst_pad_set_caps (otherpad, base_caps);
146 gst_object_unref (mulawenc);
147 gst_caps_unref (base_caps);
153 gst_mulawenc_get_type (void)
155 static GType mulawenc_type = 0;
157 if (!mulawenc_type) {
158 static const GTypeInfo mulawenc_info = {
159 sizeof (GstMuLawEncClass),
160 (GBaseInitFunc) gst_mulawenc_base_init,
162 (GClassInitFunc) gst_mulawenc_class_init,
165 sizeof (GstMuLawEnc),
167 (GInstanceInitFunc) gst_mulawenc_init,
171 g_type_register_static (GST_TYPE_ELEMENT, "GstMuLawEnc", &mulawenc_info,
174 return mulawenc_type;
178 gst_mulawenc_base_init (GstMuLawEncClass * klass)
180 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
181 const GstElementDetails mulawenc_details =
182 GST_ELEMENT_DETAILS ("Mu Law audio encoder",
183 "Codec/Encoder/Audio",
184 "Convert 16bit PCM to 8bit mu law",
185 "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
187 gst_element_class_add_pad_template (element_class,
188 gst_static_pad_template_get (&mulaw_enc_src_factory));
189 gst_element_class_add_pad_template (element_class,
190 gst_static_pad_template_get (&mulaw_enc_sink_factory));
191 gst_element_class_set_details (element_class, &mulawenc_details);
195 gst_mulawenc_class_init (GstMuLawEncClass * klass)
197 parent_class = g_type_class_peek_parent (klass);
201 gst_mulawenc_init (GstMuLawEnc * mulawenc)
204 gst_pad_new_from_static_template (&mulaw_enc_sink_factory, "sink");
205 gst_pad_set_setcaps_function (mulawenc->sinkpad, mulawenc_setcaps);
206 gst_pad_set_getcaps_function (mulawenc->sinkpad, mulawenc_getcaps);
207 gst_pad_set_chain_function (mulawenc->sinkpad, gst_mulawenc_chain);
208 gst_element_add_pad (GST_ELEMENT (mulawenc), mulawenc->sinkpad);
211 gst_pad_new_from_static_template (&mulaw_enc_src_factory, "src");
212 gst_pad_set_setcaps_function (mulawenc->srcpad, mulawenc_setcaps);
213 gst_pad_set_getcaps_function (mulawenc->srcpad, mulawenc_getcaps);
214 gst_pad_use_fixed_caps (mulawenc->srcpad);
215 gst_element_add_pad (GST_ELEMENT (mulawenc), mulawenc->srcpad);
218 mulawenc->channels = 0;
223 gst_mulawenc_chain (GstPad * pad, GstBuffer * buffer)
225 GstMuLawEnc *mulawenc;
232 GstClockTime timestamp, duration;
234 mulawenc = GST_MULAWENC (gst_pad_get_parent (pad));
236 if (!mulawenc->rate || !mulawenc->channels)
239 linear_data = (gint16 *) GST_BUFFER_DATA (buffer);
240 linear_size = GST_BUFFER_SIZE (buffer);
242 mulaw_size = linear_size / 2;
244 timestamp = GST_BUFFER_TIMESTAMP (buffer);
245 duration = GST_BUFFER_DURATION (buffer);
247 ret = gst_pad_alloc_buffer_and_set_caps (mulawenc->srcpad,
248 GST_BUFFER_OFFSET_NONE, mulaw_size, GST_PAD_CAPS (mulawenc->srcpad),
250 if (ret != GST_FLOW_OK)
253 if (duration == -1) {
254 duration = gst_util_uint64_scale_int (mulaw_size,
255 GST_SECOND, mulawenc->rate * mulawenc->channels);
258 if (GST_BUFFER_SIZE (outbuf) < mulaw_size) {
259 /* pad-alloc can suggest a smaller size */
260 gst_buffer_unref (outbuf);
261 outbuf = gst_buffer_new_and_alloc (mulaw_size);
264 mulaw_data = (guint8 *) GST_BUFFER_DATA (outbuf);
266 /* copy discont flag */
267 if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT))
268 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
270 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
271 GST_BUFFER_DURATION (outbuf) = duration;
273 gst_buffer_set_caps (outbuf, GST_PAD_CAPS (mulawenc->srcpad));
275 mulaw_encode (linear_data, mulaw_data, mulaw_size);
277 gst_buffer_unref (buffer);
279 ret = gst_pad_push (mulawenc->srcpad, outbuf);
282 gst_object_unref (mulawenc);
288 GST_DEBUG_OBJECT (mulawenc, "no format negotiated");
289 ret = GST_FLOW_NOT_NEGOTIATED;
290 gst_buffer_unref (buffer);
295 GST_DEBUG_OBJECT (mulawenc, "pad alloc failed");
296 gst_buffer_unref (buffer);