Move files from gst-plugins-ugly into the "subprojects/gst-plugins-ugly/" subdir
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-ugly / ext / amrnb / amrnbenc.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-amrnbenc
22  * @title: amrnbenc
23  * @see_also: #GstAmrnbDec, #GstAmrnbParse
24  *
25  * AMR narrowband encoder 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.wav ! wavparse ! audioconvert ! audioresample ! amrnbenc ! filesink location=abc.amr
31  * ]|
32  * Please note that the above stream misses the header, that is needed to play
33  * the stream.
34  *
35  */
36
37 #ifdef HAVE_CONFIG_H
38 #include "config.h"
39 #endif
40
41 #include "amrnbenc.h"
42
43 static GType
44 gst_amrnbenc_bandmode_get_type (void)
45 {
46   static GType gst_amrnbenc_bandmode_type = 0;
47   static const GEnumValue gst_amrnbenc_bandmode[] = {
48     {MR475, "MR475", "MR475"},
49     {MR515, "MR515", "MR515"},
50     {MR59, "MR59", "MR59"},
51     {MR67, "MR67", "MR67"},
52     {MR74, "MR74", "MR74"},
53     {MR795, "MR795", "MR795"},
54     {MR102, "MR102", "MR102"},
55     {MR122, "MR122", "MR122"},
56     {MRDTX, "MRDTX", "MRDTX"},
57     {0, NULL, NULL},
58   };
59   if (!gst_amrnbenc_bandmode_type) {
60     gst_amrnbenc_bandmode_type =
61         g_enum_register_static ("GstAmrnbEncBandMode", gst_amrnbenc_bandmode);
62   }
63   return gst_amrnbenc_bandmode_type;
64 }
65
66 #define GST_AMRNBENC_BANDMODE_TYPE (gst_amrnbenc_bandmode_get_type())
67
68 #define BANDMODE_DEFAULT MR122
69 enum
70 {
71   PROP_0,
72   PROP_BANDMODE
73 };
74
75 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
76     GST_PAD_SINK,
77     GST_PAD_ALWAYS,
78     GST_STATIC_CAPS ("audio/x-raw, format = (string) " GST_AUDIO_NE (S16) ", "
79         "layout = (string) interleaved, "
80         "rate = (int) 8000," "channels = (int) 1")
81     );
82
83 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
84     GST_PAD_SRC,
85     GST_PAD_ALWAYS,
86     GST_STATIC_CAPS ("audio/AMR, " "rate = (int) 8000, " "channels = (int) 1")
87     );
88
89 GST_DEBUG_CATEGORY_STATIC (gst_amrnbenc_debug);
90 #define GST_CAT_DEFAULT gst_amrnbenc_debug
91
92 static gboolean gst_amrnbenc_start (GstAudioEncoder * enc);
93 static gboolean gst_amrnbenc_stop (GstAudioEncoder * enc);
94 static gboolean gst_amrnbenc_set_format (GstAudioEncoder * enc,
95     GstAudioInfo * info);
96 static GstFlowReturn gst_amrnbenc_handle_frame (GstAudioEncoder * enc,
97     GstBuffer * in_buf);
98
99 #define gst_amrnbenc_parent_class parent_class
100 G_DEFINE_TYPE (GstAmrnbEnc, gst_amrnbenc, GST_TYPE_AUDIO_ENCODER);
101 GST_ELEMENT_REGISTER_DEFINE (amrnbenc, "amrnbenc", GST_RANK_SECONDARY,
102     GST_TYPE_AMRNBENC);
103
104 static void
105 gst_amrnbenc_set_property (GObject * object, guint prop_id,
106     const GValue * value, GParamSpec * pspec)
107 {
108   GstAmrnbEnc *self = GST_AMRNBENC (object);
109
110   switch (prop_id) {
111     case PROP_BANDMODE:
112       self->bandmode = g_value_get_enum (value);
113       break;
114     default:
115       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
116       break;
117   }
118   return;
119 }
120
121 static void
122 gst_amrnbenc_get_property (GObject * object, guint prop_id,
123     GValue * value, GParamSpec * pspec)
124 {
125   GstAmrnbEnc *self = GST_AMRNBENC (object);
126
127   switch (prop_id) {
128     case PROP_BANDMODE:
129       g_value_set_enum (value, self->bandmode);
130       break;
131     default:
132       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
133       break;
134   }
135   return;
136 }
137
138 static void
139 gst_amrnbenc_class_init (GstAmrnbEncClass * klass)
140 {
141   GObjectClass *object_class = G_OBJECT_CLASS (klass);
142   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
143   GstAudioEncoderClass *base_class = GST_AUDIO_ENCODER_CLASS (klass);
144
145   object_class->set_property = gst_amrnbenc_set_property;
146   object_class->get_property = gst_amrnbenc_get_property;
147
148   base_class->start = GST_DEBUG_FUNCPTR (gst_amrnbenc_start);
149   base_class->stop = GST_DEBUG_FUNCPTR (gst_amrnbenc_stop);
150   base_class->set_format = GST_DEBUG_FUNCPTR (gst_amrnbenc_set_format);
151   base_class->handle_frame = GST_DEBUG_FUNCPTR (gst_amrnbenc_handle_frame);
152
153   g_object_class_install_property (object_class, PROP_BANDMODE,
154       g_param_spec_enum ("band-mode", "Band Mode",
155           "Encoding Band Mode (Kbps)", GST_AMRNBENC_BANDMODE_TYPE,
156           BANDMODE_DEFAULT,
157           G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
158
159   gst_element_class_add_static_pad_template (element_class, &sink_template);
160   gst_element_class_add_static_pad_template (element_class, &src_template);
161
162   gst_element_class_set_static_metadata (element_class, "AMR-NB audio encoder",
163       "Codec/Encoder/Audio",
164       "Adaptive Multi-Rate Narrow-Band audio encoder",
165       "Wim Taymans <wim.taymans@gmail.com>");
166
167   GST_DEBUG_CATEGORY_INIT (gst_amrnbenc_debug, "amrnbenc", 0,
168       "AMR-NB audio encoder");
169
170   gst_type_mark_as_plugin_api (GST_AMRNBENC_BANDMODE_TYPE, 0);
171 }
172
173 static void
174 gst_amrnbenc_init (GstAmrnbEnc * amrnbenc)
175 {
176   GST_PAD_SET_ACCEPT_TEMPLATE (GST_AUDIO_ENCODER_SINK_PAD (amrnbenc));
177 }
178
179 static gboolean
180 gst_amrnbenc_start (GstAudioEncoder * enc)
181 {
182   GstAmrnbEnc *amrnbenc = GST_AMRNBENC (enc);
183
184   GST_DEBUG_OBJECT (amrnbenc, "start");
185
186   if (!(amrnbenc->handle = Encoder_Interface_init (0)))
187     return FALSE;
188
189   return TRUE;
190 }
191
192 static gboolean
193 gst_amrnbenc_stop (GstAudioEncoder * enc)
194 {
195   GstAmrnbEnc *amrnbenc = GST_AMRNBENC (enc);
196
197   GST_DEBUG_OBJECT (amrnbenc, "stop");
198
199   Encoder_Interface_exit (amrnbenc->handle);
200
201   return TRUE;
202 }
203
204 static gboolean
205 gst_amrnbenc_set_format (GstAudioEncoder * enc, GstAudioInfo * info)
206 {
207   GstAmrnbEnc *amrnbenc;
208   GstCaps *copy;
209
210   amrnbenc = GST_AMRNBENC (enc);
211
212   /* parameters already parsed for us */
213   amrnbenc->rate = GST_AUDIO_INFO_RATE (info);
214   amrnbenc->channels = GST_AUDIO_INFO_CHANNELS (info);
215
216   /* we do not really accept other input, but anyway ... */
217   /* this is not wrong but will sound bad */
218   if (amrnbenc->channels != 1) {
219     g_warning ("amrnbdec is only optimized for mono channels");
220   }
221   if (amrnbenc->rate != 8000) {
222     g_warning ("amrnbdec is only optimized for 8000 Hz samplerate");
223   }
224
225   /* create reverse caps */
226   copy = gst_caps_new_simple ("audio/AMR",
227       "channels", G_TYPE_INT, amrnbenc->channels,
228       "rate", G_TYPE_INT, amrnbenc->rate, NULL);
229
230   gst_audio_encoder_set_output_format (GST_AUDIO_ENCODER (amrnbenc), copy);
231   gst_caps_unref (copy);
232
233   /* report needs to base class: hand one frame at a time */
234   gst_audio_encoder_set_frame_samples_min (enc, 160);
235   gst_audio_encoder_set_frame_samples_max (enc, 160);
236   gst_audio_encoder_set_frame_max (enc, 1);
237
238   return TRUE;
239 }
240
241 static GstFlowReturn
242 gst_amrnbenc_handle_frame (GstAudioEncoder * enc, GstBuffer * buffer)
243 {
244   GstAmrnbEnc *amrnbenc;
245   GstFlowReturn ret;
246   GstBuffer *out;
247   GstMapInfo in_map, out_map;
248   gsize out_size;
249
250   amrnbenc = GST_AMRNBENC (enc);
251
252   g_return_val_if_fail (amrnbenc->handle, GST_FLOW_FLUSHING);
253
254   /* we don't deal with squeezing remnants, so simply discard those */
255   if (G_UNLIKELY (buffer == NULL)) {
256     GST_DEBUG_OBJECT (amrnbenc, "no data");
257     return GST_FLOW_OK;
258   }
259
260   gst_buffer_map (buffer, &in_map, GST_MAP_READ);
261
262   if (G_UNLIKELY (in_map.size < 320)) {
263     gst_buffer_unmap (buffer, &in_map);
264     GST_DEBUG_OBJECT (amrnbenc, "discarding trailing data of %" G_GSIZE_FORMAT
265         " bytes", in_map.size);
266     return gst_audio_encoder_finish_frame (enc, NULL, -1);
267   }
268
269   /* get output, max size is 32 */
270   out = gst_buffer_new_and_alloc (32);
271   /* AMR encoder actually writes into the source data buffers it gets */
272   /* should be able to handle that with what we are given */
273
274   gst_buffer_map (out, &out_map, GST_MAP_WRITE);
275   /* encode */
276   out_size =
277       Encoder_Interface_Encode (amrnbenc->handle, amrnbenc->bandmode,
278       (short *) in_map.data, out_map.data, 0);
279   gst_buffer_unmap (out, &out_map);
280   gst_buffer_resize (out, 0, out_size);
281   gst_buffer_unmap (buffer, &in_map);
282
283   GST_LOG_OBJECT (amrnbenc, "output data size %" G_GSIZE_FORMAT, out_size);
284
285   if (out_size) {
286     ret = gst_audio_encoder_finish_frame (enc, out, 160);
287   } else {
288     /* should not happen (without dtx or so at least) */
289     GST_WARNING_OBJECT (amrnbenc, "no encoded data; discarding input");
290     gst_buffer_unref (out);
291     ret = gst_audio_encoder_finish_frame (enc, NULL, -1);
292   }
293
294   return ret;
295 }