1 /* GStreamer Wavpack encoder plugin
2 * Copyright (c) 2006 Sebastian Dröge <slomo@circular-chaos.org>
4 * gstwavpackdec.c: Wavpack audio encoder
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 * SECTION:element-wavpackenc
26 * Wavpackenc encodes raw audio into a framed Wavpack stream.
27 * <ulink url="http://www.wavpack.com/">Wavpack</ulink> is an open-source
28 * audio codec that features both lossless and lossy encoding.
29 * <title>Example launch line</title>
32 * gst-launch audiotestsrc num-buffers=500 ! wavpackenc ! filesink location=sinewave.wv
34 * This pipeline encodes audio from audiotestsrc into a Wavpack file.
38 * gst-launch cdda://1 ! wavpackenc ! filesink location=track1.wv
40 * This pipeline encodes audio from an audio CD into a Wavpack file using
41 * lossless encoding (the file output will be fairly large).
45 * gst-launch cdda://1 ! wavpackenc bitrate=128000 ! filesink location=track1.wv
47 * This pipeline encodes audio from an audio CD into a Wavpack file using
48 * lossy encoding at a certain bitrate (the file will be fairly small).
54 * TODO: - add multichannel handling. channel_mask is:
68 * - add 32 bit float mode. CONFIG_FLOAT_DATA
73 #include <glib/gprintf.h>
75 #include <wavpack/wavpack.h>
76 #include "gstwavpackenc.h"
77 #include "gstwavpackcommon.h"
80 static GstFlowReturn gst_wavpack_enc_chain (GstPad * pad, GstBuffer * buffer);
81 static gboolean gst_wavpack_enc_sink_set_caps (GstPad * pad, GstCaps * caps);
82 static int gst_wavpack_enc_push_block (void *id, void *data, int32_t count);
83 static gboolean gst_wavpack_enc_sink_event (GstPad * pad, GstEvent * event);
84 static GstStateChangeReturn gst_wavpack_enc_change_state (GstElement * element,
85 GstStateChange transition);
86 static void gst_wavpack_enc_finalize (GObject * object);
87 static void gst_wavpack_enc_set_property (GObject * object, guint prop_id,
88 const GValue * value, GParamSpec * pspec);
89 static void gst_wavpack_enc_get_property (GObject * object, guint prop_id,
90 GValue * value, GParamSpec * pspec);
100 ARG_EXTRA_PROCESSING,
101 ARG_JOINT_STEREO_MODE
104 GST_DEBUG_CATEGORY_STATIC (gst_wavpack_enc_debug);
105 #define GST_CAT_DEFAULT gst_wavpack_enc_debug
107 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
110 GST_STATIC_CAPS ("audio/x-raw-int, "
113 "endianness = (int) LITTLE_ENDIAN, "
114 "channels = (int) [ 1, 2 ], "
115 "rate = (int) [ 6000, 192000 ]," "signed = (boolean) TRUE;"
119 "endianness = (int) LITTLE_ENDIAN, "
120 "channels = (int) [ 1, 2 ], "
121 "rate = (int) [ 6000, 192000 ]," "signed = (boolean) TRUE;"
125 "endianness = (int) LITTLE_ENDIAN, "
126 "channels = (int) [ 1, 2 ], "
127 "rate = (int) [ 6000, 192000 ]," "signed = (boolean) TRUE;"
131 "endianness = (int) LITTLE_ENDIAN, "
132 "channels = (int) [ 1, 2 ], "
133 "rate = (int) [ 6000, 192000 ]," "signed = (boolean) TRUE")
136 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
139 GST_STATIC_CAPS ("audio/x-wavpack, "
140 "width = (int) { 8, 16, 24, 32 }, "
141 "channels = (int) [ 1, 2 ], "
142 "rate = (int) [ 6000, 192000 ], " "framed = (boolean) TRUE")
145 static GstStaticPadTemplate wvcsrc_factory = GST_STATIC_PAD_TEMPLATE ("wvcsrc",
148 GST_STATIC_CAPS ("audio/x-wavpack-correction, " "framed = (boolean) TRUE")
151 #define DEFAULT_MODE 2
152 #define GST_TYPE_WAVPACK_ENC_MODE (gst_wavpack_enc_mode_get_type ())
154 gst_wavpack_enc_mode_get_type (void)
156 static GType qtype = 0;
159 static const GEnumValue values[] = {
161 /* Very Fast Compression is not supported yet, but will be supported
162 * in future wavpack versions */
163 {0, "Very Fast Compression", "veryfast"},
165 {1, "Fast Compression", "fast"},
166 {2, "Normal Compression", "normal"},
167 {3, "High Compression", "high"},
168 #ifndef WAVPACK_OLD_API
169 {4, "Very High Compression", "veryhigh"},
174 qtype = g_enum_register_static ("GstWavpackEncMode", values);
179 #define DEFAULT_CORRECTION_MODE 0
180 #define GST_TYPE_WAVPACK_ENC_CORRECTION_MODE (gst_wavpack_enc_correction_mode_get_type ())
182 gst_wavpack_enc_correction_mode_get_type (void)
184 static GType qtype = 0;
187 static const GEnumValue values[] = {
188 {0, "Create no correction file", "off"},
189 {1, "Create correction file", "on"},
190 {2, "Create optimized correction file", "optimized"},
194 qtype = g_enum_register_static ("GstWavpackEncCorrectionMode", values);
199 #define DEFAULT_JS_MODE 0
200 #define GST_TYPE_WAVPACK_ENC_JOINT_STEREO_MODE (gst_wavpack_enc_joint_stereo_mode_get_type ())
202 gst_wavpack_enc_joint_stereo_mode_get_type (void)
204 static GType qtype = 0;
207 static const GEnumValue values[] = {
209 {1, "left/right", "leftright"},
210 {2, "mid/side", "midside"},
214 qtype = g_enum_register_static ("GstWavpackEncJSMode", values);
219 GST_BOILERPLATE (GstWavpackEnc, gst_wavpack_enc, GstElement, GST_TYPE_ELEMENT);
222 gst_wavpack_enc_base_init (gpointer klass)
224 static GstElementDetails element_details = {
225 "Wavpack audio encoder",
226 "Codec/Encoder/Audio",
227 "Encodes audio with the Wavpack lossless/lossy audio codec",
228 "Sebastian Dröge <slomo@circular-chaos.org>"
230 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
232 /* add pad templates */
233 gst_element_class_add_pad_template (element_class,
234 gst_static_pad_template_get (&sink_factory));
235 gst_element_class_add_pad_template (element_class,
236 gst_static_pad_template_get (&src_factory));
237 gst_element_class_add_pad_template (element_class,
238 gst_static_pad_template_get (&wvcsrc_factory));
240 /* set element details */
241 gst_element_class_set_details (element_class, &element_details);
246 gst_wavpack_enc_class_init (GstWavpackEncClass * klass)
248 GObjectClass *gobject_class = (GObjectClass *) klass;
249 GstElementClass *gstelement_class = (GstElementClass *) klass;
251 parent_class = g_type_class_peek_parent (klass);
253 /* set state change handler */
254 gstelement_class->change_state =
255 GST_DEBUG_FUNCPTR (gst_wavpack_enc_change_state);
256 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_wavpack_enc_finalize);
258 /* set property handlers */
259 gobject_class->set_property =
260 GST_DEBUG_FUNCPTR (gst_wavpack_enc_set_property);
261 gobject_class->get_property =
262 GST_DEBUG_FUNCPTR (gst_wavpack_enc_get_property);
264 /* install all properties */
265 g_object_class_install_property (gobject_class, ARG_MODE,
266 g_param_spec_enum ("mode", "Encoding mode",
267 "Speed versus compression tradeoff.",
268 GST_TYPE_WAVPACK_ENC_MODE, DEFAULT_MODE, G_PARAM_READWRITE));
269 g_object_class_install_property (gobject_class, ARG_BITRATE,
270 g_param_spec_double ("bitrate", "Bitrate",
271 "Try to encode with this average bitrate (bits/sec). "
272 "This enables lossy encoding! A value smaller than 24000.0 disables this.",
273 0.0, 9600000.0, 0.0, G_PARAM_READWRITE));
274 g_object_class_install_property (gobject_class, ARG_BITSPERSAMPLE,
275 g_param_spec_double ("bits-per-sample", "Bits per sample",
276 "Try to encode with this amount of bits per sample. "
277 "This enables lossy encoding! A value smaller than 2.0 disables this.",
278 0.0, 24.0, 0.0, G_PARAM_READWRITE));
279 g_object_class_install_property (gobject_class, ARG_CORRECTION_MODE,
280 g_param_spec_enum ("correction-mode", "Correction file mode",
281 "Use this mode for correction file creation. Only works in lossy mode!",
282 GST_TYPE_WAVPACK_ENC_CORRECTION_MODE, DEFAULT_CORRECTION_MODE,
284 g_object_class_install_property (gobject_class, ARG_MD5,
285 g_param_spec_boolean ("md5", "MD5",
286 "Store MD5 hash of raw samples within the file.", FALSE,
288 g_object_class_install_property (gobject_class, ARG_EXTRA_PROCESSING,
289 g_param_spec_boolean ("extra-processing", "Extra processing",
290 "Extra encode processing.", FALSE, G_PARAM_READWRITE));
291 g_object_class_install_property (gobject_class, ARG_JOINT_STEREO_MODE,
292 g_param_spec_enum ("joint-stereo-mode", "Joint-Stereo mode",
293 "Use this joint-stereo mode.", GST_TYPE_WAVPACK_ENC_JOINT_STEREO_MODE,
294 DEFAULT_JS_MODE, G_PARAM_READWRITE));
298 gst_wavpack_enc_init (GstWavpackEnc * enc, GstWavpackEncClass * gclass)
300 enc->sinkpad = gst_pad_new_from_static_template (&sink_factory, "sink");
301 gst_pad_set_setcaps_function (enc->sinkpad,
302 GST_DEBUG_FUNCPTR (gst_wavpack_enc_sink_set_caps));
303 gst_pad_set_chain_function (enc->sinkpad,
304 GST_DEBUG_FUNCPTR (gst_wavpack_enc_chain));
305 gst_pad_set_event_function (enc->sinkpad,
306 GST_DEBUG_FUNCPTR (gst_wavpack_enc_sink_event));
307 gst_element_add_pad (GST_ELEMENT (enc), enc->sinkpad);
310 enc->srcpad = gst_pad_new_from_static_template (&src_factory, "src");
311 gst_element_add_pad (GST_ELEMENT (enc), enc->srcpad);
313 /* initialize object attributes */
314 enc->wp_config = NULL;
315 enc->wp_context = NULL;
316 enc->first_block = NULL;
317 enc->first_block_size = 0;
318 enc->md5_context = NULL;
323 enc->wv_id = g_new0 (GstWavpackEncWriteID, 1);
324 enc->wv_id->correction = FALSE;
325 enc->wv_id->wavpack_enc = enc;
326 enc->wvc_id = g_new0 (GstWavpackEncWriteID, 1);
327 enc->wvc_id->correction = TRUE;
328 enc->wvc_id->wavpack_enc = enc;
330 /* set default values of params */
331 enc->mode = DEFAULT_MODE;
333 enc->correction_mode = DEFAULT_CORRECTION_MODE;
335 enc->extra_processing = FALSE;
336 enc->joint_stereo_mode = DEFAULT_JS_MODE;
340 gst_wavpack_enc_finalize (GObject * object)
342 GstWavpackEnc *enc = GST_WAVPACK_ENC (object);
344 /* free the blockout helpers */
346 g_free (enc->wvc_id);
348 G_OBJECT_CLASS (parent_class)->finalize (object);
352 gst_wavpack_enc_sink_set_caps (GstPad * pad, GstCaps * caps)
354 GstWavpackEnc *enc = GST_WAVPACK_ENC (gst_pad_get_parent (pad));
355 GstStructure *structure = gst_caps_get_structure (caps, 0);
358 /* check caps and put relevant parts into our object attributes */
359 if (!gst_structure_get_int (structure, "channels", &enc->channels) ||
360 !gst_structure_get_int (structure, "rate", &enc->samplerate) ||
361 !gst_structure_get_int (structure, "width", &enc->width) ||
362 !(gst_structure_get_int (structure, "depth", &depth) ||
363 depth != enc->width)) {
364 GST_ELEMENT_ERROR (enc, LIBRARY, INIT, (NULL),
365 ("got invalid caps: %" GST_PTR_FORMAT, caps));
366 gst_object_unref (enc);
370 /* set fixed src pad caps now that we know what we will get */
371 caps = gst_caps_new_simple ("audio/x-wavpack",
372 "channels", G_TYPE_INT, enc->channels,
373 "rate", G_TYPE_INT, enc->samplerate,
374 "width", G_TYPE_INT, enc->width, "framed", G_TYPE_BOOLEAN, TRUE, NULL);
376 if (!gst_pad_set_caps (enc->srcpad, caps)) {
377 GST_ELEMENT_ERROR (enc, LIBRARY, INIT, (NULL),
378 ("setting caps failed: %" GST_PTR_FORMAT, caps));
379 gst_caps_unref (caps);
380 gst_object_unref (enc);
383 gst_pad_use_fixed_caps (enc->srcpad);
385 gst_caps_unref (caps);
386 gst_object_unref (enc);
391 gst_wavpack_enc_set_wp_config (GstWavpackEnc * enc)
393 enc->wp_config = g_new0 (WavpackConfig, 1);
394 /* set general stream informations in the WavpackConfig */
395 enc->wp_config->bytes_per_sample = (enc->width + 7) >> 3;
396 enc->wp_config->bits_per_sample = enc->width;
397 enc->wp_config->num_channels = enc->channels;
399 /* TODO: handle more than 2 channels correctly! */
400 if (enc->channels == 1) {
401 enc->wp_config->channel_mask = 0x4;
402 } else if (enc->channels == 2) {
403 enc->wp_config->channel_mask = 0x2 | 0x1;
405 enc->wp_config->sample_rate = enc->samplerate;
408 * Set parameters in WavpackConfig
415 enc->wp_config->flags |= CONFIG_VERY_FAST_FLAG;
416 enc->wp_config->flags |= CONFIG_FAST_FLAG;
420 enc->wp_config->flags |= CONFIG_FAST_FLAG;
422 case 2: /* default */
425 enc->wp_config->flags |= CONFIG_HIGH_FLAG;
427 #ifndef WAVPACK_OLD_API
429 enc->wp_config->flags |= CONFIG_HIGH_FLAG;
430 enc->wp_config->flags |= CONFIG_VERY_HIGH_FLAG;
435 /* Bitrate, enables lossy mode */
436 if (enc->bitrate >= 2.0) {
437 enc->wp_config->flags |= CONFIG_HYBRID_FLAG;
438 if (enc->bitrate >= 24000.0) {
439 enc->wp_config->bitrate = enc->bitrate / 1000.0;
440 enc->wp_config->flags |= CONFIG_BITRATE_KBPS;
442 enc->wp_config->bitrate = enc->bitrate;
446 /* Correction Mode, only in lossy mode */
447 if (enc->wp_config->flags & CONFIG_HYBRID_FLAG) {
448 if (enc->correction_mode > 0) {
450 gst_pad_new_from_static_template (&wvcsrc_factory, "wvcsrc");
452 /* try to add correction src pad, don't set correction mode on failure */
453 GstCaps *caps = gst_caps_new_simple ("audio/x-wavpack-correction",
454 "framed", G_TYPE_BOOLEAN, TRUE, NULL);
456 GST_DEBUG_OBJECT (enc, "Adding correction pad with caps %"
457 GST_PTR_FORMAT, caps);
458 if (!gst_pad_set_caps (enc->wvcsrcpad, caps)) {
459 enc->correction_mode = 0;
460 GST_WARNING_OBJECT (enc, "setting correction caps failed");
462 gst_pad_use_fixed_caps (enc->wvcsrcpad);
463 gst_pad_set_active (enc->wvcsrcpad, TRUE);
464 gst_element_add_pad (GST_ELEMENT (enc), enc->wvcsrcpad);
465 enc->wp_config->flags |= CONFIG_CREATE_WVC;
466 if (enc->correction_mode == 2) {
467 enc->wp_config->flags |= CONFIG_OPTIMIZE_WVC;
470 gst_caps_unref (caps);
473 if (enc->correction_mode > 0) {
474 enc->correction_mode = 0;
475 GST_WARNING_OBJECT (enc, "setting correction mode only has "
476 "any effect if a bitrate is provided.");
479 gst_element_no_more_pads (GST_ELEMENT (enc));
481 /* MD5, setup MD5 context */
482 if ((enc->md5) && !(enc->md5_context)) {
483 enc->wp_config->flags |= CONFIG_MD5_CHECKSUM;
484 enc->md5_context = g_new0 (MD5_CTX, 1);
485 MD5Init (enc->md5_context);
488 /* Extra encode processing */
489 if (enc->extra_processing) {
490 enc->wp_config->flags |= CONFIG_EXTRA_MODE;
493 /* Joint stereo mode */
494 switch (enc->joint_stereo_mode) {
495 case 0: /* default */
498 enc->wp_config->flags |= CONFIG_JOINT_OVERRIDE;
499 enc->wp_config->flags &= ~CONFIG_JOINT_STEREO;
502 enc->wp_config->flags |= (CONFIG_JOINT_OVERRIDE | CONFIG_JOINT_STEREO);
508 gst_wavpack_enc_format_samples (const uchar * src_data, uint32_t sample_count,
511 int32_t *data = g_new0 (int32_t, sample_count);
513 /* put all samples into an int32_t*, no matter what
514 * width we have and convert them from little endian
515 * to host byte order */
521 for (i = 0; i < sample_count; i++)
522 data[i] = (int32_t) (int8_t) src_data[i];
525 for (i = 0; i < sample_count; i++)
526 data[i] = (int32_t) src_data[2 * i]
527 | ((int32_t) (int8_t) src_data[2 * i + 1] << 8);
530 for (i = 0; i < sample_count; i++)
531 data[i] = (int32_t) src_data[3 * i]
532 | ((int32_t) src_data[3 * i + 1] << 8)
533 | ((int32_t) (int8_t) src_data[3 * i + 2] << 16);
536 for (i = 0; i < sample_count; i++)
537 data[i] = (int32_t) src_data[4 * i]
538 | ((int32_t) src_data[4 * i + 1] << 8)
539 | ((int32_t) src_data[4 * i + 2] << 16)
540 | ((int32_t) (int8_t) src_data[4 * i + 3] << 24);
548 gst_wavpack_enc_push_block (void *id, void *data, int32_t count)
550 GstWavpackEncWriteID *wid = (GstWavpackEncWriteID *) id;
551 GstWavpackEnc *enc = GST_WAVPACK_ENC (wid->wavpack_enc);
555 guchar *block = (guchar *) data;
557 pad = (wid->correction) ? enc->wvcsrcpad : enc->srcpad;
559 (wid->correction) ? &enc->wvcsrcpad_last_return : &enc->
562 *flow = gst_pad_alloc_buffer_and_set_caps (pad, GST_BUFFER_OFFSET_NONE,
563 count, GST_PAD_CAPS (pad), &buffer);
565 if (*flow != GST_FLOW_OK) {
566 GST_WARNING_OBJECT (enc, "flow on %s:%s = %s",
567 GST_DEBUG_PAD_NAME (pad), gst_flow_get_name (*flow));
571 g_memmove (GST_BUFFER_DATA (buffer), block, count);
573 if (count > sizeof (WavpackHeader) && memcmp (block, "wvpk", 4) == 0) {
574 /* if it's a Wavpack block set buffer timestamp and duration, etc */
577 GST_LOG_OBJECT (enc, "got %d bytes of encoded wavpack %sdata",
578 count, (wid->correction) ? "correction " : "");
580 gst_wavpack_read_header (&wph, block);
582 /* if it's the first wavpack block, send a NEW_SEGMENT event */
583 if (wph.block_index == 0) {
584 gst_pad_push_event (pad,
585 gst_event_new_new_segment (FALSE,
586 1.0, GST_FORMAT_BYTES, 0, GST_BUFFER_OFFSET_NONE, 0));
588 /* save header for later reference, so we can re-send it later on
589 * EOS with fixed up values for total sample count etc. */
590 if (enc->first_block == NULL && !wid->correction) {
591 enc->first_block = g_memdup (block, count);
592 enc->first_block_size = count;
596 /* set buffer timestamp, duration, offset, offset_end from
597 * the wavpack header */
598 GST_BUFFER_TIMESTAMP (buffer) =
599 gst_util_uint64_scale_int (GST_SECOND, wph.block_index,
601 GST_BUFFER_DURATION (buffer) =
602 gst_util_uint64_scale_int (GST_SECOND, wph.block_samples,
604 GST_BUFFER_OFFSET (buffer) = wph.block_index;
605 GST_BUFFER_OFFSET_END (buffer) = wph.block_index + wph.block_samples;
607 /* if it's something else set no timestamp and duration on the buffer */
608 GST_DEBUG_OBJECT (enc, "got %d bytes of unknown data", count);
610 GST_BUFFER_TIMESTAMP (buffer) = GST_CLOCK_TIME_NONE;
611 GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
614 /* push the buffer and forward errors */
615 *flow = gst_pad_push (pad, buffer);
617 if (*flow != GST_FLOW_OK) {
618 GST_WARNING_OBJECT (enc, "flow on %s:%s = %s",
619 GST_DEBUG_PAD_NAME (pad), gst_flow_get_name (*flow));
627 gst_wavpack_enc_chain (GstPad * pad, GstBuffer * buf)
629 GstWavpackEnc *enc = GST_WAVPACK_ENC (gst_pad_get_parent (pad));
630 uint32_t sample_count = GST_BUFFER_SIZE (buf) / ((enc->width + 7) >> 3);
634 /* reset the last returns to GST_FLOW_OK. This is only set to something else
635 * while WavpackPackSamples() or more specific gst_wavpack_enc_push_block()
636 * so not valid anymore */
637 enc->srcpad_last_return = enc->wvcsrcpad_last_return = GST_FLOW_OK;
639 GST_DEBUG ("got %u raw samples", sample_count);
641 /* check if we already have a valid WavpackContext, otherwise make one */
642 if (!enc->wp_context) {
643 /* create raw context */
645 WavpackOpenFileOutput (gst_wavpack_enc_push_block, enc->wv_id,
646 (enc->correction_mode > 0) ? enc->wvc_id : NULL);
647 if (!enc->wp_context) {
648 GST_ELEMENT_ERROR (enc, LIBRARY, INIT, (NULL),
649 ("error creating Wavpack context"));
650 gst_object_unref (enc);
651 gst_buffer_unref (buf);
652 return GST_FLOW_ERROR;
655 /* set the WavpackConfig according to our parameters */
656 gst_wavpack_enc_set_wp_config (enc);
658 /* set the configuration to the context now that we know everything
659 * and initialize the encoder */
660 if (!WavpackSetConfiguration (enc->wp_context,
661 enc->wp_config, (uint32_t) (-1))
662 || !WavpackPackInit (enc->wp_context)) {
663 GST_ELEMENT_ERROR (enc, LIBRARY, SETTINGS, (NULL),
664 ("error setting up wavpack encoding context"));
665 WavpackCloseFile (enc->wp_context);
666 gst_object_unref (enc);
667 gst_buffer_unref (buf);
668 return GST_FLOW_ERROR;
670 GST_DEBUG ("setup of encoding context successfull");
673 /* if we want to append the MD5 sum to the stream update it here
674 * with the current raw samples */
676 MD5Update (enc->md5_context, GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
679 /* put all samples into an int32_t*, no matter what
680 * width we have and convert them from little endian
681 * to host byte order */
683 gst_wavpack_enc_format_samples (GST_BUFFER_DATA (buf), sample_count,
686 gst_buffer_unref (buf);
688 /* encode and handle return values from encoding */
689 if (WavpackPackSamples (enc->wp_context, data, sample_count / enc->channels)) {
690 GST_DEBUG ("encoding samples successful");
693 if ((enc->srcpad_last_return == GST_FLOW_RESEND) ||
694 (enc->wvcsrcpad_last_return == GST_FLOW_RESEND)) {
695 ret = GST_FLOW_RESEND;
696 } else if ((enc->srcpad_last_return == GST_FLOW_OK) ||
697 (enc->wvcsrcpad_last_return == GST_FLOW_OK)) {
699 } else if ((enc->srcpad_last_return == GST_FLOW_NOT_LINKED) &&
700 (enc->wvcsrcpad_last_return == GST_FLOW_NOT_LINKED)) {
701 ret = GST_FLOW_NOT_LINKED;
702 } else if ((enc->srcpad_last_return == GST_FLOW_WRONG_STATE) &&
703 (enc->wvcsrcpad_last_return == GST_FLOW_WRONG_STATE)) {
704 ret = GST_FLOW_WRONG_STATE;
706 GST_ELEMENT_ERROR (enc, LIBRARY, ENCODE, (NULL),
707 ("encoding samples failed"));
708 ret = GST_FLOW_ERROR;
713 gst_object_unref (enc);
718 gst_wavpack_enc_rewrite_first_block (GstWavpackEnc * enc)
720 GstEvent *event = gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_BYTES,
721 0, GST_BUFFER_OFFSET_NONE, 0);
724 g_return_if_fail (enc);
725 g_return_if_fail (enc->first_block);
727 /* update the sample count in the first block */
728 WavpackUpdateNumSamples (enc->wp_context, enc->first_block);
730 /* try to seek to the beginning of the output */
731 ret = gst_pad_push_event (enc->srcpad, event);
733 /* try to rewrite the first block */
734 GST_DEBUG_OBJECT (enc, "rewriting first block ...");
735 ret = gst_wavpack_enc_push_block (enc->wv_id,
736 enc->first_block, enc->first_block_size);
738 GST_WARNING_OBJECT (enc, "rewriting of first block failed. "
739 "Seeking to first block failed!");
744 gst_wavpack_enc_sink_event (GstPad * pad, GstEvent * event)
746 GstWavpackEnc *enc = GST_WAVPACK_ENC (gst_pad_get_parent (pad));
749 GST_DEBUG ("Received %s event on sinkpad", GST_EVENT_TYPE_NAME (event));
751 switch (GST_EVENT_TYPE (event)) {
753 /* Encode all remaining samples and flush them to the src pads */
754 WavpackFlushSamples (enc->wp_context);
756 /* write the MD5 sum if we have to write one */
757 if ((enc->md5) && (enc->md5_context)) {
758 guchar md5_digest[16];
760 MD5Final (md5_digest, enc->md5_context);
761 WavpackStoreMD5Sum (enc->wp_context, md5_digest);
764 /* Try to rewrite the first frame with the correct sample number */
765 if (enc->first_block)
766 gst_wavpack_enc_rewrite_first_block (enc);
768 /* close the context if not already happened */
769 if (enc->wp_context) {
770 WavpackCloseFile (enc->wp_context);
771 enc->wp_context = NULL;
774 ret = gst_pad_event_default (pad, event);
776 case GST_EVENT_NEWSEGMENT:
777 if (enc->wp_context) {
778 GST_WARNING_OBJECT (enc, "got NEWSEGMENT after encoding "
781 /* drop NEWSEGMENT events, we create our own when pushing
782 * the first buffer to the pads */
783 gst_event_unref (event);
787 ret = gst_pad_event_default (pad, event);
791 gst_object_unref (enc);
795 static GstStateChangeReturn
796 gst_wavpack_enc_change_state (GstElement * element, GstStateChange transition)
798 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
799 GstWavpackEnc *enc = GST_WAVPACK_ENC (element);
801 switch (transition) {
802 case GST_STATE_CHANGE_NULL_TO_READY:
803 /* set the last returned GstFlowReturns of the two pads to GST_FLOW_OK
804 * as they're only set to something else in WavpackPackSamples() or more
805 * specific gst_wavpack_enc_push_block() and nothing happened there yet */
806 enc->srcpad_last_return = enc->wvcsrcpad_last_return = GST_FLOW_OK;
807 case GST_STATE_CHANGE_READY_TO_PAUSED:
808 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
813 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
815 switch (transition) {
816 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
818 case GST_STATE_CHANGE_PAUSED_TO_READY:
819 /* close and free everything stream related */
820 if (enc->wp_context) {
821 WavpackCloseFile (enc->wp_context);
822 enc->wp_context = NULL;
824 if (enc->wp_config) {
825 g_free (enc->wp_config);
826 enc->wp_config = NULL;
828 if (enc->first_block) {
829 g_free (enc->first_block);
830 enc->first_block = NULL;
831 enc->first_block_size = 0;
833 if (enc->md5_context) {
834 g_free (enc->md5_context);
835 enc->md5_context = NULL;
838 /* reset the last returns to GST_FLOW_OK. This is only set to something else
839 * while WavpackPackSamples() or more specific gst_wavpack_enc_push_block()
840 * so not valid anymore */
841 enc->srcpad_last_return = enc->wvcsrcpad_last_return = GST_FLOW_OK;
843 case GST_STATE_CHANGE_READY_TO_NULL:
853 gst_wavpack_enc_set_property (GObject * object, guint prop_id,
854 const GValue * value, GParamSpec * pspec)
856 GstWavpackEnc *enc = GST_WAVPACK_ENC (object);
860 enc->mode = g_value_get_enum (value);
863 gdouble val = g_value_get_double (value);
865 if ((val >= 24000.0) && (val <= 9600000.0)) {
872 case ARG_BITSPERSAMPLE:{
873 gdouble val = g_value_get_double (value);
875 if ((val >= 2.0) && (val <= 24.0)) {
882 case ARG_CORRECTION_MODE:
883 enc->correction_mode = g_value_get_enum (value);
886 enc->md5 = g_value_get_boolean (value);
888 case ARG_EXTRA_PROCESSING:
889 enc->extra_processing = g_value_get_boolean (value);
891 case ARG_JOINT_STEREO_MODE:
892 enc->joint_stereo_mode = g_value_get_enum (value);
895 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
901 gst_wavpack_enc_get_property (GObject * object, guint prop_id, GValue * value,
904 GstWavpackEnc *enc = GST_WAVPACK_ENC (object);
908 g_value_set_enum (value, enc->mode);
911 if (enc->bitrate >= 24000.0) {
912 g_value_set_double (value, enc->bitrate);
914 g_value_set_double (value, 0.0);
917 case ARG_BITSPERSAMPLE:
918 if (enc->bitrate <= 24.0) {
919 g_value_set_double (value, enc->bitrate);
921 g_value_set_double (value, 0.0);
924 case ARG_CORRECTION_MODE:
925 g_value_set_enum (value, enc->correction_mode);
928 g_value_set_boolean (value, enc->md5);
930 case ARG_EXTRA_PROCESSING:
931 g_value_set_boolean (value, enc->extra_processing);
933 case ARG_JOINT_STEREO_MODE:
934 g_value_set_enum (value, enc->joint_stereo_mode);
937 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
943 gst_wavpack_enc_plugin_init (GstPlugin * plugin)
945 if (!gst_element_register (plugin, "wavpackenc",
946 GST_RANK_NONE, GST_TYPE_WAVPACK_ENC))
949 GST_DEBUG_CATEGORY_INIT (gst_wavpack_enc_debug, "wavpackenc", 0,