]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - glsdk/gst-plugins-ugly0-10.git/blob - ext/amrwbdec/amrwbdec.c
amrwbdec: move define to source to avoid hiding it from the docs
[glsdk/gst-plugins-ugly0-10.git] / ext / amrwbdec / amrwbdec.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., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
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 filesrc location=abc.amr ! amrparse ! amrwbdec ! audioresample ! audioconvert ! alsasink
31  * ]|
32  * </refsect2>
33  */
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
39 #include "amrwbdec.h"
41 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
42     GST_PAD_SINK,
43     GST_PAD_ALWAYS,
44     GST_STATIC_CAPS ("audio/AMR-WB, "
45         "rate = (int) 16000, " "channels = (int) 1")
46     );
48 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
49     GST_PAD_SRC,
50     GST_PAD_ALWAYS,
51     GST_STATIC_CAPS ("audio/x-raw-int, "
52         "width = (int) 16, "
53         "depth = (int) 16, "
54         "signed = (boolean) TRUE, "
55         "endianness = (int) BYTE_ORDER, "
56         "rate = (int) 16000, " "channels = (int) 1")
57     );
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,
66   6, 6, 0, 0, 0, 1, 1
67 };
69 static gboolean gst_amrwbdec_event (GstPad * pad, GstEvent * event);
70 static GstFlowReturn gst_amrwbdec_chain (GstPad * pad, GstBuffer * buffer);
71 static gboolean gst_amrwbdec_setcaps (GstPad * pad, GstCaps * caps);
72 static GstStateChangeReturn gst_amrwbdec_state_change (GstElement * element,
73     GstStateChange transition);
75 static void gst_amrwbdec_finalize (GObject * object);
77 #define _do_init(bla) \
78     GST_DEBUG_CATEGORY_INIT (gst_amrwbdec_debug, "amrwbdec", 0, "AMR-WB audio decoder");
80 GST_BOILERPLATE_FULL (GstAmrwbDec, gst_amrwbdec, GstElement, GST_TYPE_ELEMENT,
81     _do_init);
83 static void
84 gst_amrwbdec_base_init (gpointer klass)
85 {
86   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
88   gst_element_class_add_pad_template (element_class,
89       gst_static_pad_template_get (&sink_template));
90   gst_element_class_add_pad_template (element_class,
91       gst_static_pad_template_get (&src_template));
93   gst_element_class_set_details_simple (element_class, "AMR-WB audio decoder",
94       "Codec/Decoder/Audio",
95       "Adaptive Multi-Rate Wideband audio decoder",
96       "Renato Araujo <renato.filho@indt.org.br>");
97 }
99 static void
100 gst_amrwbdec_class_init (GstAmrwbDecClass * klass)
102   GObjectClass *object_class = G_OBJECT_CLASS (klass);
103   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
105   object_class->finalize = gst_amrwbdec_finalize;
107   element_class->change_state = GST_DEBUG_FUNCPTR (gst_amrwbdec_state_change);
110 static void
111 gst_amrwbdec_init (GstAmrwbDec * amrwbdec, GstAmrwbDecClass * klass)
113   /* create the sink pad */
114   amrwbdec->sinkpad = gst_pad_new_from_static_template (&sink_template, "sink");
115   gst_pad_set_setcaps_function (amrwbdec->sinkpad, gst_amrwbdec_setcaps);
116   gst_pad_set_event_function (amrwbdec->sinkpad, gst_amrwbdec_event);
117   gst_pad_set_chain_function (amrwbdec->sinkpad, gst_amrwbdec_chain);
118   gst_element_add_pad (GST_ELEMENT (amrwbdec), amrwbdec->sinkpad);
120   /* create the src pad */
121   amrwbdec->srcpad = gst_pad_new_from_static_template (&src_template, "src");
122   gst_pad_use_fixed_caps (amrwbdec->srcpad);
123   gst_element_add_pad (GST_ELEMENT (amrwbdec), amrwbdec->srcpad);
125   amrwbdec->adapter = gst_adapter_new ();
127   /* init rest */
128   amrwbdec->handle = NULL;
129   amrwbdec->channels = 0;
130   amrwbdec->rate = 0;
131   amrwbdec->duration = 0;
132   amrwbdec->ts = -1;
135 static void
136 gst_amrwbdec_finalize (GObject * object)
138   GstAmrwbDec *amrwbdec;
140   amrwbdec = GST_AMRWBDEC (object);
142   gst_adapter_clear (amrwbdec->adapter);
143   g_object_unref (amrwbdec->adapter);
145   G_OBJECT_CLASS (parent_class)->finalize (object);
148 static gboolean
149 gst_amrwbdec_setcaps (GstPad * pad, GstCaps * caps)
151   GstStructure *structure;
152   GstAmrwbDec *amrwbdec;
153   GstCaps *copy;
155   amrwbdec = GST_AMRWBDEC (gst_pad_get_parent (pad));
157   structure = gst_caps_get_structure (caps, 0);
159   /* get channel count */
160   gst_structure_get_int (structure, "channels", &amrwbdec->channels);
161   gst_structure_get_int (structure, "rate", &amrwbdec->rate);
163   /* create reverse caps */
164   copy = gst_caps_new_simple ("audio/x-raw-int",
165       "channels", G_TYPE_INT, amrwbdec->channels,
166       "width", G_TYPE_INT, 16,
167       "depth", G_TYPE_INT, 16,
168       "endianness", G_TYPE_INT, G_BYTE_ORDER,
169       "rate", G_TYPE_INT, amrwbdec->rate, "signed", G_TYPE_BOOLEAN, TRUE, NULL);
171   amrwbdec->duration = gst_util_uint64_scale_int (GST_SECOND, L_FRAME16k,
172       amrwbdec->rate * amrwbdec->channels);
174   gst_pad_set_caps (amrwbdec->srcpad, copy);
175   gst_caps_unref (copy);
177   gst_object_unref (amrwbdec);
179   return TRUE;
182 static gboolean
183 gst_amrwbdec_event (GstPad * pad, GstEvent * event)
185   GstAmrwbDec *amrwbdec;
186   gboolean ret = TRUE;
188   amrwbdec = GST_AMRWBDEC (gst_pad_get_parent (pad));
190   switch (GST_EVENT_TYPE (event)) {
191     case GST_EVENT_FLUSH_START:
192       ret = gst_pad_push_event (amrwbdec->srcpad, event);
193       break;
194     case GST_EVENT_FLUSH_STOP:
195       ret = gst_pad_push_event (amrwbdec->srcpad, event);
196       gst_adapter_clear (amrwbdec->adapter);
197       amrwbdec->ts = -1;
198       break;
199     case GST_EVENT_EOS:
200       gst_adapter_clear (amrwbdec->adapter);
201       ret = gst_pad_push_event (amrwbdec->srcpad, event);
202       break;
203     case GST_EVENT_NEWSEGMENT:
204     {
205       GstFormat format;
206       gdouble rate, arate;
207       gint64 start, stop, time;
208       gboolean update;
210       gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
211           &start, &stop, &time);
213       /* we need time for now */
214       if (format != GST_FORMAT_TIME)
215         goto newseg_wrong_format;
217       GST_DEBUG_OBJECT (amrwbdec,
218           "newsegment: update %d, rate %g, arate %g, start %" GST_TIME_FORMAT
219           ", stop %" GST_TIME_FORMAT ", time %" GST_TIME_FORMAT,
220           update, rate, arate, GST_TIME_ARGS (start), GST_TIME_ARGS (stop),
221           GST_TIME_ARGS (time));
223       /* now configure the values */
224       gst_segment_set_newsegment_full (&amrwbdec->segment, update,
225           rate, arate, format, start, stop, time);
226       ret = gst_pad_push_event (amrwbdec->srcpad, event);
227     }
228       break;
229     default:
230       ret = gst_pad_push_event (amrwbdec->srcpad, event);
231       break;
232   }
233 done:
234   gst_object_unref (amrwbdec);
236   return ret;
238   /* ERRORS */
239 newseg_wrong_format:
240   {
241     GST_DEBUG_OBJECT (amrwbdec, "received non TIME newsegment");
242     goto done;
243   }
246 static GstFlowReturn
247 gst_amrwbdec_chain (GstPad * pad, GstBuffer * buffer)
249   GstAmrwbDec *amrwbdec;
250   GstFlowReturn ret = GST_FLOW_OK;
252   amrwbdec = GST_AMRWBDEC (gst_pad_get_parent (pad));
254   if (amrwbdec->rate == 0 || amrwbdec->channels == 0)
255     goto not_negotiated;
257   /* discontinuity, don't combine samples before and after the
258    * DISCONT */
259   if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT)) {
260     gst_adapter_clear (amrwbdec->adapter);
261     amrwbdec->ts = -1;
262     amrwbdec->discont = TRUE;
263   }
265   /* take latest timestamp, FIXME timestamp is the one of the
266    * first buffer in the adapter. */
267   if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer))
268     amrwbdec->ts = GST_BUFFER_TIMESTAMP (buffer);
270   gst_adapter_push (amrwbdec->adapter, buffer);
272   while (TRUE) {
273     GstBuffer *out;
274     const guint8 *data;
275     gint block, mode;
277     /* need to peek data to get the size */
278     if (gst_adapter_available (amrwbdec->adapter) < 1)
279       break;
280     data = gst_adapter_peek (amrwbdec->adapter, 1);
282     /* get size */
283     mode = (data[0] >> 3) & 0x0F;
284     block = block_size[mode];
286     GST_DEBUG_OBJECT (amrwbdec, "mode %d, block %d", mode, block);
288     if (!block || gst_adapter_available (amrwbdec->adapter) < block)
289       break;
291     /* the library seems to write into the source data, hence the copy. */
292     data = gst_adapter_take (amrwbdec->adapter, block);
294     /* get output */
295     out = gst_buffer_new_and_alloc (sizeof (gint16) * L_FRAME16k);
297     GST_BUFFER_DURATION (out) = amrwbdec->duration;
298     GST_BUFFER_TIMESTAMP (out) = amrwbdec->ts;
300     if (amrwbdec->ts != -1)
301       amrwbdec->ts += amrwbdec->duration;
302     if (amrwbdec->discont) {
303       GST_BUFFER_FLAG_SET (out, GST_BUFFER_FLAG_DISCONT);
304       amrwbdec->discont = FALSE;
305     }
307     gst_buffer_set_caps (out, GST_PAD_CAPS (amrwbdec->srcpad));
309     /* decode */
310     D_IF_decode (amrwbdec->handle, (unsigned char *) data,
311         (Word16 *) GST_BUFFER_DATA (out), _good_frame);
313     g_free ((gpointer) data);
315     /* send out */
316     ret = gst_pad_push (amrwbdec->srcpad, out);
317   }
319   gst_object_unref (amrwbdec);
320   return ret;
322   /* ERRORS */
323 not_negotiated:
324   {
325     GST_ELEMENT_ERROR (amrwbdec, STREAM, TYPE_NOT_FOUND, (NULL),
326         ("Decoder is not initialized"));
327     gst_object_unref (amrwbdec);
328     return GST_FLOW_NOT_NEGOTIATED;
329   }
332 static GstStateChangeReturn
333 gst_amrwbdec_state_change (GstElement * element, GstStateChange transition)
335   GstAmrwbDec *amrwbdec;
336   GstStateChangeReturn ret;
338   amrwbdec = GST_AMRWBDEC (element);
340   switch (transition) {
341     case GST_STATE_CHANGE_NULL_TO_READY:
342       if (!(amrwbdec->handle = D_IF_init ()))
343         goto init_failed;
344       break;
345     case GST_STATE_CHANGE_READY_TO_PAUSED:
346       gst_adapter_clear (amrwbdec->adapter);
347       amrwbdec->rate = 0;
348       amrwbdec->channels = 0;
349       amrwbdec->ts = -1;
350       amrwbdec->discont = TRUE;
351       gst_segment_init (&amrwbdec->segment, GST_FORMAT_TIME);
352       break;
353     default:
354       break;
355   }
357   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
359   switch (transition) {
360     case GST_STATE_CHANGE_READY_TO_NULL:
361       D_IF_exit (amrwbdec->handle);
362       break;
363     default:
364       break;
365   }
367   return ret;
369   /* ERRORS */
370 init_failed:
371   {
372     GST_ELEMENT_ERROR (amrwbdec, LIBRARY, INIT, (NULL),
373         ("Failed to open AMR Decoder"));
374     return GST_STATE_CHANGE_FAILURE;
375   }