Just make it compile with --disable-gst-debug.
[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 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include <gstflacenc.h>
28 #include <gst/audio/audio.h>
29 #include <gst/tag/tag.h>
30 #include <gst/gsttagsetter.h>
31 #include "flac_compat.h"
32
33
34 GstElementDetails flacenc_details = {
35   "FLAC encoder",
36   "Codec/Encoder/Audio",
37   "Encodes audio with the FLAC lossless audio encoder",
38   "Wim Taymans <wim.taymans@chello.be>",
39 };
40
41 #define FLAC_SINK_CAPS \
42   "audio/x-raw-int, "               \
43   "endianness = (int) BYTE_ORDER, " \
44   "signed = (boolean) TRUE, "       \
45   "width = (int) 16, "              \
46   "depth = (int) 16, "              \
47   "rate = (int) [ 11025, 48000 ], " \
48   "channels = (int) [ 1, 2 ]"
49
50 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
51     GST_PAD_SRC,
52     GST_PAD_ALWAYS,
53     GST_STATIC_CAPS ("audio/x-flac")
54     );
55
56 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
57     GST_PAD_SINK,
58     GST_PAD_ALWAYS,
59     GST_STATIC_CAPS (FLAC_SINK_CAPS)
60     );
61
62 enum
63 {
64   PROP_0,
65   PROP_QUALITY,
66   PROP_STREAMABLE_SUBSET,
67   PROP_MID_SIDE_STEREO,
68   PROP_LOOSE_MID_SIDE_STEREO,
69   PROP_BLOCKSIZE,
70   PROP_MAX_LPC_ORDER,
71   PROP_QLP_COEFF_PRECISION,
72   PROP_QLP_COEFF_PREC_SEARCH,
73   PROP_ESCAPE_CODING,
74   PROP_EXHAUSTIVE_MODEL_SEARCH,
75   PROP_MIN_RESIDUAL_PARTITION_ORDER,
76   PROP_MAX_RESIDUAL_PARTITION_ORDER,
77   PROP_RICE_PARAMETER_SEARCH_DIST
78 };
79
80 GST_DEBUG_CATEGORY_STATIC (flacenc_debug);
81 #define GST_CAT_DEFAULT flacenc_debug
82
83
84 #define _do_init(type)                                                          \
85   G_STMT_START{                                                                 \
86     static const GInterfaceInfo tag_setter_info = {                             \
87       NULL,                                                                     \
88       NULL,                                                                     \
89       NULL                                                                      \
90     };                                                                          \
91     g_type_add_interface_static (type, GST_TYPE_TAG_SETTER,                     \
92                                  &tag_setter_info);                             \
93   }G_STMT_END
94
95 GST_BOILERPLATE_FULL (GstFlacEnc, gst_flac_enc, GstElement, GST_TYPE_ELEMENT,
96     _do_init);
97
98 static void gst_flac_enc_finalize (GObject * object);
99
100 static gboolean gst_flac_enc_sink_setcaps (GstPad * pad, GstCaps * caps);
101 static gboolean gst_flac_enc_sink_event (GstPad * pad, GstEvent * event);
102 static GstFlowReturn gst_flac_enc_chain (GstPad * pad, GstBuffer * buffer);
103
104 static gboolean gst_flac_enc_update_quality (GstFlacEnc * flacenc,
105     gint quality);
106 static void gst_flac_enc_set_property (GObject * object, guint prop_id,
107     const GValue * value, GParamSpec * pspec);
108 static void gst_flac_enc_get_property (GObject * object, guint prop_id,
109     GValue * value, GParamSpec * pspec);
110 static GstStateChangeReturn gst_flac_enc_change_state (GstElement * element,
111     GstStateChange transition);
112
113 static FLAC__StreamEncoderWriteStatus
114 gst_flac_enc_write_callback (const FLAC__SeekableStreamEncoder * encoder,
115     const FLAC__byte buffer[], unsigned bytes,
116     unsigned samples, unsigned current_frame, void *client_data);
117 static FLAC__SeekableStreamEncoderSeekStatus
118 gst_flac_enc_seek_callback (const FLAC__SeekableStreamEncoder * encoder,
119     FLAC__uint64 absolute_byte_offset, void *client_data);
120 static FLAC__SeekableStreamEncoderTellStatus
121 gst_flac_enc_tell_callback (const FLAC__SeekableStreamEncoder * encoder,
122     FLAC__uint64 * absolute_byte_offset, void *client_data);
123
124 typedef struct
125 {
126   gboolean exhaustive_model_search;
127   gboolean escape_coding;
128   gboolean mid_side;
129   gboolean loose_mid_side;
130   guint qlp_coeff_precision;
131   gboolean qlp_coeff_prec_search;
132   guint min_residual_partition_order;
133   guint max_residual_partition_order;
134   guint rice_parameter_search_dist;
135   guint max_lpc_order;
136   guint blocksize;
137 }
138 GstFlacEncParams;
139
140 static const GstFlacEncParams flacenc_params[] = {
141   {FALSE, FALSE, FALSE, FALSE, 0, FALSE, 2, 2, 0, 0, 1152},
142   {FALSE, FALSE, TRUE, TRUE, 0, FALSE, 2, 2, 0, 0, 1152},
143   {FALSE, FALSE, TRUE, FALSE, 0, FALSE, 0, 3, 0, 0, 1152},
144   {FALSE, FALSE, FALSE, FALSE, 0, FALSE, 3, 3, 0, 6, 4608},
145   {FALSE, FALSE, TRUE, TRUE, 0, FALSE, 3, 3, 0, 8, 4608},
146   {FALSE, FALSE, TRUE, FALSE, 0, FALSE, 3, 3, 0, 8, 4608},
147   {FALSE, FALSE, TRUE, FALSE, 0, FALSE, 0, 4, 0, 8, 4608},
148   {TRUE, FALSE, TRUE, FALSE, 0, FALSE, 0, 6, 0, 8, 4608},
149   {TRUE, FALSE, TRUE, FALSE, 0, FALSE, 0, 6, 0, 12, 4608},
150   {TRUE, TRUE, TRUE, FALSE, 0, FALSE, 0, 16, 0, 32, 4608},
151 };
152
153 #define DEFAULT_QUALITY 5
154
155 #define GST_TYPE_FLAC_ENC_QUALITY (gst_flac_enc_quality_get_type ())
156 GType
157 gst_flac_enc_quality_get_type (void)
158 {
159   static GType qtype = 0;
160
161   if (qtype == 0) {
162     static const GEnumValue values[] = {
163       {0, "0 - Fastest compression", "0"},
164       {1, "1", "1"},
165       {2, "2", "2"},
166       {3, "3", "3"},
167       {4, "4", "4"},
168       {5, "5 - Default", "5"},
169       {6, "6", "6"},
170       {7, "7", "7"},
171       {8, "8 - Highest compression", "8"},
172       {9, "9 - Insane", "9"},
173       {0, NULL, NULL}
174     };
175
176     qtype = g_enum_register_static ("GstFlacEncQuality", values);
177   }
178   return qtype;
179 }
180
181 static void
182 gst_flac_enc_base_init (gpointer g_class)
183 {
184   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
185
186   gst_element_class_add_pad_template (element_class,
187       gst_static_pad_template_get (&src_factory));
188   gst_element_class_add_pad_template (element_class,
189       gst_static_pad_template_get (&sink_factory));
190
191   gst_element_class_set_details (element_class, &flacenc_details);
192
193   GST_DEBUG_CATEGORY_INIT (flacenc_debug, "flacenc", 0,
194       "Flac encoding element");
195 }
196
197 static void
198 gst_flac_enc_class_init (GstFlacEncClass * klass)
199 {
200   GObjectClass *gobject_class;
201   GstElementClass *gstelement_class;
202
203   gobject_class = (GObjectClass *) klass;
204   gstelement_class = (GstElementClass *) klass;
205
206   gobject_class->set_property = gst_flac_enc_set_property;
207   gobject_class->get_property = gst_flac_enc_get_property;
208   gobject_class->finalize = gst_flac_enc_finalize;
209
210   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_QUALITY,
211       g_param_spec_enum ("quality",
212           "Quality",
213           "Speed versus compression tradeoff",
214           GST_TYPE_FLAC_ENC_QUALITY, DEFAULT_QUALITY, G_PARAM_READWRITE));
215   g_object_class_install_property (G_OBJECT_CLASS (klass),
216       PROP_STREAMABLE_SUBSET, g_param_spec_boolean ("streamable_subset",
217           "Streamable subset",
218           "true to limit encoder to generating a Subset stream, else false",
219           TRUE, G_PARAM_READWRITE));
220   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_MID_SIDE_STEREO,
221       g_param_spec_boolean ("mid_side_stereo", "Do mid side stereo",
222           "Do mid side stereo (only for stereo input)",
223           flacenc_params[DEFAULT_QUALITY].mid_side, G_PARAM_READWRITE));
224   g_object_class_install_property (G_OBJECT_CLASS (klass),
225       PROP_LOOSE_MID_SIDE_STEREO, g_param_spec_boolean ("loose_mid_side_stereo",
226           "Loose mid side stereo", "Loose mid side stereo",
227           flacenc_params[DEFAULT_QUALITY].loose_mid_side, G_PARAM_READWRITE));
228   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BLOCKSIZE,
229       g_param_spec_uint ("blocksize", "Blocksize", "Blocksize in samples",
230           FLAC__MIN_BLOCK_SIZE, FLAC__MAX_BLOCK_SIZE,
231           flacenc_params[DEFAULT_QUALITY].blocksize, G_PARAM_READWRITE));
232   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_MAX_LPC_ORDER,
233       g_param_spec_uint ("max_lpc_order", "Max LPC order",
234           "Max LPC order; 0 => use only fixed predictors", 0,
235           FLAC__MAX_LPC_ORDER, flacenc_params[DEFAULT_QUALITY].max_lpc_order,
236           G_PARAM_READWRITE));
237   g_object_class_install_property (G_OBJECT_CLASS (klass),
238       PROP_QLP_COEFF_PRECISION, g_param_spec_uint ("qlp_coeff_precision",
239           "QLP coefficients precision",
240           "Precision in bits of quantized linear-predictor coefficients; 0 = automatic",
241           0, 32, flacenc_params[DEFAULT_QUALITY].qlp_coeff_precision,
242           G_PARAM_READWRITE));
243   g_object_class_install_property (G_OBJECT_CLASS (klass),
244       PROP_QLP_COEFF_PREC_SEARCH, g_param_spec_boolean ("qlp_coeff_prec_search",
245           "Do QLP coefficients precision search",
246           "false = use qlp_coeff_precision, "
247           "true = search around qlp_coeff_precision, take best",
248           flacenc_params[DEFAULT_QUALITY].qlp_coeff_prec_search,
249           G_PARAM_READWRITE));
250   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_ESCAPE_CODING,
251       g_param_spec_boolean ("escape_coding", "Do Escape coding",
252           "search for escape codes in the entropy coding stage "
253           "for slightly better compression",
254           flacenc_params[DEFAULT_QUALITY].escape_coding, G_PARAM_READWRITE));
255   g_object_class_install_property (G_OBJECT_CLASS (klass),
256       PROP_EXHAUSTIVE_MODEL_SEARCH,
257       g_param_spec_boolean ("exhaustive_model_search",
258           "Do exhaustive model search",
259           "do exhaustive search of LP coefficient quantization (expensive!)",
260           flacenc_params[DEFAULT_QUALITY].exhaustive_model_search,
261           G_PARAM_READWRITE));
262   g_object_class_install_property (G_OBJECT_CLASS (klass),
263       PROP_MIN_RESIDUAL_PARTITION_ORDER,
264       g_param_spec_uint ("min_residual_partition_order",
265           "Min residual partition order",
266           "Min residual partition order (above 4 doesn't usually help much)", 0,
267           16, flacenc_params[DEFAULT_QUALITY].min_residual_partition_order,
268           G_PARAM_READWRITE));
269   g_object_class_install_property (G_OBJECT_CLASS (klass),
270       PROP_MAX_RESIDUAL_PARTITION_ORDER,
271       g_param_spec_uint ("max_residual_partition_order",
272           "Max residual partition order",
273           "Max residual partition order (above 4 doesn't usually help much)", 0,
274           16, flacenc_params[DEFAULT_QUALITY].max_residual_partition_order,
275           G_PARAM_READWRITE));
276   g_object_class_install_property (G_OBJECT_CLASS (klass),
277       PROP_RICE_PARAMETER_SEARCH_DIST,
278       g_param_spec_uint ("rice_parameter_search_dist",
279           "rice_parameter_search_dist",
280           "0 = try only calc'd parameter k; else try all [k-dist..k+dist] "
281           "parameters, use best", 0, FLAC__MAX_RICE_PARTITION_ORDER,
282           flacenc_params[DEFAULT_QUALITY].rice_parameter_search_dist,
283           G_PARAM_READWRITE));
284
285   gstelement_class->change_state = gst_flac_enc_change_state;
286 }
287
288 static void
289 gst_flac_enc_init (GstFlacEnc * flacenc, GstFlacEncClass * klass)
290 {
291   GstElementClass *eclass = GST_ELEMENT_CLASS (klass);
292
293   flacenc->sinkpad =
294       gst_pad_new_from_template (gst_element_class_get_pad_template (eclass,
295           "sink"), "sink");
296   gst_element_add_pad (GST_ELEMENT (flacenc), flacenc->sinkpad);
297   gst_pad_set_chain_function (flacenc->sinkpad, gst_flac_enc_chain);
298   gst_pad_set_event_function (flacenc->sinkpad, gst_flac_enc_sink_event);
299   gst_pad_set_setcaps_function (flacenc->sinkpad, gst_flac_enc_sink_setcaps);
300
301   flacenc->srcpad =
302       gst_pad_new_from_template (gst_element_class_get_pad_template (eclass,
303           "src"), "src");
304   gst_pad_use_fixed_caps (flacenc->srcpad);
305   gst_element_add_pad (GST_ELEMENT (flacenc), flacenc->srcpad);
306
307   flacenc->encoder = FLAC__seekable_stream_encoder_new ();
308
309   flacenc->offset = 0;
310   flacenc->samples_written = 0;
311   gst_flac_enc_update_quality (flacenc, DEFAULT_QUALITY);
312   flacenc->tags = gst_tag_list_new ();
313 }
314
315 static void
316 gst_flac_enc_finalize (GObject * object)
317 {
318   GstFlacEnc *flacenc = GST_FLAC_ENC (object);
319
320   FLAC__seekable_stream_encoder_delete (flacenc->encoder);
321
322   G_OBJECT_CLASS (parent_class)->finalize (object);
323 }
324
325 static void
326 add_one_tag (const GstTagList * list, const gchar * tag, gpointer user_data)
327 {
328   GList *comments;
329   GList *it;
330   GstFlacEnc *flacenc = GST_FLAC_ENC (user_data);
331
332   comments = gst_tag_to_vorbis_comments (list, tag);
333   for (it = comments; it != NULL; it = it->next) {
334     FLAC__StreamMetadata_VorbisComment_Entry commment_entry;
335
336     commment_entry.length = strlen (it->data);
337     commment_entry.entry = it->data;
338     FLAC__metadata_object_vorbiscomment_insert_comment (flacenc->meta[0],
339         flacenc->meta[0]->data.vorbis_comment.num_comments,
340         commment_entry, TRUE);
341     g_free (it->data);
342   }
343   g_list_free (comments);
344 }
345
346 static void
347 gst_flac_enc_set_metadata (GstFlacEnc * flacenc)
348 {
349   const GstTagList *user_tags;
350   GstTagList *copy;
351
352   g_return_if_fail (flacenc != NULL);
353   user_tags = gst_tag_setter_get_tag_list (GST_TAG_SETTER (flacenc));
354   if ((flacenc->tags == NULL) && (user_tags == NULL)) {
355     return;
356   }
357   copy = gst_tag_list_merge (user_tags, flacenc->tags,
358       gst_tag_setter_get_tag_merge_mode (GST_TAG_SETTER (flacenc)));
359   flacenc->meta = g_malloc (sizeof (FLAC__StreamMetadata **));
360
361   flacenc->meta[0] =
362       FLAC__metadata_object_new (FLAC__METADATA_TYPE_VORBIS_COMMENT);
363   gst_tag_list_foreach (copy, add_one_tag, flacenc);
364
365   if (FLAC__seekable_stream_encoder_set_metadata (flacenc->encoder,
366           flacenc->meta, 1) != true)
367     g_warning ("Dude, i'm already initialized!");
368   gst_tag_list_free (copy);
369 }
370
371 static gboolean
372 gst_flac_enc_sink_setcaps (GstPad * pad, GstCaps * caps)
373 {
374   GstFlacEnc *flacenc;
375   GstStructure *structure;
376   FLAC__SeekableStreamEncoderState state;
377
378   /* takes a ref on flacenc */
379   flacenc = GST_FLAC_ENC (gst_pad_get_parent (pad));
380
381   if (FLAC__seekable_stream_encoder_get_state (flacenc->encoder) !=
382       FLAC__SEEKABLE_STREAM_ENCODER_UNINITIALIZED)
383     goto encoder_already_initialized;
384
385   structure = gst_caps_get_structure (caps, 0);
386
387   if (!gst_structure_get_int (structure, "channels", &flacenc->channels)
388       || !gst_structure_get_int (structure, "depth", &flacenc->depth)
389       || !gst_structure_get_int (structure, "rate", &flacenc->sample_rate))
390     /* we got caps incompatible with the template? */
391     g_return_val_if_reached (FALSE);
392
393   caps = gst_caps_new_simple ("audio/x-flac",
394       "channels", G_TYPE_INT, flacenc->channels,
395       "rate", G_TYPE_INT, flacenc->sample_rate, NULL);
396
397   if (!gst_pad_set_caps (flacenc->srcpad, caps))
398     goto setting_src_caps_failed;
399
400   gst_caps_unref (caps);
401
402   FLAC__seekable_stream_encoder_set_bits_per_sample (flacenc->encoder,
403       flacenc->depth);
404   FLAC__seekable_stream_encoder_set_sample_rate (flacenc->encoder,
405       flacenc->sample_rate);
406   FLAC__seekable_stream_encoder_set_channels (flacenc->encoder,
407       flacenc->channels);
408
409   FLAC__seekable_stream_encoder_set_write_callback (flacenc->encoder,
410       gst_flac_enc_write_callback);
411   FLAC__seekable_stream_encoder_set_seek_callback (flacenc->encoder,
412       gst_flac_enc_seek_callback);
413   FLAC__seekable_stream_encoder_set_tell_callback (flacenc->encoder,
414       gst_flac_enc_tell_callback);
415
416   FLAC__seekable_stream_encoder_set_client_data (flacenc->encoder, flacenc);
417
418   gst_flac_enc_set_metadata (flacenc);
419
420   state = FLAC__seekable_stream_encoder_init (flacenc->encoder);
421   if (state != FLAC__STREAM_ENCODER_OK)
422     goto failed_to_initialize;
423
424   gst_object_unref (flacenc);
425
426   return TRUE;
427
428 encoder_already_initialized:
429   {
430     g_warning ("flac already initialized -- fixme allow this");
431     gst_object_unref (flacenc);
432     return FALSE;
433   }
434 setting_src_caps_failed:
435   {
436     GST_DEBUG_OBJECT (flacenc,
437         "Couldn't set caps on source pad: %" GST_PTR_FORMAT, caps);
438     gst_caps_unref (caps);
439     gst_object_unref (flacenc);
440     return FALSE;
441   }
442 failed_to_initialize:
443   {
444     GST_ELEMENT_ERROR (flacenc, LIBRARY, INIT, (NULL),
445         ("could not initialize encoder (wrong parameters?)"));
446     gst_object_unref (flacenc);
447     return FALSE;
448   }
449 }
450
451 static gboolean
452 gst_flac_enc_update_quality (GstFlacEnc * flacenc, gint quality)
453 {
454   flacenc->quality = quality;
455
456 #define DO_UPDATE(name, val, str)                                               \
457   G_STMT_START {                                                                \
458     if (FLAC__seekable_stream_encoder_get_##name (flacenc->encoder) !=          \
459         flacenc_params[quality].val) {                                          \
460       FLAC__seekable_stream_encoder_set_##name (flacenc->encoder,               \
461           flacenc_params[quality].val);                                         \
462       g_object_notify (G_OBJECT (flacenc), str);                                \
463     }                                                                           \
464   } G_STMT_END
465
466   g_object_freeze_notify (G_OBJECT (flacenc));
467
468   if (flacenc->channels == 2) {
469     DO_UPDATE (do_mid_side_stereo, mid_side, "mid_side_stereo");
470     DO_UPDATE (loose_mid_side_stereo, loose_mid_side, "loose_mid_side");
471   }
472
473   DO_UPDATE (blocksize, blocksize, "blocksize");
474   DO_UPDATE (max_lpc_order, max_lpc_order, "max_lpc_order");
475   DO_UPDATE (qlp_coeff_precision, qlp_coeff_precision, "qlp_coeff_precision");
476   DO_UPDATE (do_qlp_coeff_prec_search, qlp_coeff_prec_search,
477       "qlp_coeff_prec_search");
478   DO_UPDATE (do_escape_coding, escape_coding, "escape_coding");
479   DO_UPDATE (do_exhaustive_model_search, exhaustive_model_search,
480       "exhaustive_model_search");
481   DO_UPDATE (min_residual_partition_order, min_residual_partition_order,
482       "min_residual_partition_order");
483   DO_UPDATE (max_residual_partition_order, max_residual_partition_order,
484       "max_residual_partition_order");
485   DO_UPDATE (rice_parameter_search_dist, rice_parameter_search_dist,
486       "rice_parameter_search_dist");
487
488 #undef DO_UPDATE
489
490   g_object_thaw_notify (G_OBJECT (flacenc));
491
492   return TRUE;
493 }
494
495 static FLAC__SeekableStreamEncoderSeekStatus
496 gst_flac_enc_seek_callback (const FLAC__SeekableStreamEncoder * encoder,
497     FLAC__uint64 absolute_byte_offset, void *client_data)
498 {
499   GstFlacEnc *flacenc;
500   GstEvent *event;
501   GstPad *peerpad;
502
503   flacenc = GST_FLAC_ENC (client_data);
504
505   if (flacenc->stopped)
506     return FLAC__STREAM_ENCODER_OK;
507
508   event = gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_BYTES,
509       absolute_byte_offset, GST_BUFFER_OFFSET_NONE, 0);
510
511   if ((peerpad = gst_pad_get_peer (flacenc->srcpad))) {
512 #ifndef GST_DISABLE_GST_DEBUG
513     gboolean ret = gst_pad_send_event (peerpad, event);
514 #endif
515
516     gst_object_unref (peerpad);
517
518     GST_DEBUG ("Seek to %" G_GUINT64_FORMAT " %s", absolute_byte_offset,
519         (ret) ? "succeeded" : "failed");
520   } else {
521     GST_DEBUG ("Seek to %" G_GUINT64_FORMAT " failed (no peer pad)",
522         absolute_byte_offset);
523   }
524
525   flacenc->offset = absolute_byte_offset;
526
527   return FLAC__STREAM_ENCODER_OK;
528 }
529
530 static FLAC__StreamEncoderWriteStatus
531 gst_flac_enc_write_callback (const FLAC__SeekableStreamEncoder * encoder,
532     const FLAC__byte buffer[], unsigned bytes,
533     unsigned samples, unsigned current_frame, void *client_data)
534 {
535   GstFlowReturn ret;
536   GstFlacEnc *flacenc;
537   GstBuffer *outbuf;
538
539   flacenc = GST_FLAC_ENC (client_data);
540
541   if (flacenc->stopped)
542     return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
543
544   if (gst_pad_alloc_buffer_and_set_caps (flacenc->srcpad, flacenc->offset,
545           bytes, GST_PAD_CAPS (flacenc->srcpad), &outbuf) != GST_FLOW_OK) {
546     return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
547   }
548
549   memcpy (GST_BUFFER_DATA (outbuf), buffer, bytes);
550
551   if (samples > 0 && flacenc->samples_written != (guint64) - 1) {
552     GST_BUFFER_TIMESTAMP (outbuf) =
553         GST_FRAMES_TO_CLOCK_TIME (flacenc->samples_written,
554         flacenc->sample_rate);
555     GST_BUFFER_DURATION (outbuf) =
556         GST_FRAMES_TO_CLOCK_TIME (samples, flacenc->sample_rate);
557     /* offset_end = granulepos for ogg muxer */
558     GST_BUFFER_OFFSET_END (outbuf) = flacenc->samples_written + samples;
559   } else {
560     GST_BUFFER_TIMESTAMP (outbuf) = GST_CLOCK_TIME_NONE;
561     GST_BUFFER_DURATION (outbuf) = GST_CLOCK_TIME_NONE;
562   }
563
564   GST_DEBUG ("Pushing buffer: ts=%" GST_TIME_FORMAT ", samples=%u, size=%u, "
565       "pos=%" G_GUINT64_FORMAT, GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
566       samples, bytes, flacenc->offset);
567
568   ret = gst_pad_push (flacenc->srcpad, outbuf);
569
570   flacenc->offset += bytes;
571   flacenc->samples_written += samples;
572
573   if (ret != GST_FLOW_OK && GST_FLOW_IS_FATAL (ret))
574     return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
575
576   return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
577 }
578
579 static FLAC__SeekableStreamEncoderTellStatus
580 gst_flac_enc_tell_callback (const FLAC__SeekableStreamEncoder * encoder,
581     FLAC__uint64 * absolute_byte_offset, void *client_data)
582 {
583   GstFlacEnc *flacenc = GST_FLAC_ENC (client_data);
584
585   *absolute_byte_offset = flacenc->offset;
586
587   return FLAC__STREAM_ENCODER_OK;
588 }
589
590 static gboolean
591 gst_flac_enc_sink_event (GstPad * pad, GstEvent * event)
592 {
593   GstFlacEnc *flacenc;
594   GstTagList *taglist;
595   gboolean ret = TRUE;
596
597   flacenc = GST_FLAC_ENC (gst_pad_get_parent (pad));
598
599   GST_DEBUG ("Received %s event on sinkpad", GST_EVENT_TYPE_NAME (event));
600
601   switch (GST_EVENT_TYPE (event)) {
602     case GST_EVENT_NEWSEGMENT:{
603       GstFormat format;
604       gint64 start, stream_time;
605
606       if (flacenc->offset == 0) {
607         gst_event_parse_new_segment (event, NULL, NULL, &format, &start, NULL,
608             &stream_time);
609       } else {
610         start = -1;
611       }
612       if (start != 0) {
613         if (flacenc->offset > 0)
614           GST_DEBUG ("Not handling mid-stream newsegment event");
615         else
616           GST_DEBUG ("Not handling newsegment event with non-zero start");
617       } else {
618         GstEvent *e = gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_BYTES,
619             0, -1, 0);
620
621         ret = gst_pad_push_event (flacenc->srcpad, e);
622       }
623       if (stream_time != 0) {
624         GST_DEBUG ("Not handling non-zero stream time");
625       }
626       gst_event_unref (event);
627       /* don't push it downstream, we'll generate our own via seek to 0 */
628       break;
629     }
630     case GST_EVENT_EOS:
631       FLAC__seekable_stream_encoder_finish (flacenc->encoder);
632       ret = gst_pad_event_default (pad, event);
633       break;
634     case GST_EVENT_TAG:
635       if (flacenc->tags) {
636         gst_event_parse_tag (event, &taglist);
637         gst_tag_list_insert (flacenc->tags, taglist, GST_TAG_MERGE_REPLACE);
638       } else {
639         g_assert_not_reached ();
640       }
641       ret = gst_pad_event_default (pad, event);
642       break;
643     default:
644       ret = gst_pad_event_default (pad, event);
645       break;
646   }
647
648   gst_object_unref (flacenc);
649
650   return ret;
651 }
652
653 static GstFlowReturn
654 gst_flac_enc_chain (GstPad * pad, GstBuffer * buffer)
655 {
656   GstFlacEnc *flacenc;
657   FLAC__int32 *data;
658   gulong insize;
659   gint samples, depth;
660   gulong i;
661   FLAC__bool res;
662
663   flacenc = GST_FLAC_ENC (gst_pad_get_parent (pad));
664
665   depth = flacenc->depth;
666
667   insize = GST_BUFFER_SIZE (buffer);
668   samples = insize / ((depth + 7) >> 3);
669
670   data = g_malloc (samples * sizeof (FLAC__int32));
671
672   if (depth == 8) {
673     gint8 *indata = (gint8 *) GST_BUFFER_DATA (buffer);
674
675     for (i = 0; i < samples; i++)
676       data[i] = (FLAC__int32) indata[i];
677   } else if (depth == 16) {
678     gint16 *indata = (gint16 *) GST_BUFFER_DATA (buffer);
679
680     for (i = 0; i < samples; i++)
681       data[i] = (FLAC__int32) indata[i];
682   } else {
683     g_assert_not_reached ();
684   }
685
686   gst_buffer_unref (buffer);
687
688   res = FLAC__seekable_stream_encoder_process_interleaved (flacenc->encoder,
689       (const FLAC__int32 *) data, samples / flacenc->channels);
690
691   g_free (data);
692
693   gst_object_unref (flacenc);
694
695   if (res)
696     return GST_FLOW_OK;
697   else
698     return GST_FLOW_ERROR;
699 }
700
701 static void
702 gst_flac_enc_set_property (GObject * object, guint prop_id,
703     const GValue * value, GParamSpec * pspec)
704 {
705   GstFlacEnc *this = GST_FLAC_ENC (object);
706
707   GST_OBJECT_LOCK (this);
708
709   switch (prop_id) {
710     case PROP_QUALITY:
711       gst_flac_enc_update_quality (this, g_value_get_enum (value));
712       break;
713     case PROP_STREAMABLE_SUBSET:
714       FLAC__seekable_stream_encoder_set_streamable_subset (this->encoder,
715           g_value_get_boolean (value));
716       break;
717     case PROP_MID_SIDE_STEREO:
718       FLAC__seekable_stream_encoder_set_do_mid_side_stereo (this->encoder,
719           g_value_get_boolean (value));
720       break;
721     case PROP_LOOSE_MID_SIDE_STEREO:
722       FLAC__seekable_stream_encoder_set_loose_mid_side_stereo (this->encoder,
723           g_value_get_boolean (value));
724       break;
725     case PROP_BLOCKSIZE:
726       FLAC__seekable_stream_encoder_set_blocksize (this->encoder,
727           g_value_get_uint (value));
728       break;
729     case PROP_MAX_LPC_ORDER:
730       FLAC__seekable_stream_encoder_set_max_lpc_order (this->encoder,
731           g_value_get_uint (value));
732       break;
733     case PROP_QLP_COEFF_PRECISION:
734       FLAC__seekable_stream_encoder_set_qlp_coeff_precision (this->encoder,
735           g_value_get_uint (value));
736       break;
737     case PROP_QLP_COEFF_PREC_SEARCH:
738       FLAC__seekable_stream_encoder_set_do_qlp_coeff_prec_search (this->encoder,
739           g_value_get_boolean (value));
740       break;
741     case PROP_ESCAPE_CODING:
742       FLAC__seekable_stream_encoder_set_do_escape_coding (this->encoder,
743           g_value_get_boolean (value));
744       break;
745     case PROP_EXHAUSTIVE_MODEL_SEARCH:
746       FLAC__seekable_stream_encoder_set_do_exhaustive_model_search (this->
747           encoder, g_value_get_boolean (value));
748       break;
749     case PROP_MIN_RESIDUAL_PARTITION_ORDER:
750       FLAC__seekable_stream_encoder_set_min_residual_partition_order (this->
751           encoder, g_value_get_uint (value));
752       break;
753     case PROP_MAX_RESIDUAL_PARTITION_ORDER:
754       FLAC__seekable_stream_encoder_set_max_residual_partition_order (this->
755           encoder, g_value_get_uint (value));
756       break;
757     case PROP_RICE_PARAMETER_SEARCH_DIST:
758       FLAC__seekable_stream_encoder_set_rice_parameter_search_dist (this->
759           encoder, g_value_get_uint (value));
760       break;
761     default:
762       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
763       break;
764   }
765
766   GST_OBJECT_UNLOCK (this);
767 }
768
769 static void
770 gst_flac_enc_get_property (GObject * object, guint prop_id,
771     GValue * value, GParamSpec * pspec)
772 {
773   GstFlacEnc *this = GST_FLAC_ENC (object);
774
775   GST_OBJECT_LOCK (this);
776
777   switch (prop_id) {
778     case PROP_QUALITY:
779       g_value_set_enum (value, this->quality);
780       break;
781     case PROP_STREAMABLE_SUBSET:
782       g_value_set_boolean (value,
783           FLAC__seekable_stream_encoder_get_streamable_subset (this->encoder));
784       break;
785     case PROP_MID_SIDE_STEREO:
786       g_value_set_boolean (value,
787           FLAC__seekable_stream_encoder_get_do_mid_side_stereo (this->encoder));
788       break;
789     case PROP_LOOSE_MID_SIDE_STEREO:
790       g_value_set_boolean (value,
791           FLAC__seekable_stream_encoder_get_loose_mid_side_stereo (this->
792               encoder));
793       break;
794     case PROP_BLOCKSIZE:
795       g_value_set_uint (value,
796           FLAC__seekable_stream_encoder_get_blocksize (this->encoder));
797       break;
798     case PROP_MAX_LPC_ORDER:
799       g_value_set_uint (value,
800           FLAC__seekable_stream_encoder_get_max_lpc_order (this->encoder));
801       break;
802     case PROP_QLP_COEFF_PRECISION:
803       g_value_set_uint (value,
804           FLAC__seekable_stream_encoder_get_qlp_coeff_precision (this->
805               encoder));
806       break;
807     case PROP_QLP_COEFF_PREC_SEARCH:
808       g_value_set_boolean (value,
809           FLAC__seekable_stream_encoder_get_do_qlp_coeff_prec_search (this->
810               encoder));
811       break;
812     case PROP_ESCAPE_CODING:
813       g_value_set_boolean (value,
814           FLAC__seekable_stream_encoder_get_do_escape_coding (this->encoder));
815       break;
816     case PROP_EXHAUSTIVE_MODEL_SEARCH:
817       g_value_set_boolean (value,
818           FLAC__seekable_stream_encoder_get_do_exhaustive_model_search (this->
819               encoder));
820       break;
821     case PROP_MIN_RESIDUAL_PARTITION_ORDER:
822       g_value_set_uint (value,
823           FLAC__seekable_stream_encoder_get_min_residual_partition_order (this->
824               encoder));
825       break;
826     case PROP_MAX_RESIDUAL_PARTITION_ORDER:
827       g_value_set_uint (value,
828           FLAC__seekable_stream_encoder_get_max_residual_partition_order (this->
829               encoder));
830       break;
831     case PROP_RICE_PARAMETER_SEARCH_DIST:
832       g_value_set_uint (value,
833           FLAC__seekable_stream_encoder_get_rice_parameter_search_dist (this->
834               encoder));
835       break;
836     default:
837       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
838       break;
839   }
840
841   GST_OBJECT_UNLOCK (this);
842 }
843
844 static GstStateChangeReturn
845 gst_flac_enc_change_state (GstElement * element, GstStateChange transition)
846 {
847   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
848   GstFlacEnc *flacenc = GST_FLAC_ENC (element);
849
850   switch (transition) {
851     case GST_STATE_CHANGE_NULL_TO_READY:
852     case GST_STATE_CHANGE_READY_TO_PAUSED:
853       flacenc->stopped = FALSE;
854       break;
855     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
856     default:
857       break;
858   }
859
860   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
861
862   switch (transition) {
863     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
864       break;
865     case GST_STATE_CHANGE_PAUSED_TO_READY:
866       if (FLAC__seekable_stream_encoder_get_state (flacenc->encoder) !=
867           FLAC__STREAM_ENCODER_UNINITIALIZED) {
868         flacenc->stopped = TRUE;
869         FLAC__seekable_stream_encoder_finish (flacenc->encoder);
870       }
871       flacenc->offset = 0;
872       flacenc->samples_written = 0;
873       if (flacenc->meta) {
874         FLAC__metadata_object_delete (flacenc->meta[0]);
875         g_free (flacenc->meta);
876         flacenc->meta = NULL;
877       }
878       break;
879     case GST_STATE_CHANGE_READY_TO_NULL:
880     default:
881       break;
882   }
883
884   return ret;
885 }