1 /* GStreamer
2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 *
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.
8 *
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.
13 *
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.
18 */
19 /**
20 * SECTION:element-mulawenc
21 *
22 * This element encode mulaw audio. Mulaw coding is also known as G.711.
23 */
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28 #include <gst/gst.h>
29 #include <gst/audio/audio.h>
30
31 #include "mulaw-encode.h"
32 #include "mulaw-conversion.h"
33
34 extern GstStaticPadTemplate mulaw_enc_src_factory;
35 extern GstStaticPadTemplate mulaw_enc_sink_factory;
36
37 /* Stereo signals and args */
38 enum
39 {
40 /* FILL ME */
41 LAST_SIGNAL
42 };
43
44 enum
45 {
46 PROP_0
47 };
48
49 static gboolean gst_mulawenc_start (GstAudioEncoder * audioenc);
50 static gboolean gst_mulawenc_set_format (GstAudioEncoder * enc,
51 GstAudioInfo * info);
52 static GstFlowReturn gst_mulawenc_handle_frame (GstAudioEncoder * enc,
53 GstBuffer * buffer);
54 static void gst_mulawenc_set_tags (GstMuLawEnc * mulawenc);
55
56
57 #define gst_mulawenc_parent_class parent_class
58 G_DEFINE_TYPE (GstMuLawEnc, gst_mulawenc, GST_TYPE_AUDIO_ENCODER);
59
60 /*static guint gst_stereo_signals[LAST_SIGNAL] = { 0 }; */
61
62 static gboolean
gst_mulawenc_start(GstAudioEncoder * audioenc)63 gst_mulawenc_start (GstAudioEncoder * audioenc)
64 {
65 GstMuLawEnc *mulawenc = GST_MULAWENC (audioenc);
66
67 mulawenc->channels = 0;
68 mulawenc->rate = 0;
69
70 return TRUE;
71 }
72
73
74 static void
gst_mulawenc_set_tags(GstMuLawEnc * mulawenc)75 gst_mulawenc_set_tags (GstMuLawEnc * mulawenc)
76 {
77 GstTagList *taglist;
78 guint bitrate;
79
80 /* bitrate of mulaw is 8 bits/sample * sample rate * number of channels */
81 bitrate = 8 * mulawenc->rate * mulawenc->channels;
82
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);
90
91 gst_audio_encoder_merge_tags (GST_AUDIO_ENCODER (mulawenc),
92 taglist, GST_TAG_MERGE_REPLACE);
93
94 gst_tag_list_unref (taglist);
95 }
96
97
98 static gboolean
gst_mulawenc_set_format(GstAudioEncoder * audioenc,GstAudioInfo * info)99 gst_mulawenc_set_format (GstAudioEncoder * audioenc, GstAudioInfo * info)
100 {
101 GstCaps *base_caps;
102 GstStructure *structure;
103 GstMuLawEnc *mulawenc = GST_MULAWENC (audioenc);
104 gboolean ret;
105
106 mulawenc->rate = info->rate;
107 mulawenc->channels = info->channels;
108
109 base_caps =
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);
114
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,
119 NULL);
120
121 gst_mulawenc_set_tags (mulawenc);
122
123 ret = gst_audio_encoder_set_output_format (audioenc, base_caps);
124 gst_caps_unref (base_caps);
125
126 return ret;
127 }
128
129 static GstFlowReturn
gst_mulawenc_handle_frame(GstAudioEncoder * audioenc,GstBuffer * buffer)130 gst_mulawenc_handle_frame (GstAudioEncoder * audioenc, GstBuffer * buffer)
131 {
132 GstMuLawEnc *mulawenc;
133 GstMapInfo inmap, outmap;
134 gint16 *linear_data;
135 gsize linear_size;
136 guint8 *mulaw_data;
137 guint mulaw_size;
138 GstBuffer *outbuf;
139 GstFlowReturn ret;
140
141 if (!buffer) {
142 ret = GST_FLOW_OK;
143 goto done;
144 }
145
146 mulawenc = GST_MULAWENC (audioenc);
147
148 if (!mulawenc->rate || !mulawenc->channels)
149 goto not_negotiated;
150
151 gst_buffer_map (buffer, &inmap, GST_MAP_READ);
152 linear_data = (gint16 *) inmap.data;
153 linear_size = inmap.size;
154
155 mulaw_size = linear_size / 2;
156
157 outbuf = gst_audio_encoder_allocate_output_buffer (audioenc, mulaw_size);
158
159 g_assert (outbuf);
160
161 gst_buffer_map (outbuf, &outmap, GST_MAP_WRITE);
162 mulaw_data = outmap.data;
163
164 mulaw_encode (linear_data, mulaw_data, mulaw_size);
165
166 gst_buffer_unmap (outbuf, &outmap);
167 gst_buffer_unmap (buffer, &inmap);
168
169 ret = gst_audio_encoder_finish_frame (audioenc, outbuf, -1);
170
171 done:
172
173 return ret;
174
175 not_negotiated:
176 {
177 GST_DEBUG_OBJECT (mulawenc, "no format negotiated");
178 ret = GST_FLOW_NOT_NEGOTIATED;
179 goto done;
180 }
181 }
182
183
184
185 static void
gst_mulawenc_class_init(GstMuLawEncClass * klass)186 gst_mulawenc_class_init (GstMuLawEncClass * klass)
187 {
188 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
189 GstAudioEncoderClass *audio_encoder_class = GST_AUDIO_ENCODER_CLASS (klass);
190
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);
195
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);
200
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>");
205 }
206
207 static void
gst_mulawenc_init(GstMuLawEnc * mulawenc)208 gst_mulawenc_init (GstMuLawEnc * mulawenc)
209 {
210 GST_PAD_SET_ACCEPT_TEMPLATE (GST_AUDIO_ENCODER_SINK_PAD (mulawenc));
211 }
212