2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
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.
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.
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.
24 #include <gstflacenc.h>
26 extern GstPadTemplate *enc_src_template, *enc_sink_template;
28 /* elementfactory information */
29 GstElementDetails flacenc_details = {
31 "Filter/Audio/Encoder",
32 "Encodes audio with the FLAC lossless audio encoder",
34 "Wim Taymans <wim.taymans@chello.be>",
38 /* FlacEnc signals and args */
48 static void gst_flacenc_init (FlacEnc *flacenc);
49 static void gst_flacenc_class_init (FlacEncClass *klass);
51 static void gst_flacenc_chain (GstPad *pad, GstBuffer *buf);
53 static void gst_flacenc_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
54 static void gst_flacenc_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
56 static FLAC__StreamEncoderWriteStatus
57 gst_flacenc_write_callback (const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes,
58 unsigned samples, unsigned current_frame, void *client_data);
59 static void gst_flacenc_metadata_callback (const FLAC__StreamEncoder *encoder, const FLAC__StreamMetaData *metadata,
62 static GstElementClass *parent_class = NULL;
63 /*static guint gst_flacenc_signals[LAST_SIGNAL] = { 0 }; */
66 flacenc_get_type (void)
68 static GType flacenc_type = 0;
71 static const GTypeInfo flacenc_info = {
75 (GClassInitFunc)gst_flacenc_class_init,
80 (GInstanceInitFunc)gst_flacenc_init,
82 flacenc_type = g_type_register_static (GST_TYPE_ELEMENT, "FlacEnc", &flacenc_info, 0);
88 gst_flacenc_class_init (FlacEncClass *klass)
90 GObjectClass *gobject_class;
91 GstElementClass *gstelement_class;
93 gobject_class = (GObjectClass*)klass;
94 gstelement_class = (GstElementClass*)klass;
96 parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
98 /* we have no properties atm so this is a bit silly */
99 gobject_class->set_property = gst_flacenc_set_property;
100 gobject_class->get_property = gst_flacenc_get_property;
103 static GstPadConnectReturn
104 gst_flacenc_sinkconnect (GstPad *pad, GstCaps *caps)
108 flacenc = GST_FLACENC (gst_pad_get_parent (pad));
110 if (!GST_CAPS_IS_FIXED (caps))
111 return GST_PAD_CONNECT_DELAYED;
113 gst_caps_get_int (caps, "channels", &flacenc->channels);
114 gst_caps_get_int (caps, "depth", &flacenc->depth);
115 gst_caps_get_int (caps, "rate", &flacenc->sample_rate);
117 FLAC__stream_encoder_set_bits_per_sample (flacenc->encoder, flacenc->depth);
118 FLAC__stream_encoder_set_sample_rate (flacenc->encoder, flacenc->sample_rate);
119 FLAC__stream_encoder_set_channels (flacenc->encoder, flacenc->channels);
121 return GST_PAD_CONNECT_OK;
125 gst_flacenc_init (FlacEnc *flacenc)
127 flacenc->sinkpad = gst_pad_new_from_template (enc_sink_template, "sink");
128 gst_element_add_pad(GST_ELEMENT(flacenc),flacenc->sinkpad);
129 gst_pad_set_chain_function(flacenc->sinkpad,gst_flacenc_chain);
130 gst_pad_set_connect_function (flacenc->sinkpad, gst_flacenc_sinkconnect);
132 flacenc->srcpad = gst_pad_new_from_template (enc_src_template, "src");
133 gst_element_add_pad(GST_ELEMENT(flacenc),flacenc->srcpad);
135 flacenc->encoder = FLAC__stream_encoder_new();
136 FLAC__stream_encoder_set_write_callback (flacenc->encoder, gst_flacenc_write_callback);
137 FLAC__stream_encoder_set_metadata_callback (flacenc->encoder, gst_flacenc_metadata_callback);
138 FLAC__stream_encoder_set_client_data (flacenc->encoder, flacenc);
141 static FLAC__StreamEncoderWriteStatus
142 gst_flacenc_write_callback (const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes,
143 unsigned samples, unsigned current_frame, void *client_data)
148 flacenc = GST_FLACENC (client_data);
150 outbuf = gst_buffer_new ();
151 GST_BUFFER_SIZE (outbuf) = bytes;
152 GST_BUFFER_DATA (outbuf) = g_malloc (bytes);
154 memcpy (GST_BUFFER_DATA (outbuf), buffer, bytes);
156 gst_pad_push (flacenc->srcpad, outbuf);
158 return FLAC__STREAM_ENCODER_WRITE_OK;
162 gst_flacenc_metadata_callback (const FLAC__StreamEncoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data)
167 gst_flacenc_chain (GstPad *pad,GstBuffer *buf)
170 gint32 *data[FLAC__MAX_CHANNELS];
172 gint samples, channels, depth;
176 g_return_if_fail(pad != NULL);
177 g_return_if_fail(GST_IS_PAD(pad));
178 g_return_if_fail(buf != NULL);
180 flacenc = GST_FLACENC (gst_pad_get_parent (pad));
182 channels = flacenc->channels;
183 depth = flacenc->depth;
185 insize = GST_BUFFER_SIZE (buf);
186 samples = insize / channels / ((depth+7)>>3);
188 if (FLAC__stream_encoder_get_state (flacenc->encoder) == FLAC__STREAM_ENCODER_UNINITIALIZED) {
189 FLAC__StreamEncoderState state;
191 state = FLAC__stream_encoder_init (flacenc->encoder);
193 g_assert (state == FLAC__STREAM_ENCODER_OK);
196 for (i=0; i<channels; i++) {
197 data[i] = g_malloc (samples * sizeof (gint32));
201 gint8 *indata = (gint8 *) GST_BUFFER_DATA (buf);
202 for (j=0; j<samples; j++) {
203 for (i=0; i<channels; i++) {
204 data[i][j] = (gint32) *indata++;
208 else if (depth == 16) {
209 gint16 *indata = (gint16 *) GST_BUFFER_DATA (buf);
210 for (j=0; j<samples; j++) {
211 for (i=0; i<channels; i++) {
212 data[i][j] = (gint32) *indata++;
217 res = FLAC__stream_encoder_process (flacenc->encoder, (const FLAC__int32 **)data, samples);
219 for (i=0; i<channels; i++) {
223 gst_buffer_unref(buf);
227 gst_flacenc_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
231 this = (FlacEnc *)object;
234 GST_DEBUG(0, "Unknown arg %d", prop_id);
240 gst_flacenc_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
244 this = (FlacEnc *)object;
248 GST_DEBUG(0, "Unknown arg %d", prop_id);