lame: Avoid crash when seeking before negotiating
[platform/upstream/gst-plugins-good.git] / ext / lame / gstlame.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  * Copyright (C) <2004> Wim Taymans <wim@fluendo.com>
4  * Copyright (C) <2005> Thomas Vander Stichele <thomas at apestaart dot org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 /**
23  * SECTION:element-lame
24  * @see_also: lamemp3enc, mad, vorbisenc
25  *
26  * This element encodes raw integer audio into an MPEG-1 layer 3 (MP3) stream.
27  * Note that <ulink url="http://en.wikipedia.org/wiki/MP3">MP3</ulink> is not
28  * a free format, there are licensing and patent issues to take into
29  * consideration. See <ulink url="http://www.vorbis.com/">Ogg/Vorbis</ulink>
30  * for a royalty free (and often higher quality) alternative.
31  *
32  * <refsect2>
33  * <title>Note</title>
34  * This element is deprecated, use the lamemp3enc element instead
35  * which provides a much simpler interface and results in better MP3 files.
36  * </refsect2>
37  *
38  * <refsect2>
39  * <title>Output sample rate</title>
40  * If no fixed output sample rate is negotiated on the element's src pad,
41  * the element will choose an optimal sample rate to resample to internally.
42  * For example, a 16-bit 44.1 KHz mono audio stream encoded at 48 kbit will
43  * get resampled to 32 KHz.  Use filter caps on the src pad to force a
44  * particular sample rate.
45  * </refsect2>
46  * <refsect2>
47  * <title>Writing metadata (tags)</title>
48  * Whilst the lame encoder element does claim to implement the GstTagSetter
49  * interface, it does so only for backwards compatibility reasons. Tag writing
50  * has been removed from lame. Use external elements like id3v2mux or apev2mux
51  * to add tags to your MP3 streams. The same goes for XING headers: use the
52  * xingmux element to add XING headers to your VBR mp3 file.
53  * </refsect2>
54  * <refsect2>
55  * <title>Example pipelines</title>
56  * |[
57  * gst-launch -v audiotestsrc wave=sine num-buffers=100 ! audioconvert ! lame ! filesink location=sine.mp3
58  * ]| Encode a test sine signal to MP3.
59  * |[
60  * gst-launch -v alsasrc ! audioconvert ! lame bitrate=192 ! filesink location=alsasrc.mp3
61  * ]| Record from a sound card using ALSA and encode to MP3
62  * |[
63  * gst-launch -v filesrc location=music.wav ! decodebin ! audioconvert ! audioresample ! lame bitrate=192 ! id3v2mux ! filesink location=music.mp3
64  * ]| Transcode from a .wav file to MP3 (the id3v2mux element is optional)
65  * |[
66  * gst-launch -v cdda://5 ! audioconvert ! lame bitrate=192 ! filesink location=track5.mp3
67  * ]| Encode Audio CD track 5 to MP3
68  * |[
69  * gst-launch -v audiotestsrc num-buffers=10 ! audio/x-raw-int,rate=44100,channels=1 ! lame bitrate=48 mode=3 ! filesink location=test.mp3
70  * ]| Encode to a fixed sample rate
71  * </refsect2>
72  *
73  * Last reviewed on 2007-07-24 (0.10.7)
74  */
75
76 /* FIXME 0.11: Remove all properties except the useful ones. Nobody knows what most
77  * properties are doing and they're intended for LAME developers only.
78  */
79
80 #ifdef HAVE_CONFIG_H
81 #include "config.h"
82 #endif
83
84 #include "string.h"
85 #include "gstlame.h"
86 #include "gst/gst-i18n-plugin.h"
87
88 #ifdef lame_set_preset
89 #define GST_LAME_PRESET
90 #endif
91
92 GST_DEBUG_CATEGORY_STATIC (debug);
93 #define GST_CAT_DEFAULT debug
94
95 /* elementfactory information */
96 static GstElementDetails gst_lame_details = {
97   "L.A.M.E. mp3 encoder",
98   "Codec/Encoder/Audio",
99   "High-quality free MP3 encoder (deprecated)",
100   "Erik Walthinsen <omega@cse.ogi.edu>, " "Wim Taymans <wim@fluendo.com>",
101 };
102
103 /* LAME can do MPEG-1, MPEG-2, and MPEG-2.5, so it has 9 possible
104  * sample rates it supports */
105 static GstStaticPadTemplate gst_lame_sink_template =
106 GST_STATIC_PAD_TEMPLATE ("sink",
107     GST_PAD_SINK,
108     GST_PAD_ALWAYS,
109     GST_STATIC_CAPS ("audio/x-raw-int, "
110         "endianness = (int) " G_STRINGIFY (G_BYTE_ORDER) ", "
111         "signed = (boolean) true, "
112         "width = (int) 16, "
113         "depth = (int) 16, "
114         "rate = (int) { 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000 }, "
115         "channels = (int) [ 1, 2 ]")
116     );
117
118 static GstStaticPadTemplate gst_lame_src_template =
119 GST_STATIC_PAD_TEMPLATE ("src",
120     GST_PAD_SRC,
121     GST_PAD_ALWAYS,
122     GST_STATIC_CAPS ("audio/mpeg, "
123         "mpegversion = (int) 1, "
124         "layer = (int) 3, "
125         "rate = (int) { 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000 }, "
126         "channels = (int) [ 1, 2 ]")
127     );
128
129 static struct
130 {
131   gint bitrate;
132   gfloat compression_ratio;
133   gint quality;
134   gint mode;
135   gboolean force_ms;
136   gboolean free_format;
137   gboolean copyright;
138   gboolean original;
139   gboolean error_protection;
140   gboolean extension;
141   gboolean strict_iso;
142   gboolean disable_reservoir;
143   gint vbr;
144   gint vbr_quality;
145   gint vbr_mean_bitrate;
146   gint vbr_min_bitrate;
147   gint vbr_max_bitrate;
148   gint vbr_hard_min;
149   gint lowpass_freq;
150   gint lowpass_width;
151   gint highpass_freq;
152   gint highpass_width;
153   gboolean ath_only;
154   gboolean ath_short;
155   gboolean no_ath;
156   gint ath_type;
157   gint ath_lower;
158   gboolean allow_diff_short;
159   gboolean no_short_blocks;
160   gboolean emphasis;
161   gint preset;
162 } gst_lame_default_settings;
163
164 /********** Define useful types for non-programmatic interfaces **********/
165 #define GST_TYPE_LAME_MODE (gst_lame_mode_get_type())
166 static GType
167 gst_lame_mode_get_type (void)
168 {
169   static GType lame_mode_type = 0;
170   static GEnumValue lame_modes[] = {
171     {0, "Stereo", "stereo"},
172     {1, "Joint Stereo", "joint"},
173     {2, "Dual Channel", "dual"},
174     {3, "Mono", "mono"},
175     {4, "Auto", "auto"},
176     {0, NULL, NULL}
177   };
178
179   if (!lame_mode_type) {
180     lame_mode_type = g_enum_register_static ("GstLameMode", lame_modes);
181   }
182   return lame_mode_type;
183 }
184
185 #define GST_TYPE_LAME_QUALITY (gst_lame_quality_get_type())
186 static GType
187 gst_lame_quality_get_type (void)
188 {
189   static GType lame_quality_type = 0;
190   static GEnumValue lame_quality[] = {
191     {0, "0 - Best", "0"},
192     {1, "1", "1"},
193     {2, "2", "2"},
194     {3, "3", "3"},
195     {4, "4", "4"},
196     {5, "5 - Default", "5"},
197     {6, "6", "6"},
198     {7, "7", "7"},
199     {8, "8", "8"},
200     {9, "9 - Worst", "9"},
201     {0, NULL, NULL}
202   };
203
204   if (!lame_quality_type) {
205     lame_quality_type = g_enum_register_static ("GstLameQuality", lame_quality);
206   }
207   return lame_quality_type;
208 }
209
210 #define GST_TYPE_LAME_PADDING (gst_lame_padding_get_type())
211 static GType
212 gst_lame_padding_get_type (void)
213 {
214   static GType lame_padding_type = 0;
215   static GEnumValue lame_padding[] = {
216     {0, "No Padding", "never"},
217     {1, "Always Pad", "always"},
218     {2, "Adjust Padding", "adjust"},
219     {0, NULL, NULL}
220   };
221
222   if (!lame_padding_type) {
223     lame_padding_type = g_enum_register_static ("GstLamePadding", lame_padding);
224   }
225   return lame_padding_type;
226 }
227
228 #define GST_TYPE_LAME_VBRMODE (gst_lame_vbrmode_get_type())
229 static GType
230 gst_lame_vbrmode_get_type (void)
231 {
232   static GType lame_vbrmode_type = 0;
233   static GEnumValue lame_vbrmode[] = {
234     {vbr_off, "No VBR (Constant Bitrate)", "none"},
235     {vbr_rh, "Lame's old VBR algorithm", "old"},
236     {vbr_abr, "VBR Average Bitrate", "abr"},
237     {vbr_mtrh, "Lame's new VBR algorithm", "new"},
238     {0, NULL, NULL}
239   };
240
241   if (!lame_vbrmode_type) {
242     lame_vbrmode_type = g_enum_register_static ("GstLameVbrmode", lame_vbrmode);
243   }
244
245   return lame_vbrmode_type;
246 }
247
248 #ifdef GSTLAME_PRESET
249 #define GST_TYPE_LAME_PRESET (gst_lame_preset_get_type())
250 static GType
251 gst_lame_preset_get_type (void)
252 {
253   static GType gst_lame_preset = 0;
254   static GEnumValue gst_lame_presets[] = {
255     {0, "None", "none"},
256     {MEDIUM, "Medium", "medium"},
257     {STANDARD, "Standard", "standard"},
258     {EXTREME, "Extreme", "extreme"},
259     {INSANE, "Insane", "insane"},
260     {0, NULL, NULL}
261   };
262
263   if (!gst_lame_preset) {
264     gst_lame_preset =
265         g_enum_register_static ("GstLamePreset", gst_lame_presets);
266   }
267
268   return gst_lame_preset;
269 }
270 #endif
271
272 /********** Standard stuff for signals and arguments **********/
273
274 enum
275 {
276   ARG_0,
277   ARG_BITRATE,
278   ARG_COMPRESSION_RATIO,
279   ARG_QUALITY,
280   ARG_MODE,
281   ARG_FORCE_MS,
282   ARG_FREE_FORMAT,
283   ARG_COPYRIGHT,
284   ARG_ORIGINAL,
285   ARG_ERROR_PROTECTION,
286   ARG_PADDING_TYPE,             /* FIXME: remove in 0.11 */
287   ARG_EXTENSION,
288   ARG_STRICT_ISO,
289   ARG_DISABLE_RESERVOIR,
290   ARG_VBR,
291   ARG_VBR_MEAN_BITRATE,
292   ARG_VBR_MIN_BITRATE,
293   ARG_VBR_MAX_BITRATE,
294   ARG_VBR_HARD_MIN,
295   ARG_LOWPASS_FREQ,
296   ARG_LOWPASS_WIDTH,
297   ARG_HIGHPASS_FREQ,
298   ARG_HIGHPASS_WIDTH,
299   ARG_ATH_ONLY,
300   ARG_ATH_SHORT,
301   ARG_NO_ATH,
302   ARG_ATH_LOWER,
303   ARG_CWLIMIT,                  /* FIXME: remove in 0.11 */
304   ARG_ALLOW_DIFF_SHORT,
305   ARG_NO_SHORT_BLOCKS,
306   ARG_EMPHASIS,
307   ARG_VBR_QUALITY,
308 #ifdef GSTLAME_PRESET
309   ARG_XINGHEADER,               /* FIXME: remove in 0.11 */
310   ARG_PRESET
311 #else
312   ARG_XINGHEADER                /* FIXME: remove in 0.11 */
313 #endif
314 };
315
316 static void gst_lame_base_init (gpointer g_class);
317 static void gst_lame_class_init (GstLameClass * klass);
318 static void gst_lame_init (GstLame * gst_lame);
319
320 static void gst_lame_set_property (GObject * object, guint prop_id,
321     const GValue * value, GParamSpec * pspec);
322 static void gst_lame_get_property (GObject * object, guint prop_id,
323     GValue * value, GParamSpec * pspec);
324 static gboolean gst_lame_sink_event (GstPad * pad, GstEvent * event);
325 static GstFlowReturn gst_lame_chain (GstPad * pad, GstBuffer * buf);
326 static gboolean gst_lame_setup (GstLame * lame);
327 static GstStateChangeReturn gst_lame_change_state (GstElement * element,
328     GstStateChange transition);
329
330 static GstElementClass *parent_class = NULL;
331
332 GType
333 gst_lame_get_type (void)
334 {
335   static GType gst_lame_type = 0;
336
337   if (!gst_lame_type) {
338     static const GTypeInfo gst_lame_info = {
339       sizeof (GstLameClass),
340       gst_lame_base_init,
341       NULL,
342       (GClassInitFunc) gst_lame_class_init,
343       NULL,
344       NULL,
345       sizeof (GstLame),
346       0,
347       (GInstanceInitFunc) gst_lame_init,
348     };
349
350     /* FIXME: remove support for the GstTagSetter interface in 0.11 */
351     static const GInterfaceInfo tag_setter_info = {
352       NULL,
353       NULL,
354       NULL
355     };
356     static const GInterfaceInfo preset_info = {
357       NULL,
358       NULL,
359       NULL
360     };
361
362     gst_lame_type =
363         g_type_register_static (GST_TYPE_ELEMENT, "GstLame", &gst_lame_info, 0);
364     g_type_add_interface_static (gst_lame_type, GST_TYPE_TAG_SETTER,
365         &tag_setter_info);
366     g_type_add_interface_static (gst_lame_type, GST_TYPE_PRESET, &preset_info);
367   }
368   return gst_lame_type;
369 }
370
371 static void
372 gst_lame_release_memory (GstLame * lame)
373 {
374   if (lame->lgf) {
375     lame_close (lame->lgf);
376     lame->lgf = NULL;
377   }
378 }
379
380 static void
381 gst_lame_finalize (GObject * obj)
382 {
383   gst_lame_release_memory (GST_LAME (obj));
384
385   G_OBJECT_CLASS (parent_class)->finalize (obj);
386 }
387
388 static void
389 gst_lame_base_init (gpointer g_class)
390 {
391   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
392
393   gst_element_class_add_pad_template (element_class,
394       gst_static_pad_template_get (&gst_lame_src_template));
395   gst_element_class_add_pad_template (element_class,
396       gst_static_pad_template_get (&gst_lame_sink_template));
397   gst_element_class_set_details (element_class, &gst_lame_details);
398 }
399
400 static void
401 gst_lame_class_init (GstLameClass * klass)
402 {
403   GObjectClass *gobject_class;
404   GstElementClass *gstelement_class;
405
406   gobject_class = (GObjectClass *) klass;
407   gstelement_class = (GstElementClass *) klass;
408
409   parent_class = g_type_class_peek_parent (klass);
410
411   gobject_class->set_property = gst_lame_set_property;
412   gobject_class->get_property = gst_lame_get_property;
413   gobject_class->finalize = gst_lame_finalize;
414
415   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BITRATE,
416       g_param_spec_int ("bitrate", "Bitrate (kb/s)",
417           "Bitrate in kbit/sec (8, 16, 24, 32, 40, 48, 56, 64, 80, 96, "
418           "112, 128, 160, 192, 224, 256 or 320)",
419           0, 320, gst_lame_default_settings.bitrate, G_PARAM_READWRITE));
420   /* compression ratio set to 0.0 by default otherwise it overrides the bitrate setting */
421   g_object_class_install_property (G_OBJECT_CLASS (klass),
422       ARG_COMPRESSION_RATIO, g_param_spec_float ("compression_ratio",
423           "Compression Ratio",
424           "let lame choose bitrate to achieve selected compression ratio", 0.0,
425           200.0, gst_lame_default_settings.compression_ratio,
426           G_PARAM_READWRITE));
427   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_QUALITY,
428       g_param_spec_enum ("quality", "Quality",
429           "Quality of algorithm used for encoding", GST_TYPE_LAME_QUALITY,
430           gst_lame_default_settings.quality, G_PARAM_READWRITE));
431   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MODE,
432       g_param_spec_enum ("mode", "Mode", "Encoding mode", GST_TYPE_LAME_MODE,
433           gst_lame_default_settings.mode, G_PARAM_READWRITE));
434   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FORCE_MS,
435       g_param_spec_boolean ("force-ms", "Force ms",
436           "Force ms_stereo on all frames", gst_lame_default_settings.force_ms,
437           G_PARAM_READWRITE));
438   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FREE_FORMAT,
439       g_param_spec_boolean ("free-format", "Free format",
440           "Produce a free format bitstream",
441           gst_lame_default_settings.free_format, G_PARAM_READWRITE));
442   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_COPYRIGHT,
443       g_param_spec_boolean ("copyright", "Copyright", "Mark as copyright",
444           gst_lame_default_settings.copyright, G_PARAM_READWRITE));
445   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_ORIGINAL,
446       g_param_spec_boolean ("original", "Original", "Mark as original",
447           gst_lame_default_settings.original, G_PARAM_READWRITE));
448   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_ERROR_PROTECTION,
449       g_param_spec_boolean ("error-protection", "Error protection",
450           "Adds 16 bit checksum to every frame",
451           gst_lame_default_settings.error_protection, G_PARAM_READWRITE));
452   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PADDING_TYPE,
453       g_param_spec_enum ("padding-type", "Padding type",
454           "Padding type " "(DEPRECATED: this setting has no effect)",
455           GST_TYPE_LAME_PADDING, FALSE, G_PARAM_READWRITE));
456   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_EXTENSION,
457       g_param_spec_boolean ("extension", "Extension", "Extension",
458           gst_lame_default_settings.extension, G_PARAM_READWRITE));
459   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_STRICT_ISO,
460       g_param_spec_boolean ("strict-iso", "Strict ISO",
461           "Comply as much as possible to ISO MPEG spec", TRUE,
462           G_PARAM_READWRITE));
463   g_object_class_install_property (G_OBJECT_CLASS (klass),
464       ARG_DISABLE_RESERVOIR, g_param_spec_boolean ("disable-reservoir",
465           "Disable reservoir", "Disable the bit reservoir", TRUE,
466           G_PARAM_READWRITE));
467   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_VBR,
468       g_param_spec_enum ("vbr", "VBR", "Specify bitrate mode",
469           GST_TYPE_LAME_VBRMODE, gst_lame_default_settings.vbr,
470           G_PARAM_READWRITE));
471   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_VBR_QUALITY,
472       g_param_spec_enum ("vbr-quality", "VBR Quality", "VBR Quality",
473           GST_TYPE_LAME_QUALITY, gst_lame_default_settings.vbr_quality,
474           G_PARAM_READWRITE));
475   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_VBR_MEAN_BITRATE,
476       g_param_spec_int ("vbr-mean-bitrate", "VBR mean bitrate",
477           "Specify mean VBR bitrate", 0, 320,
478           gst_lame_default_settings.vbr_mean_bitrate, G_PARAM_READWRITE));
479   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_VBR_MIN_BITRATE,
480       g_param_spec_int ("vbr-min-bitrate", "VBR min bitrate",
481           "Specify minimum VBR bitrate (8, 16, 24, 32, 40, 48, 56, 64, 80, 96, "
482           "112, 128, 160, 192, 224, 256 or 320)", 0, 320,
483           gst_lame_default_settings.vbr_min_bitrate, G_PARAM_READWRITE));
484   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_VBR_MAX_BITRATE,
485       g_param_spec_int ("vbr-max-bitrate", "VBR max bitrate",
486           "Specify maximum VBR bitrate (8, 16, 24, 32, 40, 48, 56, 64, 80, 96, "
487           "112, 128, 160, 192, 224, 256 or 320)", 0, 320,
488           gst_lame_default_settings.vbr_max_bitrate, G_PARAM_READWRITE));
489   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_VBR_HARD_MIN,
490       g_param_spec_int ("vbr-hard-min", "VBR hard min",
491           "Specify whether min VBR bitrate is a hard limit. Normally, "
492           "it can be violated for silence", 0, 1,
493           gst_lame_default_settings.vbr_hard_min, G_PARAM_READWRITE));
494   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LOWPASS_FREQ,
495       g_param_spec_int ("lowpass-freq", "Lowpass freq",
496           "frequency(kHz), lowpass filter cutoff above freq", 0, 50000,
497           gst_lame_default_settings.lowpass_freq, G_PARAM_READWRITE));
498   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LOWPASS_WIDTH,
499       g_param_spec_int ("lowpass-width", "Lowpass width",
500           "frequency(kHz) - default 15% of lowpass freq", -1, G_MAXINT,
501           gst_lame_default_settings.lowpass_width, G_PARAM_READWRITE));
502   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_HIGHPASS_FREQ,
503       g_param_spec_int ("highpass-freq", "Highpass freq",
504           "frequency(kHz), highpass filter cutoff below freq", 0, 50000,
505           gst_lame_default_settings.highpass_freq, G_PARAM_READWRITE));
506   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_HIGHPASS_WIDTH,
507       g_param_spec_int ("highpass-width", "Highpass width",
508           "frequency(kHz) - default 15% of highpass freq", -1, G_MAXINT,
509           gst_lame_default_settings.highpass_width, G_PARAM_READWRITE));
510   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_ATH_ONLY,
511       g_param_spec_boolean ("ath-only", "ATH only",
512           "Ignore GPSYCHO completely, use ATH only",
513           gst_lame_default_settings.ath_only, G_PARAM_READWRITE));
514   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_ATH_SHORT,
515       g_param_spec_boolean ("ath-short", "ATH short",
516           "Ignore GPSYCHO for short blocks, use ATH only",
517           gst_lame_default_settings.ath_short, G_PARAM_READWRITE));
518   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_NO_ATH,
519       g_param_spec_boolean ("no-ath", "No ath",
520           "turns ATH down to a flat noise floor",
521           gst_lame_default_settings.no_ath, G_PARAM_READWRITE));
522   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_ATH_LOWER,
523       g_param_spec_int ("ath-lower", "ATH lower", "lowers ATH by x dB",
524           G_MININT, G_MAXINT, gst_lame_default_settings.ath_lower,
525           G_PARAM_READWRITE));
526   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_CWLIMIT,
527       g_param_spec_int ("cwlimit", "Cwlimit",
528           "Compute tonality up to freq (in kHz) default 8.8717 "
529           "(DEPRECATED: this setting has no effect)", 0, 50000, 0,
530           G_PARAM_READWRITE));
531   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_ALLOW_DIFF_SHORT,
532       g_param_spec_boolean ("allow-diff-short", "Allow diff short",
533           "Allow diff short", gst_lame_default_settings.allow_diff_short,
534           G_PARAM_READWRITE));
535   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_NO_SHORT_BLOCKS,
536       g_param_spec_boolean ("no-short-blocks", "No short blocks",
537           "Do not use short blocks", gst_lame_default_settings.no_short_blocks,
538           G_PARAM_READWRITE));
539   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_EMPHASIS,
540       g_param_spec_boolean ("emphasis", "Emphasis", "Emphasis",
541           gst_lame_default_settings.emphasis, G_PARAM_READWRITE));
542   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_XINGHEADER,
543       g_param_spec_boolean ("xingheader", "Output Xing Header",
544           "Output Xing Header (BROKEN, use xingmux instead)", FALSE,
545           G_PARAM_READWRITE));
546 #ifdef GSTLAME_PRESET
547   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PRESET,
548       g_param_spec_enum ("preset", "Lame Preset", "Lame Preset",
549           GST_TYPE_LAME_PRESET, gst_lame_default_settings.preset,
550           G_PARAM_READWRITE));
551 #endif
552
553   gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_lame_change_state);
554 }
555
556 static gboolean
557 gst_lame_src_setcaps (GstPad * pad, GstCaps * caps)
558 {
559   GST_DEBUG_OBJECT (pad, "caps: %" GST_PTR_FORMAT, caps);
560   return TRUE;
561 }
562
563 static gboolean
564 gst_lame_sink_setcaps (GstPad * pad, GstCaps * caps)
565 {
566   GstLame *lame;
567   gint out_samplerate;
568   gint version;
569   GstStructure *structure;
570   GstCaps *othercaps;
571
572   lame = GST_LAME (GST_PAD_PARENT (pad));
573   structure = gst_caps_get_structure (caps, 0);
574
575   if (!gst_structure_get_int (structure, "rate", &lame->samplerate))
576     goto no_rate;
577   if (!gst_structure_get_int (structure, "channels", &lame->num_channels))
578     goto no_channels;
579
580   GST_DEBUG_OBJECT (lame, "setting up lame");
581   if (!gst_lame_setup (lame))
582     goto setup_failed;
583
584
585   out_samplerate = lame_get_out_samplerate (lame->lgf);
586   if (out_samplerate == 0)
587     goto zero_output_rate;
588   if (out_samplerate != lame->samplerate) {
589     GST_WARNING_OBJECT (lame,
590         "output samplerate %d is different from incoming samplerate %d",
591         out_samplerate, lame->samplerate);
592   }
593
594   version = lame_get_version (lame->lgf);
595   if (version == 0)
596     version = 2;
597   else if (version == 1)
598     version = 1;
599   else if (version == 2)
600     version = 3;
601
602   othercaps =
603       gst_caps_new_simple ("audio/mpeg",
604       "mpegversion", G_TYPE_INT, 1,
605       "mpegaudioversion", G_TYPE_INT, version,
606       "layer", G_TYPE_INT, 3,
607       "channels", G_TYPE_INT, lame->mode == MONO ? 1 : lame->num_channels,
608       "rate", G_TYPE_INT, out_samplerate, NULL);
609
610   /* and use these caps */
611   gst_pad_set_caps (lame->srcpad, othercaps);
612   gst_caps_unref (othercaps);
613
614   return TRUE;
615
616 no_rate:
617   {
618     GST_ERROR_OBJECT (lame, "input caps have no sample rate field");
619     return FALSE;
620   }
621 no_channels:
622   {
623     GST_ERROR_OBJECT (lame, "input caps have no channels field");
624     return FALSE;
625   }
626 zero_output_rate:
627   {
628     GST_ELEMENT_ERROR (lame, LIBRARY, SETTINGS, (NULL),
629         ("LAME decided on a zero sample rate"));
630     return FALSE;
631   }
632 setup_failed:
633   {
634     GST_ELEMENT_ERROR (lame, LIBRARY, SETTINGS,
635         (_("Failed to configure LAME encoder. Check your encoding parameters.")), (NULL));
636     return FALSE;
637   }
638 }
639
640 static void
641 gst_lame_init (GstLame * lame)
642 {
643   GST_DEBUG_OBJECT (lame, "starting initialization");
644
645   lame->sinkpad =
646       gst_pad_new_from_static_template (&gst_lame_sink_template, "sink");
647   gst_pad_set_event_function (lame->sinkpad,
648       GST_DEBUG_FUNCPTR (gst_lame_sink_event));
649   gst_pad_set_chain_function (lame->sinkpad,
650       GST_DEBUG_FUNCPTR (gst_lame_chain));
651   gst_pad_set_setcaps_function (lame->sinkpad,
652       GST_DEBUG_FUNCPTR (gst_lame_sink_setcaps));
653   gst_element_add_pad (GST_ELEMENT (lame), lame->sinkpad);
654
655   lame->srcpad =
656       gst_pad_new_from_static_template (&gst_lame_src_template, "src");
657   gst_pad_set_setcaps_function (lame->srcpad,
658       GST_DEBUG_FUNCPTR (gst_lame_src_setcaps));
659   gst_element_add_pad (GST_ELEMENT (lame), lame->srcpad);
660
661   lame->samplerate = 44100;
662   lame->num_channels = 2;
663   lame->setup = FALSE;
664
665   /* Set default settings */
666   lame->bitrate = gst_lame_default_settings.bitrate;
667   lame->compression_ratio = gst_lame_default_settings.compression_ratio;
668   lame->quality = gst_lame_default_settings.quality;
669   lame->mode = gst_lame_default_settings.mode;
670   lame->requested_mode = lame->mode;
671   lame->force_ms = gst_lame_default_settings.force_ms;
672   lame->free_format = gst_lame_default_settings.free_format;
673   lame->copyright = gst_lame_default_settings.copyright;
674   lame->original = gst_lame_default_settings.original;
675   lame->error_protection = gst_lame_default_settings.error_protection;
676   lame->extension = gst_lame_default_settings.extension;
677   lame->strict_iso = gst_lame_default_settings.strict_iso;
678   lame->disable_reservoir = gst_lame_default_settings.disable_reservoir;
679   lame->vbr = gst_lame_default_settings.vbr;
680   lame->vbr_quality = gst_lame_default_settings.vbr_quality;
681   lame->vbr_mean_bitrate = gst_lame_default_settings.vbr_mean_bitrate;
682   lame->vbr_min_bitrate = gst_lame_default_settings.vbr_min_bitrate;
683   lame->vbr_max_bitrate = gst_lame_default_settings.vbr_max_bitrate;
684   lame->vbr_hard_min = gst_lame_default_settings.vbr_hard_min;
685   lame->lowpass_freq = gst_lame_default_settings.lowpass_freq;
686   lame->lowpass_width = gst_lame_default_settings.lowpass_width;
687   lame->highpass_freq = gst_lame_default_settings.highpass_freq;
688   lame->highpass_width = gst_lame_default_settings.highpass_width;
689   lame->ath_only = gst_lame_default_settings.ath_only;
690   lame->ath_short = gst_lame_default_settings.ath_short;
691   lame->no_ath = gst_lame_default_settings.no_ath;
692   lame->ath_lower = gst_lame_default_settings.ath_lower;
693   lame->allow_diff_short = gst_lame_default_settings.allow_diff_short;
694   lame->no_short_blocks = gst_lame_default_settings.no_short_blocks;
695   lame->emphasis = gst_lame_default_settings.emphasis;
696   lame->preset = gst_lame_default_settings.preset;
697
698   GST_DEBUG_OBJECT (lame, "done initializing");
699 }
700
701 /* <php-emulation-mode>three underscores for ___rate is really really really
702  * private as opposed to one underscore<php-emulation-mode> */
703 /* call this MACRO outside of the NULL state so that we have a higher chance
704  * of actually having a pipeline and bus to get the message through */
705
706 #define CHECK_AND_FIXUP_BITRATE(obj,param,rate,free_format)               \
707 G_STMT_START {                                                            \
708   gint ___rate = rate;                                                    \
709   gint maxrate = 320;                                                     \
710   gint multiplier = 64;                                                   \
711   if (!free_format) {                                                     \
712     if (rate == 0) {                                                      \
713       ___rate = rate;                                                     \
714     } else if (rate <= 64) {                                              \
715       maxrate = 64; multiplier = 8;                                       \
716       if ((rate % 8) != 0) ___rate = GST_ROUND_UP_8 (rate);               \
717     } else if (rate <= 128) {                                             \
718       maxrate = 128; multiplier = 16;                                     \
719       if ((rate % 16) != 0) ___rate = GST_ROUND_UP_16 (rate);             \
720     } else if (rate <= 256) {                                             \
721       maxrate = 256; multiplier = 32;                                     \
722       if ((rate % 32) != 0) ___rate = GST_ROUND_UP_32 (rate);             \
723     } else if (rate <= 320) {                                             \
724       maxrate = 320; multiplier = 64;                                     \
725       if ((rate % 64) != 0) ___rate = GST_ROUND_UP_64 (rate);             \
726     }                                                                     \
727     if (___rate != rate) {                                                \
728       GST_ELEMENT_WARNING (obj, LIBRARY, SETTINGS,                        \
729           (_("The requested bitrate %d kbit/s for property '%s' "         \
730              "is not allowed. "                                           \
731             "The bitrate was changed to %d kbit/s."), rate,               \
732             param,  ___rate),                                             \
733           ("A bitrate below %d should be a multiple of %d.",              \
734               maxrate, multiplier));                                      \
735       rate = ___rate;                                                     \
736     }                                                                     \
737   }                                                                       \
738 } G_STMT_END
739
740 static void
741 gst_lame_set_property (GObject * object, guint prop_id, const GValue * value,
742     GParamSpec * pspec)
743 {
744   GstLame *lame;
745
746   lame = GST_LAME (object);
747
748   switch (prop_id) {
749     case ARG_BITRATE:
750       lame->bitrate = g_value_get_int (value);
751       break;
752     case ARG_COMPRESSION_RATIO:
753       lame->compression_ratio = g_value_get_float (value);
754       break;
755     case ARG_QUALITY:
756       lame->quality = g_value_get_enum (value);
757       break;
758     case ARG_MODE:
759       lame->requested_mode = g_value_get_enum (value);
760       break;
761     case ARG_FORCE_MS:
762       lame->force_ms = g_value_get_boolean (value);
763       break;
764     case ARG_FREE_FORMAT:
765       lame->free_format = g_value_get_boolean (value);
766       break;
767     case ARG_COPYRIGHT:
768       lame->copyright = g_value_get_boolean (value);
769       break;
770     case ARG_ORIGINAL:
771       lame->original = g_value_get_boolean (value);
772       break;
773     case ARG_ERROR_PROTECTION:
774       lame->error_protection = g_value_get_boolean (value);
775       break;
776     case ARG_PADDING_TYPE:
777       break;
778     case ARG_EXTENSION:
779       lame->extension = g_value_get_boolean (value);
780       break;
781     case ARG_STRICT_ISO:
782       lame->strict_iso = g_value_get_boolean (value);
783       break;
784     case ARG_DISABLE_RESERVOIR:
785       lame->disable_reservoir = g_value_get_boolean (value);
786       break;
787     case ARG_VBR:
788       lame->vbr = g_value_get_enum (value);
789       break;
790     case ARG_VBR_QUALITY:
791       lame->vbr_quality = g_value_get_enum (value);
792       break;
793     case ARG_VBR_MEAN_BITRATE:
794       lame->vbr_mean_bitrate = g_value_get_int (value);
795       break;
796     case ARG_VBR_MIN_BITRATE:
797       lame->vbr_min_bitrate = g_value_get_int (value);
798       break;
799     case ARG_VBR_MAX_BITRATE:
800       lame->vbr_max_bitrate = g_value_get_int (value);
801       break;
802     case ARG_VBR_HARD_MIN:
803       lame->vbr_hard_min = g_value_get_int (value);
804       break;
805     case ARG_LOWPASS_FREQ:
806       lame->lowpass_freq = g_value_get_int (value);
807       break;
808     case ARG_LOWPASS_WIDTH:
809       lame->lowpass_width = g_value_get_int (value);
810       break;
811     case ARG_HIGHPASS_FREQ:
812       lame->highpass_freq = g_value_get_int (value);
813       break;
814     case ARG_HIGHPASS_WIDTH:
815       lame->highpass_width = g_value_get_int (value);
816       break;
817     case ARG_ATH_ONLY:
818       lame->ath_only = g_value_get_boolean (value);
819       break;
820     case ARG_ATH_SHORT:
821       lame->ath_short = g_value_get_boolean (value);
822       break;
823     case ARG_NO_ATH:
824       lame->no_ath = g_value_get_boolean (value);
825       break;
826     case ARG_ATH_LOWER:
827       lame->ath_lower = g_value_get_int (value);
828       break;
829     case ARG_CWLIMIT:
830       break;
831     case ARG_ALLOW_DIFF_SHORT:
832       lame->allow_diff_short = g_value_get_boolean (value);
833       break;
834     case ARG_NO_SHORT_BLOCKS:
835       lame->no_short_blocks = g_value_get_boolean (value);
836       break;
837     case ARG_EMPHASIS:
838       lame->emphasis = g_value_get_boolean (value);
839       break;
840     case ARG_XINGHEADER:
841       break;
842 #ifdef GSTLAME_PRESET
843     case ARG_PRESET:
844       lame->preset = g_value_get_enum (value);
845       break;
846 #endif
847     default:
848       break;
849   }
850
851 }
852
853 static void
854 gst_lame_get_property (GObject * object, guint prop_id, GValue * value,
855     GParamSpec * pspec)
856 {
857   GstLame *lame;
858
859   lame = GST_LAME (object);
860
861   switch (prop_id) {
862     case ARG_BITRATE:
863       g_value_set_int (value, lame->bitrate);
864       break;
865     case ARG_COMPRESSION_RATIO:
866       g_value_set_float (value, lame->compression_ratio);
867       break;
868     case ARG_QUALITY:
869       g_value_set_enum (value, lame->quality);
870       break;
871     case ARG_MODE:
872       g_value_set_enum (value, lame->requested_mode);
873       break;
874     case ARG_FORCE_MS:
875       g_value_set_boolean (value, lame->force_ms);
876       break;
877     case ARG_FREE_FORMAT:
878       g_value_set_boolean (value, lame->free_format);
879       break;
880     case ARG_COPYRIGHT:
881       g_value_set_boolean (value, lame->copyright);
882       break;
883     case ARG_ORIGINAL:
884       g_value_set_boolean (value, lame->original);
885       break;
886     case ARG_ERROR_PROTECTION:
887       g_value_set_boolean (value, lame->error_protection);
888       break;
889     case ARG_PADDING_TYPE:
890       break;
891     case ARG_EXTENSION:
892       g_value_set_boolean (value, lame->extension);
893       break;
894     case ARG_STRICT_ISO:
895       g_value_set_boolean (value, lame->strict_iso);
896       break;
897     case ARG_DISABLE_RESERVOIR:
898       g_value_set_boolean (value, lame->disable_reservoir);
899       break;
900     case ARG_VBR:
901       g_value_set_enum (value, lame->vbr);
902       break;
903     case ARG_VBR_QUALITY:
904       g_value_set_enum (value, lame->vbr_quality);
905       break;
906     case ARG_VBR_MEAN_BITRATE:
907       g_value_set_int (value, lame->vbr_mean_bitrate);
908       break;
909     case ARG_VBR_MIN_BITRATE:
910       g_value_set_int (value, lame->vbr_min_bitrate);
911       break;
912     case ARG_VBR_MAX_BITRATE:
913       g_value_set_int (value, lame->vbr_max_bitrate);
914       break;
915     case ARG_VBR_HARD_MIN:
916       g_value_set_int (value, lame->vbr_hard_min);
917       break;
918     case ARG_LOWPASS_FREQ:
919       g_value_set_int (value, lame->lowpass_freq);
920       break;
921     case ARG_LOWPASS_WIDTH:
922       g_value_set_int (value, lame->lowpass_width);
923       break;
924     case ARG_HIGHPASS_FREQ:
925       g_value_set_int (value, lame->highpass_freq);
926       break;
927     case ARG_HIGHPASS_WIDTH:
928       g_value_set_int (value, lame->highpass_width);
929       break;
930     case ARG_ATH_ONLY:
931       g_value_set_boolean (value, lame->ath_only);
932       break;
933     case ARG_ATH_SHORT:
934       g_value_set_boolean (value, lame->ath_short);
935       break;
936     case ARG_NO_ATH:
937       g_value_set_boolean (value, lame->no_ath);
938       break;
939     case ARG_ATH_LOWER:
940       g_value_set_int (value, lame->ath_lower);
941       break;
942     case ARG_CWLIMIT:
943       break;
944     case ARG_ALLOW_DIFF_SHORT:
945       g_value_set_boolean (value, lame->allow_diff_short);
946       break;
947     case ARG_NO_SHORT_BLOCKS:
948       g_value_set_boolean (value, lame->no_short_blocks);
949       break;
950     case ARG_EMPHASIS:
951       g_value_set_boolean (value, lame->emphasis);
952       break;
953     case ARG_XINGHEADER:
954       break;
955 #ifdef GSTLAME_PRESET
956     case ARG_PRESET:
957       g_value_set_enum (value, lame->preset);
958       break;
959 #endif
960     default:
961       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
962       break;
963   }
964 }
965
966 static gboolean
967 gst_lame_sink_event (GstPad * pad, GstEvent * event)
968 {
969   gboolean ret;
970   GstLame *lame;
971
972   lame = GST_LAME (gst_pad_get_parent (pad));
973
974   switch (GST_EVENT_TYPE (event)) {
975     case GST_EVENT_EOS:{
976       GST_DEBUG_OBJECT (lame, "handling EOS event");
977
978       if (lame->lgf != NULL) {
979         GstBuffer *buf;
980         gint size;
981
982         buf = gst_buffer_new_and_alloc (7200);
983         size = lame_encode_flush (lame->lgf, GST_BUFFER_DATA (buf), 7200);
984
985         if (size > 0 && lame->last_flow == GST_FLOW_OK) {
986           gint64 duration;
987
988           duration = gst_util_uint64_scale (size, 8 * GST_SECOND,
989               1000 * lame->bitrate);
990
991           if (lame->last_ts == GST_CLOCK_TIME_NONE) {
992             lame->last_ts = lame->eos_ts;
993             lame->last_duration = duration;
994           } else {
995             lame->last_duration += duration;
996           }
997
998           GST_BUFFER_TIMESTAMP (buf) = lame->last_ts;
999           GST_BUFFER_DURATION (buf) = lame->last_duration;
1000           lame->last_ts = GST_CLOCK_TIME_NONE;
1001           GST_BUFFER_SIZE (buf) = size;
1002           GST_DEBUG_OBJECT (lame, "pushing final packet of %u bytes", size);
1003           gst_buffer_set_caps (buf, GST_PAD_CAPS (lame->srcpad));
1004           gst_pad_push (lame->srcpad, buf);
1005         } else {
1006           GST_DEBUG_OBJECT (lame, "no final packet (size=%d, last_flow=%s)",
1007               size, gst_flow_get_name (lame->last_flow));
1008           gst_buffer_unref (buf);
1009         }
1010       }
1011
1012       ret = gst_pad_event_default (pad, event);
1013       break;
1014     }
1015     case GST_EVENT_FLUSH_START:
1016       GST_DEBUG_OBJECT (lame, "handling FLUSH start event");
1017       /* forward event */
1018       ret = gst_pad_push_event (lame->srcpad, event);
1019       break;
1020     case GST_EVENT_FLUSH_STOP:
1021     {
1022       guchar *mp3_data = NULL;
1023       gint mp3_buffer_size;
1024
1025       GST_DEBUG_OBJECT (lame, "handling FLUSH stop event");
1026
1027       if (lame->lgf) {
1028         /* clear buffers if we already have lame set up */
1029         mp3_buffer_size = 7200;
1030         mp3_data = g_malloc (mp3_buffer_size);
1031         lame_encode_flush (lame->lgf, mp3_data, mp3_buffer_size);
1032         g_free (mp3_data);
1033       }
1034
1035       ret = gst_pad_push_event (lame->srcpad, event);
1036       break;
1037     }
1038     case GST_EVENT_TAG:
1039       GST_DEBUG_OBJECT (lame, "ignoring TAG event, passing it on");
1040       ret = gst_pad_push_event (lame->srcpad, event);
1041       break;
1042     default:
1043       ret = gst_pad_event_default (pad, event);
1044       break;
1045   }
1046   gst_object_unref (lame);
1047   return ret;
1048 }
1049
1050 static GstFlowReturn
1051 gst_lame_chain (GstPad * pad, GstBuffer * buf)
1052 {
1053   GstLame *lame;
1054   guchar *mp3_data;
1055   gint mp3_buffer_size, mp3_size;
1056   gint64 duration;
1057   GstFlowReturn result;
1058   gint num_samples;
1059   guint8 *data;
1060   guint size;
1061
1062   lame = GST_LAME (GST_PAD_PARENT (pad));
1063
1064   GST_LOG_OBJECT (lame, "entered chain");
1065
1066   if (!lame->setup)
1067     goto not_setup;
1068
1069   data = GST_BUFFER_DATA (buf);
1070   size = GST_BUFFER_SIZE (buf);
1071
1072   num_samples = size / 2;
1073
1074   /* allocate space for output */
1075   mp3_buffer_size = 1.25 * num_samples + 7200;
1076   mp3_data = g_malloc (mp3_buffer_size);
1077
1078   /* lame seems to be too stupid to get mono interleaved going */
1079   if (lame->num_channels == 1) {
1080     mp3_size = lame_encode_buffer (lame->lgf,
1081         (short int *) data,
1082         (short int *) data, num_samples, mp3_data, mp3_buffer_size);
1083   } else {
1084     mp3_size = lame_encode_buffer_interleaved (lame->lgf,
1085         (short int *) data,
1086         num_samples / lame->num_channels, mp3_data, mp3_buffer_size);
1087   }
1088
1089   GST_LOG_OBJECT (lame, "encoded %d bytes of audio to %d bytes of mp3",
1090       size, mp3_size);
1091
1092   duration = gst_util_uint64_scale_int (size, GST_SECOND,
1093       2 * lame->samplerate * lame->num_channels);
1094
1095   if (GST_BUFFER_DURATION (buf) != GST_CLOCK_TIME_NONE &&
1096       GST_BUFFER_DURATION (buf) != duration) {
1097     GST_DEBUG_OBJECT (lame, "incoming buffer had incorrect duration %"
1098         GST_TIME_FORMAT ", outgoing buffer will have correct duration %"
1099         GST_TIME_FORMAT,
1100         GST_TIME_ARGS (GST_BUFFER_DURATION (buf)), GST_TIME_ARGS (duration));
1101   }
1102
1103   if (lame->last_ts == GST_CLOCK_TIME_NONE) {
1104     lame->last_ts = GST_BUFFER_TIMESTAMP (buf);
1105     lame->last_offs = GST_BUFFER_OFFSET (buf);
1106     lame->last_duration = duration;
1107   } else {
1108     lame->last_duration += duration;
1109   }
1110
1111   gst_buffer_unref (buf);
1112
1113   if (mp3_size < 0) {
1114     g_warning ("error %d", mp3_size);
1115   }
1116
1117   if (mp3_size > 0) {
1118     GstBuffer *outbuf;
1119
1120     outbuf = gst_buffer_new ();
1121     GST_BUFFER_DATA (outbuf) = mp3_data;
1122     GST_BUFFER_MALLOCDATA (outbuf) = mp3_data;
1123     GST_BUFFER_SIZE (outbuf) = mp3_size;
1124     GST_BUFFER_TIMESTAMP (outbuf) = lame->last_ts;
1125     GST_BUFFER_OFFSET (outbuf) = lame->last_offs;
1126     GST_BUFFER_DURATION (outbuf) = lame->last_duration;
1127     gst_buffer_set_caps (outbuf, GST_PAD_CAPS (lame->srcpad));
1128
1129     result = gst_pad_push (lame->srcpad, outbuf);
1130     lame->last_flow = result;
1131     if (result != GST_FLOW_OK) {
1132       GST_DEBUG_OBJECT (lame, "flow return: %s", gst_flow_get_name (result));
1133     }
1134
1135     if (GST_CLOCK_TIME_IS_VALID (lame->last_ts))
1136       lame->eos_ts = lame->last_ts + lame->last_duration;
1137     else
1138       lame->eos_ts = GST_CLOCK_TIME_NONE;
1139     lame->last_ts = GST_CLOCK_TIME_NONE;
1140   } else {
1141     g_free (mp3_data);
1142     result = GST_FLOW_OK;
1143   }
1144
1145   return result;
1146
1147   /* ERRORS */
1148 not_setup:
1149   {
1150     gst_buffer_unref (buf);
1151     GST_ELEMENT_ERROR (lame, CORE, NEGOTIATION, (NULL),
1152         ("encoder not initialized (input is not audio?)"));
1153     return GST_FLOW_ERROR;
1154   }
1155 }
1156
1157 /* set up the encoder state */
1158 static gboolean
1159 gst_lame_setup (GstLame * lame)
1160 {
1161
1162 #define CHECK_ERROR(command) G_STMT_START {\
1163   if ((command) < 0) { \
1164     GST_ERROR_OBJECT (lame, "setup failed: " G_STRINGIFY (command)); \
1165     return FALSE; \
1166   } \
1167 }G_STMT_END
1168
1169   int retval;
1170   GstCaps *allowed_caps;
1171
1172   GST_DEBUG_OBJECT (lame, "starting setup");
1173
1174   /* check if we're already setup; if we are, we might want to check
1175    * if this initialization is compatible with the previous one */
1176   /* FIXME: do this */
1177   if (lame->setup) {
1178     GST_WARNING_OBJECT (lame, "already setup");
1179     lame->setup = FALSE;
1180   }
1181
1182   lame->lgf = lame_init ();
1183
1184   if (lame->lgf == NULL)
1185     return FALSE;
1186
1187   /* copy the parameters over */
1188   lame_set_in_samplerate (lame->lgf, lame->samplerate);
1189
1190   /* let lame choose default samplerate unless outgoing sample rate is fixed */
1191   allowed_caps = gst_pad_get_allowed_caps (lame->srcpad);
1192
1193   if (allowed_caps != NULL) {
1194     GstStructure *structure;
1195     gint samplerate;
1196
1197     structure = gst_caps_get_structure (allowed_caps, 0);
1198
1199     if (gst_structure_get_int (structure, "rate", &samplerate)) {
1200       GST_DEBUG_OBJECT (lame, "Setting sample rate to %d as fixed in src caps",
1201           samplerate);
1202       lame_set_out_samplerate (lame->lgf, samplerate);
1203     } else {
1204       GST_DEBUG_OBJECT (lame, "Letting lame choose sample rate");
1205       lame_set_out_samplerate (lame->lgf, 0);
1206     }
1207     gst_caps_unref (allowed_caps);
1208     allowed_caps = NULL;
1209   } else {
1210     GST_DEBUG_OBJECT (lame, "No peer yet, letting lame choose sample rate");
1211     lame_set_out_samplerate (lame->lgf, 0);
1212   }
1213
1214   /* force mono encoding if we only have one channel */
1215   if (lame->num_channels == 1)
1216     lame->mode = 3;
1217   else
1218     lame->mode = lame->requested_mode;
1219
1220   CHECK_ERROR (lame_set_num_channels (lame->lgf, lame->num_channels));
1221   CHECK_AND_FIXUP_BITRATE (lame, "bitrate", lame->bitrate, lame->free_format);
1222   CHECK_ERROR (lame_set_brate (lame->lgf, lame->bitrate));
1223   CHECK_ERROR (lame_set_compression_ratio (lame->lgf, lame->compression_ratio));
1224   CHECK_ERROR (lame_set_quality (lame->lgf, lame->quality));
1225   CHECK_ERROR (lame_set_mode (lame->lgf, lame->mode));
1226   CHECK_ERROR (lame_set_force_ms (lame->lgf, lame->force_ms));
1227   CHECK_ERROR (lame_set_free_format (lame->lgf, lame->free_format));
1228   CHECK_ERROR (lame_set_copyright (lame->lgf, lame->copyright));
1229   CHECK_ERROR (lame_set_original (lame->lgf, lame->original));
1230   CHECK_ERROR (lame_set_error_protection (lame->lgf, lame->error_protection));
1231   CHECK_ERROR (lame_set_extension (lame->lgf, lame->extension));
1232   CHECK_ERROR (lame_set_strict_ISO (lame->lgf, lame->strict_iso));
1233   CHECK_ERROR (lame_set_disable_reservoir (lame->lgf, lame->disable_reservoir));
1234   CHECK_ERROR (lame_set_VBR (lame->lgf, lame->vbr));
1235   CHECK_ERROR (lame_set_VBR_q (lame->lgf, lame->vbr_quality));
1236   CHECK_ERROR (lame_set_VBR_mean_bitrate_kbps (lame->lgf,
1237           lame->vbr_mean_bitrate));
1238   CHECK_AND_FIXUP_BITRATE (lame, "vbr-min-bitrate", lame->vbr_min_bitrate,
1239       lame->free_format);
1240   CHECK_ERROR (lame_set_VBR_min_bitrate_kbps (lame->lgf,
1241           lame->vbr_min_bitrate));
1242   CHECK_AND_FIXUP_BITRATE (lame, "vbr-max-bitrate", lame->vbr_max_bitrate,
1243       lame->free_format);
1244   CHECK_ERROR (lame_set_VBR_max_bitrate_kbps (lame->lgf,
1245           lame->vbr_max_bitrate));
1246   CHECK_ERROR (lame_set_VBR_hard_min (lame->lgf, lame->vbr_hard_min));
1247   CHECK_ERROR (lame_set_lowpassfreq (lame->lgf, lame->lowpass_freq));
1248   CHECK_ERROR (lame_set_lowpasswidth (lame->lgf, lame->lowpass_width));
1249   CHECK_ERROR (lame_set_highpassfreq (lame->lgf, lame->highpass_freq));
1250   CHECK_ERROR (lame_set_highpasswidth (lame->lgf, lame->highpass_width));
1251   CHECK_ERROR (lame_set_ATHonly (lame->lgf, lame->ath_only));
1252   CHECK_ERROR (lame_set_ATHshort (lame->lgf, lame->ath_short));
1253   CHECK_ERROR (lame_set_noATH (lame->lgf, lame->no_ath));
1254   CHECK_ERROR (lame_set_ATHlower (lame->lgf, lame->ath_lower));
1255   CHECK_ERROR (lame_set_allow_diff_short (lame->lgf, lame->allow_diff_short));
1256   CHECK_ERROR (lame_set_no_short_blocks (lame->lgf, lame->no_short_blocks));
1257   CHECK_ERROR (lame_set_emphasis (lame->lgf, lame->emphasis));
1258   CHECK_ERROR (lame_set_bWriteVbrTag (lame->lgf, 0));
1259 #ifdef GSTLAME_PRESET
1260   if (lame->preset > 0) {
1261     CHECK_ERROR (lame_set_preset (lame->lgf, lame->preset));
1262   }
1263 #endif
1264
1265   /* initialize the lame encoder */
1266   if ((retval = lame_init_params (lame->lgf)) >= 0) {
1267     lame->setup = TRUE;
1268     /* FIXME: it would be nice to print out the mode here */
1269     GST_INFO ("lame encoder setup (%d kbit/s, %d Hz, %d channels)",
1270         lame->bitrate, lame->samplerate, lame->num_channels);
1271   } else {
1272     GST_ERROR_OBJECT (lame, "lame_init_params returned %d", retval);
1273   }
1274
1275   GST_DEBUG_OBJECT (lame, "done with setup");
1276
1277   return lame->setup;
1278 #undef CHECK_ERROR
1279 }
1280
1281 static GstStateChangeReturn
1282 gst_lame_change_state (GstElement * element, GstStateChange transition)
1283 {
1284   GstLame *lame;
1285   GstStateChangeReturn result;
1286
1287   lame = GST_LAME (element);
1288
1289   switch (transition) {
1290     case GST_STATE_CHANGE_READY_TO_PAUSED:
1291       lame->last_flow = GST_FLOW_OK;
1292       lame->last_ts = GST_CLOCK_TIME_NONE;
1293       lame->eos_ts = GST_CLOCK_TIME_NONE;
1294       break;
1295     default:
1296       break;
1297   }
1298
1299   result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1300
1301   switch (transition) {
1302     case GST_STATE_CHANGE_READY_TO_NULL:
1303       gst_lame_release_memory (lame);
1304       break;
1305     default:
1306       break;
1307   }
1308
1309   return result;
1310 }
1311
1312 static gboolean
1313 gst_lame_get_default_settings (void)
1314 {
1315   lame_global_flags *lgf = NULL;
1316
1317   lgf = lame_init ();
1318   if (lgf == NULL) {
1319     GST_ERROR ("Error initializing LAME");
1320     return FALSE;
1321   }
1322
1323   if (lame_init_params (lgf) < 0) {
1324     GST_ERROR ("Error getting default settings");
1325     return FALSE;
1326   }
1327
1328   gst_lame_default_settings.bitrate = lame_get_brate (lgf);
1329   gst_lame_default_settings.compression_ratio = 0.0;    /* lame_get_compression_ratio (lgf); */
1330   gst_lame_default_settings.quality = lame_get_quality (lgf);
1331   gst_lame_default_settings.mode = lame_get_mode (lgf);
1332   gst_lame_default_settings.force_ms = lame_get_force_ms (lgf);
1333   gst_lame_default_settings.free_format = lame_get_free_format (lgf);
1334   gst_lame_default_settings.copyright = lame_get_copyright (lgf);
1335   gst_lame_default_settings.original = lame_get_original (lgf);
1336   gst_lame_default_settings.error_protection = lame_get_error_protection (lgf);
1337   gst_lame_default_settings.extension = lame_get_extension (lgf);
1338   gst_lame_default_settings.strict_iso = lame_get_strict_ISO (lgf);
1339   gst_lame_default_settings.disable_reservoir =
1340       lame_get_disable_reservoir (lgf);
1341   gst_lame_default_settings.vbr = lame_get_VBR (lgf);
1342   gst_lame_default_settings.vbr_quality = lame_get_VBR_q (lgf);
1343   gst_lame_default_settings.vbr_mean_bitrate =
1344       lame_get_VBR_mean_bitrate_kbps (lgf);
1345   gst_lame_default_settings.vbr_min_bitrate =
1346       lame_get_VBR_min_bitrate_kbps (lgf);
1347   gst_lame_default_settings.vbr_max_bitrate =
1348       lame_get_VBR_max_bitrate_kbps (lgf);
1349   gst_lame_default_settings.vbr_hard_min = lame_get_VBR_hard_min (lgf);
1350   gst_lame_default_settings.lowpass_freq = lame_get_lowpassfreq (lgf);
1351   gst_lame_default_settings.lowpass_width = lame_get_lowpasswidth (lgf);
1352   gst_lame_default_settings.highpass_freq = lame_get_highpassfreq (lgf);
1353   gst_lame_default_settings.highpass_width = lame_get_highpasswidth (lgf);
1354   gst_lame_default_settings.ath_only = lame_get_ATHonly (lgf);
1355   gst_lame_default_settings.ath_short = lame_get_ATHshort (lgf);
1356   gst_lame_default_settings.no_ath = lame_get_noATH (lgf);
1357   gst_lame_default_settings.ath_type = lame_get_ATHtype (lgf);
1358   gst_lame_default_settings.ath_lower = lame_get_ATHlower (lgf);
1359   gst_lame_default_settings.allow_diff_short = lame_get_allow_diff_short (lgf);
1360   gst_lame_default_settings.no_short_blocks = lame_get_no_short_blocks (lgf);
1361   gst_lame_default_settings.emphasis = lame_get_emphasis (lgf);
1362   gst_lame_default_settings.preset = 0;
1363
1364   lame_close (lgf);
1365
1366   return TRUE;
1367 }
1368
1369 gboolean
1370 gst_lame_register (GstPlugin * plugin)
1371 {
1372   GST_DEBUG_CATEGORY_INIT (debug, "lame", 0, "lame mp3 encoder");
1373
1374   if (!gst_lame_get_default_settings ())
1375     return FALSE;
1376
1377   if (!gst_element_register (plugin, "lame", GST_RANK_MARGINAL, GST_TYPE_LAME))
1378     return FALSE;
1379
1380   return TRUE;
1381 }