gstflacenc.c 27.4 KB
Newer Older
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1
/* GStreamer
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */


21
22
23
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
24
25
26
27
28
#include <stdlib.h>
#include <string.h>

#include <gstflacenc.h>

29
30
#include "flac_compat.h"

31
static GstPadTemplate *src_template, *sink_template;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
32
33
34
35

/* elementfactory information */
GstElementDetails flacenc_details = {
  "FLAC encoder",
36
  "Codec/Encoder/Audio",
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
37
38
39
40
41
42
43
44
45
46
47
48
  "Encodes audio with the FLAC lossless audio encoder",
  "Wim Taymans <wim.taymans@chello.be>",
};

/* FlacEnc signals and args */
enum {
  /* FILL ME */
  LAST_SIGNAL
};

enum {
  ARG_0,
Wim Taymans's avatar
Wim Taymans committed
49
50
51
52
53
54
55
56
57
58
59
60
61
  ARG_QUALITY,
  ARG_STREAMABLE_SUBSET,
  ARG_MID_SIDE_STEREO,
  ARG_LOOSE_MID_SIDE_STEREO,
  ARG_BLOCKSIZE,
  ARG_MAX_LPC_ORDER,
  ARG_QLP_COEFF_PRECISION,
  ARG_QLP_COEFF_PREC_SEARCH,
  ARG_ESCAPE_CODING,
  ARG_EXHAUSTIVE_MODEL_SEARCH,
  ARG_MIN_RESIDUAL_PARTITION_ORDER,
  ARG_MAX_RESIDUAL_PARTITION_ORDER,
  ARG_RICE_PARAMETER_SEARCH_DIST,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
62
63
};

64
static void             gst_flacenc_base_init           (gpointer g_class);
Wim Taymans's avatar
Wim Taymans committed
65
66
67
static void		gst_flacenc_init		(FlacEnc *flacenc);
static void		gst_flacenc_class_init		(FlacEncClass *klass);
static void 		gst_flacenc_dispose 		(GObject *object);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
68

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
69
static GstPadLinkReturn
Wim Taymans's avatar
Wim Taymans committed
70
			gst_flacenc_sinkconnect 	(GstPad *pad, GstCaps *caps);
71
static void		gst_flacenc_chain		(GstPad *pad, GstData *_data);
Wim Taymans's avatar
Wim Taymans committed
72
73
74
75
76
77

static gboolean 	gst_flacenc_update_quality 	(FlacEnc *flacenc, gint quality);
static void     	gst_flacenc_set_property        (GObject *object, guint prop_id, 
							 const GValue *value, GParamSpec *pspec);
static void     	gst_flacenc_get_property        (GObject *object, guint prop_id,
							 GValue *value, GParamSpec *pspec);
Wim Taymans's avatar
Wim Taymans committed
78
static GstElementStateReturn
Wim Taymans's avatar
Wim Taymans committed
79
			gst_flacenc_change_state 	(GstElement *element);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
80
81

static FLAC__StreamEncoderWriteStatus 
82
			gst_flacenc_write_callback 	(const FLAC__SeekableStreamEncoder *encoder, 
Wim Taymans's avatar
Wim Taymans committed
83
84
85
							 const FLAC__byte buffer[], unsigned bytes, 
				    			 unsigned samples, unsigned current_frame, 
							 void *client_data);
86
87
88
static FLAC__SeekableStreamEncoderSeekStatus
			gst_flacenc_seek_callback       (const FLAC__SeekableStreamEncoder *encoder,
			                                 FLAC__uint64 absolute_byte_offset,
Wim Taymans's avatar
Wim Taymans committed
89
							 void *client_data);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
90
91

static GstElementClass *parent_class = NULL;
92
/*static guint gst_flacenc_signals[LAST_SIGNAL] = { 0 }; */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
93
94
95
96
97
98
99
100
101

GType
flacenc_get_type (void)
{
  static GType flacenc_type = 0;

  if (!flacenc_type) {
    static const GTypeInfo flacenc_info = {
      sizeof(FlacEncClass),
102
      gst_flacenc_base_init,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
103
104
105
106
107
108
109
110
111
112
113
114
115
      NULL,
      (GClassInitFunc)gst_flacenc_class_init,
      NULL,
      NULL,
      sizeof(FlacEnc),
      0,
      (GInstanceInitFunc)gst_flacenc_init,
    };
    flacenc_type = g_type_register_static (GST_TYPE_ELEMENT, "FlacEnc", &flacenc_info, 0);
  }
  return flacenc_type;
}

Wim Taymans's avatar
Wim Taymans committed
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
typedef struct {
  gboolean 	exhaustive_model_search;
  gboolean 	escape_coding;
  gboolean 	mid_side;
  gboolean 	loose_mid_side;
  guint  	qlp_coeff_precision;
  gboolean	qlp_coeff_prec_search;
  guint 	min_residual_partition_order;
  guint 	max_residual_partition_order;
  guint 	rice_parameter_search_dist;
  guint 	max_lpc_order;
  guint		blocksize;
} FlacEncParams;

static const FlacEncParams flacenc_params[] = 
{
  { FALSE, FALSE, FALSE, FALSE, 0, FALSE, 2, 2,  0, 0,  1152 },
  { FALSE, FALSE, TRUE,  TRUE,  0, FALSE, 2, 2,  0, 0,  1152 },
  { FALSE, FALSE, TRUE,  FALSE, 0, FALSE, 0, 3,  0, 0,  1152 },
  { FALSE, FALSE, FALSE, FALSE, 0, FALSE, 3, 3,  0, 6,  4608 },
  { FALSE, FALSE, TRUE,  TRUE,  0, FALSE, 3, 3,  0, 8,  4608 },
  { FALSE, FALSE, TRUE,  FALSE, 0, FALSE, 3, 3,  0, 8,  4608 },
  { FALSE, FALSE, TRUE,  FALSE, 0, FALSE, 0, 4,  0, 8,  4608 },
  { TRUE,  FALSE, TRUE,  FALSE, 0, FALSE, 0, 6,  0, 8,  4608 },
  { TRUE,  FALSE, TRUE,  FALSE, 0, FALSE, 0, 6,  0, 12, 4608 },
  { TRUE,  TRUE,  TRUE,  FALSE, 0, FALSE, 0, 16, 0, 32, 4608 },
};

#define DEFAULT_QUALITY 5

#define GST_TYPE_FLACENC_QUALITY (gst_flacenc_quality_get_type ())
GType
gst_flacenc_quality_get_type (void)
{
  static GType qtype = 0;
  if (qtype == 0) {
    static const GEnumValue values[] = {
      { 0, "0", "0 - Fastest compression" },
      { 1, "1", "1" },
      { 2, "2", "2" },
      { 3, "3", "3" },
      { 4, "4", "4" },
      { 5, "5", "5 - Default" },
      { 6, "6", "6" },
      { 7, "7", "7" },
      { 8, "8", "8 - Highest compression " },
      { 9, "9", "9 - Insane" },
      { 0, NULL, NULL }
    };
    qtype = g_enum_register_static ("FlacEncQuality", values);
  }
  return qtype;
}

170
171
172
173
174
175
176
static GstCaps*
flac_caps_factory (void)
{
  return
   gst_caps_new (
  	"flac_flac",
  	"application/x-flac",
177
	/* gst_props_new (
178
179
 	    "rate",     	GST_PROPS_INT_RANGE (11025, 48000),
    	    "channels", 	GST_PROPS_INT_RANGE (1, 2),
180
	    NULL) */NULL);
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
}

static GstCaps*
raw_caps_factory (void)
{
  return
   gst_caps_new (
  	"flac_raw",
  	"audio/x-raw-int",
	gst_props_new (
    	    "endianness", 	GST_PROPS_INT (G_BYTE_ORDER),
    	    "signed", 		GST_PROPS_BOOLEAN (TRUE),
    	    "width", 		GST_PROPS_INT (16),
    	    "depth",    	GST_PROPS_INT (16),
    	    "rate",     	GST_PROPS_INT_RANGE (11025, 48000),
    	    "channels", 	GST_PROPS_INT_RANGE (1, 2),
	    NULL));
}

static void
gst_flacenc_base_init (gpointer g_class)
{
  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
  GstCaps *raw_caps, *flac_caps;

  raw_caps = raw_caps_factory ();
  flac_caps = flac_caps_factory ();
  
  sink_template = gst_pad_template_new ("sink", GST_PAD_SINK, 
					GST_PAD_ALWAYS, 
					raw_caps, NULL);
  src_template = gst_pad_template_new ("src", GST_PAD_SRC, 
				       GST_PAD_ALWAYS, 
				       flac_caps, NULL);
  gst_element_class_add_pad_template (element_class, sink_template);
  gst_element_class_add_pad_template (element_class, src_template);
  gst_element_class_set_details (element_class, &flacenc_details);
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
220
221
222
223
224
225
226
227
228
229
230
static void
gst_flacenc_class_init (FlacEncClass *klass)
{
  GObjectClass *gobject_class;
  GstElementClass *gstelement_class;

  gobject_class = (GObjectClass*)klass;
  gstelement_class = (GstElementClass*)klass;

  parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
  
231
  /* we have no properties atm so this is a bit silly */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
232
233
  gobject_class->set_property = gst_flacenc_set_property;
  gobject_class->get_property = gst_flacenc_get_property;
Wim Taymans's avatar
Wim Taymans committed
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
  gobject_class->dispose      = gst_flacenc_dispose;

  g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_QUALITY,
    g_param_spec_enum ("quality", 
	               "Quality", 
		       "Speed versus compression tradeoff",
                       GST_TYPE_FLACENC_QUALITY, DEFAULT_QUALITY, G_PARAM_READWRITE));
  g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_STREAMABLE_SUBSET,
    g_param_spec_boolean ("streamable_subset", 
	                  "Streamable subset", 
			  "true to limit encoder to generating a Subset stream, else false",
                          TRUE, G_PARAM_READWRITE));
  g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MID_SIDE_STEREO,
    g_param_spec_boolean ("mid_side_stereo", 
	                  "Do mid side stereo", 
	                  "Do mid side stereo (only for stereo input)", 
                          flacenc_params[DEFAULT_QUALITY].mid_side, G_PARAM_READWRITE));
  g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LOOSE_MID_SIDE_STEREO,
    g_param_spec_boolean ("loose_mid_side_stereo", 
	                  "Loose mid side stereo", 
	                  "Loose mid side stereo", 
                          flacenc_params[DEFAULT_QUALITY].loose_mid_side, G_PARAM_READWRITE));
  g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BLOCKSIZE,
    g_param_spec_uint ("blocksize", 
	               "Blocksize", 
	               "Blocksize in samples",
                       FLAC__MIN_BLOCK_SIZE, FLAC__MAX_BLOCK_SIZE, 
                       flacenc_params[DEFAULT_QUALITY].blocksize, G_PARAM_READWRITE));
  g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MAX_LPC_ORDER,
    g_param_spec_uint ("max_lpc_order", 
	               "Max LPC order", 
	               "Max LPC order; 0 => use only fixed predictors",
                       0, FLAC__MAX_LPC_ORDER,
                       flacenc_params[DEFAULT_QUALITY].max_lpc_order, G_PARAM_READWRITE));
  g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_QLP_COEFF_PRECISION,
    g_param_spec_uint ("qlp_coeff_precision", 
	               "QLP coefficients precision", 
	               "Precision in bits of quantized linear-predictor coefficients; 0 = automatic",
                       0, 32, 
                       flacenc_params[DEFAULT_QUALITY].qlp_coeff_precision, G_PARAM_READWRITE));
  g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_QLP_COEFF_PREC_SEARCH,
    g_param_spec_boolean ("qlp_coeff_prec_search", 
	                  "Do QLP coefficients precision search", 
	                  "false = use qlp_coeff_precision, "
			    "true = search around qlp_coeff_precision, take best", 
                          flacenc_params[DEFAULT_QUALITY].qlp_coeff_prec_search, G_PARAM_READWRITE));
  g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_ESCAPE_CODING,
    g_param_spec_boolean ("escape_coding", 
	                  "Do Escape coding", 
	                  "search for escape codes in the entropy coding stage "
			    "for slightly better compression", 
                          flacenc_params[DEFAULT_QUALITY].escape_coding, G_PARAM_READWRITE));
  g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_EXHAUSTIVE_MODEL_SEARCH,
    g_param_spec_boolean ("exhaustive_model_search", 
	                  "Do exhaustive model search", 
	                  "do exhaustive search of LP coefficient quantization (expensive!)",
                          flacenc_params[DEFAULT_QUALITY].exhaustive_model_search, G_PARAM_READWRITE));
  g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MIN_RESIDUAL_PARTITION_ORDER,
    g_param_spec_uint ("min_residual_partition_order", 
	               "Min residual partition order", 
	               "Min residual partition order (above 4 doesn't usually help much)",
                       0, 16, 
                       flacenc_params[DEFAULT_QUALITY].min_residual_partition_order, G_PARAM_READWRITE));
  g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MAX_RESIDUAL_PARTITION_ORDER,
    g_param_spec_uint ("max_residual_partition_order", 
	               "Max residual partition order", 
	               "Max residual partition order (above 4 doesn't usually help much)",
                       0, 16, 
                       flacenc_params[DEFAULT_QUALITY].max_residual_partition_order, G_PARAM_READWRITE));
  g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_RICE_PARAMETER_SEARCH_DIST,
    g_param_spec_uint ("rice_parameter_search_dist", 
	               "rice_parameter_search_dist", 
	               "0 = try only calc'd parameter k; else try all [k-dist..k+dist] "
		         "parameters, use best",
                       0, FLAC__MAX_RICE_PARTITION_ORDER, 
                       flacenc_params[DEFAULT_QUALITY].rice_parameter_search_dist, G_PARAM_READWRITE));
Wim Taymans's avatar
Wim Taymans committed
310
311

  gstelement_class->change_state = gst_flacenc_change_state;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
312
313
}

Wim Taymans's avatar
Wim Taymans committed
314
315
316
static void
gst_flacenc_init (FlacEnc *flacenc)
{
317
  flacenc->sinkpad = gst_pad_new_from_template (sink_template, "sink");
Wim Taymans's avatar
Wim Taymans committed
318
319
  gst_element_add_pad(GST_ELEMENT(flacenc),flacenc->sinkpad);
  gst_pad_set_chain_function(flacenc->sinkpad,gst_flacenc_chain);
320
  gst_pad_set_link_function (flacenc->sinkpad, gst_flacenc_sinkconnect);
Wim Taymans's avatar
Wim Taymans committed
321

322
  flacenc->srcpad = gst_pad_new_from_template (src_template, "src");
Wim Taymans's avatar
Wim Taymans committed
323
324
325
326
  gst_element_add_pad(GST_ELEMENT(flacenc),flacenc->srcpad);

  GST_FLAG_SET (flacenc, GST_ELEMENT_EVENT_AWARE);

327
  flacenc->encoder = FLAC__seekable_stream_encoder_new();
Wim Taymans's avatar
Wim Taymans committed
328

329
  FLAC__seekable_stream_encoder_set_write_callback (flacenc->encoder, 
Wim Taymans's avatar
Wim Taymans committed
330
		                        gst_flacenc_write_callback);
331
332
333
334
  FLAC__seekable_stream_encoder_set_seek_callback (flacenc->encoder, 
		                        gst_flacenc_seek_callback);
  					
  FLAC__seekable_stream_encoder_set_client_data (flacenc->encoder, 
Wim Taymans's avatar
Wim Taymans committed
335
336
		                        flacenc);

337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
  flacenc->metadata = GST_CAPS_NEW (
                  "flacenc_metadata",
                  "application/x-gst-metadata",
                    "DESCRIPTION",      GST_PROPS_STRING ("Track encoded with GStreamer"),
                    "DATE",             GST_PROPS_STRING (""),
                    "TRACKNUMBER",      GST_PROPS_STRING (""),
                    "TITLE",            GST_PROPS_STRING (""),
                    "ARTIST",           GST_PROPS_STRING (""),
                    "ALBUM",            GST_PROPS_STRING (""),
                    "GENRE",            GST_PROPS_STRING (""),
                    "VERSION",          GST_PROPS_STRING (""),
                    "COPYRIGHT",        GST_PROPS_STRING (""),
                    "LICENSE",          GST_PROPS_STRING (""),
                    "ORGANISATION",     GST_PROPS_STRING (""),
                    "LOCATION",         GST_PROPS_STRING (""),
                    "CONTACT",          GST_PROPS_STRING (""),
                    "ISRC",             GST_PROPS_STRING ("")
                  );
		  
Wim Taymans's avatar
Wim Taymans committed
356
357
358
359
360
361
362
363
364
365
366
367
  flacenc->negotiated = FALSE;
  flacenc->first = TRUE;
  flacenc->first_buf = NULL;
  flacenc->data = NULL;
  gst_flacenc_update_quality (flacenc, DEFAULT_QUALITY);
}

static void
gst_flacenc_dispose (GObject *object)
{
  FlacEnc *flacenc = GST_FLACENC (object);

368
  FLAC__seekable_stream_encoder_delete (flacenc->encoder);
Wim Taymans's avatar
Wim Taymans committed
369
370
371
372
  
  G_OBJECT_CLASS (parent_class)->dispose (object);
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
373
static GstPadLinkReturn
374
gst_flacenc_sinkconnect (GstPad *pad, GstCaps *caps)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
375
{
376
  GstPadLinkReturn ret;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
377
378
379
380
  FlacEnc *flacenc;

  flacenc = GST_FLACENC (gst_pad_get_parent (pad));

381
  if (!GST_CAPS_IS_FIXED (caps))
382
    return GST_PAD_LINK_DELAYED;
383

Wim Taymans's avatar
Wim Taymans committed
384
385
386
  gst_caps_get_int (caps, "channels", &flacenc->channels);
  gst_caps_get_int (caps, "depth", &flacenc->depth);
  gst_caps_get_int (caps, "rate", &flacenc->sample_rate);
387
  
388
  caps = GST_CAPS_NEW ("flacenc_srccaps",
389
                       "application/x-flac",
390
391
392
393
394
395
396
                         "channels", GST_PROPS_INT (flacenc->channels),
                         "rate", GST_PROPS_INT (flacenc->sample_rate));
  ret = gst_pad_try_set_caps (flacenc->srcpad, caps);
  if (ret <= 0) {
    return ret;
  }

397
  FLAC__seekable_stream_encoder_set_bits_per_sample (flacenc->encoder, 
Wim Taymans's avatar
Wim Taymans committed
398
		  			    flacenc->depth);
399
  FLAC__seekable_stream_encoder_set_sample_rate (flacenc->encoder, 
Wim Taymans's avatar
Wim Taymans committed
400
		  			flacenc->sample_rate);
401
  FLAC__seekable_stream_encoder_set_channels (flacenc->encoder, 
Wim Taymans's avatar
Wim Taymans committed
402
403
404
		  		     flacenc->channels);

  flacenc->negotiated = TRUE;
405

406
  return ret;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
407
408
}

Wim Taymans's avatar
Wim Taymans committed
409
410
static gboolean
gst_flacenc_update_quality (FlacEnc *flacenc, gint quality)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
411
{
Wim Taymans's avatar
Wim Taymans committed
412
413
  flacenc->quality = quality;

414
#define DO_UPDATE(name, val, str) 	                    	\
415
  G_STMT_START {                                                \
416
    if (FLAC__seekable_stream_encoder_get_##name (flacenc->encoder) != 	\
417
        flacenc_params[quality].val) {        			\
418
      FLAC__seekable_stream_encoder_set_##name (flacenc->encoder, 	\
419
420
421
422
          flacenc_params[quality].val);				\
      g_object_notify (G_OBJECT (flacenc), str);                \
    }                                                           \
  } G_STMT_END
Wim Taymans's avatar
Wim Taymans committed
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442

  g_object_freeze_notify (G_OBJECT (flacenc));

  DO_UPDATE (do_mid_side_stereo,           mid_side,                     "mid_side_stereo");
  DO_UPDATE (loose_mid_side_stereo,        loose_mid_side,               "loose_mid_side");
  DO_UPDATE (blocksize,                    blocksize,                    "blocksize");
  DO_UPDATE (max_lpc_order,                max_lpc_order,                "max_lpc_order");
  DO_UPDATE (qlp_coeff_precision,          qlp_coeff_precision,          "qlp_coeff_precision");
  DO_UPDATE (do_qlp_coeff_prec_search,     qlp_coeff_prec_search,        "qlp_coeff_prec_search");
  DO_UPDATE (do_escape_coding,             escape_coding,                "escape_coding");
  DO_UPDATE (do_exhaustive_model_search,   exhaustive_model_search,      "exhaustive_model_search");
  DO_UPDATE (min_residual_partition_order, min_residual_partition_order, "min_residual_partition_order");
  DO_UPDATE (max_residual_partition_order, max_residual_partition_order, "max_residual_partition_order");
  DO_UPDATE (rice_parameter_search_dist,   rice_parameter_search_dist,   "rice_parameter_search_dist");

#undef DO_UPDATE

  g_object_thaw_notify (G_OBJECT (flacenc));

  return TRUE;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
443
444
}

445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
static FLAC__SeekableStreamEncoderSeekStatus
gst_flacenc_seek_callback (const FLAC__SeekableStreamEncoder *encoder,
			   FLAC__uint64 absolute_byte_offset,
			   void *client_data)
{
FlacEnc *flacenc;
GstEvent *event;

  flacenc = GST_FLACENC (client_data);

  if (flacenc->stopped) 
    return FLAC__STREAM_ENCODER_OK;

  event = gst_event_new_seek ((GstSeekType)(int)(GST_FORMAT_BYTES | GST_SEEK_METHOD_SET), absolute_byte_offset); 

  if (event)
461
    gst_pad_push (flacenc->srcpad, GST_DATA (event));
462
463
464
465
 
  return  FLAC__STREAM_ENCODER_OK;
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
466
static FLAC__StreamEncoderWriteStatus 
467
gst_flacenc_write_callback (const FLAC__SeekableStreamEncoder *encoder, 
Wim Taymans's avatar
Wim Taymans committed
468
469
470
			    const FLAC__byte buffer[], unsigned bytes, 
			    unsigned samples, unsigned current_frame, 
			    void *client_data)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
471
472
473
{
  FlacEnc *flacenc;
  GstBuffer *outbuf;
474
  
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
475
476
  flacenc = GST_FLACENC (client_data);

Wim Taymans's avatar
Wim Taymans committed
477
  if (flacenc->stopped) 
Wim Taymans's avatar
Wim Taymans committed
478
    return FLAC__STREAM_ENCODER_OK;
Wim Taymans's avatar
Wim Taymans committed
479
480

  outbuf = gst_buffer_new_and_alloc (bytes);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
481
482
483

  memcpy (GST_BUFFER_DATA (outbuf), buffer, bytes);

484
485
486
487
488
489
  if (flacenc->first) {
    flacenc->first_buf = outbuf;
    gst_buffer_ref (outbuf);
    flacenc->first = FALSE;
  }

490
  gst_pad_push (flacenc->srcpad, GST_DATA (outbuf));
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
491

Wim Taymans's avatar
Wim Taymans committed
492
  return FLAC__STREAM_ENCODER_OK;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
493
494
}

495
496
static void
gst_flacenc_set_metadata (FlacEnc *flacenc, GstCaps *caps)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
497
{
498
499
500
501
502
503
  guint indice;
  guint comments;
  const gchar *meta_types[] = { "TITLE", "VERSION", "ALBUM", "TRACKNUMBER",
                                "ARTIST", "PERFORMER", "COPYRIGHT", "LICENSE",
                                "ORGANISATION", "DESCRIPTION", "GENRE", "DATE",
                                "LOCATION", "CONTACT", "ISRC", NULL };
504

505
  g_return_if_fail (flacenc != NULL);
506

507
  flacenc->meta = g_malloc (sizeof (FLAC__StreamMetadata **));
508

509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
  flacenc->meta[0] = FLAC__metadata_object_new (FLAC__METADATA_TYPE_VORBIS_COMMENT);
  
  for ( indice = 0, comments=0; meta_types[indice] != NULL; indice++) {
    if (gst_caps_has_property(caps, meta_types[indice])) {
      const gchar *entry;
      gchar *data;
      FLAC__StreamMetadata_VorbisComment_Entry commment_entry;
      
      gst_caps_get_string(caps, (gchar *)meta_types[indice], &entry);

      if (!strcmp (entry, ""))
	continue;
   
      data = g_strdup_printf("%s=%s", meta_types[indice], entry);
      commment_entry.length = GUINT32_TO_LE (strlen(entry) + strlen(meta_types[indice]) + 1);
      commment_entry.entry  =  g_convert (data, commment_entry.length, "UTF8", "ISO-8859-1", NULL, NULL, NULL);

      FLAC__metadata_object_vorbiscomment_insert_comment (flacenc->meta[0], comments++, commment_entry, TRUE);
    }
  }
529

530
531
  FLAC__seekable_stream_encoder_set_metadata(flacenc->encoder, flacenc->meta, 1);
}
532
533


Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
534
535

static void
536
gst_flacenc_chain (GstPad *pad, GstData *_data)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
537
{
538
  GstBuffer *buf = GST_BUFFER (_data);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
539
  FlacEnc *flacenc;
Wim Taymans's avatar
Wim Taymans committed
540
  FLAC__int32 *data;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
541
  gulong insize;
Wim Taymans's avatar
Wim Taymans committed
542
543
544
  gint samples, depth;
  gulong i;
  FLAC__bool res;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
545
546
547
548
549

  g_return_if_fail(buf != NULL);

  flacenc = GST_FLACENC (gst_pad_get_parent (pad));

550
551
552
553
554
  if (GST_IS_EVENT (buf)) {
    GstEvent *event = GST_EVENT (buf);

    switch (GST_EVENT_TYPE (event)) {
      case GST_EVENT_EOS:
555
	FLAC__seekable_stream_encoder_finish(flacenc->encoder);
556
557
558
559
560
561
562
      default:
	gst_pad_event_default (pad, event);
        break;
    }
    return;
  }

Wim Taymans's avatar
Wim Taymans committed
563
  if (!flacenc->negotiated) {
564
565
    gst_element_error (GST_ELEMENT (flacenc),
		    "format not negotiated");
Wim Taymans's avatar
Wim Taymans committed
566
567
568
    return;
  }

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
569
570
571
  depth = flacenc->depth;

  insize = GST_BUFFER_SIZE (buf);
Wim Taymans's avatar
Wim Taymans committed
572
  samples = insize / ((depth+7)>>3);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
573

574
575
  if (FLAC__seekable_stream_encoder_get_state (flacenc->encoder) == 
		  FLAC__SEEKABLE_STREAM_ENCODER_UNINITIALIZED) 
Wim Taymans's avatar
Wim Taymans committed
576
  {
577
    FLAC__SeekableStreamEncoderState state;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
578

579
580
    gst_flacenc_set_metadata (flacenc, flacenc->metadata);
    state = FLAC__seekable_stream_encoder_init (flacenc->encoder);
Wim Taymans's avatar
Wim Taymans committed
581
    if (state != FLAC__STREAM_ENCODER_OK) {
582
583
      gst_element_error (GST_ELEMENT (flacenc),
		         "could not initialize encoder (wrong parameters?)");
Wim Taymans's avatar
Wim Taymans committed
584
585
      return;
    }
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
586
587
  }

Wim Taymans's avatar
Wim Taymans committed
588
589
590
  /* we keep a pointer in the flacenc struct because we are freeing the data
   * after a push opreration that might never return */
  data = flacenc->data = g_malloc (samples * sizeof (FLAC__int32));
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
591
592
593
    
  if (depth == 8) {
    gint8 *indata = (gint8 *) GST_BUFFER_DATA (buf);
Wim Taymans's avatar
Wim Taymans committed
594
    
595

Wim Taymans's avatar
Wim Taymans committed
596
597
    for (i=0; i<samples; i++) {
      data[i] = (FLAC__int32) *indata++;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
598
599
600
601
    }
  }
  else if (depth == 16) {
    gint16 *indata = (gint16 *) GST_BUFFER_DATA (buf);
Wim Taymans's avatar
Wim Taymans committed
602
603
604

    for (i=0; i<samples; i++) {
      data[i] = (FLAC__int32) *indata++;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
605
606
607
    }
  }

Wim Taymans's avatar
Wim Taymans committed
608
  gst_buffer_unref(buf);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
609

610
  res = FLAC__seekable_stream_encoder_process_interleaved (flacenc->encoder, 
Wim Taymans's avatar
Wim Taymans committed
611
	                      (const FLAC__int32 *) data, samples / flacenc->channels);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
612

Wim Taymans's avatar
Wim Taymans committed
613
614
615
616
  g_free (flacenc->data);
  flacenc->data = NULL;

  if (!res) {
617
618
    gst_element_error (GST_ELEMENT (flacenc),
		         "encoding error");
Wim Taymans's avatar
Wim Taymans committed
619
  }
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
620
621
622
}

static void
Wim Taymans's avatar
Wim Taymans committed
623
624
gst_flacenc_set_property (GObject *object, guint prop_id,
		          const GValue *value, GParamSpec *pspec)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
625
626
627
628
629
{
  FlacEnc *this;
  
  this = (FlacEnc *)object;
  switch (prop_id) {
Wim Taymans's avatar
Wim Taymans committed
630
631
632
633
    case ARG_QUALITY:
      gst_flacenc_update_quality (this, g_value_get_enum (value));
      break;
    case ARG_STREAMABLE_SUBSET:
634
      FLAC__seekable_stream_encoder_set_streamable_subset (this->encoder, 
Wim Taymans's avatar
Wim Taymans committed
635
636
637
	                   g_value_get_boolean (value));
      break;
    case ARG_MID_SIDE_STEREO:
638
      FLAC__seekable_stream_encoder_set_do_mid_side_stereo (this->encoder, 
Wim Taymans's avatar
Wim Taymans committed
639
640
641
	                   g_value_get_boolean (value));
      break;
    case ARG_LOOSE_MID_SIDE_STEREO:
642
      FLAC__seekable_stream_encoder_set_loose_mid_side_stereo (this->encoder, 
Wim Taymans's avatar
Wim Taymans committed
643
644
645
	                   g_value_get_boolean (value));
      break;
    case ARG_BLOCKSIZE:
646
      FLAC__seekable_stream_encoder_set_blocksize (this->encoder, 
Wim Taymans's avatar
Wim Taymans committed
647
648
649
	                   g_value_get_uint (value));
      break;
    case ARG_MAX_LPC_ORDER:
650
      FLAC__seekable_stream_encoder_set_max_lpc_order (this->encoder, 
Wim Taymans's avatar
Wim Taymans committed
651
652
653
	                   g_value_get_uint (value));
      break;
    case ARG_QLP_COEFF_PRECISION:
654
      FLAC__seekable_stream_encoder_set_qlp_coeff_precision (this->encoder, 
Wim Taymans's avatar
Wim Taymans committed
655
656
657
	                   g_value_get_uint (value));
      break;
    case ARG_QLP_COEFF_PREC_SEARCH:
658
      FLAC__seekable_stream_encoder_set_do_qlp_coeff_prec_search (this->encoder, 
Wim Taymans's avatar
Wim Taymans committed
659
660
661
	                   g_value_get_boolean (value));
      break;
    case ARG_ESCAPE_CODING:
662
      FLAC__seekable_stream_encoder_set_do_escape_coding (this->encoder, 
Wim Taymans's avatar
Wim Taymans committed
663
664
665
	                   g_value_get_boolean (value));
      break;
    case ARG_EXHAUSTIVE_MODEL_SEARCH:
666
      FLAC__seekable_stream_encoder_set_do_exhaustive_model_search (this->encoder, 
Wim Taymans's avatar
Wim Taymans committed
667
668
669
	                   g_value_get_boolean (value));
      break;
    case ARG_MIN_RESIDUAL_PARTITION_ORDER:
670
      FLAC__seekable_stream_encoder_set_min_residual_partition_order (this->encoder, 
Wim Taymans's avatar
Wim Taymans committed
671
672
673
	                   g_value_get_uint (value));
      break;
    case ARG_MAX_RESIDUAL_PARTITION_ORDER:
674
      FLAC__seekable_stream_encoder_set_max_residual_partition_order (this->encoder, 
Wim Taymans's avatar
Wim Taymans committed
675
676
677
	                   g_value_get_uint (value));
      break;
    case ARG_RICE_PARAMETER_SEARCH_DIST:
678
      FLAC__seekable_stream_encoder_set_rice_parameter_search_dist (this->encoder, 
Wim Taymans's avatar
Wim Taymans committed
679
680
681
682
683
		                                 g_value_get_uint (value));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      return;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
684
685
686
687
  }
}

static void
Wim Taymans's avatar
Wim Taymans committed
688
689
gst_flacenc_get_property (GObject *object, guint prop_id, 
		          GValue *value, GParamSpec *pspec)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
690
691
692
693
694
695
{
  FlacEnc *this;
  
  this = (FlacEnc *)object;
  
  switch (prop_id) {
Wim Taymans's avatar
Wim Taymans committed
696
697
698
699
700
    case ARG_QUALITY:
      g_value_set_enum (value, this->quality);
      break;
    case ARG_STREAMABLE_SUBSET:
      g_value_set_boolean (value, 
701
	      FLAC__seekable_stream_encoder_get_streamable_subset (this->encoder));
Wim Taymans's avatar
Wim Taymans committed
702
703
704
      break;
    case ARG_MID_SIDE_STEREO:
      g_value_set_boolean (value, 
705
	      FLAC__seekable_stream_encoder_get_do_mid_side_stereo (this->encoder));
Wim Taymans's avatar
Wim Taymans committed
706
707
708
      break;
    case ARG_LOOSE_MID_SIDE_STEREO:
      g_value_set_boolean (value, 
709
	      FLAC__seekable_stream_encoder_get_loose_mid_side_stereo (this->encoder));
Wim Taymans's avatar
Wim Taymans committed
710
711
712
      break;
    case ARG_BLOCKSIZE:
      g_value_set_uint (value, 
713
	      FLAC__seekable_stream_encoder_get_blocksize (this->encoder));
Wim Taymans's avatar
Wim Taymans committed
714
715
716
      break;
    case ARG_MAX_LPC_ORDER:
      g_value_set_uint (value, 
717
	      FLAC__seekable_stream_encoder_get_max_lpc_order (this->encoder));
Wim Taymans's avatar
Wim Taymans committed
718
719
720
      break;
    case ARG_QLP_COEFF_PRECISION:
      g_value_set_uint (value, 
721
	      FLAC__seekable_stream_encoder_get_qlp_coeff_precision (this->encoder));
Wim Taymans's avatar
Wim Taymans committed
722
723
724
      break;
    case ARG_QLP_COEFF_PREC_SEARCH:
      g_value_set_boolean (value, 
725
	      FLAC__seekable_stream_encoder_get_do_qlp_coeff_prec_search (this->encoder));
Wim Taymans's avatar
Wim Taymans committed
726
727
728
      break;
    case ARG_ESCAPE_CODING:
      g_value_set_boolean (value, 
729
	      FLAC__seekable_stream_encoder_get_do_escape_coding (this->encoder));
Wim Taymans's avatar
Wim Taymans committed
730
731
732
      break;
    case ARG_EXHAUSTIVE_MODEL_SEARCH:
      g_value_set_boolean (value, 
733
	      FLAC__seekable_stream_encoder_get_do_exhaustive_model_search (this->encoder));
Wim Taymans's avatar
Wim Taymans committed
734
735
736
      break;
    case ARG_MIN_RESIDUAL_PARTITION_ORDER:
      g_value_set_uint (value, 
737
	      FLAC__seekable_stream_encoder_get_min_residual_partition_order (this->encoder));
Wim Taymans's avatar
Wim Taymans committed
738
739
740
      break;
    case ARG_MAX_RESIDUAL_PARTITION_ORDER:
      g_value_set_uint (value, 
741
	      FLAC__seekable_stream_encoder_get_max_residual_partition_order (this->encoder));
Wim Taymans's avatar
Wim Taymans committed
742
743
744
      break;
    case ARG_RICE_PARAMETER_SEARCH_DIST:
      g_value_set_uint (value, 
745
	      FLAC__seekable_stream_encoder_get_rice_parameter_search_dist (this->encoder));
Wim Taymans's avatar
Wim Taymans committed
746
747
748
749
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
750
751
752
  }
}

Wim Taymans's avatar
Wim Taymans committed
753
754
755
756
757
758
759
760
static GstElementStateReturn
gst_flacenc_change_state (GstElement *element)
{
  FlacEnc *flacenc = GST_FLACENC (element);

  switch (GST_STATE_TRANSITION (element)) {
    case GST_STATE_NULL_TO_READY:
    case GST_STATE_READY_TO_PAUSED:
Wim Taymans's avatar
Wim Taymans committed
761
762
      flacenc->first = TRUE;
      flacenc->stopped = FALSE;
Wim Taymans's avatar
Wim Taymans committed
763
764
765
766
767
      break;
    case GST_STATE_PAUSED_TO_PLAYING:
    case GST_STATE_PLAYING_TO_PAUSED:
      break;
    case GST_STATE_PAUSED_TO_READY:
768
      if (FLAC__seekable_stream_encoder_get_state (flacenc->encoder) != 
Wim Taymans's avatar
Wim Taymans committed
769
770
		        FLAC__STREAM_ENCODER_UNINITIALIZED) {
        flacenc->stopped = TRUE;
771
        FLAC__seekable_stream_encoder_finish (flacenc->encoder);
Wim Taymans's avatar
Wim Taymans committed
772
      }
Wim Taymans's avatar
Wim Taymans committed
773
      flacenc->negotiated = FALSE;
Wim Taymans's avatar
Wim Taymans committed
774
775
776
777
778
      if (flacenc->first_buf)
	gst_buffer_unref (flacenc->first_buf);
      flacenc->first_buf = NULL;
      g_free (flacenc->data);
      flacenc->data = NULL;
779
780
781
782
783
      if (flacenc->meta) {
	FLAC__metadata_object_delete (flacenc->meta[0]);
	g_free (flacenc->meta);
	flacenc->meta = NULL;
      }
Wim Taymans's avatar
Wim Taymans committed
784
785
786
787
788
789
790
791
792
793
794
      break;
    case GST_STATE_READY_TO_NULL:
    default:
      break;
  }

  if (GST_ELEMENT_CLASS (parent_class)->change_state)
    return GST_ELEMENT_CLASS (parent_class)->change_state (element);

  return GST_STATE_SUCCESS;
}