Merging gst-plugins-ugly
[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  * @title: amrnbdec
23  * @see_also: #GstAmrnbEnc, #GstAmrParse
24  *
25  * AMR narrowband decoder based on the
26  * [opencore codec implementation](http://sourceforge.net/projects/opencore-amr).
27  *
28  * ## Example launch line
29  * |[
30  * gst-launch-1.0 filesrc location=abc.amr ! amrparse ! amrnbdec ! audioconvert ! audioresample ! autoaudiosink
31  * ]|
32  *
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 GstFlowReturn gst_amrnbdec_parse (GstAudioDecoder * dec,
100     GstAdapter * adapter, 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 GST_ELEMENT_REGISTER_DEFINE (amrnbdec, "amrnbdec", GST_RANK_PRIMARY,
107     GST_TYPE_AMRNBDEC);
108
109 static void
110 gst_amrnbdec_class_init (GstAmrnbDecClass * klass)
111 {
112   GObjectClass *object_class = G_OBJECT_CLASS (klass);
113   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
114   GstAudioDecoderClass *base_class = GST_AUDIO_DECODER_CLASS (klass);
115
116   object_class->set_property = gst_amrnbdec_set_property;
117   object_class->get_property = gst_amrnbdec_get_property;
118
119   gst_element_class_add_static_pad_template (element_class, &sink_template);
120   gst_element_class_add_static_pad_template (element_class, &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.freedesktop.org>");
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   gst_type_mark_as_plugin_api (GST_AMRNB_VARIANT_TYPE, 0);
143 }
144
145 static void
146 gst_amrnbdec_init (GstAmrnbDec * amrnbdec)
147 {
148   gst_audio_decoder_set_needs_format (GST_AUDIO_DECODER (amrnbdec), TRUE);
149   gst_audio_decoder_set_use_default_pad_acceptcaps (GST_AUDIO_DECODER_CAST
150       (amrnbdec), TRUE);
151   GST_PAD_SET_ACCEPT_TEMPLATE (GST_AUDIO_DECODER_SINK_PAD (amrnbdec));
152 }
153
154 static gboolean
155 gst_amrnbdec_start (GstAudioDecoder * dec)
156 {
157   GstAmrnbDec *amrnbdec = GST_AMRNBDEC (dec);
158
159   GST_DEBUG_OBJECT (dec, "start");
160   if (!(amrnbdec->handle = Decoder_Interface_init ()))
161     return FALSE;
162
163   amrnbdec->rate = 0;
164   amrnbdec->channels = 0;
165
166   return TRUE;
167 }
168
169 static gboolean
170 gst_amrnbdec_stop (GstAudioDecoder * dec)
171 {
172   GstAmrnbDec *amrnbdec = GST_AMRNBDEC (dec);
173
174   GST_DEBUG_OBJECT (dec, "stop");
175   Decoder_Interface_exit (amrnbdec->handle);
176
177   return TRUE;
178 }
179
180 static void
181 gst_amrnbdec_set_property (GObject * object, guint prop_id,
182     const GValue * value, GParamSpec * pspec)
183 {
184   GstAmrnbDec *self = GST_AMRNBDEC (object);
185
186   switch (prop_id) {
187     case PROP_VARIANT:
188       self->variant = g_value_get_enum (value);
189       break;
190     default:
191       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
192       break;
193   }
194   return;
195 }
196
197 static void
198 gst_amrnbdec_get_property (GObject * object, guint prop_id,
199     GValue * value, GParamSpec * pspec)
200 {
201   GstAmrnbDec *self = GST_AMRNBDEC (object);
202
203   switch (prop_id) {
204     case PROP_VARIANT:
205       g_value_set_enum (value, self->variant);
206       break;
207     default:
208       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
209       break;
210   }
211   return;
212 }
213
214 static gboolean
215 gst_amrnbdec_set_format (GstAudioDecoder * dec, GstCaps * caps)
216 {
217   GstStructure *structure;
218   GstAmrnbDec *amrnbdec;
219   GstAudioInfo info;
220
221   amrnbdec = GST_AMRNBDEC (dec);
222
223   structure = gst_caps_get_structure (caps, 0);
224
225   /* get channel count */
226   gst_structure_get_int (structure, "channels", &amrnbdec->channels);
227   gst_structure_get_int (structure, "rate", &amrnbdec->rate);
228
229   /* create reverse caps */
230   gst_audio_info_init (&info);
231   gst_audio_info_set_format (&info,
232       GST_AUDIO_FORMAT_S16, amrnbdec->rate, amrnbdec->channels, NULL);
233
234   return gst_audio_decoder_set_output_format (dec, &info);
235 }
236
237 static GstFlowReturn
238 gst_amrnbdec_parse (GstAudioDecoder * dec, GstAdapter * adapter,
239     gint * offset, gint * length)
240 {
241   GstAmrnbDec *amrnbdec = GST_AMRNBDEC (dec);
242   guint8 head[1];
243   guint size;
244   gboolean sync, eos;
245   gint block, mode;
246
247   size = gst_adapter_available (adapter);
248   if (size < 1)
249     return GST_FLOW_ERROR;
250
251   gst_audio_decoder_get_parse_state (dec, &sync, &eos);
252
253   /* need to peek data to get the size */
254   gst_adapter_copy (adapter, head, 0, 1);
255
256   /* get size */
257   switch (amrnbdec->variant) {
258     case GST_AMRNB_VARIANT_IF1:
259       mode = (head[0] >> 3) & 0x0F;
260       block = block_size_if1[mode] + 1;
261       break;
262     case GST_AMRNB_VARIANT_IF2:
263       mode = head[0] & 0x0F;
264       block = block_size_if2[mode] + 1;
265       break;
266     default:
267       g_assert_not_reached ();
268       return GST_FLOW_ERROR;
269       break;
270   }
271
272   GST_DEBUG_OBJECT (amrnbdec, "mode %d, block %d", mode, block);
273
274   if (block > size)
275     return GST_FLOW_EOS;
276
277   *offset = 0;
278   *length = block;
279
280   return GST_FLOW_OK;
281 }
282
283 static GstFlowReturn
284 gst_amrnbdec_handle_frame (GstAudioDecoder * dec, GstBuffer * buffer)
285 {
286   GstAmrnbDec *amrnbdec;
287   GstMapInfo inmap, outmap;
288   GstBuffer *out;
289
290   amrnbdec = GST_AMRNBDEC (dec);
291
292   /* no fancy flushing */
293   if (!buffer || !gst_buffer_get_size (buffer))
294     return GST_FLOW_OK;
295
296   gst_buffer_map (buffer, &inmap, GST_MAP_READ);
297
298   /* get output */
299   out = gst_buffer_new_and_alloc (160 * 2);
300   /* decode */
301   gst_buffer_map (out, &outmap, GST_MAP_WRITE);
302   Decoder_Interface_Decode (amrnbdec->handle, inmap.data,
303       (gint16 *) outmap.data, 0);
304   gst_buffer_unmap (out, &outmap);
305
306   gst_buffer_unmap (buffer, &inmap);
307
308   return gst_audio_decoder_finish_frame (dec, out, 1);
309 }