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 gboolean gst_mulawenc_event (GstPad * pad, GstEvent * event);
48 static GstFlowReturn gst_mulawenc_chain (GstPad * pad, GstBuffer * buffer);
50 #define gst_mulawenc_parent_class parent_class
51 G_DEFINE_TYPE (GstMuLawEnc, gst_mulawenc, GST_TYPE_ELEMENT);
53 /*static guint gst_stereo_signals[LAST_SIGNAL] = { 0 }; */
56 mulawenc_getcaps (GstPad * pad, GstCaps * filter)
58 GstMuLawEnc *mulawenc;
60 GstCaps *othercaps, *result;
65 mulawenc = GST_MULAWENC (GST_PAD_PARENT (pad));
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;
72 name = "audio/x-raw-int";
73 otherpad = mulawenc->srcpad;
75 /* get caps from the peer, this can return NULL when there is no peer */
76 othercaps = gst_pad_peer_query_caps (otherpad, filter);
78 /* get the template caps to make sure we return something acceptable */
79 templ = gst_pad_get_pad_template_caps (pad);
82 /* there was a peer */
83 othercaps = gst_caps_make_writable (othercaps);
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;
89 structure = gst_caps_get_structure (othercaps, i);
92 gst_structure_set_name (structure, name);
94 if (pad == mulawenc->srcpad) {
95 /* remove the fields we don't want */
96 gst_structure_remove_fields (structure, "width", "depth", "endianness",
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);
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);
110 /* there was no peer, return the template caps */
111 result = gst_caps_copy (templ);
117 gst_mulawenc_query (GstPad * pad, GstQuery * query)
121 switch (GST_QUERY_TYPE (query)) {
124 GstCaps *filter, *caps;
126 gst_query_parse_caps (query, &filter);
127 caps = mulawenc_getcaps (pad, filter);
128 gst_query_set_caps_result (query, caps);
129 gst_caps_unref (caps);
135 res = gst_pad_query_default (pad, query);
143 mulawenc_setcaps (GstMuLawEnc * mulawenc, GstCaps * caps)
145 GstStructure *structure;
148 structure = gst_caps_get_structure (caps, 0);
149 gst_structure_get_int (structure, "channels", &mulawenc->channels);
150 gst_structure_get_int (structure, "rate", &mulawenc->rate);
152 base_caps = gst_caps_copy (gst_pad_get_pad_template_caps (mulawenc->srcpad));
154 structure = gst_caps_get_structure (base_caps, 0);
155 gst_structure_set (structure, "rate", G_TYPE_INT, mulawenc->rate, NULL);
156 gst_structure_set (structure, "channels", G_TYPE_INT, mulawenc->channels,
159 gst_pad_set_caps (mulawenc->srcpad, base_caps);
161 gst_caps_unref (base_caps);
167 gst_mulawenc_class_init (GstMuLawEncClass * klass)
169 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
171 gst_element_class_add_pad_template (element_class,
172 gst_static_pad_template_get (&mulaw_enc_src_factory));
173 gst_element_class_add_pad_template (element_class,
174 gst_static_pad_template_get (&mulaw_enc_sink_factory));
176 gst_element_class_set_details_simple (element_class, "Mu Law audio encoder",
177 "Codec/Encoder/Audio",
178 "Convert 16bit PCM to 8bit mu law",
179 "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
183 gst_mulawenc_init (GstMuLawEnc * mulawenc)
186 gst_pad_new_from_static_template (&mulaw_enc_sink_factory, "sink");
187 gst_pad_set_query_function (mulawenc->sinkpad, gst_mulawenc_query);
188 gst_pad_set_event_function (mulawenc->sinkpad, gst_mulawenc_event);
189 gst_pad_set_chain_function (mulawenc->sinkpad, gst_mulawenc_chain);
190 gst_element_add_pad (GST_ELEMENT (mulawenc), mulawenc->sinkpad);
193 gst_pad_new_from_static_template (&mulaw_enc_src_factory, "src");
194 gst_pad_set_query_function (mulawenc->srcpad, gst_mulawenc_query);
195 gst_element_add_pad (GST_ELEMENT (mulawenc), mulawenc->srcpad);
198 mulawenc->channels = 0;
203 gst_mulawenc_event (GstPad * pad, GstEvent * event)
205 GstMuLawEnc *mulawenc;
208 mulawenc = GST_MULAWENC (GST_PAD_PARENT (pad));
210 switch (GST_EVENT_TYPE (event)) {
215 gst_event_parse_caps (event, &caps);
216 mulawenc_setcaps (mulawenc, caps);
217 gst_event_unref (event);
223 res = gst_pad_event_default (pad, event);
230 gst_mulawenc_chain (GstPad * pad, GstBuffer * buffer)
232 GstMuLawEnc *mulawenc;
239 GstClockTime timestamp, duration;
241 mulawenc = GST_MULAWENC (gst_pad_get_parent (pad));
243 if (!mulawenc->rate || !mulawenc->channels)
246 linear_data = gst_buffer_map (buffer, &linear_size, NULL, GST_MAP_READ);
248 mulaw_size = linear_size / 2;
250 timestamp = GST_BUFFER_TIMESTAMP (buffer);
251 duration = GST_BUFFER_DURATION (buffer);
253 outbuf = gst_buffer_new_allocate (NULL, mulaw_size, 0);
255 if (duration == -1) {
256 duration = gst_util_uint64_scale_int (mulaw_size,
257 GST_SECOND, mulawenc->rate * mulawenc->channels);
260 mulaw_data = gst_buffer_map (outbuf, NULL, NULL, GST_MAP_WRITE);
262 /* copy discont flag */
263 if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT))
264 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
266 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
267 GST_BUFFER_DURATION (outbuf) = duration;
269 mulaw_encode (linear_data, mulaw_data, mulaw_size);
271 gst_buffer_unmap (outbuf, mulaw_data, -1);
272 gst_buffer_unmap (buffer, linear_data, -1);
273 gst_buffer_unref (buffer);
275 ret = gst_pad_push (mulawenc->srcpad, outbuf);
278 gst_object_unref (mulawenc);
284 GST_DEBUG_OBJECT (mulawenc, "no format negotiated");
285 ret = GST_FLOW_NOT_NEGOTIATED;
286 gst_buffer_unref (buffer);