amrnbdec: Remove some dead code
[platform/upstream/gstreamer.git] / ext / amrnb / amrnbdec.c
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-amrnbdec
22  * @see_also: #GstAmrnbEnc, #GstAmrParse
23  *
24  * AMR narrowband 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 filesrc location=abc.amr ! amrparse ! amrnbdec ! audioresample ! audioconvert ! alsasink
31  * ]|
32  * </refsect2>
33  */
34
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38
39 #include "amrnbdec.h"
40
41 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
42     GST_PAD_SINK,
43     GST_PAD_ALWAYS,
44     GST_STATIC_CAPS ("audio/AMR, " "rate = (int) 8000, " "channels = (int) 1")
45     );
46
47 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
48     GST_PAD_SRC,
49     GST_PAD_ALWAYS,
50     GST_STATIC_CAPS ("audio/x-raw, format = (string) " GST_AUDIO_NE (S16) ", "
51         "layout = (string) interleaved, "
52         "rate = (int) 8000," "channels = (int) 1")
53     );
54
55 GST_DEBUG_CATEGORY_STATIC (gst_amrnbdec_debug);
56 #define GST_CAT_DEFAULT gst_amrnbdec_debug
57
58 static const gint block_size_if1[16] = { 12, 13, 15, 17, 19, 20, 26, 31, 5,
59   0, 0, 0, 0, 0, 0, 0
60 };
61
62 static const gint block_size_if2[16] = { 12, 13, 15, 17, 18, 20, 25, 30, 5,
63   0, 0, 0, 0, 0, 0, 0
64 };
65
66 static GType
67 gst_amrnb_variant_get_type (void)
68 {
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"},
73     {0, NULL, NULL},
74   };
75   if (!gst_amrnb_variant_type) {
76     gst_amrnb_variant_type =
77         g_enum_register_static ("GstAmrnbVariant", gst_amrnb_variant);
78   }
79   return gst_amrnb_variant_type;
80 }
81
82 #define GST_AMRNB_VARIANT_TYPE (gst_amrnb_variant_get_type())
83
84 #define VARIANT_DEFAULT GST_AMRNB_VARIANT_IF1
85 enum
86 {
87   PROP_0,
88   PROP_VARIANT
89 };
90
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);
95
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,
102     GstBuffer * buffer);
103
104 #define gst_amrnbdec_parent_class parent_class
105 G_DEFINE_TYPE (GstAmrnbDec, gst_amrnbdec, GST_TYPE_AUDIO_DECODER);
106
107 static void
108 gst_amrnbdec_class_init (GstAmrnbDecClass * klass)
109 {
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);
113
114   object_class->set_property = gst_amrnbdec_set_property;
115   object_class->get_property = gst_amrnbdec_get_property;
116
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));
121
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>");
126
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);
132
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,
136           VARIANT_DEFAULT,
137           G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
138
139   GST_DEBUG_CATEGORY_INIT (gst_amrnbdec_debug, "amrnbdec", 0,
140       "AMR-NB audio decoder");
141 }
142
143 static void
144 gst_amrnbdec_init (GstAmrnbDec * amrnbdec)
145 {
146   gst_audio_decoder_set_needs_format (GST_AUDIO_DECODER (amrnbdec), TRUE);
147 }
148
149 static gboolean
150 gst_amrnbdec_start (GstAudioDecoder * dec)
151 {
152   GstAmrnbDec *amrnbdec = GST_AMRNBDEC (dec);
153
154   GST_DEBUG_OBJECT (dec, "start");
155   if (!(amrnbdec->handle = Decoder_Interface_init ()))
156     return FALSE;
157
158   amrnbdec->rate = 0;
159   amrnbdec->channels = 0;
160
161   return TRUE;
162 }
163
164 static gboolean
165 gst_amrnbdec_stop (GstAudioDecoder * dec)
166 {
167   GstAmrnbDec *amrnbdec = GST_AMRNBDEC (dec);
168
169   GST_DEBUG_OBJECT (dec, "stop");
170   Decoder_Interface_exit (amrnbdec->handle);
171
172   return TRUE;
173 }
174
175 static void
176 gst_amrnbdec_set_property (GObject * object, guint prop_id,
177     const GValue * value, GParamSpec * pspec)
178 {
179   GstAmrnbDec *self = GST_AMRNBDEC (object);
180
181   switch (prop_id) {
182     case PROP_VARIANT:
183       self->variant = g_value_get_enum (value);
184       break;
185     default:
186       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
187       break;
188   }
189   return;
190 }
191
192 static void
193 gst_amrnbdec_get_property (GObject * object, guint prop_id,
194     GValue * value, GParamSpec * pspec)
195 {
196   GstAmrnbDec *self = GST_AMRNBDEC (object);
197
198   switch (prop_id) {
199     case PROP_VARIANT:
200       g_value_set_enum (value, self->variant);
201       break;
202     default:
203       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
204       break;
205   }
206   return;
207 }
208
209 static gboolean
210 gst_amrnbdec_set_format (GstAudioDecoder * dec, GstCaps * caps)
211 {
212   GstStructure *structure;
213   GstAmrnbDec *amrnbdec;
214   GstAudioInfo info;
215
216   amrnbdec = GST_AMRNBDEC (dec);
217
218   structure = gst_caps_get_structure (caps, 0);
219
220   /* get channel count */
221   gst_structure_get_int (structure, "channels", &amrnbdec->channels);
222   gst_structure_get_int (structure, "rate", &amrnbdec->rate);
223
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);
228
229   return gst_audio_decoder_set_output_format (dec, &info);
230 }
231
232 static GstFlowReturn
233 gst_amrnbdec_parse (GstAudioDecoder * dec, GstAdapter * adapter,
234     gint * offset, gint * length)
235 {
236   GstAmrnbDec *amrnbdec = GST_AMRNBDEC (dec);
237   guint8 head[1];
238   guint size;
239   gboolean sync, eos;
240   gint block, mode;
241
242   size = gst_adapter_available (adapter);
243   g_return_val_if_fail (size > 0, GST_FLOW_ERROR);
244
245   gst_audio_decoder_get_parse_state (dec, &sync, &eos);
246
247   /* need to peek data to get the size */
248   if (size < 1)
249     return GST_FLOW_ERROR;
250
251   gst_adapter_copy (adapter, head, 0, 1);
252
253   /* get size */
254   switch (amrnbdec->variant) {
255     case GST_AMRNB_VARIANT_IF1:
256       mode = (head[0] >> 3) & 0x0F;
257       block = block_size_if1[mode] + 1;
258       break;
259     case GST_AMRNB_VARIANT_IF2:
260       mode = head[0] & 0x0F;
261       block = block_size_if2[mode] + 1;
262       break;
263     default:
264       g_assert_not_reached ();
265       return GST_FLOW_ERROR;
266       break;
267   }
268
269   GST_DEBUG_OBJECT (amrnbdec, "mode %d, block %d", mode, block);
270
271   if (block > size)
272     return GST_FLOW_EOS;
273
274   *offset = 0;
275   *length = block;
276
277   return GST_FLOW_OK;
278 }
279
280 static GstFlowReturn
281 gst_amrnbdec_handle_frame (GstAudioDecoder * dec, GstBuffer * buffer)
282 {
283   GstAmrnbDec *amrnbdec;
284   GstMapInfo inmap, outmap;
285   GstBuffer *out;
286
287   amrnbdec = GST_AMRNBDEC (dec);
288
289   /* no fancy flushing */
290   if (!buffer || !gst_buffer_get_size (buffer))
291     return GST_FLOW_OK;
292
293   gst_buffer_map (buffer, &inmap, GST_MAP_READ);
294
295   /* get output */
296   out = gst_buffer_new_and_alloc (160 * 2);
297   /* decode */
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);
302
303   gst_buffer_unmap (buffer, &inmap);
304
305   return gst_audio_decoder_finish_frame (dec, out, 1);
306 }