Small cleanups
[platform/upstream/gst-plugins-good.git] / ext / flac / gstflacenc.c
1 /* GStreamer
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 *gst_flacenc_src_template, *gst_flacenc_sink_template;
27
28 /* elementfactory information */
29 GstElementDetails flacenc_details = {
30   "FLAC encoder",
31   "Codec/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, 
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);
59
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, 
64                                                  void *client_data);
65 static void     gst_flacenc_metadata_callback   (const FLAC__StreamEncoder *encoder, 
66                                                  const FLAC__StreamMetadata *metadata, 
67                                                  void *client_data);
68
69 static GstElementClass *parent_class = NULL;
70 /*static guint gst_flacenc_signals[LAST_SIGNAL] = { 0 }; */
71
72 GType
73 flacenc_get_type (void)
74 {
75   static GType flacenc_type = 0;
76
77   if (!flacenc_type) {
78     static const GTypeInfo flacenc_info = {
79       sizeof(FlacEncClass),
80       NULL,
81       NULL,
82       (GClassInitFunc)gst_flacenc_class_init,
83       NULL,
84       NULL,
85       sizeof(FlacEnc),
86       0,
87       (GInstanceInitFunc)gst_flacenc_init,
88     };
89     flacenc_type = g_type_register_static (GST_TYPE_ELEMENT, "FlacEnc", &flacenc_info, 0);
90   }
91   return flacenc_type;
92 }
93
94 static void
95 gst_flacenc_class_init (FlacEncClass *klass)
96 {
97   GObjectClass *gobject_class;
98   GstElementClass *gstelement_class;
99
100   gobject_class = (GObjectClass*)klass;
101   gstelement_class = (GstElementClass*)klass;
102
103   parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
104   
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;
108
109   gstelement_class->change_state = gst_flacenc_change_state;
110 }
111
112 static GstPadConnectReturn
113 gst_flacenc_sinkconnect (GstPad *pad, GstCaps *caps)
114 {
115   FlacEnc *flacenc;
116
117   flacenc = GST_FLACENC (gst_pad_get_parent (pad));
118
119   if (!GST_CAPS_IS_FIXED (caps))
120     return GST_PAD_CONNECT_DELAYED;
121
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);
125
126   FLAC__stream_encoder_set_bits_per_sample (flacenc->encoder, 
127                                             flacenc->depth);
128   FLAC__stream_encoder_set_sample_rate (flacenc->encoder, 
129                                         flacenc->sample_rate);
130   FLAC__stream_encoder_set_channels (flacenc->encoder, 
131                                      flacenc->channels);
132
133   flacenc->negotiated = TRUE;
134
135   return GST_PAD_CONNECT_OK;
136 }
137
138 static void
139 gst_flacenc_init (FlacEnc *flacenc)
140 {
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);
145
146   flacenc->srcpad = gst_pad_new_from_template (gst_flacenc_src_template, "src");
147   gst_element_add_pad(GST_ELEMENT(flacenc),flacenc->srcpad);
148
149   flacenc->first = TRUE;
150   flacenc->first_buf = NULL;
151   flacenc->encoder = FLAC__stream_encoder_new();
152
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, 
158                                         flacenc);
159
160   GST_FLAG_SET (flacenc, GST_ELEMENT_EVENT_AWARE);
161
162   flacenc->negotiated = FALSE;
163 }
164
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, 
169                             void *client_data)
170 {
171   FlacEnc *flacenc;
172   GstBuffer *outbuf;
173
174   flacenc = GST_FLACENC (client_data);
175
176   outbuf = gst_buffer_new ();
177   GST_BUFFER_SIZE (outbuf) = bytes;
178   GST_BUFFER_DATA (outbuf) = g_malloc (bytes);
179
180   memcpy (GST_BUFFER_DATA (outbuf), buffer, bytes);
181
182   if (flacenc->first) {
183     flacenc->first_buf = outbuf;
184     gst_buffer_ref (outbuf);
185     flacenc->first = FALSE;
186   }
187
188   gst_pad_push (flacenc->srcpad, outbuf);
189
190   return FLAC__STREAM_ENCODER_WRITE_OK;
191 }
192
193 static void 
194 gst_flacenc_metadata_callback (const FLAC__StreamEncoder *encoder, 
195                                const FLAC__StreamMetadata *metadata, 
196                                void *client_data)
197 {
198   GstEvent *event;
199   FlacEnc *flacenc;
200
201   flacenc = GST_FLACENC (client_data);
202
203   event = gst_event_new_discontinuous (FALSE, GST_FORMAT_BYTES, 0, NULL);
204   gst_pad_push (flacenc->srcpad, GST_BUFFER (event));
205
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;
210
211     guint8 *data = GST_BUFFER_DATA (flacenc->first_buf);
212     GstBuffer *outbuf = flacenc->first_buf;
213
214     /* this looks evil but is actually how one is supposed to write
215      * the stream stats according to the FLAC examples */
216
217     memcpy (&data[26], metadata->data.stream_info.md5sum, 16);
218     
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);
225
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);
229
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);
233
234     flacenc->first_buf = NULL;
235     gst_pad_push (flacenc->srcpad, outbuf);
236   }
237 }
238
239 static void
240 gst_flacenc_chain (GstPad *pad, GstBuffer *buf)
241 {
242   FlacEnc *flacenc;
243   gint32 *data[FLAC__MAX_CHANNELS];
244   gulong insize;
245   gint samples, channels, depth;
246   gulong i, j;
247   gboolean res;
248
249   g_return_if_fail(pad != NULL);
250   g_return_if_fail(GST_IS_PAD(pad));
251   g_return_if_fail(buf != NULL);
252
253   flacenc = GST_FLACENC (gst_pad_get_parent (pad));
254
255   if (GST_IS_EVENT (buf)) {
256     GstEvent *event = GST_EVENT (buf);
257
258     switch (GST_EVENT_TYPE (event)) {
259       case GST_EVENT_EOS:
260         FLAC__stream_encoder_finish(flacenc->encoder);
261       default:
262         gst_pad_event_default (pad, event);
263         break;
264     }
265     return;
266   }
267
268   if (!flacenc->negotiated) {
269     gst_element_error (GST_ELEMENT (flacenc),
270                     "format not negotiated");
271     return;
272   }
273
274   channels = flacenc->channels;
275   depth = flacenc->depth;
276
277   insize = GST_BUFFER_SIZE (buf);
278   samples = insize / channels / ((depth+7)>>3);
279
280   if (FLAC__stream_encoder_get_state (flacenc->encoder) == 
281                   FLAC__STREAM_ENCODER_UNINITIALIZED) 
282   {
283     FLAC__StreamEncoderState state;
284
285     state = FLAC__stream_encoder_init (flacenc->encoder);
286     
287     g_assert (state == FLAC__STREAM_ENCODER_OK);
288   }
289
290   for (i=0; i<channels; i++) {
291     data[i] = g_malloc (samples * sizeof (gint32));
292   }
293     
294   if (depth == 8) {
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++;
299       }
300     }
301   }
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++;
307       }
308     }
309   }
310
311   res = FLAC__stream_encoder_process (flacenc->encoder, 
312                                       (const FLAC__int32 **)data, samples);
313
314   for (i=0; i<channels; i++) {
315     g_free (data[i]);
316   }
317
318   gst_buffer_unref(buf);
319 }
320
321 static void
322 gst_flacenc_set_property (GObject *object, guint prop_id,
323                           const GValue *value, GParamSpec *pspec)
324 {
325   FlacEnc *this;
326   
327   this = (FlacEnc *)object;
328   switch (prop_id) {
329   default:
330     GST_DEBUG(0, "Unknown arg %d", prop_id);
331     return;
332   }
333 }
334
335 static void
336 gst_flacenc_get_property (GObject *object, guint prop_id, 
337                           GValue *value, GParamSpec *pspec)
338 {
339   FlacEnc *this;
340   
341   this = (FlacEnc *)object;
342   
343   switch (prop_id) {
344   default:
345     GST_DEBUG(0, "Unknown arg %d", prop_id);
346     break;
347   }
348 }
349
350 static GstElementStateReturn
351 gst_flacenc_change_state (GstElement *element)
352 {
353   FlacEnc *flacenc = GST_FLACENC (element);
354
355   switch (GST_STATE_TRANSITION (element)) {
356     case GST_STATE_NULL_TO_READY:
357     case GST_STATE_READY_TO_PAUSED:
358       break;
359     case GST_STATE_PAUSED_TO_PLAYING:
360     case GST_STATE_PLAYING_TO_PAUSED:
361       break;
362     case GST_STATE_PAUSED_TO_READY:
363       flacenc->negotiated = FALSE;
364       break;
365     case GST_STATE_READY_TO_NULL:
366     default:
367       break;
368   }
369
370   if (GST_ELEMENT_CLASS (parent_class)->change_state)
371     return GST_ELEMENT_CLASS (parent_class)->change_state (element);
372
373   return GST_STATE_SUCCESS;
374 }
375