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., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, 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_start (GstAudioEncoder * audioenc);
50 static gboolean gst_mulawenc_set_format (GstAudioEncoder * enc,
52 static GstFlowReturn gst_mulawenc_handle_frame (GstAudioEncoder * enc,
54 static void gst_mulawenc_set_tags (GstMuLawEnc * mulawenc);
57 #define gst_mulawenc_parent_class parent_class
58 G_DEFINE_TYPE (GstMuLawEnc, gst_mulawenc, GST_TYPE_AUDIO_ENCODER);
60 /*static guint gst_stereo_signals[LAST_SIGNAL] = { 0 }; */
63 gst_mulawenc_start (GstAudioEncoder * audioenc)
65 GstMuLawEnc *mulawenc = GST_MULAWENC (audioenc);
67 mulawenc->channels = 0;
75 gst_mulawenc_set_tags (GstMuLawEnc * mulawenc)
80 /* bitrate of mulaw is 8 bits/sample * sample rate * number of channels */
81 bitrate = 8 * mulawenc->rate * mulawenc->channels;
83 taglist = gst_tag_list_new_empty ();
84 gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE,
85 GST_TAG_MAXIMUM_BITRATE, bitrate, NULL);
86 gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE,
87 GST_TAG_MINIMUM_BITRATE, bitrate, NULL);
88 gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE,
89 GST_TAG_BITRATE, bitrate, NULL);
91 gst_audio_encoder_merge_tags (GST_AUDIO_ENCODER (mulawenc),
92 taglist, GST_TAG_MERGE_REPLACE);
94 gst_tag_list_unref (taglist);
99 gst_mulawenc_set_format (GstAudioEncoder * audioenc, GstAudioInfo * info)
102 GstStructure *structure;
103 GstMuLawEnc *mulawenc = GST_MULAWENC (audioenc);
106 mulawenc->rate = info->rate;
107 mulawenc->channels = info->channels;
110 gst_pad_get_pad_template_caps (GST_AUDIO_ENCODER_SRC_PAD (audioenc));
111 g_assert (base_caps);
112 base_caps = gst_caps_make_writable (base_caps);
113 g_assert (base_caps);
115 structure = gst_caps_get_structure (base_caps, 0);
116 g_assert (structure);
117 gst_structure_set (structure, "rate", G_TYPE_INT, mulawenc->rate, NULL);
118 gst_structure_set (structure, "channels", G_TYPE_INT, mulawenc->channels,
121 gst_mulawenc_set_tags (mulawenc);
123 ret = gst_audio_encoder_set_output_format (audioenc, base_caps);
124 gst_caps_unref (base_caps);
130 gst_mulawenc_handle_frame (GstAudioEncoder * audioenc, GstBuffer * buffer)
132 GstMuLawEnc *mulawenc;
133 GstMapInfo inmap, outmap;
146 mulawenc = GST_MULAWENC (audioenc);
148 if (!mulawenc->rate || !mulawenc->channels)
151 gst_buffer_map (buffer, &inmap, GST_MAP_READ);
152 linear_data = (gint16 *) inmap.data;
153 linear_size = inmap.size;
155 mulaw_size = linear_size / 2;
157 outbuf = gst_audio_encoder_allocate_output_buffer (audioenc, mulaw_size);
161 gst_buffer_map (outbuf, &outmap, GST_MAP_WRITE);
162 mulaw_data = outmap.data;
164 mulaw_encode (linear_data, mulaw_data, mulaw_size);
166 gst_buffer_unmap (outbuf, &outmap);
167 gst_buffer_unmap (buffer, &inmap);
169 ret = gst_audio_encoder_finish_frame (audioenc, outbuf, -1);
177 GST_DEBUG_OBJECT (mulawenc, "no format negotiated");
178 ret = GST_FLOW_NOT_NEGOTIATED;
186 gst_mulawenc_class_init (GstMuLawEncClass * klass)
188 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
189 GstAudioEncoderClass *audio_encoder_class = GST_AUDIO_ENCODER_CLASS (klass);
191 audio_encoder_class->start = GST_DEBUG_FUNCPTR (gst_mulawenc_start);
192 audio_encoder_class->set_format = GST_DEBUG_FUNCPTR (gst_mulawenc_set_format);
193 audio_encoder_class->handle_frame =
194 GST_DEBUG_FUNCPTR (gst_mulawenc_handle_frame);
196 gst_element_class_add_static_pad_template (element_class,
197 &mulaw_enc_src_factory);
198 gst_element_class_add_static_pad_template (element_class,
199 &mulaw_enc_sink_factory);
201 gst_element_class_set_static_metadata (element_class, "Mu Law audio encoder",
202 "Codec/Encoder/Audio",
203 "Convert 16bit PCM to 8bit mu law",
204 "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
208 gst_mulawenc_init (GstMuLawEnc * mulawenc)
210 GST_PAD_SET_ACCEPT_TEMPLATE (GST_AUDIO_ENCODER_SINK_PAD (mulawenc));