_ Change from StreamEncoder to SeekableStreamEncoder _ Add a seek callback to fill...
[platform/upstream/gstreamer.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 #include "flac_compat.h"
27
28 extern GstPadTemplate *gst_flacenc_src_template, *gst_flacenc_sink_template;
29
30 /* elementfactory information */
31 GstElementDetails flacenc_details = {
32   "FLAC encoder",
33   "Codec/Audio/Encoder",
34   "LGPL",
35   "Encodes audio with the FLAC lossless audio encoder",
36   VERSION,
37   "Wim Taymans <wim.taymans@chello.be>",
38   "(C) 2001",
39 };
40
41 /* FlacEnc signals and args */
42 enum {
43   /* FILL ME */
44   LAST_SIGNAL
45 };
46
47 enum {
48   ARG_0,
49   ARG_QUALITY,
50   ARG_STREAMABLE_SUBSET,
51   ARG_MID_SIDE_STEREO,
52   ARG_LOOSE_MID_SIDE_STEREO,
53   ARG_BLOCKSIZE,
54   ARG_MAX_LPC_ORDER,
55   ARG_QLP_COEFF_PRECISION,
56   ARG_QLP_COEFF_PREC_SEARCH,
57   ARG_ESCAPE_CODING,
58   ARG_EXHAUSTIVE_MODEL_SEARCH,
59   ARG_MIN_RESIDUAL_PARTITION_ORDER,
60   ARG_MAX_RESIDUAL_PARTITION_ORDER,
61   ARG_RICE_PARAMETER_SEARCH_DIST,
62 };
63
64 static void             gst_flacenc_init                (FlacEnc *flacenc);
65 static void             gst_flacenc_class_init          (FlacEncClass *klass);
66 static void             gst_flacenc_dispose             (GObject *object);
67
68 static GstPadLinkReturn
69                         gst_flacenc_sinkconnect         (GstPad *pad, GstCaps *caps);
70 static void             gst_flacenc_chain               (GstPad *pad, GstBuffer *buf);
71
72 static gboolean         gst_flacenc_update_quality      (FlacEnc *flacenc, gint quality);
73 static void             gst_flacenc_set_property        (GObject *object, guint prop_id, 
74                                                          const GValue *value, GParamSpec *pspec);
75 static void             gst_flacenc_get_property        (GObject *object, guint prop_id,
76                                                          GValue *value, GParamSpec *pspec);
77 static GstElementStateReturn
78                         gst_flacenc_change_state        (GstElement *element);
79
80 static FLAC__StreamEncoderWriteStatus 
81                         gst_flacenc_write_callback      (const FLAC__SeekableStreamEncoder *encoder, 
82                                                          const FLAC__byte buffer[], unsigned bytes, 
83                                                          unsigned samples, unsigned current_frame, 
84                                                          void *client_data);
85 static FLAC__SeekableStreamEncoderSeekStatus
86                         gst_flacenc_seek_callback       (const FLAC__SeekableStreamEncoder *encoder,
87                                                          FLAC__uint64 absolute_byte_offset,
88                                                          void *client_data);
89
90 static GstElementClass *parent_class = NULL;
91 /*static guint gst_flacenc_signals[LAST_SIGNAL] = { 0 }; */
92
93 GType
94 flacenc_get_type (void)
95 {
96   static GType flacenc_type = 0;
97
98   if (!flacenc_type) {
99     static const GTypeInfo flacenc_info = {
100       sizeof(FlacEncClass),
101       NULL,
102       NULL,
103       (GClassInitFunc)gst_flacenc_class_init,
104       NULL,
105       NULL,
106       sizeof(FlacEnc),
107       0,
108       (GInstanceInitFunc)gst_flacenc_init,
109     };
110     flacenc_type = g_type_register_static (GST_TYPE_ELEMENT, "FlacEnc", &flacenc_info, 0);
111   }
112   return flacenc_type;
113 }
114
115 typedef struct {
116   gboolean      exhaustive_model_search;
117   gboolean      escape_coding;
118   gboolean      mid_side;
119   gboolean      loose_mid_side;
120   guint         qlp_coeff_precision;
121   gboolean      qlp_coeff_prec_search;
122   guint         min_residual_partition_order;
123   guint         max_residual_partition_order;
124   guint         rice_parameter_search_dist;
125   guint         max_lpc_order;
126   guint         blocksize;
127 } FlacEncParams;
128
129 static const FlacEncParams flacenc_params[] = 
130 {
131   { FALSE, FALSE, FALSE, FALSE, 0, FALSE, 2, 2,  0, 0,  1152 },
132   { FALSE, FALSE, TRUE,  TRUE,  0, FALSE, 2, 2,  0, 0,  1152 },
133   { FALSE, FALSE, TRUE,  FALSE, 0, FALSE, 0, 3,  0, 0,  1152 },
134   { FALSE, FALSE, FALSE, FALSE, 0, FALSE, 3, 3,  0, 6,  4608 },
135   { FALSE, FALSE, TRUE,  TRUE,  0, FALSE, 3, 3,  0, 8,  4608 },
136   { FALSE, FALSE, TRUE,  FALSE, 0, FALSE, 3, 3,  0, 8,  4608 },
137   { FALSE, FALSE, TRUE,  FALSE, 0, FALSE, 0, 4,  0, 8,  4608 },
138   { TRUE,  FALSE, TRUE,  FALSE, 0, FALSE, 0, 6,  0, 8,  4608 },
139   { TRUE,  FALSE, TRUE,  FALSE, 0, FALSE, 0, 6,  0, 12, 4608 },
140   { TRUE,  TRUE,  TRUE,  FALSE, 0, FALSE, 0, 16, 0, 32, 4608 },
141 };
142
143 #define DEFAULT_QUALITY 5
144
145 #define GST_TYPE_FLACENC_QUALITY (gst_flacenc_quality_get_type ())
146 GType
147 gst_flacenc_quality_get_type (void)
148 {
149   static GType qtype = 0;
150   if (qtype == 0) {
151     static const GEnumValue values[] = {
152       { 0, "0", "0 - Fastest compression" },
153       { 1, "1", "1" },
154       { 2, "2", "2" },
155       { 3, "3", "3" },
156       { 4, "4", "4" },
157       { 5, "5", "5 - Default" },
158       { 6, "6", "6" },
159       { 7, "7", "7" },
160       { 8, "8", "8 - Highest compression " },
161       { 9, "9", "9 - Insane" },
162       { 0, NULL, NULL }
163     };
164     qtype = g_enum_register_static ("FlacEncQuality", values);
165   }
166   return qtype;
167 }
168
169 static void
170 gst_flacenc_class_init (FlacEncClass *klass)
171 {
172   GObjectClass *gobject_class;
173   GstElementClass *gstelement_class;
174
175   gobject_class = (GObjectClass*)klass;
176   gstelement_class = (GstElementClass*)klass;
177
178   parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
179   
180   /* we have no properties atm so this is a bit silly */
181   gobject_class->set_property = gst_flacenc_set_property;
182   gobject_class->get_property = gst_flacenc_get_property;
183   gobject_class->dispose      = gst_flacenc_dispose;
184
185   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_QUALITY,
186     g_param_spec_enum ("quality", 
187                        "Quality", 
188                        "Speed versus compression tradeoff",
189                        GST_TYPE_FLACENC_QUALITY, DEFAULT_QUALITY, G_PARAM_READWRITE));
190   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_STREAMABLE_SUBSET,
191     g_param_spec_boolean ("streamable_subset", 
192                           "Streamable subset", 
193                           "true to limit encoder to generating a Subset stream, else false",
194                           TRUE, G_PARAM_READWRITE));
195   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MID_SIDE_STEREO,
196     g_param_spec_boolean ("mid_side_stereo", 
197                           "Do mid side stereo", 
198                           "Do mid side stereo (only for stereo input)", 
199                           flacenc_params[DEFAULT_QUALITY].mid_side, G_PARAM_READWRITE));
200   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LOOSE_MID_SIDE_STEREO,
201     g_param_spec_boolean ("loose_mid_side_stereo", 
202                           "Loose mid side stereo", 
203                           "Loose mid side stereo", 
204                           flacenc_params[DEFAULT_QUALITY].loose_mid_side, G_PARAM_READWRITE));
205   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BLOCKSIZE,
206     g_param_spec_uint ("blocksize", 
207                        "Blocksize", 
208                        "Blocksize in samples",
209                        FLAC__MIN_BLOCK_SIZE, FLAC__MAX_BLOCK_SIZE, 
210                        flacenc_params[DEFAULT_QUALITY].blocksize, G_PARAM_READWRITE));
211   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MAX_LPC_ORDER,
212     g_param_spec_uint ("max_lpc_order", 
213                        "Max LPC order", 
214                        "Max LPC order; 0 => use only fixed predictors",
215                        0, FLAC__MAX_LPC_ORDER,
216                        flacenc_params[DEFAULT_QUALITY].max_lpc_order, G_PARAM_READWRITE));
217   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_QLP_COEFF_PRECISION,
218     g_param_spec_uint ("qlp_coeff_precision", 
219                        "QLP coefficients precision", 
220                        "Precision in bits of quantized linear-predictor coefficients; 0 = automatic",
221                        0, 32, 
222                        flacenc_params[DEFAULT_QUALITY].qlp_coeff_precision, G_PARAM_READWRITE));
223   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_QLP_COEFF_PREC_SEARCH,
224     g_param_spec_boolean ("qlp_coeff_prec_search", 
225                           "Do QLP coefficients precision search", 
226                           "false = use qlp_coeff_precision, "
227                             "true = search around qlp_coeff_precision, take best", 
228                           flacenc_params[DEFAULT_QUALITY].qlp_coeff_prec_search, G_PARAM_READWRITE));
229   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_ESCAPE_CODING,
230     g_param_spec_boolean ("escape_coding", 
231                           "Do Escape coding", 
232                           "search for escape codes in the entropy coding stage "
233                             "for slightly better compression", 
234                           flacenc_params[DEFAULT_QUALITY].escape_coding, G_PARAM_READWRITE));
235   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_EXHAUSTIVE_MODEL_SEARCH,
236     g_param_spec_boolean ("exhaustive_model_search", 
237                           "Do exhaustive model search", 
238                           "do exhaustive search of LP coefficient quantization (expensive!)",
239                           flacenc_params[DEFAULT_QUALITY].exhaustive_model_search, G_PARAM_READWRITE));
240   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MIN_RESIDUAL_PARTITION_ORDER,
241     g_param_spec_uint ("min_residual_partition_order", 
242                        "Min residual partition order", 
243                        "Min residual partition order (above 4 doesn't usually help much)",
244                        0, 16, 
245                        flacenc_params[DEFAULT_QUALITY].min_residual_partition_order, G_PARAM_READWRITE));
246   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MAX_RESIDUAL_PARTITION_ORDER,
247     g_param_spec_uint ("max_residual_partition_order", 
248                        "Max residual partition order", 
249                        "Max residual partition order (above 4 doesn't usually help much)",
250                        0, 16, 
251                        flacenc_params[DEFAULT_QUALITY].max_residual_partition_order, G_PARAM_READWRITE));
252   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_RICE_PARAMETER_SEARCH_DIST,
253     g_param_spec_uint ("rice_parameter_search_dist", 
254                        "rice_parameter_search_dist", 
255                        "0 = try only calc'd parameter k; else try all [k-dist..k+dist] "
256                          "parameters, use best",
257                        0, FLAC__MAX_RICE_PARTITION_ORDER, 
258                        flacenc_params[DEFAULT_QUALITY].rice_parameter_search_dist, G_PARAM_READWRITE));
259
260   gstelement_class->change_state = gst_flacenc_change_state;
261 }
262
263 static void
264 gst_flacenc_init (FlacEnc *flacenc)
265 {
266   flacenc->sinkpad = gst_pad_new_from_template (gst_flacenc_sink_template, "sink");
267   gst_element_add_pad(GST_ELEMENT(flacenc),flacenc->sinkpad);
268   gst_pad_set_chain_function(flacenc->sinkpad,gst_flacenc_chain);
269   gst_pad_set_link_function (flacenc->sinkpad, gst_flacenc_sinkconnect);
270
271   flacenc->srcpad = gst_pad_new_from_template (gst_flacenc_src_template, "src");
272   gst_element_add_pad(GST_ELEMENT(flacenc),flacenc->srcpad);
273
274   GST_FLAG_SET (flacenc, GST_ELEMENT_EVENT_AWARE);
275
276   flacenc->encoder = FLAC__seekable_stream_encoder_new();
277
278   FLAC__seekable_stream_encoder_set_write_callback (flacenc->encoder, 
279                                         gst_flacenc_write_callback);
280   FLAC__seekable_stream_encoder_set_seek_callback (flacenc->encoder, 
281                                         gst_flacenc_seek_callback);
282                                         
283   FLAC__seekable_stream_encoder_set_client_data (flacenc->encoder, 
284                                         flacenc);
285
286   flacenc->metadata = GST_CAPS_NEW (
287                   "flacenc_metadata",
288                   "application/x-gst-metadata",
289                     "DESCRIPTION",      GST_PROPS_STRING ("Track encoded with GStreamer"),
290                     "DATE",             GST_PROPS_STRING (""),
291                     "TRACKNUMBER",      GST_PROPS_STRING (""),
292                     "TITLE",            GST_PROPS_STRING (""),
293                     "ARTIST",           GST_PROPS_STRING (""),
294                     "ALBUM",            GST_PROPS_STRING (""),
295                     "GENRE",            GST_PROPS_STRING (""),
296                     "VERSION",          GST_PROPS_STRING (""),
297                     "COPYRIGHT",        GST_PROPS_STRING (""),
298                     "LICENSE",          GST_PROPS_STRING (""),
299                     "ORGANISATION",     GST_PROPS_STRING (""),
300                     "LOCATION",         GST_PROPS_STRING (""),
301                     "CONTACT",          GST_PROPS_STRING (""),
302                     "ISRC",             GST_PROPS_STRING ("")
303                   );
304                   
305   flacenc->negotiated = FALSE;
306   flacenc->first = TRUE;
307   flacenc->first_buf = NULL;
308   flacenc->data = NULL;
309   gst_flacenc_update_quality (flacenc, DEFAULT_QUALITY);
310 }
311
312 static void
313 gst_flacenc_dispose (GObject *object)
314 {
315   FlacEnc *flacenc = GST_FLACENC (object);
316
317   FLAC__seekable_stream_encoder_delete (flacenc->encoder);
318   
319   G_OBJECT_CLASS (parent_class)->dispose (object);
320 }
321
322 static GstPadLinkReturn
323 gst_flacenc_sinkconnect (GstPad *pad, GstCaps *caps)
324 {
325   FlacEnc *flacenc;
326
327   flacenc = GST_FLACENC (gst_pad_get_parent (pad));
328
329   if (!GST_CAPS_IS_FIXED (caps))
330     return GST_PAD_LINK_DELAYED;
331
332   gst_caps_get_int (caps, "channels", &flacenc->channels);
333   gst_caps_get_int (caps, "depth", &flacenc->depth);
334   gst_caps_get_int (caps, "rate", &flacenc->sample_rate);
335   
336   FLAC__seekable_stream_encoder_set_bits_per_sample (flacenc->encoder, 
337                                             flacenc->depth);
338   FLAC__seekable_stream_encoder_set_sample_rate (flacenc->encoder, 
339                                         flacenc->sample_rate);
340   FLAC__seekable_stream_encoder_set_channels (flacenc->encoder, 
341                                      flacenc->channels);
342
343   flacenc->negotiated = TRUE;
344
345   return GST_PAD_LINK_OK;
346 }
347
348 static gboolean
349 gst_flacenc_update_quality (FlacEnc *flacenc, gint quality)
350 {
351   flacenc->quality = quality;
352
353 #define DO_UPDATE(name, val, str)                               \
354   G_STMT_START {                                                \
355     if (FLAC__seekable_stream_encoder_get_##name (flacenc->encoder) !=  \
356         flacenc_params[quality].val) {                          \
357       FLAC__seekable_stream_encoder_set_##name (flacenc->encoder,       \
358           flacenc_params[quality].val);                         \
359       g_object_notify (G_OBJECT (flacenc), str);                \
360     }                                                           \
361   } G_STMT_END
362
363   g_object_freeze_notify (G_OBJECT (flacenc));
364
365   DO_UPDATE (do_mid_side_stereo,           mid_side,                     "mid_side_stereo");
366   DO_UPDATE (loose_mid_side_stereo,        loose_mid_side,               "loose_mid_side");
367   DO_UPDATE (blocksize,                    blocksize,                    "blocksize");
368   DO_UPDATE (max_lpc_order,                max_lpc_order,                "max_lpc_order");
369   DO_UPDATE (qlp_coeff_precision,          qlp_coeff_precision,          "qlp_coeff_precision");
370   DO_UPDATE (do_qlp_coeff_prec_search,     qlp_coeff_prec_search,        "qlp_coeff_prec_search");
371   DO_UPDATE (do_escape_coding,             escape_coding,                "escape_coding");
372   DO_UPDATE (do_exhaustive_model_search,   exhaustive_model_search,      "exhaustive_model_search");
373   DO_UPDATE (min_residual_partition_order, min_residual_partition_order, "min_residual_partition_order");
374   DO_UPDATE (max_residual_partition_order, max_residual_partition_order, "max_residual_partition_order");
375   DO_UPDATE (rice_parameter_search_dist,   rice_parameter_search_dist,   "rice_parameter_search_dist");
376
377 #undef DO_UPDATE
378
379   g_object_thaw_notify (G_OBJECT (flacenc));
380
381   return TRUE;
382 }
383
384 static FLAC__SeekableStreamEncoderSeekStatus
385 gst_flacenc_seek_callback (const FLAC__SeekableStreamEncoder *encoder,
386                            FLAC__uint64 absolute_byte_offset,
387                            void *client_data)
388 {
389 FlacEnc *flacenc;
390 GstEvent *event;
391
392   flacenc = GST_FLACENC (client_data);
393
394   if (flacenc->stopped) 
395     return FLAC__STREAM_ENCODER_OK;
396
397   event = gst_event_new_seek ((GstSeekType)(int)(GST_FORMAT_BYTES | GST_SEEK_METHOD_SET), absolute_byte_offset); 
398
399   if (event)
400     gst_pad_push (flacenc->srcpad, GST_BUFFER (event));
401  
402   return  FLAC__STREAM_ENCODER_OK;
403 }
404
405 static FLAC__StreamEncoderWriteStatus 
406 gst_flacenc_write_callback (const FLAC__SeekableStreamEncoder *encoder, 
407                             const FLAC__byte buffer[], unsigned bytes, 
408                             unsigned samples, unsigned current_frame, 
409                             void *client_data)
410 {
411   FlacEnc *flacenc;
412   GstBuffer *outbuf;
413   
414   flacenc = GST_FLACENC (client_data);
415
416   if (flacenc->stopped) 
417     return FLAC__STREAM_ENCODER_OK;
418
419   outbuf = gst_buffer_new_and_alloc (bytes);
420
421   memcpy (GST_BUFFER_DATA (outbuf), buffer, bytes);
422
423   if (flacenc->first) {
424     flacenc->first_buf = outbuf;
425     gst_buffer_ref (outbuf);
426     flacenc->first = FALSE;
427   }
428
429   gst_pad_push (flacenc->srcpad, outbuf);
430
431   return FLAC__STREAM_ENCODER_OK;
432 }
433
434 static void
435 gst_flacenc_set_metadata (FlacEnc *flacenc, GstCaps *caps)
436 {
437   guint indice;
438   guint comments;
439   const gchar *meta_types[] = { "TITLE", "VERSION", "ALBUM", "TRACKNUMBER",
440                                 "ARTIST", "PERFORMER", "COPYRIGHT", "LICENSE",
441                                 "ORGANISATION", "DESCRIPTION", "GENRE", "DATE",
442                                 "LOCATION", "CONTACT", "ISRC", NULL };
443
444   g_return_if_fail (flacenc != NULL);
445
446   flacenc->meta = g_malloc (sizeof (FLAC__StreamMetadata **));
447
448   flacenc->meta[0] = FLAC__metadata_object_new (FLAC__METADATA_TYPE_VORBIS_COMMENT);
449   
450   for ( indice = 0, comments=0; meta_types[indice] != NULL; indice++) {
451     if (gst_caps_has_property(caps, meta_types[indice])) {
452       const gchar *entry;
453       gchar *data;
454       FLAC__StreamMetadata_VorbisComment_Entry commment_entry;
455       
456       gst_caps_get_string(caps, (gchar *)meta_types[indice], &entry);
457
458       if (!strcmp (entry, ""))
459         continue;
460    
461       data = g_strdup_printf("%s=%s", meta_types[indice], entry);
462       commment_entry.length = GUINT32_TO_LE (strlen(entry) + strlen(meta_types[indice]) + 1);
463       commment_entry.entry  =  g_convert (data, commment_entry.length, "UTF8", "ISO-8859-1", NULL, NULL, NULL);
464
465       FLAC__metadata_object_vorbiscomment_insert_comment (flacenc->meta[0], comments++, commment_entry, TRUE);
466     }
467   }
468
469   FLAC__seekable_stream_encoder_set_metadata(flacenc->encoder, flacenc->meta, 1);
470 }
471
472
473
474 static void
475 gst_flacenc_chain (GstPad *pad, GstBuffer *buf)
476 {
477   FlacEnc *flacenc;
478   FLAC__int32 *data;
479   gulong insize;
480   gint samples, depth;
481   gulong i;
482   FLAC__bool res;
483
484   g_return_if_fail(buf != NULL);
485
486   flacenc = GST_FLACENC (gst_pad_get_parent (pad));
487
488   if (GST_IS_EVENT (buf)) {
489     GstEvent *event = GST_EVENT (buf);
490
491     switch (GST_EVENT_TYPE (event)) {
492       case GST_EVENT_EOS:
493         FLAC__seekable_stream_encoder_finish(flacenc->encoder);
494       default:
495         gst_pad_event_default (pad, event);
496         break;
497     }
498     return;
499   }
500
501   if (!flacenc->negotiated) {
502     gst_element_error (GST_ELEMENT (flacenc),
503                     "format not negotiated");
504     return;
505   }
506
507   depth = flacenc->depth;
508
509   insize = GST_BUFFER_SIZE (buf);
510   samples = insize / ((depth+7)>>3);
511
512   if (FLAC__seekable_stream_encoder_get_state (flacenc->encoder) == 
513                   FLAC__SEEKABLE_STREAM_ENCODER_UNINITIALIZED) 
514   {
515     FLAC__SeekableStreamEncoderState state;
516
517     gst_flacenc_set_metadata (flacenc, flacenc->metadata);
518     state = FLAC__seekable_stream_encoder_init (flacenc->encoder);
519     if (state != FLAC__STREAM_ENCODER_OK) {
520       gst_element_error (GST_ELEMENT (flacenc),
521                          "could not initialize encoder (wrong parameters?)");
522       return;
523     }
524   }
525
526   /* we keep a pointer in the flacenc struct because we are freeing the data
527    * after a push opreration that might never return */
528   data = flacenc->data = g_malloc (samples * sizeof (FLAC__int32));
529     
530   if (depth == 8) {
531     gint8 *indata = (gint8 *) GST_BUFFER_DATA (buf);
532     
533
534     for (i=0; i<samples; i++) {
535       data[i] = (FLAC__int32) *indata++;
536     }
537   }
538   else if (depth == 16) {
539     gint16 *indata = (gint16 *) GST_BUFFER_DATA (buf);
540
541     for (i=0; i<samples; i++) {
542       data[i] = (FLAC__int32) *indata++;
543     }
544   }
545
546   gst_buffer_unref(buf);
547
548   res = FLAC__seekable_stream_encoder_process_interleaved (flacenc->encoder, 
549                               (const FLAC__int32 *) data, samples / flacenc->channels);
550
551   g_free (flacenc->data);
552   flacenc->data = NULL;
553
554   if (!res) {
555     gst_element_error (GST_ELEMENT (flacenc),
556                          "encoding error");
557   }
558 }
559
560 static void
561 gst_flacenc_set_property (GObject *object, guint prop_id,
562                           const GValue *value, GParamSpec *pspec)
563 {
564   FlacEnc *this;
565   
566   this = (FlacEnc *)object;
567   switch (prop_id) {
568     case ARG_QUALITY:
569       gst_flacenc_update_quality (this, g_value_get_enum (value));
570       break;
571     case ARG_STREAMABLE_SUBSET:
572       FLAC__seekable_stream_encoder_set_streamable_subset (this->encoder, 
573                            g_value_get_boolean (value));
574       break;
575     case ARG_MID_SIDE_STEREO:
576       FLAC__seekable_stream_encoder_set_do_mid_side_stereo (this->encoder, 
577                            g_value_get_boolean (value));
578       break;
579     case ARG_LOOSE_MID_SIDE_STEREO:
580       FLAC__seekable_stream_encoder_set_loose_mid_side_stereo (this->encoder, 
581                            g_value_get_boolean (value));
582       break;
583     case ARG_BLOCKSIZE:
584       FLAC__seekable_stream_encoder_set_blocksize (this->encoder, 
585                            g_value_get_uint (value));
586       break;
587     case ARG_MAX_LPC_ORDER:
588       FLAC__seekable_stream_encoder_set_max_lpc_order (this->encoder, 
589                            g_value_get_uint (value));
590       break;
591     case ARG_QLP_COEFF_PRECISION:
592       FLAC__seekable_stream_encoder_set_qlp_coeff_precision (this->encoder, 
593                            g_value_get_uint (value));
594       break;
595     case ARG_QLP_COEFF_PREC_SEARCH:
596       FLAC__seekable_stream_encoder_set_do_qlp_coeff_prec_search (this->encoder, 
597                            g_value_get_boolean (value));
598       break;
599     case ARG_ESCAPE_CODING:
600       FLAC__seekable_stream_encoder_set_do_escape_coding (this->encoder, 
601                            g_value_get_boolean (value));
602       break;
603     case ARG_EXHAUSTIVE_MODEL_SEARCH:
604       FLAC__seekable_stream_encoder_set_do_exhaustive_model_search (this->encoder, 
605                            g_value_get_boolean (value));
606       break;
607     case ARG_MIN_RESIDUAL_PARTITION_ORDER:
608       FLAC__seekable_stream_encoder_set_min_residual_partition_order (this->encoder, 
609                            g_value_get_uint (value));
610       break;
611     case ARG_MAX_RESIDUAL_PARTITION_ORDER:
612       FLAC__seekable_stream_encoder_set_max_residual_partition_order (this->encoder, 
613                            g_value_get_uint (value));
614       break;
615     case ARG_RICE_PARAMETER_SEARCH_DIST:
616       FLAC__seekable_stream_encoder_set_rice_parameter_search_dist (this->encoder, 
617                                                  g_value_get_uint (value));
618       break;
619     default:
620       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
621       return;
622   }
623 }
624
625 static void
626 gst_flacenc_get_property (GObject *object, guint prop_id, 
627                           GValue *value, GParamSpec *pspec)
628 {
629   FlacEnc *this;
630   
631   this = (FlacEnc *)object;
632   
633   switch (prop_id) {
634     case ARG_QUALITY:
635       g_value_set_enum (value, this->quality);
636       break;
637     case ARG_STREAMABLE_SUBSET:
638       g_value_set_boolean (value, 
639               FLAC__seekable_stream_encoder_get_streamable_subset (this->encoder));
640       break;
641     case ARG_MID_SIDE_STEREO:
642       g_value_set_boolean (value, 
643               FLAC__seekable_stream_encoder_get_do_mid_side_stereo (this->encoder));
644       break;
645     case ARG_LOOSE_MID_SIDE_STEREO:
646       g_value_set_boolean (value, 
647               FLAC__seekable_stream_encoder_get_loose_mid_side_stereo (this->encoder));
648       break;
649     case ARG_BLOCKSIZE:
650       g_value_set_uint (value, 
651               FLAC__seekable_stream_encoder_get_blocksize (this->encoder));
652       break;
653     case ARG_MAX_LPC_ORDER:
654       g_value_set_uint (value, 
655               FLAC__seekable_stream_encoder_get_max_lpc_order (this->encoder));
656       break;
657     case ARG_QLP_COEFF_PRECISION:
658       g_value_set_uint (value, 
659               FLAC__seekable_stream_encoder_get_qlp_coeff_precision (this->encoder));
660       break;
661     case ARG_QLP_COEFF_PREC_SEARCH:
662       g_value_set_boolean (value, 
663               FLAC__seekable_stream_encoder_get_do_qlp_coeff_prec_search (this->encoder));
664       break;
665     case ARG_ESCAPE_CODING:
666       g_value_set_boolean (value, 
667               FLAC__seekable_stream_encoder_get_do_escape_coding (this->encoder));
668       break;
669     case ARG_EXHAUSTIVE_MODEL_SEARCH:
670       g_value_set_boolean (value, 
671               FLAC__seekable_stream_encoder_get_do_exhaustive_model_search (this->encoder));
672       break;
673     case ARG_MIN_RESIDUAL_PARTITION_ORDER:
674       g_value_set_uint (value, 
675               FLAC__seekable_stream_encoder_get_min_residual_partition_order (this->encoder));
676       break;
677     case ARG_MAX_RESIDUAL_PARTITION_ORDER:
678       g_value_set_uint (value, 
679               FLAC__seekable_stream_encoder_get_max_residual_partition_order (this->encoder));
680       break;
681     case ARG_RICE_PARAMETER_SEARCH_DIST:
682       g_value_set_uint (value, 
683               FLAC__seekable_stream_encoder_get_rice_parameter_search_dist (this->encoder));
684       break;
685     default:
686       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
687       break;
688   }
689 }
690
691 static GstElementStateReturn
692 gst_flacenc_change_state (GstElement *element)
693 {
694   FlacEnc *flacenc = GST_FLACENC (element);
695
696   switch (GST_STATE_TRANSITION (element)) {
697     case GST_STATE_NULL_TO_READY:
698     case GST_STATE_READY_TO_PAUSED:
699       flacenc->first = TRUE;
700       flacenc->stopped = FALSE;
701       break;
702     case GST_STATE_PAUSED_TO_PLAYING:
703     case GST_STATE_PLAYING_TO_PAUSED:
704       break;
705     case GST_STATE_PAUSED_TO_READY:
706       if (FLAC__seekable_stream_encoder_get_state (flacenc->encoder) != 
707                         FLAC__STREAM_ENCODER_UNINITIALIZED) {
708         flacenc->stopped = TRUE;
709         FLAC__seekable_stream_encoder_finish (flacenc->encoder);
710       }
711       flacenc->negotiated = FALSE;
712       if (flacenc->first_buf)
713         gst_buffer_unref (flacenc->first_buf);
714       flacenc->first_buf = NULL;
715       g_free (flacenc->data);
716       flacenc->data = NULL;
717       FLAC__metadata_object_delete (flacenc->meta[0]);
718       g_free (flacenc->meta);
719       break;
720     case GST_STATE_READY_TO_NULL:
721     default:
722       break;
723   }
724
725   if (GST_ELEMENT_CLASS (parent_class)->change_state)
726     return GST_ELEMENT_CLASS (parent_class)->change_state (element);
727
728   return GST_STATE_SUCCESS;
729 }