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., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 * SECTION:element-wavpackenc
25 * WavpackEnc encodes raw audio into a framed Wavpack stream.
26 * <ulink url="http://www.wavpack.com/">Wavpack</ulink> is an open-source
27 * audio codec that features both lossless and lossy encoding.
30 * <title>Example launch line</title>
32 * gst-launch-1.0 audiotestsrc num-buffers=500 ! audioconvert ! wavpackenc ! filesink location=sinewave.wv
33 * ]| This pipeline encodes audio from audiotestsrc into a Wavpack file. The audioconvert element is needed
34 * as the Wavpack encoder only accepts input with 32 bit width.
36 * gst-launch-1.0 cdda://1 ! audioconvert ! wavpackenc ! filesink location=track1.wv
37 * ]| This pipeline encodes audio from an audio CD into a Wavpack file using
38 * lossless encoding (the file output will be fairly large).
40 * gst-launch-1.0 cdda://1 ! audioconvert ! wavpackenc bitrate=128000 ! filesink location=track1.wv
41 * ]| This pipeline encodes audio from an audio CD into a Wavpack file using
42 * lossy encoding at a certain bitrate (the file will be fairly small).
47 * TODO: - add 32 bit float mode. CONFIG_FLOAT_DATA
52 #include <glib/gprintf.h>
54 #include <wavpack/wavpack.h>
55 #include "gstwavpackenc.h"
56 #include "gstwavpackcommon.h"
58 static gboolean gst_wavpack_enc_start (GstAudioEncoder * enc);
59 static gboolean gst_wavpack_enc_stop (GstAudioEncoder * enc);
60 static gboolean gst_wavpack_enc_set_format (GstAudioEncoder * enc,
62 static GstFlowReturn gst_wavpack_enc_handle_frame (GstAudioEncoder * enc,
64 static gboolean gst_wavpack_enc_sink_event (GstAudioEncoder * enc,
67 static int gst_wavpack_enc_push_block (void *id, void *data, int32_t count);
68 static GstFlowReturn gst_wavpack_enc_drain (GstWavpackEnc * enc);
70 static void gst_wavpack_enc_set_property (GObject * object, guint prop_id,
71 const GValue * value, GParamSpec * pspec);
72 static void gst_wavpack_enc_get_property (GObject * object, guint prop_id,
73 GValue * value, GParamSpec * pspec);
87 GST_DEBUG_CATEGORY_STATIC (gst_wavpack_enc_debug);
88 #define GST_CAT_DEFAULT gst_wavpack_enc_debug
90 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
93 GST_STATIC_CAPS ("audio/x-raw, "
94 "format = (string) " GST_AUDIO_NE (S32) ", "
95 "layout = (string) interleaved, "
96 "channels = (int) [ 1, 8 ], " "rate = (int) [ 6000, 192000 ]")
99 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
102 GST_STATIC_CAPS ("audio/x-wavpack, "
103 "depth = (int) [ 1, 32 ], "
104 "channels = (int) [ 1, 8 ], "
105 "rate = (int) [ 6000, 192000 ], " "framed = (boolean) TRUE")
108 static GstStaticPadTemplate wvcsrc_factory = GST_STATIC_PAD_TEMPLATE ("wvcsrc",
111 GST_STATIC_CAPS ("audio/x-wavpack-correction, " "framed = (boolean) TRUE")
116 GST_WAVPACK_ENC_MODE_VERY_FAST = 0,
117 GST_WAVPACK_ENC_MODE_FAST,
118 GST_WAVPACK_ENC_MODE_DEFAULT,
119 GST_WAVPACK_ENC_MODE_HIGH,
120 GST_WAVPACK_ENC_MODE_VERY_HIGH
123 #define GST_TYPE_WAVPACK_ENC_MODE (gst_wavpack_enc_mode_get_type ())
125 gst_wavpack_enc_mode_get_type (void)
127 static GType qtype = 0;
130 static const GEnumValue values[] = {
132 /* Very Fast Compression is not supported yet, but will be supported
133 * in future wavpack versions */
134 {GST_WAVPACK_ENC_MODE_VERY_FAST, "Very Fast Compression", "veryfast"},
136 {GST_WAVPACK_ENC_MODE_FAST, "Fast Compression", "fast"},
137 {GST_WAVPACK_ENC_MODE_DEFAULT, "Normal Compression", "normal"},
138 {GST_WAVPACK_ENC_MODE_HIGH, "High Compression", "high"},
139 {GST_WAVPACK_ENC_MODE_VERY_HIGH, "Very High Compression", "veryhigh"},
143 qtype = g_enum_register_static ("GstWavpackEncMode", values);
150 GST_WAVPACK_CORRECTION_MODE_OFF = 0,
151 GST_WAVPACK_CORRECTION_MODE_ON,
152 GST_WAVPACK_CORRECTION_MODE_OPTIMIZED
155 #define GST_TYPE_WAVPACK_ENC_CORRECTION_MODE (gst_wavpack_enc_correction_mode_get_type ())
157 gst_wavpack_enc_correction_mode_get_type (void)
159 static GType qtype = 0;
162 static const GEnumValue values[] = {
163 {GST_WAVPACK_CORRECTION_MODE_OFF, "Create no correction file", "off"},
164 {GST_WAVPACK_CORRECTION_MODE_ON, "Create correction file", "on"},
165 {GST_WAVPACK_CORRECTION_MODE_OPTIMIZED,
166 "Create optimized correction file", "optimized"},
170 qtype = g_enum_register_static ("GstWavpackEncCorrectionMode", values);
177 GST_WAVPACK_JS_MODE_AUTO = 0,
178 GST_WAVPACK_JS_MODE_LEFT_RIGHT,
179 GST_WAVPACK_JS_MODE_MID_SIDE
182 #define GST_TYPE_WAVPACK_ENC_JOINT_STEREO_MODE (gst_wavpack_enc_joint_stereo_mode_get_type ())
184 gst_wavpack_enc_joint_stereo_mode_get_type (void)
186 static GType qtype = 0;
189 static const GEnumValue values[] = {
190 {GST_WAVPACK_JS_MODE_AUTO, "auto", "auto"},
191 {GST_WAVPACK_JS_MODE_LEFT_RIGHT, "left/right", "leftright"},
192 {GST_WAVPACK_JS_MODE_MID_SIDE, "mid/side", "midside"},
196 qtype = g_enum_register_static ("GstWavpackEncJSMode", values);
201 #define gst_wavpack_enc_parent_class parent_class
202 G_DEFINE_TYPE (GstWavpackEnc, gst_wavpack_enc, GST_TYPE_AUDIO_ENCODER);
205 gst_wavpack_enc_class_init (GstWavpackEncClass * klass)
207 GObjectClass *gobject_class = (GObjectClass *) klass;
208 GstElementClass *element_class = (GstElementClass *) (klass);
209 GstAudioEncoderClass *base_class = (GstAudioEncoderClass *) (klass);
211 /* add pad templates */
212 gst_element_class_add_pad_template (element_class,
213 gst_static_pad_template_get (&sink_factory));
214 gst_element_class_add_pad_template (element_class,
215 gst_static_pad_template_get (&src_factory));
216 gst_element_class_add_pad_template (element_class,
217 gst_static_pad_template_get (&wvcsrc_factory));
219 /* set element details */
220 gst_element_class_set_static_metadata (element_class, "Wavpack audio encoder",
221 "Codec/Encoder/Audio",
222 "Encodes audio with the Wavpack lossless/lossy audio codec",
223 "Sebastian Dröge <slomo@circular-chaos.org>");
225 /* set property handlers */
226 gobject_class->set_property = gst_wavpack_enc_set_property;
227 gobject_class->get_property = gst_wavpack_enc_get_property;
229 base_class->start = GST_DEBUG_FUNCPTR (gst_wavpack_enc_start);
230 base_class->stop = GST_DEBUG_FUNCPTR (gst_wavpack_enc_stop);
231 base_class->set_format = GST_DEBUG_FUNCPTR (gst_wavpack_enc_set_format);
232 base_class->handle_frame = GST_DEBUG_FUNCPTR (gst_wavpack_enc_handle_frame);
233 base_class->sink_event = GST_DEBUG_FUNCPTR (gst_wavpack_enc_sink_event);
235 /* install all properties */
236 g_object_class_install_property (gobject_class, ARG_MODE,
237 g_param_spec_enum ("mode", "Encoding mode",
238 "Speed versus compression tradeoff.",
239 GST_TYPE_WAVPACK_ENC_MODE, GST_WAVPACK_ENC_MODE_DEFAULT,
240 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
241 g_object_class_install_property (gobject_class, ARG_BITRATE,
242 g_param_spec_uint ("bitrate", "Bitrate",
243 "Try to encode with this average bitrate (bits/sec). "
244 "This enables lossy encoding, values smaller than 24000 disable it again.",
245 0, 9600000, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
246 g_object_class_install_property (gobject_class, ARG_BITSPERSAMPLE,
247 g_param_spec_double ("bits-per-sample", "Bits per sample",
248 "Try to encode with this amount of bits per sample. "
249 "This enables lossy encoding, values smaller than 2.0 disable it again.",
250 0.0, 24.0, 0.0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
251 g_object_class_install_property (gobject_class, ARG_CORRECTION_MODE,
252 g_param_spec_enum ("correction-mode", "Correction stream mode",
253 "Use this mode for the correction stream. Only works in lossy mode!",
254 GST_TYPE_WAVPACK_ENC_CORRECTION_MODE, GST_WAVPACK_CORRECTION_MODE_OFF,
255 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
256 g_object_class_install_property (gobject_class, ARG_MD5,
257 g_param_spec_boolean ("md5", "MD5",
258 "Store MD5 hash of raw samples within the file.", FALSE,
259 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
260 g_object_class_install_property (gobject_class, ARG_EXTRA_PROCESSING,
261 g_param_spec_uint ("extra-processing", "Extra processing",
262 "Use better but slower filters for better compression/quality.",
263 0, 6, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
264 g_object_class_install_property (gobject_class, ARG_JOINT_STEREO_MODE,
265 g_param_spec_enum ("joint-stereo-mode", "Joint-Stereo mode",
266 "Use this joint-stereo mode.", GST_TYPE_WAVPACK_ENC_JOINT_STEREO_MODE,
267 GST_WAVPACK_JS_MODE_AUTO,
268 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
272 gst_wavpack_enc_reset (GstWavpackEnc * enc)
274 /* close and free everything stream related if we already did something */
275 if (enc->wp_context) {
276 WavpackCloseFile (enc->wp_context);
277 enc->wp_context = NULL;
279 if (enc->wp_config) {
280 g_free (enc->wp_config);
281 enc->wp_config = NULL;
283 if (enc->first_block) {
284 g_free (enc->first_block);
285 enc->first_block = NULL;
287 enc->first_block_size = 0;
288 if (enc->md5_context) {
289 g_checksum_free (enc->md5_context);
290 enc->md5_context = NULL;
292 if (enc->pending_segment)
293 gst_event_unref (enc->pending_segment);
294 enc->pending_segment = NULL;
296 if (enc->pending_buffer) {
297 gst_buffer_unref (enc->pending_buffer);
298 enc->pending_buffer = NULL;
299 enc->pending_offset = 0;
302 /* reset the last returns to GST_FLOW_OK. This is only set to something else
303 * while WavpackPackSamples() or more specific gst_wavpack_enc_push_block()
304 * so not valid anymore */
305 enc->srcpad_last_return = enc->wvcsrcpad_last_return = GST_FLOW_OK;
307 /* reset stream information */
311 enc->channel_mask = 0;
312 enc->need_channel_remap = FALSE;
314 enc->timestamp_offset = GST_CLOCK_TIME_NONE;
315 enc->next_ts = GST_CLOCK_TIME_NONE;
319 gst_wavpack_enc_init (GstWavpackEnc * enc)
321 GstAudioEncoder *benc = GST_AUDIO_ENCODER (enc);
323 /* initialize object attributes */
324 enc->wp_config = NULL;
325 enc->wp_context = NULL;
326 enc->first_block = NULL;
327 enc->md5_context = NULL;
328 gst_wavpack_enc_reset (enc);
330 enc->wv_id.correction = FALSE;
331 enc->wv_id.wavpack_enc = enc;
332 enc->wv_id.passthrough = FALSE;
333 enc->wvc_id.correction = TRUE;
334 enc->wvc_id.wavpack_enc = enc;
335 enc->wvc_id.passthrough = FALSE;
337 /* set default values of params */
338 enc->mode = GST_WAVPACK_ENC_MODE_DEFAULT;
341 enc->correction_mode = GST_WAVPACK_CORRECTION_MODE_OFF;
343 enc->extra_processing = 0;
344 enc->joint_stereo_mode = GST_WAVPACK_JS_MODE_AUTO;
346 /* require perfect ts */
347 gst_audio_encoder_set_perfect_timestamp (benc, TRUE);
349 GST_PAD_SET_ACCEPT_TEMPLATE (GST_AUDIO_ENCODER_SINK_PAD (enc));
354 gst_wavpack_enc_start (GstAudioEncoder * enc)
356 GST_DEBUG_OBJECT (enc, "start");
362 gst_wavpack_enc_stop (GstAudioEncoder * enc)
364 GstWavpackEnc *wpenc = GST_WAVPACK_ENC (enc);
366 GST_DEBUG_OBJECT (enc, "stop");
367 gst_wavpack_enc_reset (wpenc);
373 gst_wavpack_enc_set_format (GstAudioEncoder * benc, GstAudioInfo * info)
375 GstWavpackEnc *enc = GST_WAVPACK_ENC (benc);
376 GstAudioChannelPosition *pos;
377 GstAudioChannelPosition opos[64] = { GST_AUDIO_CHANNEL_POSITION_INVALID, };
381 /* we may be configured again, but that change should have cleanup context */
382 g_assert (enc->wp_context == NULL);
384 enc->channels = GST_AUDIO_INFO_CHANNELS (info);
385 enc->depth = GST_AUDIO_INFO_DEPTH (info);
386 enc->samplerate = GST_AUDIO_INFO_RATE (info);
388 pos = info->position;
391 /* If one channel is NONE they'll be all undefined */
392 if (pos != NULL && pos[0] == GST_AUDIO_CHANNEL_POSITION_NONE) {
393 goto invalid_channels;
397 gst_wavpack_get_channel_mask_from_positions (pos, enc->channels);
398 enc->need_channel_remap =
399 gst_wavpack_set_channel_mapping (pos, enc->channels,
400 enc->channel_mapping);
402 /* wavpack caps hold gst mask, not wavpack mask */
403 gst_audio_channel_positions_to_mask (opos, enc->channels, FALSE, &mask);
405 /* set fixed src pad caps now that we know what we will get */
406 caps = gst_caps_new_simple ("audio/x-wavpack",
407 "channels", G_TYPE_INT, enc->channels,
408 "rate", G_TYPE_INT, enc->samplerate,
409 "depth", G_TYPE_INT, enc->depth, "framed", G_TYPE_BOOLEAN, TRUE, NULL);
412 gst_caps_set_simple (caps, "channel-mask", GST_TYPE_BITMASK, mask, NULL);
414 if (!gst_audio_encoder_set_output_format (benc, caps))
415 goto setting_src_caps_failed;
417 gst_caps_unref (caps);
419 /* no special feedback to base class; should provide all available samples */
424 setting_src_caps_failed:
426 GST_DEBUG_OBJECT (enc,
427 "Couldn't set caps on source pad: %" GST_PTR_FORMAT, caps);
428 gst_caps_unref (caps);
433 GST_DEBUG_OBJECT (enc, "input has invalid channel layout");
439 gst_wavpack_enc_set_wp_config (GstWavpackEnc * enc)
441 enc->wp_config = g_new0 (WavpackConfig, 1);
442 /* set general stream informations in the WavpackConfig */
443 enc->wp_config->bytes_per_sample = GST_ROUND_UP_8 (enc->depth) / 8;
444 enc->wp_config->bits_per_sample = enc->depth;
445 enc->wp_config->num_channels = enc->channels;
446 enc->wp_config->channel_mask = enc->channel_mask;
447 enc->wp_config->sample_rate = enc->samplerate;
450 * Set parameters in WavpackConfig
456 case GST_WAVPACK_ENC_MODE_VERY_FAST:
457 enc->wp_config->flags |= CONFIG_VERY_FAST_FLAG;
458 enc->wp_config->flags |= CONFIG_FAST_FLAG;
461 case GST_WAVPACK_ENC_MODE_FAST:
462 enc->wp_config->flags |= CONFIG_FAST_FLAG;
464 case GST_WAVPACK_ENC_MODE_DEFAULT:
466 case GST_WAVPACK_ENC_MODE_HIGH:
467 enc->wp_config->flags |= CONFIG_HIGH_FLAG;
469 case GST_WAVPACK_ENC_MODE_VERY_HIGH:
470 enc->wp_config->flags |= CONFIG_HIGH_FLAG;
471 enc->wp_config->flags |= CONFIG_VERY_HIGH_FLAG;
475 /* Bitrate, enables lossy mode */
477 enc->wp_config->flags |= CONFIG_HYBRID_FLAG;
478 enc->wp_config->flags |= CONFIG_BITRATE_KBPS;
479 enc->wp_config->bitrate = enc->bitrate / 1000.0;
480 } else if (enc->bps) {
481 enc->wp_config->flags |= CONFIG_HYBRID_FLAG;
482 enc->wp_config->bitrate = enc->bps;
485 /* Correction Mode, only in lossy mode */
486 if (enc->wp_config->flags & CONFIG_HYBRID_FLAG) {
487 if (enc->correction_mode > GST_WAVPACK_CORRECTION_MODE_OFF) {
488 GstCaps *caps = gst_caps_new_simple ("audio/x-wavpack-correction",
489 "framed", G_TYPE_BOOLEAN, TRUE, NULL);
492 gst_pad_new_from_static_template (&wvcsrc_factory, "wvcsrc");
494 /* try to add correction src pad, don't set correction mode on failure */
495 GST_DEBUG_OBJECT (enc, "Adding correction pad with caps %"
496 GST_PTR_FORMAT, caps);
497 if (!gst_pad_set_caps (enc->wvcsrcpad, caps)) {
498 enc->correction_mode = 0;
499 GST_WARNING_OBJECT (enc, "setting correction caps failed");
501 gst_pad_use_fixed_caps (enc->wvcsrcpad);
502 gst_pad_set_active (enc->wvcsrcpad, TRUE);
503 gst_element_add_pad (GST_ELEMENT (enc), enc->wvcsrcpad);
504 enc->wp_config->flags |= CONFIG_CREATE_WVC;
505 if (enc->correction_mode == GST_WAVPACK_CORRECTION_MODE_OPTIMIZED) {
506 enc->wp_config->flags |= CONFIG_OPTIMIZE_WVC;
509 gst_caps_unref (caps);
512 if (enc->correction_mode > GST_WAVPACK_CORRECTION_MODE_OFF) {
513 enc->correction_mode = 0;
514 GST_WARNING_OBJECT (enc, "setting correction mode only has "
515 "any effect if a bitrate is provided.");
518 gst_element_no_more_pads (GST_ELEMENT (enc));
520 /* MD5, setup MD5 context */
521 if ((enc->md5) && !(enc->md5_context)) {
522 enc->wp_config->flags |= CONFIG_MD5_CHECKSUM;
523 enc->md5_context = g_checksum_new (G_CHECKSUM_MD5);
526 /* Extra encode processing */
527 if (enc->extra_processing) {
528 enc->wp_config->flags |= CONFIG_EXTRA_MODE;
529 enc->wp_config->xmode = enc->extra_processing;
532 /* Joint stereo mode */
533 switch (enc->joint_stereo_mode) {
534 case GST_WAVPACK_JS_MODE_AUTO:
536 case GST_WAVPACK_JS_MODE_LEFT_RIGHT:
537 enc->wp_config->flags |= CONFIG_JOINT_OVERRIDE;
538 enc->wp_config->flags &= ~CONFIG_JOINT_STEREO;
540 case GST_WAVPACK_JS_MODE_MID_SIDE:
541 enc->wp_config->flags |= (CONFIG_JOINT_OVERRIDE | CONFIG_JOINT_STEREO);
547 gst_wavpack_enc_push_block (void *id, void *data, int32_t count)
549 GstWavpackEncWriteID *wid = (GstWavpackEncWriteID *) id;
550 GstWavpackEnc *enc = GST_WAVPACK_ENC (wid->wavpack_enc);
554 guchar *block = (guchar *) data;
557 pad = (wid->correction) ? enc->wvcsrcpad : GST_AUDIO_ENCODER_SRC_PAD (enc);
559 (wid->correction) ? &enc->
560 wvcsrcpad_last_return : &enc->srcpad_last_return;
562 buffer = gst_buffer_new_and_alloc (count);
563 gst_buffer_fill (buffer, 0, data, count);
565 if (count > sizeof (WavpackHeader) && memcmp (block, "wvpk", 4) == 0) {
566 /* if it's a Wavpack block set buffer timestamp and duration, etc */
569 GST_LOG_OBJECT (enc, "got %d bytes of encoded wavpack %sdata",
570 count, (wid->correction) ? "correction " : "");
572 gst_wavpack_read_header (&wph, block);
574 /* Only set when pushing the first buffer again, in that case
575 * we don't want to delay the buffer or push newsegment events
577 if (!wid->passthrough) {
578 /* Only push complete blocks */
579 if (enc->pending_buffer == NULL) {
580 enc->pending_buffer = buffer;
581 enc->pending_offset = wph.block_index;
582 } else if (enc->pending_offset == wph.block_index) {
583 enc->pending_buffer = gst_buffer_append (enc->pending_buffer, buffer);
585 GST_ERROR ("Got incomplete block, dropping");
586 gst_buffer_unref (enc->pending_buffer);
587 enc->pending_buffer = buffer;
588 enc->pending_offset = wph.block_index;
591 /* Is this the not-final block of multi-channel data? If so, just
592 * accumulate and return here. */
593 if (!(wph.flags & FINAL_BLOCK) && ((block[32] & ID_OPTIONAL_DATA) == 0))
596 buffer = enc->pending_buffer;
597 enc->pending_buffer = NULL;
598 enc->pending_offset = 0;
600 /* only send segment on correction pad,
601 * regular pad is handled normally by baseclass */
602 if (wid->correction && enc->pending_segment) {
603 gst_pad_push_event (pad, enc->pending_segment);
604 enc->pending_segment = NULL;
607 if (wph.block_index == 0) {
608 /* save header for later reference, so we can re-send it later on
609 * EOS with fixed up values for total sample count etc. */
610 if (enc->first_block == NULL && !wid->correction) {
613 gst_buffer_map (buffer, &map, GST_MAP_READ);
614 enc->first_block = g_memdup (map.data, map.size);
615 enc->first_block_size = map.size;
616 gst_buffer_unmap (buffer, &map);
620 samples = wph.block_samples;
622 /* decorate buffer */
623 /* NOTE: this will get overwritten by baseclass, but stay for those
624 * that are pushed directly
625 * FIXME: add setting to baseclass to avoid overwriting it ?? */
626 GST_BUFFER_OFFSET (buffer) = wph.block_index;
627 GST_BUFFER_OFFSET_END (buffer) = wph.block_index + wph.block_samples;
629 /* if it's something else set no timestamp and duration on the buffer */
630 GST_DEBUG_OBJECT (enc, "got %d bytes of unknown data", count);
633 if (wid->correction || wid->passthrough) {
634 /* push the buffer and forward errors */
635 GST_DEBUG_OBJECT (enc, "pushing buffer with %" G_GSIZE_FORMAT " bytes",
636 gst_buffer_get_size (buffer));
637 *flow = gst_pad_push (pad, buffer);
639 GST_DEBUG_OBJECT (enc, "handing frame of %" G_GSIZE_FORMAT " bytes",
640 gst_buffer_get_size (buffer));
641 *flow = gst_audio_encoder_finish_frame (GST_AUDIO_ENCODER (enc), buffer,
645 if (*flow != GST_FLOW_OK) {
646 GST_WARNING_OBJECT (enc, "flow on %s:%s = %s",
647 GST_DEBUG_PAD_NAME (pad), gst_flow_get_name (*flow));
655 gst_wavpack_enc_fix_channel_order (GstWavpackEnc * enc, gint32 * data,
661 for (i = 0; i < nsamples / enc->channels; i++) {
662 for (j = 0; j < enc->channels; j++) {
663 tmp[enc->channel_mapping[j]] = data[j];
665 for (j = 0; j < enc->channels; j++) {
668 data += enc->channels;
673 gst_wavpack_enc_handle_frame (GstAudioEncoder * benc, GstBuffer * buf)
675 GstWavpackEnc *enc = GST_WAVPACK_ENC (benc);
676 uint32_t sample_count;
680 /* base class ensures configuration */
681 g_return_val_if_fail (enc->depth != 0, GST_FLOW_NOT_NEGOTIATED);
683 /* reset the last returns to GST_FLOW_OK. This is only set to something else
684 * while WavpackPackSamples() or more specific gst_wavpack_enc_push_block()
685 * so not valid anymore */
686 enc->srcpad_last_return = enc->wvcsrcpad_last_return = GST_FLOW_OK;
688 if (G_UNLIKELY (!buf))
689 return gst_wavpack_enc_drain (enc);
691 sample_count = gst_buffer_get_size (buf) / 4;
692 GST_DEBUG_OBJECT (enc, "got %u raw samples", sample_count);
694 /* check if we already have a valid WavpackContext, otherwise make one */
695 if (!enc->wp_context) {
696 /* create raw context */
698 WavpackOpenFileOutput (gst_wavpack_enc_push_block, &enc->wv_id,
699 (enc->correction_mode > 0) ? &enc->wvc_id : NULL);
700 if (!enc->wp_context)
703 /* set the WavpackConfig according to our parameters */
704 gst_wavpack_enc_set_wp_config (enc);
706 /* set the configuration to the context now that we know everything
707 * and initialize the encoder */
708 if (!WavpackSetConfiguration (enc->wp_context,
709 enc->wp_config, (uint32_t) (-1))
710 || !WavpackPackInit (enc->wp_context)) {
711 WavpackCloseFile (enc->wp_context);
714 GST_DEBUG_OBJECT (enc, "setup of encoding context successfull");
717 if (enc->need_channel_remap) {
718 buf = gst_buffer_make_writable (buf);
719 gst_buffer_map (buf, &map, GST_MAP_WRITE);
720 gst_wavpack_enc_fix_channel_order (enc, (gint32 *) map.data, sample_count);
721 gst_buffer_unmap (buf, &map);
724 gst_buffer_map (buf, &map, GST_MAP_READ);
726 /* if we want to append the MD5 sum to the stream update it here
727 * with the current raw samples */
729 g_checksum_update (enc->md5_context, map.data, map.size);
732 /* encode and handle return values from encoding */
733 if (WavpackPackSamples (enc->wp_context, (int32_t *) map.data,
734 sample_count / enc->channels)) {
735 GST_DEBUG_OBJECT (enc, "encoding samples successful");
736 gst_buffer_unmap (buf, &map);
739 gst_buffer_unmap (buf, &map);
740 if ((enc->srcpad_last_return == GST_FLOW_OK) ||
741 (enc->wvcsrcpad_last_return == GST_FLOW_OK)) {
743 } else if ((enc->srcpad_last_return == GST_FLOW_NOT_LINKED) &&
744 (enc->wvcsrcpad_last_return == GST_FLOW_NOT_LINKED)) {
745 ret = GST_FLOW_NOT_LINKED;
746 } else if ((enc->srcpad_last_return == GST_FLOW_FLUSHING) &&
747 (enc->wvcsrcpad_last_return == GST_FLOW_FLUSHING)) {
748 ret = GST_FLOW_FLUSHING;
750 goto encoding_failed;
760 GST_ELEMENT_ERROR (enc, LIBRARY, ENCODE, (NULL),
761 ("encoding samples failed"));
762 ret = GST_FLOW_ERROR;
767 GST_ELEMENT_ERROR (enc, LIBRARY, SETTINGS, (NULL),
768 ("error setting up wavpack encoding context"));
769 ret = GST_FLOW_ERROR;
774 GST_ELEMENT_ERROR (enc, LIBRARY, INIT, (NULL),
775 ("error creating Wavpack context"));
776 ret = GST_FLOW_ERROR;
782 gst_wavpack_enc_rewrite_first_block (GstWavpackEnc * enc)
787 gboolean seekable = FALSE;
789 g_return_if_fail (enc);
790 g_return_if_fail (enc->first_block);
792 /* update the sample count in the first block */
793 WavpackUpdateNumSamples (enc->wp_context, enc->first_block);
795 /* try to seek to the beginning of the output */
796 query = gst_query_new_seeking (GST_FORMAT_BYTES);
797 if (gst_pad_peer_query (GST_AUDIO_ENCODER_SRC_PAD (enc), query)) {
800 gst_query_parse_seeking (query, &format, &seekable, NULL, NULL);
801 if (format != GST_FORMAT_BYTES)
804 GST_LOG_OBJECT (enc, "SEEKING query not handled");
806 gst_query_unref (query);
809 GST_DEBUG_OBJECT (enc, "downstream not seekable; not rewriting");
813 gst_segment_init (&segment, GST_FORMAT_BYTES);
814 ret = gst_pad_push_event (GST_AUDIO_ENCODER_SRC_PAD (enc),
815 gst_event_new_segment (&segment));
817 /* try to rewrite the first block */
818 GST_DEBUG_OBJECT (enc, "rewriting first block ...");
819 enc->wv_id.passthrough = TRUE;
820 ret = gst_wavpack_enc_push_block (&enc->wv_id,
821 enc->first_block, enc->first_block_size);
822 enc->wv_id.passthrough = FALSE;
823 g_free (enc->first_block);
824 enc->first_block = NULL;
826 GST_WARNING_OBJECT (enc, "rewriting of first block failed. "
827 "Seeking to first block failed!");
832 gst_wavpack_enc_drain (GstWavpackEnc * enc)
834 if (!enc->wp_context)
837 GST_DEBUG_OBJECT (enc, "draining");
839 /* Encode all remaining samples and flush them to the src pads */
840 WavpackFlushSamples (enc->wp_context);
842 /* Drop all remaining data, this is no complete block otherwise
843 * it would've been pushed already */
844 if (enc->pending_buffer) {
845 gst_buffer_unref (enc->pending_buffer);
846 enc->pending_buffer = NULL;
847 enc->pending_offset = 0;
850 /* write the MD5 sum if we have to write one */
851 if ((enc->md5) && (enc->md5_context)) {
852 guint8 md5_digest[16];
853 gsize digest_len = sizeof (md5_digest);
855 g_checksum_get_digest (enc->md5_context, md5_digest, &digest_len);
856 if (digest_len == sizeof (md5_digest)) {
857 WavpackStoreMD5Sum (enc->wp_context, md5_digest);
858 WavpackFlushSamples (enc->wp_context);
860 GST_WARNING_OBJECT (enc, "Calculating MD5 digest failed");
863 /* Try to rewrite the first frame with the correct sample number */
864 if (enc->first_block)
865 gst_wavpack_enc_rewrite_first_block (enc);
867 /* close the context if not already happened */
868 if (enc->wp_context) {
869 WavpackCloseFile (enc->wp_context);
870 enc->wp_context = NULL;
877 gst_wavpack_enc_sink_event (GstAudioEncoder * benc, GstEvent * event)
879 GstWavpackEnc *enc = GST_WAVPACK_ENC (benc);
881 GST_DEBUG_OBJECT (enc, "Received %s event on sinkpad",
882 GST_EVENT_TYPE_NAME (event));
884 switch (GST_EVENT_TYPE (event)) {
885 case GST_EVENT_SEGMENT:
886 if (enc->wp_context) {
887 GST_WARNING_OBJECT (enc, "got NEWSEGMENT after encoding "
890 /* peek and hold NEWSEGMENT events for sending on correction pad */
891 if (enc->pending_segment)
892 gst_event_unref (enc->pending_segment);
893 enc->pending_segment = gst_event_ref (event);
899 /* baseclass handles rest */
900 return GST_AUDIO_ENCODER_CLASS (parent_class)->sink_event (benc, event);
904 gst_wavpack_enc_set_property (GObject * object, guint prop_id,
905 const GValue * value, GParamSpec * pspec)
907 GstWavpackEnc *enc = GST_WAVPACK_ENC (object);
911 enc->mode = g_value_get_enum (value);
914 guint val = g_value_get_uint (value);
916 if ((val >= 24000) && (val <= 9600000)) {
925 case ARG_BITSPERSAMPLE:{
926 gdouble val = g_value_get_double (value);
928 if ((val >= 2.0) && (val <= 24.0)) {
937 case ARG_CORRECTION_MODE:
938 enc->correction_mode = g_value_get_enum (value);
941 enc->md5 = g_value_get_boolean (value);
943 case ARG_EXTRA_PROCESSING:
944 enc->extra_processing = g_value_get_uint (value);
946 case ARG_JOINT_STEREO_MODE:
947 enc->joint_stereo_mode = g_value_get_enum (value);
950 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
956 gst_wavpack_enc_get_property (GObject * object, guint prop_id, GValue * value,
959 GstWavpackEnc *enc = GST_WAVPACK_ENC (object);
963 g_value_set_enum (value, enc->mode);
966 if (enc->bps == 0.0) {
967 g_value_set_uint (value, enc->bitrate);
969 g_value_set_uint (value, 0);
972 case ARG_BITSPERSAMPLE:
973 if (enc->bitrate == 0) {
974 g_value_set_double (value, enc->bps);
976 g_value_set_double (value, 0.0);
979 case ARG_CORRECTION_MODE:
980 g_value_set_enum (value, enc->correction_mode);
983 g_value_set_boolean (value, enc->md5);
985 case ARG_EXTRA_PROCESSING:
986 g_value_set_uint (value, enc->extra_processing);
988 case ARG_JOINT_STEREO_MODE:
989 g_value_set_enum (value, enc->joint_stereo_mode);
992 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
998 gst_wavpack_enc_plugin_init (GstPlugin * plugin)
1000 if (!gst_element_register (plugin, "wavpackenc",
1001 GST_RANK_NONE, GST_TYPE_WAVPACK_ENC))
1004 GST_DEBUG_CATEGORY_INIT (gst_wavpack_enc_debug, "wavpackenc", 0,