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 <gst/audio/audio.h>
31 #include "mulaw-encode.h"
32 #include "mulaw-conversion.h"
34 extern GstStaticPadTemplate mulaw_enc_src_factory;
35 extern GstStaticPadTemplate mulaw_enc_sink_factory;
37 /* Stereo signals and args */
49 static gboolean gst_mulawenc_event (GstPad * pad, GstObject * parent,
51 static GstFlowReturn gst_mulawenc_chain (GstPad * pad, GstObject * parent,
54 #define gst_mulawenc_parent_class parent_class
55 G_DEFINE_TYPE (GstMuLawEnc, gst_mulawenc, GST_TYPE_ELEMENT);
57 /*static guint gst_stereo_signals[LAST_SIGNAL] = { 0 }; */
60 mulawenc_getcaps (GstPad * pad, GstCaps * filter)
62 GstMuLawEnc *mulawenc;
64 GstCaps *othercaps, *result;
69 mulawenc = GST_MULAWENC (GST_PAD_PARENT (pad));
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;
77 otherpad = mulawenc->srcpad;
79 /* get caps from the peer, this can return NULL when there is no peer */
80 othercaps = gst_pad_peer_query_caps (otherpad, NULL);
82 /* get the template caps to make sure we return something acceptable */
83 templ = gst_pad_get_pad_template_caps (pad);
86 /* there was a peer */
87 othercaps = gst_caps_make_writable (othercaps);
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;
93 structure = gst_caps_get_structure (othercaps, i);
96 gst_structure_set_name (structure, name);
98 if (pad == mulawenc->srcpad) {
99 /* remove the fields we don't want */
100 gst_structure_remove_fields (structure, "format", NULL);
102 /* add fixed fields */
103 gst_structure_set (structure, "format", G_TYPE_STRING,
104 GST_AUDIO_NE (S16), NULL);
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 gst_caps_unref (templ);
112 /* there was no peer, return the template caps */
115 if (filter && result) {
118 temp = gst_caps_intersect (result, filter);
119 gst_caps_unref (result);
127 gst_mulawenc_query (GstPad * pad, GstObject * parent, GstQuery * query)
131 switch (GST_QUERY_TYPE (query)) {
134 GstCaps *filter, *caps;
136 gst_query_parse_caps (query, &filter);
137 caps = mulawenc_getcaps (pad, filter);
138 gst_query_set_caps_result (query, caps);
139 gst_caps_unref (caps);
145 res = gst_pad_query_default (pad, parent, query);
153 mulawenc_setcaps (GstMuLawEnc * mulawenc, GstCaps * caps)
155 GstStructure *structure;
158 structure = gst_caps_get_structure (caps, 0);
159 gst_structure_get_int (structure, "channels", &mulawenc->channels);
160 gst_structure_get_int (structure, "rate", &mulawenc->rate);
162 base_caps = gst_pad_get_pad_template_caps (mulawenc->srcpad);
163 base_caps = gst_caps_make_writable (base_caps);
165 structure = gst_caps_get_structure (base_caps, 0);
166 gst_structure_set (structure, "rate", G_TYPE_INT, mulawenc->rate, NULL);
167 gst_structure_set (structure, "channels", G_TYPE_INT, mulawenc->channels,
170 gst_pad_set_caps (mulawenc->srcpad, base_caps);
172 gst_caps_unref (base_caps);
178 gst_mulawenc_class_init (GstMuLawEncClass * klass)
180 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
182 gst_element_class_add_pad_template (element_class,
183 gst_static_pad_template_get (&mulaw_enc_src_factory));
184 gst_element_class_add_pad_template (element_class,
185 gst_static_pad_template_get (&mulaw_enc_sink_factory));
187 gst_element_class_set_static_metadata (element_class, "Mu Law audio encoder",
188 "Codec/Encoder/Audio",
189 "Convert 16bit PCM to 8bit mu law",
190 "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
194 gst_mulawenc_init (GstMuLawEnc * mulawenc)
197 gst_pad_new_from_static_template (&mulaw_enc_sink_factory, "sink");
198 gst_pad_set_query_function (mulawenc->sinkpad, gst_mulawenc_query);
199 gst_pad_set_event_function (mulawenc->sinkpad, gst_mulawenc_event);
200 gst_pad_set_chain_function (mulawenc->sinkpad, gst_mulawenc_chain);
201 gst_element_add_pad (GST_ELEMENT (mulawenc), mulawenc->sinkpad);
204 gst_pad_new_from_static_template (&mulaw_enc_src_factory, "src");
205 gst_pad_set_query_function (mulawenc->srcpad, gst_mulawenc_query);
206 gst_element_add_pad (GST_ELEMENT (mulawenc), mulawenc->srcpad);
209 mulawenc->channels = 0;
214 gst_mulawenc_event (GstPad * pad, GstObject * parent, GstEvent * event)
216 GstMuLawEnc *mulawenc;
219 mulawenc = GST_MULAWENC (parent);
221 switch (GST_EVENT_TYPE (event)) {
226 gst_event_parse_caps (event, &caps);
227 mulawenc_setcaps (mulawenc, caps);
228 gst_event_unref (event);
234 res = gst_pad_event_default (pad, parent, event);
241 gst_mulawenc_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
243 GstMuLawEnc *mulawenc;
244 GstMapInfo inmap, outmap;
251 GstClockTime timestamp, duration;
253 mulawenc = GST_MULAWENC (parent);
255 if (!mulawenc->rate || !mulawenc->channels)
258 gst_buffer_map (buffer, &inmap, GST_MAP_READ);
259 linear_data = (gint16 *) inmap.data;
260 linear_size = inmap.size;
262 mulaw_size = linear_size / 2;
264 timestamp = GST_BUFFER_TIMESTAMP (buffer);
265 duration = GST_BUFFER_DURATION (buffer);
267 outbuf = gst_buffer_new_allocate (NULL, mulaw_size, NULL);
269 if (duration == -1) {
270 duration = gst_util_uint64_scale_int (mulaw_size,
271 GST_SECOND, mulawenc->rate * mulawenc->channels);
274 gst_buffer_map (outbuf, &outmap, GST_MAP_WRITE);
275 mulaw_data = outmap.data;
277 /* copy discont flag */
278 if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT))
279 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
281 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
282 GST_BUFFER_DURATION (outbuf) = duration;
284 mulaw_encode (linear_data, mulaw_data, mulaw_size);
286 gst_buffer_unmap (outbuf, &outmap);
287 gst_buffer_unmap (buffer, &inmap);
288 gst_buffer_unref (buffer);
290 ret = gst_pad_push (mulawenc->srcpad, outbuf);
298 GST_DEBUG_OBJECT (mulawenc, "no format negotiated");
299 ret = GST_FLOW_NOT_NEGOTIATED;
300 gst_buffer_unref (buffer);