removal of //-style comments don't link plugins to core libs -- the versioning is...
[platform/upstream/gst-plugins-good.git] / ext / flac / gstflacenc.c
1 /* Gnome-Streamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
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., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include <gstflacenc.h>
25
26 extern GstPadTemplate *enc_src_template, *enc_sink_template;
27
28 /* elementfactory information */
29 GstElementDetails flacenc_details = {
30   "FLAC encoder",
31   "Filter/Audio/Encoder",
32   "Encodes audio with the FLAC lossless audio encoder",
33   VERSION,
34   "Wim Taymans <wim.taymans@chello.be>",
35   "(C) 2001",
36 };
37
38 /* FlacEnc signals and args */
39 enum {
40   /* FILL ME */
41   LAST_SIGNAL
42 };
43
44 enum {
45   ARG_0,
46 };
47
48 static void     gst_flacenc_init                (FlacEnc *flacenc);
49 static void     gst_flacenc_class_init          (FlacEncClass *klass);
50
51 static void     gst_flacenc_chain               (GstPad *pad, GstBuffer *buf);
52
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);
55
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, 
60                                                  void *client_data);
61
62 static GstElementClass *parent_class = NULL;
63 /*static guint gst_flacenc_signals[LAST_SIGNAL] = { 0 }; */
64
65 GType
66 flacenc_get_type (void)
67 {
68   static GType flacenc_type = 0;
69
70   if (!flacenc_type) {
71     static const GTypeInfo flacenc_info = {
72       sizeof(FlacEncClass),
73       NULL,
74       NULL,
75       (GClassInitFunc)gst_flacenc_class_init,
76       NULL,
77       NULL,
78       sizeof(FlacEnc),
79       0,
80       (GInstanceInitFunc)gst_flacenc_init,
81     };
82     flacenc_type = g_type_register_static (GST_TYPE_ELEMENT, "FlacEnc", &flacenc_info, 0);
83   }
84   return flacenc_type;
85 }
86
87 static void
88 gst_flacenc_class_init (FlacEncClass *klass)
89 {
90   GObjectClass *gobject_class;
91   GstElementClass *gstelement_class;
92
93   gobject_class = (GObjectClass*)klass;
94   gstelement_class = (GstElementClass*)klass;
95
96   parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
97   
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;
101 }
102
103 static GstPadConnectReturn
104 gst_flacenc_sinkconnect (GstPad *pad, GstCaps *caps)
105 {
106   FlacEnc *flacenc;
107
108   flacenc = GST_FLACENC (gst_pad_get_parent (pad));
109
110   if (!GST_CAPS_IS_FIXED (caps))
111     return GST_PAD_CONNECT_DELAYED;
112
113   flacenc->channels = gst_caps_get_int (caps, "channels");
114   flacenc->depth = gst_caps_get_int (caps, "depth");
115   flacenc->sample_rate = gst_caps_get_int (caps, "rate");
116
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);
120
121   return GST_PAD_CONNECT_OK;
122 }
123
124 static void
125 gst_flacenc_init (FlacEnc *flacenc)
126 {
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);
131
132   flacenc->srcpad = gst_pad_new_from_template (enc_src_template, "src");
133   gst_element_add_pad(GST_ELEMENT(flacenc),flacenc->srcpad);
134
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);
139 }
140
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)
144 {
145   FlacEnc *flacenc;
146   GstBuffer *outbuf;
147
148   flacenc = GST_FLACENC (client_data);
149
150   outbuf = gst_buffer_new ();
151   GST_BUFFER_SIZE (outbuf) = bytes;
152   GST_BUFFER_DATA (outbuf) = g_malloc (bytes);
153
154   memcpy (GST_BUFFER_DATA (outbuf), buffer, bytes);
155
156   gst_pad_push (flacenc->srcpad, outbuf);
157
158   return FLAC__STREAM_ENCODER_WRITE_OK;
159 }
160
161 static void 
162 gst_flacenc_metadata_callback (const FLAC__StreamEncoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data)
163 {
164 }
165
166 static void
167 gst_flacenc_chain (GstPad *pad,GstBuffer *buf)
168 {
169   FlacEnc *flacenc;
170   gint32 *data[FLAC__MAX_CHANNELS];
171   gulong insize;
172   gint samples, channels, depth;
173   gulong i, j;
174   gboolean res;
175
176   g_return_if_fail(pad != NULL);
177   g_return_if_fail(GST_IS_PAD(pad));
178   g_return_if_fail(buf != NULL);
179
180   flacenc = GST_FLACENC (gst_pad_get_parent (pad));
181
182   channels = flacenc->channels;
183   depth = flacenc->depth;
184
185   insize = GST_BUFFER_SIZE (buf);
186   samples = insize / channels / ((depth+7)>>3);
187
188   if (FLAC__stream_encoder_get_state (flacenc->encoder) == FLAC__STREAM_ENCODER_UNINITIALIZED) {
189     FLAC__StreamEncoderState state;
190
191     state = FLAC__stream_encoder_init (flacenc->encoder);
192     
193     g_assert (state == FLAC__STREAM_ENCODER_OK);
194   }
195
196   for (i=0; i<channels; i++) {
197     data[i] = g_malloc (samples * sizeof (gint32));
198   }
199     
200   if (depth == 8) {
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++;
205       }
206     }
207   }
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++;
213       }
214     }
215   }
216
217   res = FLAC__stream_encoder_process (flacenc->encoder, (const FLAC__int32 **)data, samples);
218
219   for (i=0; i<channels; i++) {
220     g_free (data[i]);
221   }
222
223   gst_buffer_unref(buf);
224 }
225
226 static void
227 gst_flacenc_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
228 {
229   FlacEnc *this;
230   
231   this = (FlacEnc *)object;
232   switch (prop_id) {
233   default:
234     GST_DEBUG(0, "Unknown arg %d\n", prop_id);
235     return;
236   }
237 }
238
239 static void
240 gst_flacenc_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
241 {
242   FlacEnc *this;
243   
244   this = (FlacEnc *)object;
245   
246   switch (prop_id) {
247   default:
248     GST_DEBUG(0, "Unknown arg %d\n", prop_id);
249     break;
250   }
251 }
252