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_get_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 mulawenc_setcaps (GstMuLawEnc * mulawenc, GstCaps * caps)
119 GstStructure *structure;
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);
126 base_caps = gst_caps_copy (gst_pad_get_pad_template_caps (mulawenc->srcpad));
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,
133 gst_pad_set_caps (mulawenc->srcpad, base_caps);
135 gst_caps_unref (base_caps);
141 gst_mulawenc_class_init (GstMuLawEncClass * klass)
143 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
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));
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>");
157 gst_mulawenc_init (GstMuLawEnc * mulawenc)
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);
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);
173 mulawenc->channels = 0;
178 gst_mulawenc_event (GstPad * pad, GstEvent * event)
180 GstMuLawEnc *mulawenc;
183 mulawenc = GST_MULAWENC (GST_PAD_PARENT (pad));
185 switch (GST_EVENT_TYPE (event)) {
190 gst_event_parse_caps (event, &caps);
191 mulawenc_setcaps (mulawenc, caps);
192 gst_event_unref (event);
198 res = gst_pad_event_default (pad, event);
205 gst_mulawenc_chain (GstPad * pad, GstBuffer * buffer)
207 GstMuLawEnc *mulawenc;
214 GstClockTime timestamp, duration;
216 mulawenc = GST_MULAWENC (gst_pad_get_parent (pad));
218 if (!mulawenc->rate || !mulawenc->channels)
221 linear_data = gst_buffer_map (buffer, &linear_size, NULL, GST_MAP_READ);
223 mulaw_size = linear_size / 2;
225 timestamp = GST_BUFFER_TIMESTAMP (buffer);
226 duration = GST_BUFFER_DURATION (buffer);
228 outbuf = gst_buffer_new_allocate (NULL, mulaw_size, 0);
230 if (duration == -1) {
231 duration = gst_util_uint64_scale_int (mulaw_size,
232 GST_SECOND, mulawenc->rate * mulawenc->channels);
235 mulaw_data = gst_buffer_map (outbuf, NULL, NULL, GST_MAP_WRITE);
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);
241 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
242 GST_BUFFER_DURATION (outbuf) = duration;
244 mulaw_encode (linear_data, mulaw_data, mulaw_size);
246 gst_buffer_unmap (outbuf, mulaw_data, -1);
247 gst_buffer_unmap (buffer, linear_data, -1);
248 gst_buffer_unref (buffer);
250 ret = gst_pad_push (mulawenc->srcpad, outbuf);
253 gst_object_unref (mulawenc);
259 GST_DEBUG_OBJECT (mulawenc, "no format negotiated");
260 ret = GST_FLOW_NOT_NEGOTIATED;
261 gst_buffer_unref (buffer);