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-amrwbdec
23 * @see_also: #GstAmrwbEnc
25 * AMR wideband decoder based on the
26 * [opencore codec implementation](http://sourceforge.net/projects/opencore-amr).
28 * ## Example launch line
30 * gst-launch-1.0 filesrc location=abc.amr ! amrparse ! amrwbdec ! audioconvert ! audioresample ! autoaudiosink
39 #include <gst/audio/audio.h>
43 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
46 GST_STATIC_CAPS ("audio/AMR-WB, "
47 "rate = (int) 16000, " "channels = (int) 1")
50 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
53 GST_STATIC_CAPS ("audio/x-raw, "
54 "format = (string) " GST_AUDIO_NE (S16) ", "
55 "layout = (string) interleaved, "
56 "rate = (int) 16000, " "channels = (int) 1")
59 GST_DEBUG_CATEGORY_STATIC (gst_amrwbdec_debug);
60 #define GST_CAT_DEFAULT gst_amrwbdec_debug
62 #define L_FRAME16k 320 /* Frame size at 16kHz */
64 static const unsigned char block_size[16] =
65 { 18, 24, 33, 37, 41, 47, 51, 59, 61,
69 static gboolean gst_amrwbdec_start (GstAudioDecoder * dec);
70 static gboolean gst_amrwbdec_stop (GstAudioDecoder * dec);
71 static gboolean gst_amrwbdec_set_format (GstAudioDecoder * dec, GstCaps * caps);
72 static GstFlowReturn gst_amrwbdec_parse (GstAudioDecoder * dec,
73 GstAdapter * adapter, gint * offset, gint * length);
74 static GstFlowReturn gst_amrwbdec_handle_frame (GstAudioDecoder * dec,
77 #define gst_amrwbdec_parent_class parent_class
78 G_DEFINE_TYPE (GstAmrwbDec, gst_amrwbdec, GST_TYPE_AUDIO_DECODER);
79 GST_ELEMENT_REGISTER_DEFINE (amrwbdec, "amrwbdec",
80 GST_RANK_PRIMARY, GST_TYPE_AMRWBDEC);
83 gst_amrwbdec_class_init (GstAmrwbDecClass * klass)
85 GstAudioDecoderClass *base_class = GST_AUDIO_DECODER_CLASS (klass);
86 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
88 gst_element_class_add_static_pad_template (element_class, &sink_template);
89 gst_element_class_add_static_pad_template (element_class, &src_template);
91 gst_element_class_set_static_metadata (element_class, "AMR-WB audio decoder",
92 "Codec/Decoder/Audio",
93 "Adaptive Multi-Rate Wideband audio decoder",
94 "Renato Araujo <renato.filho@indt.org.br>");
96 base_class->start = GST_DEBUG_FUNCPTR (gst_amrwbdec_start);
97 base_class->stop = GST_DEBUG_FUNCPTR (gst_amrwbdec_stop);
98 base_class->set_format = GST_DEBUG_FUNCPTR (gst_amrwbdec_set_format);
99 base_class->parse = GST_DEBUG_FUNCPTR (gst_amrwbdec_parse);
100 base_class->handle_frame = GST_DEBUG_FUNCPTR (gst_amrwbdec_handle_frame);
102 GST_DEBUG_CATEGORY_INIT (gst_amrwbdec_debug, "amrwbdec", 0,
103 "AMR-WB audio decoder");
107 gst_amrwbdec_init (GstAmrwbDec * amrwbdec)
109 gst_audio_decoder_set_needs_format (GST_AUDIO_DECODER (amrwbdec), TRUE);
110 gst_audio_decoder_set_use_default_pad_acceptcaps (GST_AUDIO_DECODER_CAST
112 GST_PAD_SET_ACCEPT_TEMPLATE (GST_AUDIO_DECODER_SINK_PAD (amrwbdec));
116 gst_amrwbdec_start (GstAudioDecoder * dec)
118 GstAmrwbDec *amrwbdec = GST_AMRWBDEC (dec);
120 GST_DEBUG_OBJECT (dec, "start");
121 if (!(amrwbdec->handle = D_IF_init ()))
125 amrwbdec->channels = 0;
131 gst_amrwbdec_stop (GstAudioDecoder * dec)
133 GstAmrwbDec *amrwbdec = GST_AMRWBDEC (dec);
135 GST_DEBUG_OBJECT (dec, "stop");
136 D_IF_exit (amrwbdec->handle);
142 gst_amrwbdec_set_format (GstAudioDecoder * dec, GstCaps * caps)
144 GstStructure *structure;
145 GstAmrwbDec *amrwbdec;
148 amrwbdec = GST_AMRWBDEC (dec);
150 structure = gst_caps_get_structure (caps, 0);
152 /* get channel count */
153 gst_structure_get_int (structure, "channels", &amrwbdec->channels);
154 gst_structure_get_int (structure, "rate", &amrwbdec->rate);
156 /* create reverse caps */
157 gst_audio_info_init (&info);
158 gst_audio_info_set_format (&info,
159 GST_AUDIO_FORMAT_S16, amrwbdec->rate, amrwbdec->channels, NULL);
161 gst_audio_decoder_set_output_format (dec, &info);
167 gst_amrwbdec_parse (GstAudioDecoder * dec, GstAdapter * adapter,
168 gint * offset, gint * length)
170 GstAmrwbDec *amrwbdec = GST_AMRWBDEC (dec);
176 size = gst_adapter_available (adapter);
178 return GST_FLOW_ERROR;
180 gst_audio_decoder_get_parse_state (dec, &sync, &eos);
182 /* need to peek data to get the size */
183 gst_adapter_copy (adapter, header, 0, 1);
184 mode = (header[0] >> 3) & 0x0F;
185 block = block_size[mode];
187 GST_DEBUG_OBJECT (amrwbdec, "mode %d, block %d", mode, block);
195 /* no frame yet, skip one byte */
196 GST_LOG_OBJECT (amrwbdec, "skipping byte");
205 gst_amrwbdec_handle_frame (GstAudioDecoder * dec, GstBuffer * buffer)
207 GstAmrwbDec *amrwbdec;
209 GstMapInfo inmap, outmap;
211 amrwbdec = GST_AMRWBDEC (dec);
213 /* no fancy flushing */
214 if (!buffer || !gst_buffer_get_size (buffer))
217 /* the library seems to write into the source data, hence the copy. */
218 /* should be no problem */
219 gst_buffer_map (buffer, &inmap, GST_MAP_READ);
222 out = gst_buffer_new_and_alloc (sizeof (gint16) * L_FRAME16k);
223 gst_buffer_map (out, &outmap, GST_MAP_WRITE);
226 D_IF_decode (amrwbdec->handle, (unsigned char *) inmap.data,
227 (short int *) outmap.data, _good_frame);
229 gst_buffer_unmap (out, &outmap);
230 gst_buffer_unmap (buffer, &inmap);
233 return gst_audio_decoder_finish_frame (dec, out, 1);