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