FLAC updates, implement sample accurate seeking, add some convert/query functions...
[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 *enc_src_template, *enc_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, 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   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);
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->first = TRUE;
136   flacenc->first_buf = NULL;
137   flacenc->encoder = FLAC__stream_encoder_new();
138
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);
142
143   GST_FLAG_SET (flacenc, GST_ELEMENT_EVENT_AWARE);
144 }
145
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)
149 {
150   FlacEnc *flacenc;
151   GstBuffer *outbuf;
152
153   flacenc = GST_FLACENC (client_data);
154
155   outbuf = gst_buffer_new ();
156   GST_BUFFER_SIZE (outbuf) = bytes;
157   GST_BUFFER_DATA (outbuf) = g_malloc (bytes);
158
159   memcpy (GST_BUFFER_DATA (outbuf), buffer, bytes);
160
161   if (flacenc->first) {
162     flacenc->first_buf = outbuf;
163     gst_buffer_ref (outbuf);
164     flacenc->first = FALSE;
165   }
166
167   gst_pad_push (flacenc->srcpad, outbuf);
168
169   return FLAC__STREAM_ENCODER_WRITE_OK;
170 }
171
172 static void 
173 gst_flacenc_metadata_callback (const FLAC__StreamEncoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data)
174 {
175   GstEvent *event;
176   FlacEnc *flacenc;
177
178   flacenc = GST_FLACENC (client_data);
179
180   event = gst_event_new_discontinuous (FALSE, GST_FORMAT_BYTES, 0, NULL);
181   gst_pad_push (flacenc->srcpad, GST_BUFFER (event));
182
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;
187
188     guint8 *data = GST_BUFFER_DATA (flacenc->first_buf);
189     GstBuffer *outbuf = flacenc->first_buf;
190
191     /* this looks evil but is actually how one is supposed to write
192      * the stream stats according to the FLAC examples */
193
194     memcpy (&data[26], metadata->data.stream_info.md5sum, 16);
195     
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);
202
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);
206
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);
210
211     flacenc->first_buf = NULL;
212     gst_pad_push (flacenc->srcpad, outbuf);
213   }
214 }
215
216 static void
217 gst_flacenc_chain (GstPad *pad, GstBuffer *buf)
218 {
219   FlacEnc *flacenc;
220   gint32 *data[FLAC__MAX_CHANNELS];
221   gulong insize;
222   gint samples, channels, depth;
223   gulong i, j;
224   gboolean res;
225
226   g_return_if_fail(pad != NULL);
227   g_return_if_fail(GST_IS_PAD(pad));
228   g_return_if_fail(buf != NULL);
229
230   flacenc = GST_FLACENC (gst_pad_get_parent (pad));
231
232   if (GST_IS_EVENT (buf)) {
233     GstEvent *event = GST_EVENT (buf);
234
235     switch (GST_EVENT_TYPE (event)) {
236       case GST_EVENT_EOS:
237         FLAC__stream_encoder_finish(flacenc->encoder);
238       default:
239         gst_pad_event_default (pad, event);
240         break;
241     }
242     return;
243   }
244
245   channels = flacenc->channels;
246   depth = flacenc->depth;
247
248   insize = GST_BUFFER_SIZE (buf);
249   samples = insize / channels / ((depth+7)>>3);
250
251   if (FLAC__stream_encoder_get_state (flacenc->encoder) == FLAC__STREAM_ENCODER_UNINITIALIZED) {
252     FLAC__StreamEncoderState state;
253
254     state = FLAC__stream_encoder_init (flacenc->encoder);
255     
256     g_assert (state == FLAC__STREAM_ENCODER_OK);
257   }
258
259   for (i=0; i<channels; i++) {
260     data[i] = g_malloc (samples * sizeof (gint32));
261   }
262     
263   if (depth == 8) {
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++;
268       }
269     }
270   }
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++;
276       }
277     }
278   }
279
280   res = FLAC__stream_encoder_process (flacenc->encoder, (const FLAC__int32 **)data, samples);
281
282   for (i=0; i<channels; i++) {
283     g_free (data[i]);
284   }
285
286   gst_buffer_unref(buf);
287 }
288
289 static void
290 gst_flacenc_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
291 {
292   FlacEnc *this;
293   
294   this = (FlacEnc *)object;
295   switch (prop_id) {
296   default:
297     GST_DEBUG(0, "Unknown arg %d", prop_id);
298     return;
299   }
300 }
301
302 static void
303 gst_flacenc_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
304 {
305   FlacEnc *this;
306   
307   this = (FlacEnc *)object;
308   
309   switch (prop_id) {
310   default:
311     GST_DEBUG(0, "Unknown arg %d", prop_id);
312     break;
313   }
314 }
315