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 *gst_flacenc_src_template, *gst_flacenc_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,
54 const GValue *value, GParamSpec *pspec);
55 static void gst_flacenc_get_property (GObject *object, guint prop_id,
56 GValue *value, GParamSpec *pspec);
57 static GstElementStateReturn
58 gst_flacenc_change_state (GstElement *element);
60 static FLAC__StreamEncoderWriteStatus
61 gst_flacenc_write_callback (const FLAC__StreamEncoder *encoder,
62 const FLAC__byte buffer[], unsigned bytes,
63 unsigned samples, unsigned current_frame,
65 static void gst_flacenc_metadata_callback (const FLAC__StreamEncoder *encoder,
66 const FLAC__StreamMetadata *metadata,
69 static GstElementClass *parent_class = NULL;
70 /*static guint gst_flacenc_signals[LAST_SIGNAL] = { 0 }; */
73 flacenc_get_type (void)
75 static GType flacenc_type = 0;
78 static const GTypeInfo flacenc_info = {
82 (GClassInitFunc)gst_flacenc_class_init,
87 (GInstanceInitFunc)gst_flacenc_init,
89 flacenc_type = g_type_register_static (GST_TYPE_ELEMENT, "FlacEnc", &flacenc_info, 0);
95 gst_flacenc_class_init (FlacEncClass *klass)
97 GObjectClass *gobject_class;
98 GstElementClass *gstelement_class;
100 gobject_class = (GObjectClass*)klass;
101 gstelement_class = (GstElementClass*)klass;
103 parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
105 /* we have no properties atm so this is a bit silly */
106 gobject_class->set_property = gst_flacenc_set_property;
107 gobject_class->get_property = gst_flacenc_get_property;
109 gstelement_class->change_state = gst_flacenc_change_state;
112 static GstPadConnectReturn
113 gst_flacenc_sinkconnect (GstPad *pad, GstCaps *caps)
117 flacenc = GST_FLACENC (gst_pad_get_parent (pad));
119 if (!GST_CAPS_IS_FIXED (caps))
120 return GST_PAD_CONNECT_DELAYED;
122 gst_caps_get_int (caps, "channels", &flacenc->channels);
123 gst_caps_get_int (caps, "depth", &flacenc->depth);
124 gst_caps_get_int (caps, "rate", &flacenc->sample_rate);
126 FLAC__stream_encoder_set_bits_per_sample (flacenc->encoder,
128 FLAC__stream_encoder_set_sample_rate (flacenc->encoder,
129 flacenc->sample_rate);
130 FLAC__stream_encoder_set_channels (flacenc->encoder,
133 flacenc->negotiated = TRUE;
135 return GST_PAD_CONNECT_OK;
139 gst_flacenc_init (FlacEnc *flacenc)
141 flacenc->sinkpad = gst_pad_new_from_template (gst_flacenc_sink_template, "sink");
142 gst_element_add_pad(GST_ELEMENT(flacenc),flacenc->sinkpad);
143 gst_pad_set_chain_function(flacenc->sinkpad,gst_flacenc_chain);
144 gst_pad_set_connect_function (flacenc->sinkpad, gst_flacenc_sinkconnect);
146 flacenc->srcpad = gst_pad_new_from_template (gst_flacenc_src_template, "src");
147 gst_element_add_pad(GST_ELEMENT(flacenc),flacenc->srcpad);
149 flacenc->first = TRUE;
150 flacenc->first_buf = NULL;
151 flacenc->encoder = FLAC__stream_encoder_new();
153 FLAC__stream_encoder_set_write_callback (flacenc->encoder,
154 gst_flacenc_write_callback);
155 FLAC__stream_encoder_set_metadata_callback (flacenc->encoder,
156 gst_flacenc_metadata_callback);
157 FLAC__stream_encoder_set_client_data (flacenc->encoder,
160 GST_FLAG_SET (flacenc, GST_ELEMENT_EVENT_AWARE);
162 flacenc->negotiated = FALSE;
165 static FLAC__StreamEncoderWriteStatus
166 gst_flacenc_write_callback (const FLAC__StreamEncoder *encoder,
167 const FLAC__byte buffer[], unsigned bytes,
168 unsigned samples, unsigned current_frame,
174 flacenc = GST_FLACENC (client_data);
176 outbuf = gst_buffer_new ();
177 GST_BUFFER_SIZE (outbuf) = bytes;
178 GST_BUFFER_DATA (outbuf) = g_malloc (bytes);
180 memcpy (GST_BUFFER_DATA (outbuf), buffer, bytes);
182 if (flacenc->first) {
183 flacenc->first_buf = outbuf;
184 gst_buffer_ref (outbuf);
185 flacenc->first = FALSE;
188 gst_pad_push (flacenc->srcpad, outbuf);
190 return FLAC__STREAM_ENCODER_WRITE_OK;
194 gst_flacenc_metadata_callback (const FLAC__StreamEncoder *encoder,
195 const FLAC__StreamMetadata *metadata,
201 flacenc = GST_FLACENC (client_data);
203 event = gst_event_new_discontinuous (FALSE, GST_FORMAT_BYTES, 0, NULL);
204 gst_pad_push (flacenc->srcpad, GST_BUFFER (event));
206 if (flacenc->first_buf) {
207 const FLAC__uint64 samples = metadata->data.stream_info.total_samples;
208 const unsigned min_framesize = metadata->data.stream_info.min_framesize;
209 const unsigned max_framesize = metadata->data.stream_info.max_framesize;
211 guint8 *data = GST_BUFFER_DATA (flacenc->first_buf);
212 GstBuffer *outbuf = flacenc->first_buf;
214 /* this looks evil but is actually how one is supposed to write
215 * the stream stats according to the FLAC examples */
217 memcpy (&data[26], metadata->data.stream_info.md5sum, 16);
219 data[21] = (data[21] & 0xf0) |
220 (FLAC__byte)((samples >> 32) & 0x0f);
221 data[22] = (FLAC__byte)((samples >> 24) & 0xff);
222 data[23] = (FLAC__byte)((samples >> 16) & 0xff);
223 data[24] = (FLAC__byte)((samples >> 8 ) & 0xff);
224 data[25] = (FLAC__byte)((samples ) & 0xff);
226 data[12] = (FLAC__byte)((min_framesize >> 16) & 0xFF);
227 data[13] = (FLAC__byte)((min_framesize >> 8 ) & 0xFF);
228 data[14] = (FLAC__byte)((min_framesize ) & 0xFF);
230 data[15] = (FLAC__byte)((max_framesize >> 16) & 0xFF);
231 data[16] = (FLAC__byte)((max_framesize >> 8 ) & 0xFF);
232 data[17] = (FLAC__byte)((max_framesize ) & 0xFF);
234 flacenc->first_buf = NULL;
235 gst_pad_push (flacenc->srcpad, outbuf);
240 gst_flacenc_chain (GstPad *pad, GstBuffer *buf)
243 gint32 *data[FLAC__MAX_CHANNELS];
245 gint samples, channels, depth;
249 g_return_if_fail(pad != NULL);
250 g_return_if_fail(GST_IS_PAD(pad));
251 g_return_if_fail(buf != NULL);
253 flacenc = GST_FLACENC (gst_pad_get_parent (pad));
255 if (GST_IS_EVENT (buf)) {
256 GstEvent *event = GST_EVENT (buf);
258 switch (GST_EVENT_TYPE (event)) {
260 FLAC__stream_encoder_finish(flacenc->encoder);
262 gst_pad_event_default (pad, event);
268 if (!flacenc->negotiated) {
269 gst_element_error (GST_ELEMENT (flacenc),
270 "format not negotiated");
274 channels = flacenc->channels;
275 depth = flacenc->depth;
277 insize = GST_BUFFER_SIZE (buf);
278 samples = insize / channels / ((depth+7)>>3);
280 if (FLAC__stream_encoder_get_state (flacenc->encoder) ==
281 FLAC__STREAM_ENCODER_UNINITIALIZED)
283 FLAC__StreamEncoderState state;
285 state = FLAC__stream_encoder_init (flacenc->encoder);
287 g_assert (state == FLAC__STREAM_ENCODER_OK);
290 for (i=0; i<channels; i++) {
291 data[i] = g_malloc (samples * sizeof (gint32));
295 gint8 *indata = (gint8 *) GST_BUFFER_DATA (buf);
296 for (j=0; j<samples; j++) {
297 for (i=0; i<channels; i++) {
298 data[i][j] = (gint32) *indata++;
302 else if (depth == 16) {
303 gint16 *indata = (gint16 *) GST_BUFFER_DATA (buf);
304 for (j=0; j<samples; j++) {
305 for (i=0; i<channels; i++) {
306 data[i][j] = (gint32) *indata++;
311 res = FLAC__stream_encoder_process (flacenc->encoder,
312 (const FLAC__int32 **)data, samples);
314 for (i=0; i<channels; i++) {
318 gst_buffer_unref(buf);
322 gst_flacenc_set_property (GObject *object, guint prop_id,
323 const GValue *value, GParamSpec *pspec)
327 this = (FlacEnc *)object;
330 GST_DEBUG(0, "Unknown arg %d", prop_id);
336 gst_flacenc_get_property (GObject *object, guint prop_id,
337 GValue *value, GParamSpec *pspec)
341 this = (FlacEnc *)object;
345 GST_DEBUG(0, "Unknown arg %d", prop_id);
350 static GstElementStateReturn
351 gst_flacenc_change_state (GstElement *element)
353 FlacEnc *flacenc = GST_FLACENC (element);
355 switch (GST_STATE_TRANSITION (element)) {
356 case GST_STATE_NULL_TO_READY:
357 case GST_STATE_READY_TO_PAUSED:
359 case GST_STATE_PAUSED_TO_PLAYING:
360 case GST_STATE_PLAYING_TO_PAUSED:
362 case GST_STATE_PAUSED_TO_READY:
363 flacenc->negotiated = FALSE;
365 case GST_STATE_READY_TO_NULL:
370 if (GST_ELEMENT_CLASS (parent_class)->change_state)
371 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
373 return GST_STATE_SUCCESS;