• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * <ulink url="http://www.penguin.cz/~utx/amr">reference codec implementation</ulink>.
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 
119 static void
gst_voamrwbenc_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)120 gst_voamrwbenc_set_property (GObject * object, guint prop_id,
121     const GValue * value, GParamSpec * pspec)
122 {
123   GstVoAmrWbEnc *self = GST_VOAMRWBENC (object);
124 
125   switch (prop_id) {
126     case PROP_BANDMODE:
127       self->bandmode = g_value_get_enum (value);
128       break;
129     default:
130       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
131       break;
132   }
133 
134   return;
135 }
136 
137 static void
gst_voamrwbenc_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)138 gst_voamrwbenc_get_property (GObject * object, guint prop_id,
139     GValue * value, GParamSpec * pspec)
140 {
141   GstVoAmrWbEnc *self = GST_VOAMRWBENC (object);
142 
143   switch (prop_id) {
144     case PROP_BANDMODE:
145       g_value_set_enum (value, self->bandmode);
146       break;
147     default:
148       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
149       break;
150   }
151 
152   return;
153 }
154 
155 static void
gst_voamrwbenc_class_init(GstVoAmrWbEncClass * klass)156 gst_voamrwbenc_class_init (GstVoAmrWbEncClass * klass)
157 {
158   GObjectClass *object_class = G_OBJECT_CLASS (klass);
159   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
160   GstAudioEncoderClass *base_class = GST_AUDIO_ENCODER_CLASS (klass);
161 
162   object_class->set_property = gst_voamrwbenc_set_property;
163   object_class->get_property = gst_voamrwbenc_get_property;
164 
165   gst_element_class_add_static_pad_template (element_class, &sink_template);
166   gst_element_class_add_static_pad_template (element_class, &src_template);
167 
168   gst_element_class_set_static_metadata (element_class, "AMR-WB audio encoder",
169       "Codec/Encoder/Audio",
170       "Adaptive Multi-Rate Wideband audio encoder",
171       "Renato Araujo <renato.filho@indt.org.br>");
172 
173   base_class->start = GST_DEBUG_FUNCPTR (gst_voamrwbenc_start);
174   base_class->stop = GST_DEBUG_FUNCPTR (gst_voamrwbenc_stop);
175   base_class->set_format = GST_DEBUG_FUNCPTR (gst_voamrwbenc_set_format);
176   base_class->handle_frame = GST_DEBUG_FUNCPTR (gst_voamrwbenc_handle_frame);
177 
178   g_object_class_install_property (object_class, PROP_BANDMODE,
179       g_param_spec_enum ("band-mode", "Band Mode",
180           "Encoding Band Mode (Kbps)", GST_VOAMRWBENC_BANDMODE_TYPE,
181           BANDMODE_DEFAULT,
182           G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
183 
184   GST_DEBUG_CATEGORY_INIT (gst_voamrwbenc_debug, "voamrwbenc", 0,
185       "voamrwb encoder");
186 }
187 
188 static void
gst_voamrwbenc_init(GstVoAmrWbEnc * amrwbenc)189 gst_voamrwbenc_init (GstVoAmrWbEnc * amrwbenc)
190 {
191   GST_PAD_SET_ACCEPT_TEMPLATE (GST_AUDIO_ENCODER_SINK_PAD (amrwbenc));
192 
193   /* init rest */
194   amrwbenc->handle = NULL;
195   amrwbenc->channels = 0;
196   amrwbenc->rate = 0;
197 }
198 
199 static gboolean
gst_voamrwbenc_start(GstAudioEncoder * enc)200 gst_voamrwbenc_start (GstAudioEncoder * enc)
201 {
202   GstVoAmrWbEnc *voamrwbenc = GST_VOAMRWBENC (enc);
203 
204   GST_DEBUG_OBJECT (enc, "start");
205 
206   if (!(voamrwbenc->handle = E_IF_init ()))
207     return FALSE;
208 
209   voamrwbenc->rate = 0;
210   voamrwbenc->channels = 0;
211 
212   return TRUE;
213 }
214 
215 static gboolean
gst_voamrwbenc_stop(GstAudioEncoder * enc)216 gst_voamrwbenc_stop (GstAudioEncoder * enc)
217 {
218   GstVoAmrWbEnc *voamrwbenc = GST_VOAMRWBENC (enc);
219 
220   GST_DEBUG_OBJECT (enc, "stop");
221 
222   if (voamrwbenc->handle) {
223     E_IF_exit (voamrwbenc->handle);
224     voamrwbenc->handle = NULL;
225   }
226 
227   return TRUE;
228 }
229 
230 static gboolean
gst_voamrwbenc_set_format(GstAudioEncoder * benc,GstAudioInfo * info)231 gst_voamrwbenc_set_format (GstAudioEncoder * benc, GstAudioInfo * info)
232 {
233   GstVoAmrWbEnc *amrwbenc;
234   GstCaps *copy;
235 
236   amrwbenc = GST_VOAMRWBENC (benc);
237 
238   /* get channel count */
239   amrwbenc->channels = GST_AUDIO_INFO_CHANNELS (info);
240   amrwbenc->rate = GST_AUDIO_INFO_RATE (info);
241 
242   /* this is not wrong but will sound bad */
243   if (amrwbenc->channels != 1) {
244     GST_WARNING ("amrwbdec is only optimized for mono channels");
245   }
246   if (amrwbenc->rate != 16000) {
247     GST_WARNING ("amrwbdec is only optimized for 16000 Hz samplerate");
248   }
249 
250   /* create reverse caps */
251   copy = gst_caps_new_simple ("audio/AMR-WB",
252       "channels", G_TYPE_INT, amrwbenc->channels,
253       "rate", G_TYPE_INT, amrwbenc->rate, NULL);
254 
255   gst_audio_encoder_set_output_format (GST_AUDIO_ENCODER (amrwbenc), copy);
256   gst_caps_unref (copy);
257 
258   /* report needs to base class: one frame at a time */
259   gst_audio_encoder_set_frame_samples_min (benc, L_FRAME16k);
260   gst_audio_encoder_set_frame_samples_max (benc, L_FRAME16k);
261   gst_audio_encoder_set_frame_max (benc, 1);
262 
263   return TRUE;
264 }
265 
266 static GstFlowReturn
gst_voamrwbenc_handle_frame(GstAudioEncoder * benc,GstBuffer * buffer)267 gst_voamrwbenc_handle_frame (GstAudioEncoder * benc, GstBuffer * buffer)
268 {
269   GstVoAmrWbEnc *amrwbenc;
270   GstFlowReturn ret = GST_FLOW_OK;
271   const int buffer_size = sizeof (short) * L_FRAME16k;
272   GstBuffer *out;
273   gint outsize;
274   GstMapInfo map, omap;
275 
276   amrwbenc = GST_VOAMRWBENC (benc);
277 
278   g_return_val_if_fail (amrwbenc->handle, GST_FLOW_NOT_NEGOTIATED);
279 
280   /* we don't deal with squeezing remnants, so simply discard those */
281   if (G_UNLIKELY (buffer == NULL)) {
282     GST_DEBUG_OBJECT (amrwbenc, "no data");
283     goto done;
284   }
285 
286   gst_buffer_map (buffer, &map, GST_MAP_READ);
287 
288   if (G_UNLIKELY (map.size < buffer_size)) {
289     GST_DEBUG_OBJECT (amrwbenc, "discarding trailing data %d", (gint) map.size);
290     gst_buffer_unmap (buffer, &map);
291     ret = gst_audio_encoder_finish_frame (benc, NULL, -1);
292     goto done;
293   }
294 
295   out = gst_buffer_new_and_alloc (buffer_size);
296   gst_buffer_map (out, &omap, GST_MAP_WRITE);
297   /* encode */
298   outsize = E_IF_encode (amrwbenc->handle, amrwbenc->bandmode,
299       (const short *) map.data, (unsigned char *) omap.data, 0);
300 
301   GST_LOG_OBJECT (amrwbenc, "encoded to %d bytes", outsize);
302   gst_buffer_unmap (out, &omap);
303   gst_buffer_unmap (buffer, &map);
304   gst_buffer_resize (out, 0, outsize);
305 
306   ret = gst_audio_encoder_finish_frame (benc, out, L_FRAME16k);
307 
308 done:
309   return ret;
310 }
311