1 /* GStreamer Adaptive Multi-Rate Narrow-Band (AMR-NB) plugin
2 * Copyright (C) 2004 Ronald Bultje <rbultje@ronald.bitfreak.net>
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.
21 * SECTION:element-amrnbdec
22 * @see_also: #GstAmrnbEnc, #GstAmrParse
24 * AMR narrowband decoder based on the
25 * <ulink url="http://sourceforge.net/projects/opencore-amr">opencore codec implementation</ulink>.
28 * <title>Example launch line</title>
30 * gst-launch filesrc location=abc.amr ! amrparse ! amrnbdec ! audioresample ! audioconvert ! alsasink
41 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
44 GST_STATIC_CAPS ("audio/AMR, " "rate = (int) 8000, " "channels = (int) 1")
47 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
50 GST_STATIC_CAPS ("audio/x-raw, format = (string) " GST_AUDIO_NE (S16) ", "
51 "layout = (string) interleaved, "
52 "rate = (int) 8000," "channels = (int) 1")
55 GST_DEBUG_CATEGORY_STATIC (gst_amrnbdec_debug);
56 #define GST_CAT_DEFAULT gst_amrnbdec_debug
58 static const gint block_size_if1[16] = { 12, 13, 15, 17, 19, 20, 26, 31, 5,
62 static const gint block_size_if2[16] = { 12, 13, 15, 17, 18, 20, 25, 30, 5,
67 gst_amrnb_variant_get_type (void)
69 static GType gst_amrnb_variant_type = 0;
70 static const GEnumValue gst_amrnb_variant[] = {
71 {GST_AMRNB_VARIANT_IF1, "IF1", "IF1"},
72 {GST_AMRNB_VARIANT_IF2, "IF2", "IF2"},
75 if (!gst_amrnb_variant_type) {
76 gst_amrnb_variant_type =
77 g_enum_register_static ("GstAmrnbVariant", gst_amrnb_variant);
79 return gst_amrnb_variant_type;
82 #define GST_AMRNB_VARIANT_TYPE (gst_amrnb_variant_get_type())
84 #define VARIANT_DEFAULT GST_AMRNB_VARIANT_IF1
91 static void gst_amrnbdec_set_property (GObject * object, guint prop_id,
92 const GValue * value, GParamSpec * pspec);
93 static void gst_amrnbdec_get_property (GObject * object, guint prop_id,
94 GValue * value, GParamSpec * pspec);
96 static gboolean gst_amrnbdec_start (GstAudioDecoder * dec);
97 static gboolean gst_amrnbdec_stop (GstAudioDecoder * dec);
98 static gboolean gst_amrnbdec_set_format (GstAudioDecoder * dec, GstCaps * caps);
99 static gboolean gst_amrnbdec_parse (GstAudioDecoder * dec, GstAdapter * adapter,
100 gint * offset, gint * length);
101 static GstFlowReturn gst_amrnbdec_handle_frame (GstAudioDecoder * dec,
104 #define gst_amrnbdec_parent_class parent_class
105 G_DEFINE_TYPE (GstAmrnbDec, gst_amrnbdec, GST_TYPE_AUDIO_DECODER);
108 gst_amrnbdec_class_init (GstAmrnbDecClass * klass)
110 GObjectClass *object_class = G_OBJECT_CLASS (klass);
111 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
112 GstAudioDecoderClass *base_class = GST_AUDIO_DECODER_CLASS (klass);
114 object_class->set_property = gst_amrnbdec_set_property;
115 object_class->get_property = gst_amrnbdec_get_property;
117 gst_element_class_add_pad_template (element_class,
118 gst_static_pad_template_get (&sink_template));
119 gst_element_class_add_pad_template (element_class,
120 gst_static_pad_template_get (&src_template));
122 gst_element_class_set_static_metadata (element_class, "AMR-NB audio decoder",
123 "Codec/Decoder/Audio",
124 "Adaptive Multi-Rate Narrow-Band audio decoder",
125 "GStreamer maintainers <gstreamer-devel@lists.sourceforge.net>");
127 base_class->start = GST_DEBUG_FUNCPTR (gst_amrnbdec_start);
128 base_class->stop = GST_DEBUG_FUNCPTR (gst_amrnbdec_stop);
129 base_class->set_format = GST_DEBUG_FUNCPTR (gst_amrnbdec_set_format);
130 base_class->parse = GST_DEBUG_FUNCPTR (gst_amrnbdec_parse);
131 base_class->handle_frame = GST_DEBUG_FUNCPTR (gst_amrnbdec_handle_frame);
133 g_object_class_install_property (object_class, PROP_VARIANT,
134 g_param_spec_enum ("variant", "Variant",
135 "The decoder variant", GST_AMRNB_VARIANT_TYPE,
137 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
139 GST_DEBUG_CATEGORY_INIT (gst_amrnbdec_debug, "amrnbdec", 0,
140 "AMR-NB audio decoder");
144 gst_amrnbdec_init (GstAmrnbDec * amrnbdec)
146 gst_audio_decoder_set_needs_format (GST_AUDIO_DECODER (amrnbdec), TRUE);
150 gst_amrnbdec_start (GstAudioDecoder * dec)
152 GstAmrnbDec *amrnbdec = GST_AMRNBDEC (dec);
154 GST_DEBUG_OBJECT (dec, "start");
155 if (!(amrnbdec->handle = Decoder_Interface_init ()))
159 amrnbdec->channels = 0;
165 gst_amrnbdec_stop (GstAudioDecoder * dec)
167 GstAmrnbDec *amrnbdec = GST_AMRNBDEC (dec);
169 GST_DEBUG_OBJECT (dec, "stop");
170 Decoder_Interface_exit (amrnbdec->handle);
176 gst_amrnbdec_set_property (GObject * object, guint prop_id,
177 const GValue * value, GParamSpec * pspec)
179 GstAmrnbDec *self = GST_AMRNBDEC (object);
183 self->variant = g_value_get_enum (value);
186 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
193 gst_amrnbdec_get_property (GObject * object, guint prop_id,
194 GValue * value, GParamSpec * pspec)
196 GstAmrnbDec *self = GST_AMRNBDEC (object);
200 g_value_set_enum (value, self->variant);
203 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
210 gst_amrnbdec_set_format (GstAudioDecoder * dec, GstCaps * caps)
212 GstStructure *structure;
213 GstAmrnbDec *amrnbdec;
216 amrnbdec = GST_AMRNBDEC (dec);
218 structure = gst_caps_get_structure (caps, 0);
220 /* get channel count */
221 gst_structure_get_int (structure, "channels", &amrnbdec->channels);
222 gst_structure_get_int (structure, "rate", &amrnbdec->rate);
224 /* create reverse caps */
225 gst_audio_info_init (&info);
226 gst_audio_info_set_format (&info,
227 GST_AUDIO_FORMAT_S16, amrnbdec->rate, amrnbdec->channels, NULL);
229 return gst_audio_decoder_set_output_format (dec, &info);
233 gst_amrnbdec_parse (GstAudioDecoder * dec, GstAdapter * adapter,
234 gint * offset, gint * length)
236 GstAmrnbDec *amrnbdec = GST_AMRNBDEC (dec);
242 size = gst_adapter_available (adapter);
243 g_return_val_if_fail (size > 0, GST_FLOW_ERROR);
245 gst_audio_decoder_get_parse_state (dec, &sync, &eos);
247 /* need to peek data to get the size */
249 return GST_FLOW_ERROR;
251 gst_adapter_copy (adapter, head, 0, 1);
254 switch (amrnbdec->variant) {
255 case GST_AMRNB_VARIANT_IF1:
256 mode = (head[0] >> 3) & 0x0F;
257 block = block_size_if1[mode] + 1;
259 case GST_AMRNB_VARIANT_IF2:
260 mode = head[0] & 0x0F;
261 block = block_size_if2[mode] + 1;
264 g_assert_not_reached ();
265 return GST_FLOW_ERROR;
269 GST_DEBUG_OBJECT (amrnbdec, "mode %d, block %d", mode, block);
281 gst_amrnbdec_handle_frame (GstAudioDecoder * dec, GstBuffer * buffer)
283 GstAmrnbDec *amrnbdec;
284 GstMapInfo inmap, outmap;
287 amrnbdec = GST_AMRNBDEC (dec);
289 /* no fancy flushing */
290 if (!buffer || !gst_buffer_get_size (buffer))
293 gst_buffer_map (buffer, &inmap, GST_MAP_READ);
296 out = gst_buffer_new_and_alloc (160 * 2);
298 gst_buffer_map (out, &outmap, GST_MAP_WRITE);
299 Decoder_Interface_Decode (amrnbdec->handle, inmap.data,
300 (gint16 *) outmap.data, 0);
301 gst_buffer_unmap (out, &outmap);
303 gst_buffer_unmap (buffer, &inmap);
305 return gst_audio_decoder_finish_frame (dec, out, 1);