1 /* GStreamer Adaptive Multi-Rate Wide-Band (AMR-WB) plugin
2 * Copyright (C) 2006 Edgard Lima <edgard.lima@gmail.com>
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 /**
21 * SECTION:element-voamrwbenc
22 * @title: voamrwbenc
23 * @see_also: #GstAmrWbDec, #GstAmrWbParse
24 *
25 * AMR wideband encoder based on the
26 * [reference codec implementation](http://www.penguin.cz/~utx/amr).
27 *
28 * ## Example launch line
29 * |[
30 * gst-launch filesrc location=abc.wav ! wavparse ! audioresample ! audioconvert ! voamrwbenc ! filesink location=abc.amr
31 * ]|
32 * Please note that the above stream misses the header, that is needed to play
33 * the stream.
34 *
35 */
36
37 #ifdef HAVE_CONFIG_H
38 #include "config.h"
39 #endif
40
41 #include "gstvoamrwbenc.h"
42
43 #define MR660 0
44 #define MR885 1
45 #define MR1265 2
46 #define MR1425 2
47 #define MR1585 3
48 #define MR1825 4
49 #define MR1985 5
50 #define MR2305 6
51 #define MR2385 7
52 #define MRDTX 8
53
54 #define L_FRAME16k 320 /* Frame size at 16kHz */
55
56 static GType
gst_voamrwbenc_bandmode_get_type(void)57 gst_voamrwbenc_bandmode_get_type (void)
58 {
59 static GType gst_voamrwbenc_bandmode_type = 0;
60 static const GEnumValue gst_voamrwbenc_bandmode[] = {
61 {MR660, "MR660", "MR660"},
62 {MR885, "MR885", "MR885"},
63 {MR1265, "MR1265", "MR1265"},
64 {MR1425, "MR1425", "MR1425"},
65 {MR1585, "MR1585", "MR1585"},
66 {MR1825, "MR1825", "MR1825"},
67 {MR1985, "MR1985", "MR1985"},
68 {MR2305, "MR2305", "MR2305"},
69 {MR2385, "MR2385", "MR2385"},
70 {MRDTX, "MRDTX", "MRDTX"},
71 {0, NULL, NULL},
72 };
73 if (!gst_voamrwbenc_bandmode_type) {
74 gst_voamrwbenc_bandmode_type =
75 g_enum_register_static ("GstVoAmrWbEncBandMode",
76 gst_voamrwbenc_bandmode);
77 }
78 return gst_voamrwbenc_bandmode_type;
79 }
80
81 #define GST_VOAMRWBENC_BANDMODE_TYPE (gst_voamrwbenc_bandmode_get_type())
82
83 #define BANDMODE_DEFAULT MR660
84
85 enum
86 {
87 PROP_0,
88 PROP_BANDMODE
89 };
90
91 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
92 GST_PAD_SINK,
93 GST_PAD_ALWAYS,
94 GST_STATIC_CAPS ("audio/x-raw, "
95 "format = (string) " GST_AUDIO_NE (S16) ", "
96 "layout = (string) interleaved, "
97 "rate = (int) 16000, " "channels = (int) 1")
98 );
99
100 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
101 GST_PAD_SRC,
102 GST_PAD_ALWAYS,
103 GST_STATIC_CAPS ("audio/AMR-WB, "
104 "rate = (int) 16000, " "channels = (int) 1")
105 );
106
107 GST_DEBUG_CATEGORY_STATIC (gst_voamrwbenc_debug);
108 #define GST_CAT_DEFAULT gst_voamrwbenc_debug
109
110 static gboolean gst_voamrwbenc_start (GstAudioEncoder * enc);
111 static gboolean gst_voamrwbenc_stop (GstAudioEncoder * enc);
112 static gboolean gst_voamrwbenc_set_format (GstAudioEncoder * enc,
113 GstAudioInfo * info);
114 static GstFlowReturn gst_voamrwbenc_handle_frame (GstAudioEncoder * enc,
115 GstBuffer * in_buf);
116
117 G_DEFINE_TYPE (GstVoAmrWbEnc, gst_voamrwbenc, GST_TYPE_AUDIO_ENCODER);
118 GST_ELEMENT_REGISTER_DEFINE (voamrwbenc, "voamrwbenc",
119 GST_RANK_SECONDARY, GST_TYPE_VOAMRWBENC);
120
121 static void
gst_voamrwbenc_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)122 gst_voamrwbenc_set_property (GObject * object, guint prop_id,
123 const GValue * value, GParamSpec * pspec)
124 {
125 GstVoAmrWbEnc *self = GST_VOAMRWBENC (object);
126
127 switch (prop_id) {
128 case PROP_BANDMODE:
129 self->bandmode = g_value_get_enum (value);
130 break;
131 default:
132 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
133 break;
134 }
135
136 return;
137 }
138
139 static void
gst_voamrwbenc_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)140 gst_voamrwbenc_get_property (GObject * object, guint prop_id,
141 GValue * value, GParamSpec * pspec)
142 {
143 GstVoAmrWbEnc *self = GST_VOAMRWBENC (object);
144
145 switch (prop_id) {
146 case PROP_BANDMODE:
147 g_value_set_enum (value, self->bandmode);
148 break;
149 default:
150 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
151 break;
152 }
153
154 return;
155 }
156
157 static void
gst_voamrwbenc_class_init(GstVoAmrWbEncClass * klass)158 gst_voamrwbenc_class_init (GstVoAmrWbEncClass * klass)
159 {
160 GObjectClass *object_class = G_OBJECT_CLASS (klass);
161 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
162 GstAudioEncoderClass *base_class = GST_AUDIO_ENCODER_CLASS (klass);
163
164 object_class->set_property = gst_voamrwbenc_set_property;
165 object_class->get_property = gst_voamrwbenc_get_property;
166
167 gst_element_class_add_static_pad_template (element_class, &sink_template);
168 gst_element_class_add_static_pad_template (element_class, &src_template);
169
170 gst_element_class_set_static_metadata (element_class, "AMR-WB audio encoder",
171 "Codec/Encoder/Audio",
172 "Adaptive Multi-Rate Wideband audio encoder",
173 "Renato Araujo <renato.filho@indt.org.br>");
174
175 base_class->start = GST_DEBUG_FUNCPTR (gst_voamrwbenc_start);
176 base_class->stop = GST_DEBUG_FUNCPTR (gst_voamrwbenc_stop);
177 base_class->set_format = GST_DEBUG_FUNCPTR (gst_voamrwbenc_set_format);
178 base_class->handle_frame = GST_DEBUG_FUNCPTR (gst_voamrwbenc_handle_frame);
179
180 g_object_class_install_property (object_class, PROP_BANDMODE,
181 g_param_spec_enum ("band-mode", "Band Mode",
182 "Encoding Band Mode (Kbps)", GST_VOAMRWBENC_BANDMODE_TYPE,
183 BANDMODE_DEFAULT,
184 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
185
186 GST_DEBUG_CATEGORY_INIT (gst_voamrwbenc_debug, "voamrwbenc", 0,
187 "voamrwb encoder");
188
189 gst_type_mark_as_plugin_api (GST_VOAMRWBENC_BANDMODE_TYPE, 0);
190 }
191
192 static void
gst_voamrwbenc_init(GstVoAmrWbEnc * amrwbenc)193 gst_voamrwbenc_init (GstVoAmrWbEnc * amrwbenc)
194 {
195 GST_PAD_SET_ACCEPT_TEMPLATE (GST_AUDIO_ENCODER_SINK_PAD (amrwbenc));
196
197 /* init rest */
198 amrwbenc->handle = NULL;
199 amrwbenc->channels = 0;
200 amrwbenc->rate = 0;
201 }
202
203 static gboolean
gst_voamrwbenc_start(GstAudioEncoder * enc)204 gst_voamrwbenc_start (GstAudioEncoder * enc)
205 {
206 GstVoAmrWbEnc *voamrwbenc = GST_VOAMRWBENC (enc);
207
208 GST_DEBUG_OBJECT (enc, "start");
209
210 if (!(voamrwbenc->handle = E_IF_init ()))
211 return FALSE;
212
213 voamrwbenc->rate = 0;
214 voamrwbenc->channels = 0;
215
216 return TRUE;
217 }
218
219 static gboolean
gst_voamrwbenc_stop(GstAudioEncoder * enc)220 gst_voamrwbenc_stop (GstAudioEncoder * enc)
221 {
222 GstVoAmrWbEnc *voamrwbenc = GST_VOAMRWBENC (enc);
223
224 GST_DEBUG_OBJECT (enc, "stop");
225
226 if (voamrwbenc->handle) {
227 E_IF_exit (voamrwbenc->handle);
228 voamrwbenc->handle = NULL;
229 }
230
231 return TRUE;
232 }
233
234 static gboolean
gst_voamrwbenc_set_format(GstAudioEncoder * benc,GstAudioInfo * info)235 gst_voamrwbenc_set_format (GstAudioEncoder * benc, GstAudioInfo * info)
236 {
237 GstVoAmrWbEnc *amrwbenc;
238 GstCaps *copy;
239
240 amrwbenc = GST_VOAMRWBENC (benc);
241
242 /* get channel count */
243 amrwbenc->channels = GST_AUDIO_INFO_CHANNELS (info);
244 amrwbenc->rate = GST_AUDIO_INFO_RATE (info);
245
246 /* this is not wrong but will sound bad */
247 if (amrwbenc->channels != 1) {
248 GST_WARNING ("amrwbdec is only optimized for mono channels");
249 }
250 if (amrwbenc->rate != 16000) {
251 GST_WARNING ("amrwbdec is only optimized for 16000 Hz samplerate");
252 }
253
254 /* create reverse caps */
255 copy = gst_caps_new_simple ("audio/AMR-WB",
256 "channels", G_TYPE_INT, amrwbenc->channels,
257 "rate", G_TYPE_INT, amrwbenc->rate, NULL);
258
259 gst_audio_encoder_set_output_format (GST_AUDIO_ENCODER (amrwbenc), copy);
260 gst_caps_unref (copy);
261
262 /* report needs to base class: one frame at a time */
263 gst_audio_encoder_set_frame_samples_min (benc, L_FRAME16k);
264 gst_audio_encoder_set_frame_samples_max (benc, L_FRAME16k);
265 gst_audio_encoder_set_frame_max (benc, 1);
266
267 return TRUE;
268 }
269
270 static GstFlowReturn
gst_voamrwbenc_handle_frame(GstAudioEncoder * benc,GstBuffer * buffer)271 gst_voamrwbenc_handle_frame (GstAudioEncoder * benc, GstBuffer * buffer)
272 {
273 GstVoAmrWbEnc *amrwbenc;
274 GstFlowReturn ret = GST_FLOW_OK;
275 const int buffer_size = sizeof (short) * L_FRAME16k;
276 GstBuffer *out;
277 gint outsize;
278 GstMapInfo map, omap;
279
280 amrwbenc = GST_VOAMRWBENC (benc);
281
282 g_return_val_if_fail (amrwbenc->handle, GST_FLOW_NOT_NEGOTIATED);
283
284 /* we don't deal with squeezing remnants, so simply discard those */
285 if (G_UNLIKELY (buffer == NULL)) {
286 GST_DEBUG_OBJECT (amrwbenc, "no data");
287 goto done;
288 }
289
290 gst_buffer_map (buffer, &map, GST_MAP_READ);
291
292 if (G_UNLIKELY (map.size < buffer_size)) {
293 GST_DEBUG_OBJECT (amrwbenc, "discarding trailing data %d", (gint) map.size);
294 gst_buffer_unmap (buffer, &map);
295 ret = gst_audio_encoder_finish_frame (benc, NULL, -1);
296 goto done;
297 }
298
299 out = gst_buffer_new_and_alloc (buffer_size);
300 gst_buffer_map (out, &omap, GST_MAP_WRITE);
301 /* encode */
302 outsize = E_IF_encode (amrwbenc->handle, amrwbenc->bandmode,
303 (const short *) map.data, (unsigned char *) omap.data, 0);
304
305 GST_LOG_OBJECT (amrwbenc, "encoded to %d bytes", outsize);
306 gst_buffer_unmap (out, &omap);
307 gst_buffer_unmap (buffer, &map);
308 gst_buffer_resize (out, 0, outsize);
309
310 ret = gst_audio_encoder_finish_frame (benc, out, L_FRAME16k);
311
312 done:
313 return ret;
314 }
315