Added Lots of properties
[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   ARG_QUALITY,
47   ARG_STREAMABLE_SUBSET,
48   ARG_MID_SIDE_STEREO,
49   ARG_LOOSE_MID_SIDE_STEREO,
50   ARG_BLOCKSIZE,
51   ARG_MAX_LPC_ORDER,
52   ARG_QLP_COEFF_PRECISION,
53   ARG_QLP_COEFF_PREC_SEARCH,
54   ARG_ESCAPE_CODING,
55   ARG_EXHAUSTIVE_MODEL_SEARCH,
56   ARG_MIN_RESIDUAL_PARTITION_ORDER,
57   ARG_MAX_RESIDUAL_PARTITION_ORDER,
58   ARG_RICE_PARAMETER_SEARCH_DIST,
59 };
60
61 static void             gst_flacenc_init                (FlacEnc *flacenc);
62 static void             gst_flacenc_class_init          (FlacEncClass *klass);
63 static void             gst_flacenc_dispose             (GObject *object);
64
65 static GstPadConnectReturn
66                         gst_flacenc_sinkconnect         (GstPad *pad, GstCaps *caps);
67 static void             gst_flacenc_chain               (GstPad *pad, GstBuffer *buf);
68
69 static gboolean         gst_flacenc_update_quality      (FlacEnc *flacenc, gint quality);
70 static void             gst_flacenc_set_property        (GObject *object, guint prop_id, 
71                                                          const GValue *value, GParamSpec *pspec);
72 static void             gst_flacenc_get_property        (GObject *object, guint prop_id,
73                                                          GValue *value, GParamSpec *pspec);
74 static GstElementStateReturn
75                         gst_flacenc_change_state        (GstElement *element);
76
77 static FLAC__StreamEncoderWriteStatus 
78                         gst_flacenc_write_callback      (const FLAC__StreamEncoder *encoder, 
79                                                          const FLAC__byte buffer[], unsigned bytes, 
80                                                          unsigned samples, unsigned current_frame, 
81                                                          void *client_data);
82 static void             gst_flacenc_metadata_callback   (const FLAC__StreamEncoder *encoder, 
83                                                          const FLAC__StreamMetadata *metadata, 
84                                                          void *client_data);
85
86 static GstElementClass *parent_class = NULL;
87 /*static guint gst_flacenc_signals[LAST_SIGNAL] = { 0 }; */
88
89 GType
90 flacenc_get_type (void)
91 {
92   static GType flacenc_type = 0;
93
94   if (!flacenc_type) {
95     static const GTypeInfo flacenc_info = {
96       sizeof(FlacEncClass),
97       NULL,
98       NULL,
99       (GClassInitFunc)gst_flacenc_class_init,
100       NULL,
101       NULL,
102       sizeof(FlacEnc),
103       0,
104       (GInstanceInitFunc)gst_flacenc_init,
105     };
106     flacenc_type = g_type_register_static (GST_TYPE_ELEMENT, "FlacEnc", &flacenc_info, 0);
107   }
108   return flacenc_type;
109 }
110
111 typedef struct {
112   gboolean      exhaustive_model_search;
113   gboolean      escape_coding;
114   gboolean      mid_side;
115   gboolean      loose_mid_side;
116   guint         qlp_coeff_precision;
117   gboolean      qlp_coeff_prec_search;
118   guint         min_residual_partition_order;
119   guint         max_residual_partition_order;
120   guint         rice_parameter_search_dist;
121   guint         max_lpc_order;
122   guint         blocksize;
123 } FlacEncParams;
124
125 static const FlacEncParams flacenc_params[] = 
126 {
127   { FALSE, FALSE, FALSE, FALSE, 0, FALSE, 2, 2,  0, 0,  1152 },
128   { FALSE, FALSE, TRUE,  TRUE,  0, FALSE, 2, 2,  0, 0,  1152 },
129   { FALSE, FALSE, TRUE,  FALSE, 0, FALSE, 0, 3,  0, 0,  1152 },
130   { FALSE, FALSE, FALSE, FALSE, 0, FALSE, 3, 3,  0, 6,  4608 },
131   { FALSE, FALSE, TRUE,  TRUE,  0, FALSE, 3, 3,  0, 8,  4608 },
132   { FALSE, FALSE, TRUE,  FALSE, 0, FALSE, 3, 3,  0, 8,  4608 },
133   { FALSE, FALSE, TRUE,  FALSE, 0, FALSE, 0, 4,  0, 8,  4608 },
134   { TRUE,  FALSE, TRUE,  FALSE, 0, FALSE, 0, 6,  0, 8,  4608 },
135   { TRUE,  FALSE, TRUE,  FALSE, 0, FALSE, 0, 6,  0, 12, 4608 },
136   { TRUE,  TRUE,  TRUE,  FALSE, 0, FALSE, 0, 16, 0, 32, 4608 },
137 };
138
139 #define DEFAULT_QUALITY 5
140
141 #define GST_TYPE_FLACENC_QUALITY (gst_flacenc_quality_get_type ())
142 GType
143 gst_flacenc_quality_get_type (void)
144 {
145   static GType qtype = 0;
146   if (qtype == 0) {
147     static const GEnumValue values[] = {
148       { 0, "0", "0 - Fastest compression" },
149       { 1, "1", "1" },
150       { 2, "2", "2" },
151       { 3, "3", "3" },
152       { 4, "4", "4" },
153       { 5, "5", "5 - Default" },
154       { 6, "6", "6" },
155       { 7, "7", "7" },
156       { 8, "8", "8 - Highest compression " },
157       { 9, "9", "9 - Insane" },
158       { 0, NULL, NULL }
159     };
160     qtype = g_enum_register_static ("FlacEncQuality", values);
161   }
162   return qtype;
163 }
164
165 static void
166 gst_flacenc_class_init (FlacEncClass *klass)
167 {
168   GObjectClass *gobject_class;
169   GstElementClass *gstelement_class;
170
171   gobject_class = (GObjectClass*)klass;
172   gstelement_class = (GstElementClass*)klass;
173
174   parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
175   
176   /* we have no properties atm so this is a bit silly */
177   gobject_class->set_property = gst_flacenc_set_property;
178   gobject_class->get_property = gst_flacenc_get_property;
179   gobject_class->dispose      = gst_flacenc_dispose;
180
181   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_QUALITY,
182     g_param_spec_enum ("quality", 
183                        "Quality", 
184                        "Speed versus compression tradeoff",
185                        GST_TYPE_FLACENC_QUALITY, DEFAULT_QUALITY, G_PARAM_READWRITE));
186   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_STREAMABLE_SUBSET,
187     g_param_spec_boolean ("streamable_subset", 
188                           "Streamable subset", 
189                           "true to limit encoder to generating a Subset stream, else false",
190                           TRUE, G_PARAM_READWRITE));
191   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MID_SIDE_STEREO,
192     g_param_spec_boolean ("mid_side_stereo", 
193                           "Do mid side stereo", 
194                           "Do mid side stereo (only for stereo input)", 
195                           flacenc_params[DEFAULT_QUALITY].mid_side, G_PARAM_READWRITE));
196   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LOOSE_MID_SIDE_STEREO,
197     g_param_spec_boolean ("loose_mid_side_stereo", 
198                           "Loose mid side stereo", 
199                           "Loose mid side stereo", 
200                           flacenc_params[DEFAULT_QUALITY].loose_mid_side, G_PARAM_READWRITE));
201   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BLOCKSIZE,
202     g_param_spec_uint ("blocksize", 
203                        "Blocksize", 
204                        "Blocksize in samples",
205                        FLAC__MIN_BLOCK_SIZE, FLAC__MAX_BLOCK_SIZE, 
206                        flacenc_params[DEFAULT_QUALITY].blocksize, G_PARAM_READWRITE));
207   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MAX_LPC_ORDER,
208     g_param_spec_uint ("max_lpc_order", 
209                        "Max LPC order", 
210                        "Max LPC order; 0 => use only fixed predictors",
211                        0, FLAC__MAX_LPC_ORDER,
212                        flacenc_params[DEFAULT_QUALITY].max_lpc_order, G_PARAM_READWRITE));
213   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_QLP_COEFF_PRECISION,
214     g_param_spec_uint ("qlp_coeff_precision", 
215                        "QLP coefficients precision", 
216                        "Precision in bits of quantized linear-predictor coefficients; 0 = automatic",
217                        0, 32, 
218                        flacenc_params[DEFAULT_QUALITY].qlp_coeff_precision, G_PARAM_READWRITE));
219   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_QLP_COEFF_PREC_SEARCH,
220     g_param_spec_boolean ("qlp_coeff_prec_search", 
221                           "Do QLP coefficients precision search", 
222                           "false = use qlp_coeff_precision, "
223                             "true = search around qlp_coeff_precision, take best", 
224                           flacenc_params[DEFAULT_QUALITY].qlp_coeff_prec_search, G_PARAM_READWRITE));
225   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_ESCAPE_CODING,
226     g_param_spec_boolean ("escape_coding", 
227                           "Do Escape coding", 
228                           "search for escape codes in the entropy coding stage "
229                             "for slightly better compression", 
230                           flacenc_params[DEFAULT_QUALITY].escape_coding, G_PARAM_READWRITE));
231   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_EXHAUSTIVE_MODEL_SEARCH,
232     g_param_spec_boolean ("exhaustive_model_search", 
233                           "Do exhaustive model search", 
234                           "do exhaustive search of LP coefficient quantization (expensive!)",
235                           flacenc_params[DEFAULT_QUALITY].exhaustive_model_search, G_PARAM_READWRITE));
236   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MIN_RESIDUAL_PARTITION_ORDER,
237     g_param_spec_uint ("min_residual_partition_order", 
238                        "Min residual partition order", 
239                        "Min residual partition order (above 4 doesn't usually help much)",
240                        0, 16, 
241                        flacenc_params[DEFAULT_QUALITY].min_residual_partition_order, G_PARAM_READWRITE));
242   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MAX_RESIDUAL_PARTITION_ORDER,
243     g_param_spec_uint ("max_residual_partition_order", 
244                        "Max residual partition order", 
245                        "Max residual partition order (above 4 doesn't usually help much)",
246                        0, 16, 
247                        flacenc_params[DEFAULT_QUALITY].max_residual_partition_order, G_PARAM_READWRITE));
248   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_RICE_PARAMETER_SEARCH_DIST,
249     g_param_spec_uint ("rice_parameter_search_dist", 
250                        "rice_parameter_search_dist", 
251                        "0 = try only calc'd parameter k; else try all [k-dist..k+dist] "
252                          "parameters, use best",
253                        0, FLAC__MAX_RICE_PARTITION_ORDER, 
254                        flacenc_params[DEFAULT_QUALITY].rice_parameter_search_dist, G_PARAM_READWRITE));
255
256   gstelement_class->change_state = gst_flacenc_change_state;
257 }
258
259 static void
260 gst_flacenc_init (FlacEnc *flacenc)
261 {
262   flacenc->sinkpad = gst_pad_new_from_template (gst_flacenc_sink_template, "sink");
263   gst_element_add_pad(GST_ELEMENT(flacenc),flacenc->sinkpad);
264   gst_pad_set_chain_function(flacenc->sinkpad,gst_flacenc_chain);
265   gst_pad_set_connect_function (flacenc->sinkpad, gst_flacenc_sinkconnect);
266
267   flacenc->srcpad = gst_pad_new_from_template (gst_flacenc_src_template, "src");
268   gst_element_add_pad(GST_ELEMENT(flacenc),flacenc->srcpad);
269
270   GST_FLAG_SET (flacenc, GST_ELEMENT_EVENT_AWARE);
271
272   flacenc->encoder = FLAC__stream_encoder_new();
273
274   FLAC__stream_encoder_set_write_callback (flacenc->encoder, 
275                                         gst_flacenc_write_callback);
276   FLAC__stream_encoder_set_metadata_callback (flacenc->encoder, 
277                                         gst_flacenc_metadata_callback);
278   FLAC__stream_encoder_set_client_data (flacenc->encoder, 
279                                         flacenc);
280
281   flacenc->negotiated = FALSE;
282   flacenc->first = TRUE;
283   flacenc->first_buf = NULL;
284   flacenc->data = NULL;
285   gst_flacenc_update_quality (flacenc, DEFAULT_QUALITY);
286 }
287
288 static void
289 gst_flacenc_dispose (GObject *object)
290 {
291   FlacEnc *flacenc = GST_FLACENC (object);
292
293   FLAC__stream_encoder_delete (flacenc->encoder);
294   
295   G_OBJECT_CLASS (parent_class)->dispose (object);
296 }
297
298 static GstPadConnectReturn
299 gst_flacenc_sinkconnect (GstPad *pad, GstCaps *caps)
300 {
301   FlacEnc *flacenc;
302
303   flacenc = GST_FLACENC (gst_pad_get_parent (pad));
304
305   if (!GST_CAPS_IS_FIXED (caps))
306     return GST_PAD_CONNECT_DELAYED;
307
308   gst_caps_get_int (caps, "channels", &flacenc->channels);
309   gst_caps_get_int (caps, "depth", &flacenc->depth);
310   gst_caps_get_int (caps, "rate", &flacenc->sample_rate);
311
312   FLAC__stream_encoder_set_bits_per_sample (flacenc->encoder, 
313                                             flacenc->depth);
314   FLAC__stream_encoder_set_sample_rate (flacenc->encoder, 
315                                         flacenc->sample_rate);
316   FLAC__stream_encoder_set_channels (flacenc->encoder, 
317                                      flacenc->channels);
318
319   flacenc->negotiated = TRUE;
320
321   return GST_PAD_CONNECT_OK;
322 }
323
324 static gboolean
325 gst_flacenc_update_quality (FlacEnc *flacenc, gint quality)
326 {
327   flacenc->quality = quality;
328
329 #define DO_UPDATE(name, val, str)                               \
330 G_STMT_START{                                                   \
331   if (FLAC__stream_encoder_get_##name (flacenc->encoder) !=     \
332       flacenc_params[quality].##val) {                          \
333     FLAC__stream_encoder_set_##name (flacenc->encoder,          \
334       flacenc_params[quality].##val);                           \
335     g_object_notify (G_OBJECT (flacenc), str);                  \
336   };                                                            \
337 } G_STMT_END
338
339   g_object_freeze_notify (G_OBJECT (flacenc));
340
341   DO_UPDATE (do_mid_side_stereo,           mid_side,                     "mid_side_stereo");
342   DO_UPDATE (loose_mid_side_stereo,        loose_mid_side,               "loose_mid_side");
343   DO_UPDATE (blocksize,                    blocksize,                    "blocksize");
344   DO_UPDATE (max_lpc_order,                max_lpc_order,                "max_lpc_order");
345   DO_UPDATE (qlp_coeff_precision,          qlp_coeff_precision,          "qlp_coeff_precision");
346   DO_UPDATE (do_qlp_coeff_prec_search,     qlp_coeff_prec_search,        "qlp_coeff_prec_search");
347   DO_UPDATE (do_escape_coding,             escape_coding,                "escape_coding");
348   DO_UPDATE (do_exhaustive_model_search,   exhaustive_model_search,      "exhaustive_model_search");
349   DO_UPDATE (min_residual_partition_order, min_residual_partition_order, "min_residual_partition_order");
350   DO_UPDATE (max_residual_partition_order, max_residual_partition_order, "max_residual_partition_order");
351   DO_UPDATE (rice_parameter_search_dist,   rice_parameter_search_dist,   "rice_parameter_search_dist");
352
353 #undef DO_UPDATE
354
355   g_object_thaw_notify (G_OBJECT (flacenc));
356
357   return TRUE;
358 }
359
360 static FLAC__StreamEncoderWriteStatus 
361 gst_flacenc_write_callback (const FLAC__StreamEncoder *encoder, 
362                             const FLAC__byte buffer[], unsigned bytes, 
363                             unsigned samples, unsigned current_frame, 
364                             void *client_data)
365 {
366   FlacEnc *flacenc;
367   GstBuffer *outbuf;
368
369   flacenc = GST_FLACENC (client_data);
370
371   if (flacenc->stopped) 
372     return FLAC__STREAM_ENCODER_WRITE_OK;
373
374   outbuf = gst_buffer_new_and_alloc (bytes);
375
376   memcpy (GST_BUFFER_DATA (outbuf), buffer, bytes);
377
378   if (flacenc->first) {
379     flacenc->first_buf = outbuf;
380     gst_buffer_ref (outbuf);
381     flacenc->first = FALSE;
382   }
383
384   gst_pad_push (flacenc->srcpad, outbuf);
385
386   return FLAC__STREAM_ENCODER_WRITE_OK;
387 }
388
389 static void 
390 gst_flacenc_metadata_callback (const FLAC__StreamEncoder *encoder, 
391                                const FLAC__StreamMetadata *metadata, 
392                                void *client_data)
393 {
394   GstEvent *event;
395   FlacEnc *flacenc;
396
397   flacenc = GST_FLACENC (client_data);
398
399   if (flacenc->stopped) 
400     return;
401
402   event = gst_event_new_discontinuous (FALSE, GST_FORMAT_BYTES, 0, NULL);
403   gst_pad_push (flacenc->srcpad, GST_BUFFER (event));
404
405   if (flacenc->first_buf) {
406     const FLAC__uint64 samples = metadata->data.stream_info.total_samples;
407     const unsigned min_framesize = metadata->data.stream_info.min_framesize;
408     const unsigned max_framesize = metadata->data.stream_info.max_framesize;
409
410     guint8 *data = GST_BUFFER_DATA (flacenc->first_buf);
411     GstBuffer *outbuf = flacenc->first_buf;
412
413     /* this looks evil but is actually how one is supposed to write
414      * the stream stats according to the FLAC examples */
415
416     memcpy (&data[26], metadata->data.stream_info.md5sum, 16);
417     
418     data[21] = (data[21] & 0xf0) | 
419                (FLAC__byte)((samples >> 32) & 0x0f);
420     data[22] = (FLAC__byte)((samples >> 24) & 0xff);
421     data[23] = (FLAC__byte)((samples >> 16) & 0xff);
422     data[24] = (FLAC__byte)((samples >> 8 ) & 0xff);
423     data[25] = (FLAC__byte)((samples      ) & 0xff);
424
425     data[12] = (FLAC__byte)((min_framesize >> 16) & 0xFF);
426     data[13] = (FLAC__byte)((min_framesize >> 8 ) & 0xFF);
427     data[14] = (FLAC__byte)((min_framesize      ) & 0xFF);
428
429     data[15] = (FLAC__byte)((max_framesize >> 16) & 0xFF);
430     data[16] = (FLAC__byte)((max_framesize >> 8 ) & 0xFF);
431     data[17] = (FLAC__byte)((max_framesize      ) & 0xFF);
432
433     flacenc->first_buf = NULL;
434     gst_pad_push (flacenc->srcpad, outbuf);
435   }
436 }
437
438 static void
439 gst_flacenc_chain (GstPad *pad, GstBuffer *buf)
440 {
441   FlacEnc *flacenc;
442   FLAC__int32 *data;
443   gulong insize;
444   gint samples, depth;
445   gulong i;
446   FLAC__bool res;
447
448   g_return_if_fail(buf != NULL);
449
450   flacenc = GST_FLACENC (gst_pad_get_parent (pad));
451
452   if (GST_IS_EVENT (buf)) {
453     GstEvent *event = GST_EVENT (buf);
454
455     switch (GST_EVENT_TYPE (event)) {
456       case GST_EVENT_EOS:
457         FLAC__stream_encoder_finish(flacenc->encoder);
458       default:
459         gst_pad_event_default (pad, event);
460         break;
461     }
462     return;
463   }
464
465   if (!flacenc->negotiated) {
466     gst_element_error (GST_ELEMENT (flacenc),
467                     "format not negotiated");
468     return;
469   }
470
471   depth = flacenc->depth;
472
473   insize = GST_BUFFER_SIZE (buf);
474   samples = insize / ((depth+7)>>3);
475
476   if (FLAC__stream_encoder_get_state (flacenc->encoder) == 
477                   FLAC__STREAM_ENCODER_UNINITIALIZED) 
478   {
479     FLAC__StreamEncoderState state;
480
481     state = FLAC__stream_encoder_init (flacenc->encoder);
482     if (state != FLAC__STREAM_ENCODER_OK) {
483       gst_element_error (GST_ELEMENT (flacenc),
484                          "could not initialize encoder (wrong parameters?)");
485       return;
486     }
487   }
488
489   /* we keep a pointer in the flacenc struct because we are freeing the data
490    * after a push opreration that might never return */
491   data = flacenc->data = g_malloc (samples * sizeof (FLAC__int32));
492     
493   if (depth == 8) {
494     gint8 *indata = (gint8 *) GST_BUFFER_DATA (buf);
495     
496     for (i=0; i<samples; i++) {
497       data[i] = (FLAC__int32) *indata++;
498     }
499   }
500   else if (depth == 16) {
501     gint16 *indata = (gint16 *) GST_BUFFER_DATA (buf);
502
503     for (i=0; i<samples; i++) {
504       data[i] = (FLAC__int32) *indata++;
505     }
506   }
507
508   gst_buffer_unref(buf);
509
510   res = FLAC__stream_encoder_process_interleaved (flacenc->encoder, 
511                               (const FLAC__int32 *) data, samples / flacenc->channels);
512
513   g_free (flacenc->data);
514   flacenc->data = NULL;
515
516   if (!res) {
517     gst_element_error (GST_ELEMENT (flacenc),
518                          "encoding error");
519   }
520 }
521
522 static void
523 gst_flacenc_set_property (GObject *object, guint prop_id,
524                           const GValue *value, GParamSpec *pspec)
525 {
526   FlacEnc *this;
527   
528   this = (FlacEnc *)object;
529   switch (prop_id) {
530     case ARG_QUALITY:
531       gst_flacenc_update_quality (this, g_value_get_enum (value));
532       break;
533     case ARG_STREAMABLE_SUBSET:
534       FLAC__stream_encoder_set_streamable_subset (this->encoder, 
535                            g_value_get_boolean (value));
536       break;
537     case ARG_MID_SIDE_STEREO:
538       FLAC__stream_encoder_set_do_mid_side_stereo (this->encoder, 
539                            g_value_get_boolean (value));
540       break;
541     case ARG_LOOSE_MID_SIDE_STEREO:
542       FLAC__stream_encoder_set_loose_mid_side_stereo (this->encoder, 
543                            g_value_get_boolean (value));
544       break;
545     case ARG_BLOCKSIZE:
546       FLAC__stream_encoder_set_blocksize (this->encoder, 
547                            g_value_get_uint (value));
548       break;
549     case ARG_MAX_LPC_ORDER:
550       FLAC__stream_encoder_set_max_lpc_order (this->encoder, 
551                            g_value_get_uint (value));
552       break;
553     case ARG_QLP_COEFF_PRECISION:
554       FLAC__stream_encoder_set_qlp_coeff_precision (this->encoder, 
555                            g_value_get_uint (value));
556       break;
557     case ARG_QLP_COEFF_PREC_SEARCH:
558       FLAC__stream_encoder_set_do_qlp_coeff_prec_search (this->encoder, 
559                            g_value_get_boolean (value));
560       break;
561     case ARG_ESCAPE_CODING:
562       FLAC__stream_encoder_set_do_escape_coding (this->encoder, 
563                            g_value_get_boolean (value));
564       break;
565     case ARG_EXHAUSTIVE_MODEL_SEARCH:
566       FLAC__stream_encoder_set_do_exhaustive_model_search (this->encoder, 
567                            g_value_get_boolean (value));
568       break;
569     case ARG_MIN_RESIDUAL_PARTITION_ORDER:
570       FLAC__stream_encoder_set_min_residual_partition_order (this->encoder, 
571                            g_value_get_uint (value));
572       break;
573     case ARG_MAX_RESIDUAL_PARTITION_ORDER:
574       FLAC__stream_encoder_set_max_residual_partition_order (this->encoder, 
575                            g_value_get_uint (value));
576       break;
577     case ARG_RICE_PARAMETER_SEARCH_DIST:
578       FLAC__stream_encoder_set_rice_parameter_search_dist (this->encoder, 
579                                                  g_value_get_uint (value));
580       break;
581     default:
582       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
583       return;
584   }
585 }
586
587 static void
588 gst_flacenc_get_property (GObject *object, guint prop_id, 
589                           GValue *value, GParamSpec *pspec)
590 {
591   FlacEnc *this;
592   
593   this = (FlacEnc *)object;
594   
595   switch (prop_id) {
596     case ARG_QUALITY:
597       g_value_set_enum (value, this->quality);
598       break;
599     case ARG_STREAMABLE_SUBSET:
600       g_value_set_boolean (value, 
601               FLAC__stream_encoder_get_streamable_subset (this->encoder));
602       break;
603     case ARG_MID_SIDE_STEREO:
604       g_value_set_boolean (value, 
605               FLAC__stream_encoder_get_do_mid_side_stereo (this->encoder));
606       break;
607     case ARG_LOOSE_MID_SIDE_STEREO:
608       g_value_set_boolean (value, 
609               FLAC__stream_encoder_get_loose_mid_side_stereo (this->encoder));
610       break;
611     case ARG_BLOCKSIZE:
612       g_value_set_uint (value, 
613               FLAC__stream_encoder_get_blocksize (this->encoder));
614       break;
615     case ARG_MAX_LPC_ORDER:
616       g_value_set_uint (value, 
617               FLAC__stream_encoder_get_max_lpc_order (this->encoder));
618       break;
619     case ARG_QLP_COEFF_PRECISION:
620       g_value_set_uint (value, 
621               FLAC__stream_encoder_get_qlp_coeff_precision (this->encoder));
622       break;
623     case ARG_QLP_COEFF_PREC_SEARCH:
624       g_value_set_boolean (value, 
625               FLAC__stream_encoder_get_do_qlp_coeff_prec_search (this->encoder));
626       break;
627     case ARG_ESCAPE_CODING:
628       g_value_set_boolean (value, 
629               FLAC__stream_encoder_get_do_escape_coding (this->encoder));
630       break;
631     case ARG_EXHAUSTIVE_MODEL_SEARCH:
632       g_value_set_boolean (value, 
633               FLAC__stream_encoder_get_do_exhaustive_model_search (this->encoder));
634       break;
635     case ARG_MIN_RESIDUAL_PARTITION_ORDER:
636       g_value_set_uint (value, 
637               FLAC__stream_encoder_get_min_residual_partition_order (this->encoder));
638       break;
639     case ARG_MAX_RESIDUAL_PARTITION_ORDER:
640       g_value_set_uint (value, 
641               FLAC__stream_encoder_get_max_residual_partition_order (this->encoder));
642       break;
643     case ARG_RICE_PARAMETER_SEARCH_DIST:
644       g_value_set_uint (value, 
645               FLAC__stream_encoder_get_rice_parameter_search_dist (this->encoder));
646       break;
647     default:
648       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
649       break;
650   }
651 }
652
653 static GstElementStateReturn
654 gst_flacenc_change_state (GstElement *element)
655 {
656   FlacEnc *flacenc = GST_FLACENC (element);
657
658   switch (GST_STATE_TRANSITION (element)) {
659     case GST_STATE_NULL_TO_READY:
660     case GST_STATE_READY_TO_PAUSED:
661       flacenc->first = TRUE;
662       flacenc->stopped = FALSE;
663       break;
664     case GST_STATE_PAUSED_TO_PLAYING:
665     case GST_STATE_PLAYING_TO_PAUSED:
666       break;
667     case GST_STATE_PAUSED_TO_READY:
668       if (FLAC__stream_encoder_get_state (flacenc->encoder) != 
669                         FLAC__STREAM_ENCODER_UNINITIALIZED) {
670         flacenc->stopped = TRUE;
671         FLAC__stream_encoder_finish (flacenc->encoder);
672       }
673       flacenc->negotiated = FALSE;
674       if (flacenc->first_buf)
675         gst_buffer_unref (flacenc->first_buf);
676       flacenc->first_buf = NULL;
677       g_free (flacenc->data);
678       flacenc->data = NULL;
679       break;
680     case GST_STATE_READY_TO_NULL:
681     default:
682       break;
683   }
684
685   if (GST_ELEMENT_CLASS (parent_class)->change_state)
686     return GST_ELEMENT_CLASS (parent_class)->change_state (element);
687
688   return GST_STATE_SUCCESS;
689 }
690