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 "Codec/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->first = TRUE;
136 flacenc->first_buf = NULL;
137 flacenc->encoder = FLAC__stream_encoder_new();
139 FLAC__stream_encoder_set_write_callback (flacenc->encoder, gst_flacenc_write_callback);
140 FLAC__stream_encoder_set_metadata_callback (flacenc->encoder, gst_flacenc_metadata_callback);
141 FLAC__stream_encoder_set_client_data (flacenc->encoder, flacenc);
143 GST_FLAG_SET (flacenc, GST_ELEMENT_EVENT_AWARE);
146 static FLAC__StreamEncoderWriteStatus
147 gst_flacenc_write_callback (const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes,
148 unsigned samples, unsigned current_frame, void *client_data)
153 flacenc = GST_FLACENC (client_data);
155 outbuf = gst_buffer_new ();
156 GST_BUFFER_SIZE (outbuf) = bytes;
157 GST_BUFFER_DATA (outbuf) = g_malloc (bytes);
159 memcpy (GST_BUFFER_DATA (outbuf), buffer, bytes);
161 if (flacenc->first) {
162 flacenc->first_buf = outbuf;
163 gst_buffer_ref (outbuf);
164 flacenc->first = FALSE;
167 gst_pad_push (flacenc->srcpad, outbuf);
169 return FLAC__STREAM_ENCODER_WRITE_OK;
173 gst_flacenc_metadata_callback (const FLAC__StreamEncoder *encoder, const FLAC__StreamMetadata *metadata, void *client_data)
178 flacenc = GST_FLACENC (client_data);
180 event = gst_event_new_discontinuous (FALSE, GST_FORMAT_BYTES, 0, NULL);
181 gst_pad_push (flacenc->srcpad, GST_BUFFER (event));
183 if (flacenc->first_buf) {
184 const FLAC__uint64 samples = metadata->data.stream_info.total_samples;
185 const unsigned min_framesize = metadata->data.stream_info.min_framesize;
186 const unsigned max_framesize = metadata->data.stream_info.max_framesize;
188 guint8 *data = GST_BUFFER_DATA (flacenc->first_buf);
189 GstBuffer *outbuf = flacenc->first_buf;
191 /* this looks evil but is actually how one is supposed to write
192 * the stream stats according to the FLAC examples */
194 memcpy (&data[26], metadata->data.stream_info.md5sum, 16);
196 data[21] = (data[21] & 0xf0) |
197 (FLAC__byte)((samples >> 32) & 0x0f);
198 data[22] = (FLAC__byte)((samples >> 24) & 0xff);
199 data[23] = (FLAC__byte)((samples >> 16) & 0xff);
200 data[24] = (FLAC__byte)((samples >> 8 ) & 0xff);
201 data[25] = (FLAC__byte)((samples ) & 0xff);
203 data[12] = (FLAC__byte)((min_framesize >> 16) & 0xFF);
204 data[13] = (FLAC__byte)((min_framesize >> 8 ) & 0xFF);
205 data[14] = (FLAC__byte)((min_framesize ) & 0xFF);
207 data[15] = (FLAC__byte)((max_framesize >> 16) & 0xFF);
208 data[16] = (FLAC__byte)((max_framesize >> 8 ) & 0xFF);
209 data[17] = (FLAC__byte)((max_framesize ) & 0xFF);
211 flacenc->first_buf = NULL;
212 gst_pad_push (flacenc->srcpad, outbuf);
217 gst_flacenc_chain (GstPad *pad, GstBuffer *buf)
220 gint32 *data[FLAC__MAX_CHANNELS];
222 gint samples, channels, depth;
226 g_return_if_fail(pad != NULL);
227 g_return_if_fail(GST_IS_PAD(pad));
228 g_return_if_fail(buf != NULL);
230 flacenc = GST_FLACENC (gst_pad_get_parent (pad));
232 if (GST_IS_EVENT (buf)) {
233 GstEvent *event = GST_EVENT (buf);
235 switch (GST_EVENT_TYPE (event)) {
237 FLAC__stream_encoder_finish(flacenc->encoder);
239 gst_pad_event_default (pad, event);
245 channels = flacenc->channels;
246 depth = flacenc->depth;
248 insize = GST_BUFFER_SIZE (buf);
249 samples = insize / channels / ((depth+7)>>3);
251 if (FLAC__stream_encoder_get_state (flacenc->encoder) == FLAC__STREAM_ENCODER_UNINITIALIZED) {
252 FLAC__StreamEncoderState state;
254 state = FLAC__stream_encoder_init (flacenc->encoder);
256 g_assert (state == FLAC__STREAM_ENCODER_OK);
259 for (i=0; i<channels; i++) {
260 data[i] = g_malloc (samples * sizeof (gint32));
264 gint8 *indata = (gint8 *) GST_BUFFER_DATA (buf);
265 for (j=0; j<samples; j++) {
266 for (i=0; i<channels; i++) {
267 data[i][j] = (gint32) *indata++;
271 else if (depth == 16) {
272 gint16 *indata = (gint16 *) GST_BUFFER_DATA (buf);
273 for (j=0; j<samples; j++) {
274 for (i=0; i<channels; i++) {
275 data[i][j] = (gint32) *indata++;
280 res = FLAC__stream_encoder_process (flacenc->encoder, (const FLAC__int32 **)data, samples);
282 for (i=0; i<channels; i++) {
286 gst_buffer_unref(buf);
290 gst_flacenc_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
294 this = (FlacEnc *)object;
297 GST_DEBUG(0, "Unknown arg %d", prop_id);
303 gst_flacenc_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
307 this = (FlacEnc *)object;
311 GST_DEBUG(0, "Unknown arg %d", prop_id);