1 /* GStreamer Adaptive Multi-Rate Narrow-Band (AMR-NB) plugin
2 * Copyright (C) 2004 Ronald Bultje <rbultje@ronald.bitfreak.net>
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-amrwbdec
22 * @see_also: #GstAmrwbEnc
23 *
24 * AMR wideband decoder based on the
25 * <ulink url="http://sourceforge.net/projects/opencore-amr">opencore codec implementation</ulink>.
26 *
27 * <refsect2>
28 * <title>Example launch line</title>
29 * |[
30 * gst-launch-1.0 filesrc location=abc.amr ! amrparse ! amrwbdec ! audioconvert ! audioresample ! autoaudiosink
31 * ]|
32 * </refsect2>
33 */
34
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38
39 #include <gst/audio/audio.h>
40
41 #include "amrwbdec.h"
42
43 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
44 GST_PAD_SINK,
45 GST_PAD_ALWAYS,
46 GST_STATIC_CAPS ("audio/AMR-WB, "
47 "rate = (int) 16000, " "channels = (int) 1")
48 );
49
50 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
51 GST_PAD_SRC,
52 GST_PAD_ALWAYS,
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")
57 );
58
59 GST_DEBUG_CATEGORY_STATIC (gst_amrwbdec_debug);
60 #define GST_CAT_DEFAULT gst_amrwbdec_debug
61
62 #define L_FRAME16k 320 /* Frame size at 16kHz */
63
64 static const unsigned char block_size[16] =
65 { 18, 24, 33, 37, 41, 47, 51, 59, 61,
66 6, 0, 0, 0, 0, 1, 1
67 };
68
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,
75 GstBuffer * buffer);
76
77 #define gst_amrwbdec_parent_class parent_class
78 G_DEFINE_TYPE (GstAmrwbDec, gst_amrwbdec, GST_TYPE_AUDIO_DECODER);
79
80 static void
gst_amrwbdec_class_init(GstAmrwbDecClass * klass)81 gst_amrwbdec_class_init (GstAmrwbDecClass * klass)
82 {
83 GstAudioDecoderClass *base_class = GST_AUDIO_DECODER_CLASS (klass);
84 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
85
86 gst_element_class_add_static_pad_template (element_class, &sink_template);
87 gst_element_class_add_static_pad_template (element_class, &src_template);
88
89 gst_element_class_set_static_metadata (element_class, "AMR-WB audio decoder",
90 "Codec/Decoder/Audio",
91 "Adaptive Multi-Rate Wideband audio decoder",
92 "Renato Araujo <renato.filho@indt.org.br>");
93
94 base_class->start = GST_DEBUG_FUNCPTR (gst_amrwbdec_start);
95 base_class->stop = GST_DEBUG_FUNCPTR (gst_amrwbdec_stop);
96 base_class->set_format = GST_DEBUG_FUNCPTR (gst_amrwbdec_set_format);
97 base_class->parse = GST_DEBUG_FUNCPTR (gst_amrwbdec_parse);
98 base_class->handle_frame = GST_DEBUG_FUNCPTR (gst_amrwbdec_handle_frame);
99
100 GST_DEBUG_CATEGORY_INIT (gst_amrwbdec_debug, "amrwbdec", 0,
101 "AMR-WB audio decoder");
102 }
103
104 static void
gst_amrwbdec_init(GstAmrwbDec * amrwbdec)105 gst_amrwbdec_init (GstAmrwbDec * amrwbdec)
106 {
107 gst_audio_decoder_set_needs_format (GST_AUDIO_DECODER (amrwbdec), TRUE);
108 gst_audio_decoder_set_use_default_pad_acceptcaps (GST_AUDIO_DECODER_CAST
109 (amrwbdec), TRUE);
110 GST_PAD_SET_ACCEPT_TEMPLATE (GST_AUDIO_DECODER_SINK_PAD (amrwbdec));
111 }
112
113 static gboolean
gst_amrwbdec_start(GstAudioDecoder * dec)114 gst_amrwbdec_start (GstAudioDecoder * dec)
115 {
116 GstAmrwbDec *amrwbdec = GST_AMRWBDEC (dec);
117
118 GST_DEBUG_OBJECT (dec, "start");
119 if (!(amrwbdec->handle = D_IF_init ()))
120 return FALSE;
121
122 amrwbdec->rate = 0;
123 amrwbdec->channels = 0;
124
125 return TRUE;
126 }
127
128 static gboolean
gst_amrwbdec_stop(GstAudioDecoder * dec)129 gst_amrwbdec_stop (GstAudioDecoder * dec)
130 {
131 GstAmrwbDec *amrwbdec = GST_AMRWBDEC (dec);
132
133 GST_DEBUG_OBJECT (dec, "stop");
134 D_IF_exit (amrwbdec->handle);
135
136 return TRUE;
137 }
138
139 static gboolean
gst_amrwbdec_set_format(GstAudioDecoder * dec,GstCaps * caps)140 gst_amrwbdec_set_format (GstAudioDecoder * dec, GstCaps * caps)
141 {
142 GstStructure *structure;
143 GstAmrwbDec *amrwbdec;
144 GstAudioInfo info;
145
146 amrwbdec = GST_AMRWBDEC (dec);
147
148 structure = gst_caps_get_structure (caps, 0);
149
150 /* get channel count */
151 gst_structure_get_int (structure, "channels", &amrwbdec->channels);
152 gst_structure_get_int (structure, "rate", &amrwbdec->rate);
153
154 /* create reverse caps */
155 gst_audio_info_init (&info);
156 gst_audio_info_set_format (&info,
157 GST_AUDIO_FORMAT_S16, amrwbdec->rate, amrwbdec->channels, NULL);
158
159 gst_audio_decoder_set_output_format (dec, &info);
160
161 return TRUE;
162 }
163
164 static GstFlowReturn
gst_amrwbdec_parse(GstAudioDecoder * dec,GstAdapter * adapter,gint * offset,gint * length)165 gst_amrwbdec_parse (GstAudioDecoder * dec, GstAdapter * adapter,
166 gint * offset, gint * length)
167 {
168 GstAmrwbDec *amrwbdec = GST_AMRWBDEC (dec);
169 guint8 header[1];
170 guint size;
171 gboolean sync, eos;
172 gint block, mode;
173
174 size = gst_adapter_available (adapter);
175 if (size < 1)
176 return GST_FLOW_ERROR;
177
178 gst_audio_decoder_get_parse_state (dec, &sync, &eos);
179
180 /* need to peek data to get the size */
181 gst_adapter_copy (adapter, header, 0, 1);
182 mode = (header[0] >> 3) & 0x0F;
183 block = block_size[mode];
184
185 GST_DEBUG_OBJECT (amrwbdec, "mode %d, block %d", mode, block);
186
187 if (block) {
188 if (block > size)
189 return GST_FLOW_EOS;
190 *offset = 0;
191 *length = block;
192 } else {
193 /* no frame yet, skip one byte */
194 GST_LOG_OBJECT (amrwbdec, "skipping byte");
195 *offset = 1;
196 return GST_FLOW_EOS;
197 }
198
199 return GST_FLOW_OK;
200 }
201
202 static GstFlowReturn
gst_amrwbdec_handle_frame(GstAudioDecoder * dec,GstBuffer * buffer)203 gst_amrwbdec_handle_frame (GstAudioDecoder * dec, GstBuffer * buffer)
204 {
205 GstAmrwbDec *amrwbdec;
206 GstBuffer *out;
207 GstMapInfo inmap, outmap;
208
209 amrwbdec = GST_AMRWBDEC (dec);
210
211 /* no fancy flushing */
212 if (!buffer || !gst_buffer_get_size (buffer))
213 return GST_FLOW_OK;
214
215 /* the library seems to write into the source data, hence the copy. */
216 /* should be no problem */
217 gst_buffer_map (buffer, &inmap, GST_MAP_READ);
218
219 /* get output */
220 out = gst_buffer_new_and_alloc (sizeof (gint16) * L_FRAME16k);
221 gst_buffer_map (out, &outmap, GST_MAP_WRITE);
222
223 /* decode */
224 D_IF_decode (amrwbdec->handle, (unsigned char *) inmap.data,
225 (short int *) outmap.data, _good_frame);
226
227 gst_buffer_unmap (out, &outmap);
228 gst_buffer_unmap (buffer, &inmap);
229
230 /* send out */
231 return gst_audio_decoder_finish_frame (dec, out, 1);
232 }
233