Merging gst-plugins-ugly
[platform/upstream/gstreamer.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., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 /**
21  * SECTION:element-amrwbdec
22  * @title: amrwbdec
23  * @see_also: #GstAmrwbEnc
24  *
25  * AMR wideband 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 ! amrwbdec ! audioconvert ! audioresample ! autoaudiosink
31  * ]|
32  *
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 GST_ELEMENT_REGISTER_DEFINE (amrwbdec, "amrwbdec",
80     GST_RANK_PRIMARY, GST_TYPE_AMRWBDEC);
81
82 static void
83 gst_amrwbdec_class_init (GstAmrwbDecClass * klass)
84 {
85   GstAudioDecoderClass *base_class = GST_AUDIO_DECODER_CLASS (klass);
86   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
87
88   gst_element_class_add_static_pad_template (element_class, &sink_template);
89   gst_element_class_add_static_pad_template (element_class, &src_template);
90
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>");
95
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);
101
102   GST_DEBUG_CATEGORY_INIT (gst_amrwbdec_debug, "amrwbdec", 0,
103       "AMR-WB audio decoder");
104 }
105
106 static void
107 gst_amrwbdec_init (GstAmrwbDec * amrwbdec)
108 {
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
111       (amrwbdec), TRUE);
112   GST_PAD_SET_ACCEPT_TEMPLATE (GST_AUDIO_DECODER_SINK_PAD (amrwbdec));
113 }
114
115 static gboolean
116 gst_amrwbdec_start (GstAudioDecoder * dec)
117 {
118   GstAmrwbDec *amrwbdec = GST_AMRWBDEC (dec);
119
120   GST_DEBUG_OBJECT (dec, "start");
121   if (!(amrwbdec->handle = D_IF_init ()))
122     return FALSE;
123
124   amrwbdec->rate = 0;
125   amrwbdec->channels = 0;
126
127   return TRUE;
128 }
129
130 static gboolean
131 gst_amrwbdec_stop (GstAudioDecoder * dec)
132 {
133   GstAmrwbDec *amrwbdec = GST_AMRWBDEC (dec);
134
135   GST_DEBUG_OBJECT (dec, "stop");
136   D_IF_exit (amrwbdec->handle);
137
138   return TRUE;
139 }
140
141 static gboolean
142 gst_amrwbdec_set_format (GstAudioDecoder * dec, GstCaps * caps)
143 {
144   GstStructure *structure;
145   GstAmrwbDec *amrwbdec;
146   GstAudioInfo info;
147
148   amrwbdec = GST_AMRWBDEC (dec);
149
150   structure = gst_caps_get_structure (caps, 0);
151
152   /* get channel count */
153   gst_structure_get_int (structure, "channels", &amrwbdec->channels);
154   gst_structure_get_int (structure, "rate", &amrwbdec->rate);
155
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);
160
161   gst_audio_decoder_set_output_format (dec, &info);
162
163   return TRUE;
164 }
165
166 static GstFlowReturn
167 gst_amrwbdec_parse (GstAudioDecoder * dec, GstAdapter * adapter,
168     gint * offset, gint * length)
169 {
170   GstAmrwbDec *amrwbdec = GST_AMRWBDEC (dec);
171   guint8 header[1];
172   guint size;
173   gboolean sync, eos;
174   gint block, mode;
175
176   size = gst_adapter_available (adapter);
177   if (size < 1)
178     return GST_FLOW_ERROR;
179
180   gst_audio_decoder_get_parse_state (dec, &sync, &eos);
181
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];
186
187   GST_DEBUG_OBJECT (amrwbdec, "mode %d, block %d", mode, block);
188
189   if (block) {
190     if (block > size)
191       return GST_FLOW_EOS;
192     *offset = 0;
193     *length = block;
194   } else {
195     /* no frame yet, skip one byte */
196     GST_LOG_OBJECT (amrwbdec, "skipping byte");
197     *offset = 1;
198     return GST_FLOW_EOS;
199   }
200
201   return GST_FLOW_OK;
202 }
203
204 static GstFlowReturn
205 gst_amrwbdec_handle_frame (GstAudioDecoder * dec, GstBuffer * buffer)
206 {
207   GstAmrwbDec *amrwbdec;
208   GstBuffer *out;
209   GstMapInfo inmap, outmap;
210
211   amrwbdec = GST_AMRWBDEC (dec);
212
213   /* no fancy flushing */
214   if (!buffer || !gst_buffer_get_size (buffer))
215     return GST_FLOW_OK;
216
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);
220
221   /* get output */
222   out = gst_buffer_new_and_alloc (sizeof (gint16) * L_FRAME16k);
223   gst_buffer_map (out, &outmap, GST_MAP_WRITE);
224
225   /* decode */
226   D_IF_decode (amrwbdec->handle, (unsigned char *) inmap.data,
227       (short int *) outmap.data, _good_frame);
228
229   gst_buffer_unmap (out, &outmap);
230   gst_buffer_unmap (buffer, &inmap);
231
232   /* send out */
233   return gst_audio_decoder_finish_frame (dec, out, 1);
234 }