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 1
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[] = {
160 {0, "Fast Compression", "0"},
162 {2, "High Compression", "2"},
166 qtype = g_enum_register_static ("GstWavpackEncMode", values);
171 #define DEFAULT_CORRECTION_MODE 0
172 #define GST_TYPE_WAVPACK_ENC_CORRECTION_MODE (gst_wavpack_enc_correction_mode_get_type ())
174 gst_wavpack_enc_correction_mode_get_type (void)
176 static GType qtype = 0;
179 static const GEnumValue values[] = {
180 {0, "Create no correction file (default)", "0"},
181 {1, "Create correction file", "1"},
182 {2, "Create optimized correction file", "2"},
186 qtype = g_enum_register_static ("GstWavpackEncCorrectionMode", values);
191 #define DEFAULT_JS_MODE 0
192 #define GST_TYPE_WAVPACK_ENC_JOINT_STEREO_MODE (gst_wavpack_enc_joint_stereo_mode_get_type ())
194 gst_wavpack_enc_joint_stereo_mode_get_type (void)
196 static GType qtype = 0;
199 static const GEnumValue values[] = {
200 {0, "auto (default)", "0"},
201 {1, "left/right", "1"},
202 {2, "mid/side", "2"},
206 qtype = g_enum_register_static ("GstWavpackEncJSMode", values);
211 GST_BOILERPLATE (GstWavpackEnc, gst_wavpack_enc, GstElement, GST_TYPE_ELEMENT);
214 gst_wavpack_enc_base_init (gpointer klass)
216 static GstElementDetails element_details = {
217 "Wavpack audio encoder",
218 "Codec/Encoder/Audio",
219 "Encodes audio with the Wavpack lossless/lossy audio codec",
220 "Sebastian Dröge <slomo@circular-chaos.org>"
222 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
224 /* add pad templates */
225 gst_element_class_add_pad_template (element_class,
226 gst_static_pad_template_get (&sink_factory));
227 gst_element_class_add_pad_template (element_class,
228 gst_static_pad_template_get (&src_factory));
229 gst_element_class_add_pad_template (element_class,
230 gst_static_pad_template_get (&wvcsrc_factory));
232 /* set element details */
233 gst_element_class_set_details (element_class, &element_details);
238 gst_wavpack_enc_class_init (GstWavpackEncClass * klass)
240 GObjectClass *gobject_class = (GObjectClass *) klass;
241 GstElementClass *gstelement_class = (GstElementClass *) klass;
243 parent_class = g_type_class_peek_parent (klass);
245 /* set state change handler */
246 gstelement_class->change_state =
247 GST_DEBUG_FUNCPTR (gst_wavpack_enc_change_state);
248 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_wavpack_enc_finalize);
250 /* set property handlers */
251 gobject_class->set_property =
252 GST_DEBUG_FUNCPTR (gst_wavpack_enc_set_property);
253 gobject_class->get_property =
254 GST_DEBUG_FUNCPTR (gst_wavpack_enc_get_property);
256 /* install all properties */
257 g_object_class_install_property (gobject_class, ARG_MODE,
258 g_param_spec_enum ("mode", "Encoding mode",
259 "Speed versus compression tradeoff.",
260 GST_TYPE_WAVPACK_ENC_MODE, DEFAULT_MODE, G_PARAM_READWRITE));
261 g_object_class_install_property (gobject_class, ARG_BITRATE,
262 g_param_spec_double ("bitrate", "Bitrate",
263 "Try to encode with this average bitrate (bits/sec). "
264 "This enables lossy encoding! A value smaller than 24000.0 disables this.",
265 0.0, 9600000.0, 0.0, G_PARAM_READWRITE));
266 g_object_class_install_property (gobject_class, ARG_BITSPERSAMPLE,
267 g_param_spec_double ("bits-per-sample", "Bits per sample",
268 "Try to encode with this amount of bits per sample. "
269 "This enables lossy encoding! A value smaller than 2.0 disables this.",
270 0.0, 24.0, 0.0, G_PARAM_READWRITE));
271 g_object_class_install_property (gobject_class, ARG_CORRECTION_MODE,
272 g_param_spec_enum ("correction-mode", "Correction file mode",
273 "Use this mode for correction file creation. Only works in lossy mode!",
274 GST_TYPE_WAVPACK_ENC_CORRECTION_MODE, DEFAULT_CORRECTION_MODE,
276 g_object_class_install_property (gobject_class, ARG_MD5,
277 g_param_spec_boolean ("md5", "MD5",
278 "Store MD5 hash of raw samples within the file.", FALSE,
280 g_object_class_install_property (gobject_class, ARG_EXTRA_PROCESSING,
281 g_param_spec_boolean ("extra-processing", "Extra processing",
282 "Extra encode processing.", FALSE, G_PARAM_READWRITE));
283 g_object_class_install_property (gobject_class, ARG_JOINT_STEREO_MODE,
284 g_param_spec_enum ("joint-stereo-mode", "Joint-Stereo mode",
285 "Use this joint-stereo mode.", GST_TYPE_WAVPACK_ENC_JOINT_STEREO_MODE,
286 DEFAULT_JS_MODE, G_PARAM_READWRITE));
290 gst_wavpack_enc_init (GstWavpackEnc * enc, GstWavpackEncClass * gclass)
292 enc->sinkpad = gst_pad_new_from_static_template (&sink_factory, "sink");
293 gst_pad_set_setcaps_function (enc->sinkpad,
294 GST_DEBUG_FUNCPTR (gst_wavpack_enc_sink_set_caps));
295 gst_pad_set_chain_function (enc->sinkpad,
296 GST_DEBUG_FUNCPTR (gst_wavpack_enc_chain));
297 gst_pad_set_event_function (enc->sinkpad,
298 GST_DEBUG_FUNCPTR (gst_wavpack_enc_sink_event));
299 gst_element_add_pad (GST_ELEMENT (enc), enc->sinkpad);
302 enc->srcpad = gst_pad_new_from_static_template (&src_factory, "src");
303 gst_element_add_pad (GST_ELEMENT (enc), enc->srcpad);
305 /* initialize object attributes */
306 enc->wp_config = NULL;
307 enc->wp_context = NULL;
308 enc->first_block = NULL;
309 enc->first_block_size = 0;
310 enc->md5_context = NULL;
315 enc->wv_id = g_new0 (GstWavpackEncWriteID, 1);
316 enc->wv_id->correction = FALSE;
317 enc->wv_id->wavpack_enc = enc;
318 enc->wvc_id = g_new0 (GstWavpackEncWriteID, 1);
319 enc->wvc_id->correction = TRUE;
320 enc->wvc_id->wavpack_enc = enc;
322 /* set default values of params */
325 enc->correction_mode = 0;
327 enc->extra_processing = FALSE;
328 enc->joint_stereo_mode = 0;
332 gst_wavpack_enc_finalize (GObject * object)
334 GstWavpackEnc *enc = GST_WAVPACK_ENC (object);
336 /* free the blockout helpers */
338 g_free (enc->wvc_id);
340 G_OBJECT_CLASS (parent_class)->finalize (object);
344 gst_wavpack_enc_sink_set_caps (GstPad * pad, GstCaps * caps)
346 GstWavpackEnc *enc = GST_WAVPACK_ENC (gst_pad_get_parent (pad));
347 GstStructure *structure = gst_caps_get_structure (caps, 0);
350 /* check caps and put relevant parts into our object attributes */
351 if (!gst_structure_get_int (structure, "channels", &enc->channels) ||
352 !gst_structure_get_int (structure, "rate", &enc->samplerate) ||
353 !gst_structure_get_int (structure, "width", &enc->width) ||
354 !(gst_structure_get_int (structure, "depth", &depth) ||
355 depth != enc->width)) {
356 GST_ELEMENT_ERROR (enc, LIBRARY, INIT, (NULL),
357 ("got invalid caps: %" GST_PTR_FORMAT, caps));
358 gst_object_unref (enc);
362 /* set fixed src pad caps now that we know what we will get */
363 caps = gst_caps_new_simple ("audio/x-wavpack",
364 "channels", G_TYPE_INT, enc->channels,
365 "rate", G_TYPE_INT, enc->samplerate,
366 "width", G_TYPE_INT, enc->width, "framed", G_TYPE_BOOLEAN, TRUE, NULL);
368 if (!gst_pad_set_caps (enc->srcpad, caps)) {
369 GST_ELEMENT_ERROR (enc, LIBRARY, INIT, (NULL),
370 ("setting caps failed: %" GST_PTR_FORMAT, caps));
371 gst_caps_unref (caps);
372 gst_object_unref (enc);
375 gst_pad_use_fixed_caps (enc->srcpad);
377 gst_caps_unref (caps);
378 gst_object_unref (enc);
383 gst_wavpack_enc_set_wp_config (GstWavpackEnc * enc)
385 enc->wp_config = g_new0 (WavpackConfig, 1);
386 /* set general stream informations in the WavpackConfig */
387 enc->wp_config->bytes_per_sample = (enc->width + 7) >> 3;
388 enc->wp_config->bits_per_sample = enc->width;
389 enc->wp_config->num_channels = enc->channels;
391 /* TODO: handle more than 2 channels correctly! */
392 if (enc->channels == 1) {
393 enc->wp_config->channel_mask = 0x4;
394 } else if (enc->channels == 2) {
395 enc->wp_config->channel_mask = 0x2 | 0x1;
397 enc->wp_config->sample_rate = enc->samplerate;
400 * Set parameters in WavpackConfig
406 enc->wp_config->flags |= CONFIG_FAST_FLAG;
408 case 1: /* default */
411 enc->wp_config->flags |= CONFIG_HIGH_FLAG;
415 /* Bitrate, enables lossy mode */
416 if (enc->bitrate >= 2.0) {
417 enc->wp_config->flags |= CONFIG_HYBRID_FLAG;
418 if (enc->bitrate >= 24000.0) {
419 enc->wp_config->bitrate = enc->bitrate / 1000.0;
420 enc->wp_config->flags |= CONFIG_BITRATE_KBPS;
422 enc->wp_config->bitrate = enc->bitrate;
426 /* Correction Mode, only in lossy mode */
427 if (enc->wp_config->flags & CONFIG_HYBRID_FLAG) {
428 if (enc->correction_mode > 0) {
430 gst_pad_new_from_static_template (&wvcsrc_factory, "wvcsrc");
432 /* try to add correction src pad, don't set correction mode on failure */
433 GstCaps *caps = gst_caps_new_simple ("audio/x-wavpack-correction",
434 "framed", G_TYPE_BOOLEAN, TRUE, NULL);
436 gst_element_no_more_pads (GST_ELEMENT (enc));
438 GST_DEBUG_OBJECT (enc, "Adding correction pad with caps %"
439 GST_PTR_FORMAT, caps);
440 if (!gst_pad_set_caps (enc->wvcsrcpad, caps)) {
441 enc->correction_mode = 0;
442 GST_WARNING_OBJECT (enc, "setting correction caps failed");
444 gst_pad_use_fixed_caps (enc->wvcsrcpad);
445 gst_pad_set_active (enc->wvcsrcpad, TRUE);
446 gst_element_add_pad (GST_ELEMENT (enc), enc->wvcsrcpad);
447 enc->wp_config->flags |= CONFIG_CREATE_WVC;
448 if (enc->correction_mode == 2) {
449 enc->wp_config->flags |= CONFIG_OPTIMIZE_WVC;
452 gst_caps_unref (caps);
455 if (enc->correction_mode > 0) {
456 enc->correction_mode = 0;
457 GST_WARNING_OBJECT (enc, "setting correction mode only has "
458 "any effect if a bitrate is provided.");
461 gst_element_no_more_pads (GST_ELEMENT (enc));
463 /* MD5, setup MD5 context */
464 if ((enc->md5) && !(enc->md5_context)) {
465 enc->wp_config->flags |= CONFIG_MD5_CHECKSUM;
466 enc->md5_context = g_new0 (MD5_CTX, 1);
467 MD5Init (enc->md5_context);
470 /* Extra encode processing */
471 if (enc->extra_processing) {
472 enc->wp_config->flags |= CONFIG_EXTRA_MODE;
475 /* Joint stereo mode */
476 switch (enc->joint_stereo_mode) {
477 case 0: /* default */
480 enc->wp_config->flags |= CONFIG_JOINT_OVERRIDE;
481 enc->wp_config->flags &= ~CONFIG_JOINT_STEREO;
484 enc->wp_config->flags |= (CONFIG_JOINT_OVERRIDE | CONFIG_JOINT_STEREO);
490 gst_wavpack_enc_format_samples (const uchar * src_data, uint32_t sample_count,
493 int32_t *data = g_new0 (int32_t, sample_count);
495 /* put all samples into an int32_t*, no matter what
496 * width we have and convert them from little endian
497 * to host byte order */
503 for (i = 0; i < sample_count; i++)
504 data[i] = (int32_t) (int8_t) src_data[i];
507 for (i = 0; i < sample_count; i++)
508 data[i] = (int32_t) src_data[2 * i]
509 | ((int32_t) (int8_t) src_data[2 * i + 1] << 8);
512 for (i = 0; i < sample_count; i++)
513 data[i] = (int32_t) src_data[3 * i]
514 | ((int32_t) src_data[3 * i + 1] << 8)
515 | ((int32_t) (int8_t) src_data[3 * i + 2] << 16);
518 for (i = 0; i < sample_count; i++)
519 data[i] = (int32_t) src_data[4 * i]
520 | ((int32_t) src_data[4 * i + 1] << 8)
521 | ((int32_t) src_data[4 * i + 2] << 16)
522 | ((int32_t) (int8_t) src_data[4 * i + 3] << 24);
530 gst_wavpack_enc_push_block (void *id, void *data, int32_t count)
532 GstWavpackEncWriteID *wid = (GstWavpackEncWriteID *) id;
533 GstWavpackEnc *enc = GST_WAVPACK_ENC (wid->wavpack_enc);
537 guchar *block = (guchar *) data;
539 pad = (wid->correction) ? enc->wvcsrcpad : enc->srcpad;
541 (wid->correction) ? &enc->wvcsrcpad_last_return : &enc->
544 *flow = gst_pad_alloc_buffer_and_set_caps (pad, GST_BUFFER_OFFSET_NONE,
545 count, GST_PAD_CAPS (pad), &buffer);
547 if (*flow != GST_FLOW_OK) {
548 GST_WARNING_OBJECT (enc, "flow on %s:%s = %s",
549 GST_DEBUG_PAD_NAME (pad), gst_flow_get_name (*flow));
553 g_memmove (GST_BUFFER_DATA (buffer), block, count);
555 if (count > sizeof (WavpackHeader) && memcmp (block, "wvpk", 4) == 0) {
556 /* if it's a Wavpack block set buffer timestamp and duration, etc */
559 GST_LOG_OBJECT (enc, "got %d bytes of encoded wavpack %sdata",
560 count, (wid->correction) ? "correction " : "");
562 gst_wavpack_read_header (&wph, block);
564 /* if it's the first wavpack block, send a NEW_SEGMENT event */
565 if (wph.block_index == 0) {
566 gst_pad_push_event (pad,
567 gst_event_new_new_segment (FALSE,
568 1.0, GST_FORMAT_BYTES, 0, GST_BUFFER_OFFSET_NONE, 0));
570 /* save header for later reference, so we can re-send it later on
571 * EOS with fixed up values for total sample count etc. */
572 if (enc->first_block == NULL && !wid->correction) {
573 enc->first_block = g_memdup (block, count);
574 enc->first_block_size = count;
578 /* set buffer timestamp, duration, offset, offset_end from
579 * the wavpack header */
580 GST_BUFFER_TIMESTAMP (buffer) =
581 gst_util_uint64_scale_int (GST_SECOND, wph.block_index,
583 GST_BUFFER_DURATION (buffer) =
584 gst_util_uint64_scale_int (GST_SECOND, wph.block_samples,
586 GST_BUFFER_OFFSET (buffer) = wph.block_index;
587 GST_BUFFER_OFFSET_END (buffer) = wph.block_index + wph.block_samples;
589 /* if it's something else set no timestamp and duration on the buffer */
590 GST_DEBUG_OBJECT (enc, "got %d bytes of unknown data", count);
592 GST_BUFFER_TIMESTAMP (buffer) = GST_CLOCK_TIME_NONE;
593 GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
596 /* push the buffer and forward errors */
597 *flow = gst_pad_push (pad, buffer);
599 if (*flow != GST_FLOW_OK) {
600 GST_WARNING_OBJECT (enc, "flow on %s:%s = %s",
601 GST_DEBUG_PAD_NAME (pad), gst_flow_get_name (*flow));
609 gst_wavpack_enc_chain (GstPad * pad, GstBuffer * buf)
611 GstWavpackEnc *enc = GST_WAVPACK_ENC (gst_pad_get_parent (pad));
612 uint32_t sample_count = GST_BUFFER_SIZE (buf) / ((enc->width + 7) >> 3);
616 /* reset the last returns to GST_FLOW_OK. This is only set to something else
617 * while WavpackPackSamples() or more specific gst_wavpack_enc_push_block()
618 * so not valid anymore */
619 enc->srcpad_last_return = enc->wvcsrcpad_last_return = GST_FLOW_OK;
621 GST_DEBUG ("got %u raw samples", sample_count);
623 /* check if we already have a valid WavpackContext, otherwise make one */
624 if (!enc->wp_context) {
625 /* create raw context */
627 WavpackOpenFileOutput (gst_wavpack_enc_push_block, enc->wv_id,
628 (enc->correction_mode > 0) ? enc->wvc_id : NULL);
629 if (!enc->wp_context) {
630 GST_ELEMENT_ERROR (enc, LIBRARY, INIT, (NULL),
631 ("error creating Wavpack context"));
632 gst_object_unref (enc);
633 gst_buffer_unref (buf);
634 return GST_FLOW_ERROR;
637 /* set the WavpackConfig according to our parameters */
638 gst_wavpack_enc_set_wp_config (enc);
640 /* set the configuration to the context now that we know everything
641 * and initialize the encoder */
642 if (!WavpackSetConfiguration (enc->wp_context,
643 enc->wp_config, (uint32_t) (-1))
644 || !WavpackPackInit (enc->wp_context)) {
645 GST_ELEMENT_ERROR (enc, LIBRARY, SETTINGS, (NULL),
646 ("error setting up wavpack encoding context"));
647 WavpackCloseFile (enc->wp_context);
648 gst_object_unref (enc);
649 gst_buffer_unref (buf);
650 return GST_FLOW_ERROR;
652 GST_DEBUG ("setup of encoding context successfull");
655 /* if we want to append the MD5 sum to the stream update it here
656 * with the current raw samples */
658 MD5Update (enc->md5_context, GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
661 /* put all samples into an int32_t*, no matter what
662 * width we have and convert them from little endian
663 * to host byte order */
665 gst_wavpack_enc_format_samples (GST_BUFFER_DATA (buf), sample_count,
668 gst_buffer_unref (buf);
670 /* encode and handle return values from encoding */
671 if (WavpackPackSamples (enc->wp_context, data, sample_count / enc->channels)) {
672 GST_DEBUG ("encoding samples successful");
675 if ((enc->srcpad_last_return == GST_FLOW_RESEND) ||
676 (enc->wvcsrcpad_last_return == GST_FLOW_RESEND)) {
677 ret = GST_FLOW_RESEND;
678 } else if ((enc->srcpad_last_return == GST_FLOW_OK) ||
679 (enc->wvcsrcpad_last_return == GST_FLOW_OK)) {
681 } else if ((enc->srcpad_last_return == GST_FLOW_NOT_LINKED) &&
682 (enc->wvcsrcpad_last_return == GST_FLOW_NOT_LINKED)) {
683 ret = GST_FLOW_NOT_LINKED;
684 } else if ((enc->srcpad_last_return == GST_FLOW_WRONG_STATE) &&
685 (enc->wvcsrcpad_last_return == GST_FLOW_WRONG_STATE)) {
686 ret = GST_FLOW_WRONG_STATE;
688 GST_ELEMENT_ERROR (enc, LIBRARY, ENCODE, (NULL),
689 ("encoding samples failed"));
690 ret = GST_FLOW_ERROR;
695 gst_object_unref (enc);
700 gst_wavpack_enc_rewrite_first_block (GstWavpackEnc * enc)
702 GstEvent *event = gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_BYTES,
703 0, GST_BUFFER_OFFSET_NONE, 0);
706 g_return_if_fail (enc);
707 g_return_if_fail (enc->first_block);
709 /* update the sample count in the first block */
710 WavpackUpdateNumSamples (enc->wp_context, enc->first_block);
712 /* try to seek to the beginning of the output */
713 ret = gst_pad_push_event (enc->srcpad, event);
715 /* try to rewrite the first block */
716 GST_DEBUG_OBJECT (enc, "rewriting first block ...");
717 ret = gst_wavpack_enc_push_block (enc->wv_id,
718 enc->first_block, enc->first_block_size);
720 GST_WARNING_OBJECT (enc, "rewriting of first block failed. "
721 "Seeking to first block failed!");
726 gst_wavpack_enc_sink_event (GstPad * pad, GstEvent * event)
728 GstWavpackEnc *enc = GST_WAVPACK_ENC (gst_pad_get_parent (pad));
731 GST_DEBUG ("Received %s event on sinkpad", GST_EVENT_TYPE_NAME (event));
733 switch (GST_EVENT_TYPE (event)) {
735 /* Encode all remaining samples and flush them to the src pads */
736 WavpackFlushSamples (enc->wp_context);
738 /* write the MD5 sum if we have to write one */
739 if ((enc->md5) && (enc->md5_context)) {
740 guchar md5_digest[16];
742 MD5Final (md5_digest, enc->md5_context);
743 WavpackStoreMD5Sum (enc->wp_context, md5_digest);
746 /* Try to rewrite the first frame with the correct sample number */
747 if (enc->first_block)
748 gst_wavpack_enc_rewrite_first_block (enc);
750 /* close the context if not already happened */
751 if (enc->wp_context) {
752 WavpackCloseFile (enc->wp_context);
753 enc->wp_context = NULL;
756 ret = gst_pad_event_default (pad, event);
758 case GST_EVENT_NEWSEGMENT:
759 if (enc->wp_context) {
760 GST_WARNING_OBJECT (enc, "got NEWSEGMENT after encoding "
763 /* drop NEWSEGMENT events, we create our own when pushing
764 * the first buffer to the pads */
765 gst_event_unref (event);
769 ret = gst_pad_event_default (pad, event);
773 gst_object_unref (enc);
777 static GstStateChangeReturn
778 gst_wavpack_enc_change_state (GstElement * element, GstStateChange transition)
780 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
781 GstWavpackEnc *enc = GST_WAVPACK_ENC (element);
783 switch (transition) {
784 case GST_STATE_CHANGE_NULL_TO_READY:
785 /* set the last returned GstFlowReturns of the two pads to GST_FLOW_OK
786 * as they're only set to something else in WavpackPackSamples() or more
787 * specific gst_wavpack_enc_push_block() and nothing happened there yet */
788 enc->srcpad_last_return = enc->wvcsrcpad_last_return = GST_FLOW_OK;
789 case GST_STATE_CHANGE_READY_TO_PAUSED:
790 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
795 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
797 switch (transition) {
798 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
800 case GST_STATE_CHANGE_PAUSED_TO_READY:
801 /* close and free everything stream related */
802 if (enc->wp_context) {
803 WavpackCloseFile (enc->wp_context);
804 enc->wp_context = NULL;
806 if (enc->wp_config) {
807 g_free (enc->wp_config);
808 enc->wp_config = NULL;
810 if (enc->first_block) {
811 g_free (enc->first_block);
812 enc->first_block = NULL;
813 enc->first_block_size = 0;
815 if (enc->md5_context) {
816 g_free (enc->md5_context);
817 enc->md5_context = NULL;
820 /* reset the last returns to GST_FLOW_OK. This is only set to something else
821 * while WavpackPackSamples() or more specific gst_wavpack_enc_push_block()
822 * so not valid anymore */
823 enc->srcpad_last_return = enc->wvcsrcpad_last_return = GST_FLOW_OK;
825 case GST_STATE_CHANGE_READY_TO_NULL:
835 gst_wavpack_enc_set_property (GObject * object, guint prop_id,
836 const GValue * value, GParamSpec * pspec)
838 GstWavpackEnc *enc = GST_WAVPACK_ENC (object);
842 enc->mode = g_value_get_enum (value);
845 gdouble val = g_value_get_double (value);
847 if ((val >= 24000.0) && (val <= 9600000.0)) {
854 case ARG_BITSPERSAMPLE:{
855 gdouble val = g_value_get_double (value);
857 if ((val >= 2.0) && (val <= 24.0)) {
864 case ARG_CORRECTION_MODE:
865 enc->correction_mode = g_value_get_enum (value);
868 enc->md5 = g_value_get_boolean (value);
870 case ARG_EXTRA_PROCESSING:
871 enc->extra_processing = g_value_get_boolean (value);
873 case ARG_JOINT_STEREO_MODE:
874 enc->joint_stereo_mode = g_value_get_enum (value);
877 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
883 gst_wavpack_enc_get_property (GObject * object, guint prop_id, GValue * value,
886 GstWavpackEnc *enc = GST_WAVPACK_ENC (object);
890 g_value_set_enum (value, enc->mode);
893 if (enc->bitrate >= 24000.0) {
894 g_value_set_double (value, enc->bitrate);
896 g_value_set_double (value, 0.0);
899 case ARG_BITSPERSAMPLE:
900 if (enc->bitrate <= 24.0) {
901 g_value_set_double (value, enc->bitrate);
903 g_value_set_double (value, 0.0);
906 case ARG_CORRECTION_MODE:
907 g_value_set_enum (value, enc->correction_mode);
910 g_value_set_boolean (value, enc->md5);
912 case ARG_EXTRA_PROCESSING:
913 g_value_set_boolean (value, enc->extra_processing);
915 case ARG_JOINT_STEREO_MODE:
916 g_value_set_enum (value, enc->joint_stereo_mode);
919 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
925 gst_wavpack_enc_plugin_init (GstPlugin * plugin)
927 if (!gst_element_register (plugin, "wavpackenc",
928 GST_RANK_NONE, GST_TYPE_WAVPACK_ENC))
931 GST_DEBUG_CATEGORY_INIT (gst_wavpack_enc_debug, "wavpackenc", 0,