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_static_pad_template (element_class, &sink_factory);
213 gst_element_class_add_static_pad_template (element_class, &src_factory);
214 gst_element_class_add_static_pad_template (element_class, &wvcsrc_factory);
216 /* set element details */
217 gst_element_class_set_static_metadata (element_class, "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 /* set property handlers */
223 gobject_class->set_property = gst_wavpack_enc_set_property;
224 gobject_class->get_property = gst_wavpack_enc_get_property;
226 base_class->start = GST_DEBUG_FUNCPTR (gst_wavpack_enc_start);
227 base_class->stop = GST_DEBUG_FUNCPTR (gst_wavpack_enc_stop);
228 base_class->set_format = GST_DEBUG_FUNCPTR (gst_wavpack_enc_set_format);
229 base_class->handle_frame = GST_DEBUG_FUNCPTR (gst_wavpack_enc_handle_frame);
230 base_class->sink_event = GST_DEBUG_FUNCPTR (gst_wavpack_enc_sink_event);
232 /* install all properties */
233 g_object_class_install_property (gobject_class, ARG_MODE,
234 g_param_spec_enum ("mode", "Encoding mode",
235 "Speed versus compression tradeoff.",
236 GST_TYPE_WAVPACK_ENC_MODE, GST_WAVPACK_ENC_MODE_DEFAULT,
237 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
238 g_object_class_install_property (gobject_class, ARG_BITRATE,
239 g_param_spec_uint ("bitrate", "Bitrate",
240 "Try to encode with this average bitrate (bits/sec). "
241 "This enables lossy encoding, values smaller than 24000 disable it again.",
242 0, 9600000, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
243 g_object_class_install_property (gobject_class, ARG_BITSPERSAMPLE,
244 g_param_spec_double ("bits-per-sample", "Bits per sample",
245 "Try to encode with this amount of bits per sample. "
246 "This enables lossy encoding, values smaller than 2.0 disable it again.",
247 0.0, 24.0, 0.0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
248 g_object_class_install_property (gobject_class, ARG_CORRECTION_MODE,
249 g_param_spec_enum ("correction-mode", "Correction stream mode",
250 "Use this mode for the correction stream. Only works in lossy mode!",
251 GST_TYPE_WAVPACK_ENC_CORRECTION_MODE, GST_WAVPACK_CORRECTION_MODE_OFF,
252 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
253 g_object_class_install_property (gobject_class, ARG_MD5,
254 g_param_spec_boolean ("md5", "MD5",
255 "Store MD5 hash of raw samples within the file.", FALSE,
256 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
257 g_object_class_install_property (gobject_class, ARG_EXTRA_PROCESSING,
258 g_param_spec_uint ("extra-processing", "Extra processing",
259 "Use better but slower filters for better compression/quality.",
260 0, 6, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
261 g_object_class_install_property (gobject_class, ARG_JOINT_STEREO_MODE,
262 g_param_spec_enum ("joint-stereo-mode", "Joint-Stereo mode",
263 "Use this joint-stereo mode.", GST_TYPE_WAVPACK_ENC_JOINT_STEREO_MODE,
264 GST_WAVPACK_JS_MODE_AUTO,
265 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
269 gst_wavpack_enc_reset (GstWavpackEnc * enc)
271 /* close and free everything stream related if we already did something */
272 if (enc->wp_context) {
273 WavpackCloseFile (enc->wp_context);
274 enc->wp_context = NULL;
276 if (enc->wp_config) {
277 g_free (enc->wp_config);
278 enc->wp_config = NULL;
280 if (enc->first_block) {
281 g_free (enc->first_block);
282 enc->first_block = NULL;
284 enc->first_block_size = 0;
285 if (enc->md5_context) {
286 g_checksum_free (enc->md5_context);
287 enc->md5_context = NULL;
289 if (enc->pending_segment)
290 gst_event_unref (enc->pending_segment);
291 enc->pending_segment = NULL;
293 if (enc->pending_buffer) {
294 gst_buffer_unref (enc->pending_buffer);
295 enc->pending_buffer = NULL;
296 enc->pending_offset = 0;
299 /* reset the last returns to GST_FLOW_OK. This is only set to something else
300 * while WavpackPackSamples() or more specific gst_wavpack_enc_push_block()
301 * so not valid anymore */
302 enc->srcpad_last_return = enc->wvcsrcpad_last_return = GST_FLOW_OK;
304 /* reset stream information */
308 enc->channel_mask = 0;
309 enc->need_channel_remap = FALSE;
311 enc->timestamp_offset = GST_CLOCK_TIME_NONE;
312 enc->next_ts = GST_CLOCK_TIME_NONE;
316 gst_wavpack_enc_init (GstWavpackEnc * enc)
318 GstAudioEncoder *benc = GST_AUDIO_ENCODER (enc);
320 /* initialize object attributes */
321 enc->wp_config = NULL;
322 enc->wp_context = NULL;
323 enc->first_block = NULL;
324 enc->md5_context = NULL;
325 gst_wavpack_enc_reset (enc);
327 enc->wv_id.correction = FALSE;
328 enc->wv_id.wavpack_enc = enc;
329 enc->wv_id.passthrough = FALSE;
330 enc->wvc_id.correction = TRUE;
331 enc->wvc_id.wavpack_enc = enc;
332 enc->wvc_id.passthrough = FALSE;
334 /* set default values of params */
335 enc->mode = GST_WAVPACK_ENC_MODE_DEFAULT;
338 enc->correction_mode = GST_WAVPACK_CORRECTION_MODE_OFF;
340 enc->extra_processing = 0;
341 enc->joint_stereo_mode = GST_WAVPACK_JS_MODE_AUTO;
343 /* require perfect ts */
344 gst_audio_encoder_set_perfect_timestamp (benc, TRUE);
346 GST_PAD_SET_ACCEPT_TEMPLATE (GST_AUDIO_ENCODER_SINK_PAD (enc));
351 gst_wavpack_enc_start (GstAudioEncoder * enc)
353 GST_DEBUG_OBJECT (enc, "start");
359 gst_wavpack_enc_stop (GstAudioEncoder * enc)
361 GstWavpackEnc *wpenc = GST_WAVPACK_ENC (enc);
363 GST_DEBUG_OBJECT (enc, "stop");
364 gst_wavpack_enc_reset (wpenc);
370 gst_wavpack_enc_set_format (GstAudioEncoder * benc, GstAudioInfo * info)
372 GstWavpackEnc *enc = GST_WAVPACK_ENC (benc);
373 GstAudioChannelPosition *pos;
374 GstAudioChannelPosition opos[64] = { GST_AUDIO_CHANNEL_POSITION_INVALID, };
378 /* we may be configured again, but that change should have cleanup context */
379 g_assert (enc->wp_context == NULL);
381 enc->channels = GST_AUDIO_INFO_CHANNELS (info);
382 enc->depth = GST_AUDIO_INFO_DEPTH (info);
383 enc->samplerate = GST_AUDIO_INFO_RATE (info);
385 pos = info->position;
388 /* If one channel is NONE they'll be all undefined */
389 if (pos != NULL && pos[0] == GST_AUDIO_CHANNEL_POSITION_NONE) {
390 goto invalid_channels;
394 gst_wavpack_get_channel_mask_from_positions (pos, enc->channels);
395 enc->need_channel_remap =
396 gst_wavpack_set_channel_mapping (pos, enc->channels,
397 enc->channel_mapping);
399 /* wavpack caps hold gst mask, not wavpack mask */
400 gst_audio_channel_positions_to_mask (opos, enc->channels, FALSE, &mask);
402 /* set fixed src pad caps now that we know what we will get */
403 caps = gst_caps_new_simple ("audio/x-wavpack",
404 "channels", G_TYPE_INT, enc->channels,
405 "rate", G_TYPE_INT, enc->samplerate,
406 "depth", G_TYPE_INT, enc->depth, "framed", G_TYPE_BOOLEAN, TRUE, NULL);
409 gst_caps_set_simple (caps, "channel-mask", GST_TYPE_BITMASK, mask, NULL);
411 if (!gst_audio_encoder_set_output_format (benc, caps))
412 goto setting_src_caps_failed;
414 gst_caps_unref (caps);
416 /* no special feedback to base class; should provide all available samples */
421 setting_src_caps_failed:
423 GST_DEBUG_OBJECT (enc,
424 "Couldn't set caps on source pad: %" GST_PTR_FORMAT, caps);
425 gst_caps_unref (caps);
430 GST_DEBUG_OBJECT (enc, "input has invalid channel layout");
436 gst_wavpack_enc_set_wp_config (GstWavpackEnc * enc)
438 enc->wp_config = g_new0 (WavpackConfig, 1);
439 /* set general stream informations in the WavpackConfig */
440 enc->wp_config->bytes_per_sample = GST_ROUND_UP_8 (enc->depth) / 8;
441 enc->wp_config->bits_per_sample = enc->depth;
442 enc->wp_config->num_channels = enc->channels;
443 enc->wp_config->channel_mask = enc->channel_mask;
444 enc->wp_config->sample_rate = enc->samplerate;
447 * Set parameters in WavpackConfig
453 case GST_WAVPACK_ENC_MODE_VERY_FAST:
454 enc->wp_config->flags |= CONFIG_VERY_FAST_FLAG;
455 enc->wp_config->flags |= CONFIG_FAST_FLAG;
458 case GST_WAVPACK_ENC_MODE_FAST:
459 enc->wp_config->flags |= CONFIG_FAST_FLAG;
461 case GST_WAVPACK_ENC_MODE_DEFAULT:
463 case GST_WAVPACK_ENC_MODE_HIGH:
464 enc->wp_config->flags |= CONFIG_HIGH_FLAG;
466 case GST_WAVPACK_ENC_MODE_VERY_HIGH:
467 enc->wp_config->flags |= CONFIG_HIGH_FLAG;
468 enc->wp_config->flags |= CONFIG_VERY_HIGH_FLAG;
472 /* Bitrate, enables lossy mode */
474 enc->wp_config->flags |= CONFIG_HYBRID_FLAG;
475 enc->wp_config->flags |= CONFIG_BITRATE_KBPS;
476 enc->wp_config->bitrate = enc->bitrate / 1000.0;
477 } else if (enc->bps) {
478 enc->wp_config->flags |= CONFIG_HYBRID_FLAG;
479 enc->wp_config->bitrate = enc->bps;
482 /* Correction Mode, only in lossy mode */
483 if (enc->wp_config->flags & CONFIG_HYBRID_FLAG) {
484 if (enc->correction_mode > GST_WAVPACK_CORRECTION_MODE_OFF) {
485 GstCaps *caps = gst_caps_new_simple ("audio/x-wavpack-correction",
486 "framed", G_TYPE_BOOLEAN, TRUE, NULL);
489 gst_pad_new_from_static_template (&wvcsrc_factory, "wvcsrc");
491 /* try to add correction src pad, don't set correction mode on failure */
492 GST_DEBUG_OBJECT (enc, "Adding correction pad with caps %"
493 GST_PTR_FORMAT, caps);
494 if (!gst_pad_set_caps (enc->wvcsrcpad, caps)) {
495 enc->correction_mode = 0;
496 GST_WARNING_OBJECT (enc, "setting correction caps failed");
498 gst_pad_use_fixed_caps (enc->wvcsrcpad);
499 gst_pad_set_active (enc->wvcsrcpad, TRUE);
500 gst_element_add_pad (GST_ELEMENT (enc), enc->wvcsrcpad);
501 enc->wp_config->flags |= CONFIG_CREATE_WVC;
502 if (enc->correction_mode == GST_WAVPACK_CORRECTION_MODE_OPTIMIZED) {
503 enc->wp_config->flags |= CONFIG_OPTIMIZE_WVC;
506 gst_caps_unref (caps);
509 if (enc->correction_mode > GST_WAVPACK_CORRECTION_MODE_OFF) {
510 enc->correction_mode = 0;
511 GST_WARNING_OBJECT (enc, "setting correction mode only has "
512 "any effect if a bitrate is provided.");
515 gst_element_no_more_pads (GST_ELEMENT (enc));
517 /* MD5, setup MD5 context */
518 if ((enc->md5) && !(enc->md5_context)) {
519 enc->wp_config->flags |= CONFIG_MD5_CHECKSUM;
520 enc->md5_context = g_checksum_new (G_CHECKSUM_MD5);
523 /* Extra encode processing */
524 if (enc->extra_processing) {
525 enc->wp_config->flags |= CONFIG_EXTRA_MODE;
526 enc->wp_config->xmode = enc->extra_processing;
529 /* Joint stereo mode */
530 switch (enc->joint_stereo_mode) {
531 case GST_WAVPACK_JS_MODE_AUTO:
533 case GST_WAVPACK_JS_MODE_LEFT_RIGHT:
534 enc->wp_config->flags |= CONFIG_JOINT_OVERRIDE;
535 enc->wp_config->flags &= ~CONFIG_JOINT_STEREO;
537 case GST_WAVPACK_JS_MODE_MID_SIDE:
538 enc->wp_config->flags |= (CONFIG_JOINT_OVERRIDE | CONFIG_JOINT_STEREO);
544 gst_wavpack_enc_push_block (void *id, void *data, int32_t count)
546 GstWavpackEncWriteID *wid = (GstWavpackEncWriteID *) id;
547 GstWavpackEnc *enc = GST_WAVPACK_ENC (wid->wavpack_enc);
551 guchar *block = (guchar *) data;
554 pad = (wid->correction) ? enc->wvcsrcpad : GST_AUDIO_ENCODER_SRC_PAD (enc);
556 (wid->correction) ? &enc->
557 wvcsrcpad_last_return : &enc->srcpad_last_return;
559 buffer = gst_buffer_new_and_alloc (count);
560 gst_buffer_fill (buffer, 0, data, count);
562 if (count > sizeof (WavpackHeader) && memcmp (block, "wvpk", 4) == 0) {
563 /* if it's a Wavpack block set buffer timestamp and duration, etc */
566 GST_LOG_OBJECT (enc, "got %d bytes of encoded wavpack %sdata",
567 count, (wid->correction) ? "correction " : "");
569 gst_wavpack_read_header (&wph, block);
571 /* Only set when pushing the first buffer again, in that case
572 * we don't want to delay the buffer or push newsegment events
574 if (!wid->passthrough) {
575 /* Only push complete blocks */
576 if (enc->pending_buffer == NULL) {
577 enc->pending_buffer = buffer;
578 enc->pending_offset = wph.block_index;
579 } else if (enc->pending_offset == wph.block_index) {
580 enc->pending_buffer = gst_buffer_append (enc->pending_buffer, buffer);
582 GST_ERROR ("Got incomplete block, dropping");
583 gst_buffer_unref (enc->pending_buffer);
584 enc->pending_buffer = buffer;
585 enc->pending_offset = wph.block_index;
588 /* Is this the not-final block of multi-channel data? If so, just
589 * accumulate and return here. */
590 if (!(wph.flags & FINAL_BLOCK) && ((block[32] & ID_OPTIONAL_DATA) == 0))
593 buffer = enc->pending_buffer;
594 enc->pending_buffer = NULL;
595 enc->pending_offset = 0;
597 /* only send segment on correction pad,
598 * regular pad is handled normally by baseclass */
599 if (wid->correction && enc->pending_segment) {
600 gst_pad_push_event (pad, enc->pending_segment);
601 enc->pending_segment = NULL;
604 if (wph.block_index == 0) {
605 /* save header for later reference, so we can re-send it later on
606 * EOS with fixed up values for total sample count etc. */
607 if (enc->first_block == NULL && !wid->correction) {
610 gst_buffer_map (buffer, &map, GST_MAP_READ);
611 enc->first_block = g_memdup (map.data, map.size);
612 enc->first_block_size = map.size;
613 gst_buffer_unmap (buffer, &map);
617 samples = wph.block_samples;
619 /* decorate buffer */
620 /* NOTE: this will get overwritten by baseclass, but stay for those
621 * that are pushed directly
622 * FIXME: add setting to baseclass to avoid overwriting it ?? */
623 GST_BUFFER_OFFSET (buffer) = wph.block_index;
624 GST_BUFFER_OFFSET_END (buffer) = wph.block_index + wph.block_samples;
626 /* if it's something else set no timestamp and duration on the buffer */
627 GST_DEBUG_OBJECT (enc, "got %d bytes of unknown data", count);
630 if (wid->correction || wid->passthrough) {
631 /* push the buffer and forward errors */
632 GST_DEBUG_OBJECT (enc, "pushing buffer with %" G_GSIZE_FORMAT " bytes",
633 gst_buffer_get_size (buffer));
634 *flow = gst_pad_push (pad, buffer);
636 GST_DEBUG_OBJECT (enc, "handing frame of %" G_GSIZE_FORMAT " bytes",
637 gst_buffer_get_size (buffer));
638 *flow = gst_audio_encoder_finish_frame (GST_AUDIO_ENCODER (enc), buffer,
642 if (*flow != GST_FLOW_OK) {
643 GST_WARNING_OBJECT (enc, "flow on %s:%s = %s",
644 GST_DEBUG_PAD_NAME (pad), gst_flow_get_name (*flow));
652 gst_wavpack_enc_fix_channel_order (GstWavpackEnc * enc, gint32 * data,
658 for (i = 0; i < nsamples / enc->channels; i++) {
659 for (j = 0; j < enc->channels; j++) {
660 tmp[enc->channel_mapping[j]] = data[j];
662 for (j = 0; j < enc->channels; j++) {
665 data += enc->channels;
670 gst_wavpack_enc_handle_frame (GstAudioEncoder * benc, GstBuffer * buf)
672 GstWavpackEnc *enc = GST_WAVPACK_ENC (benc);
673 uint32_t sample_count;
677 /* base class ensures configuration */
678 g_return_val_if_fail (enc->depth != 0, GST_FLOW_NOT_NEGOTIATED);
680 /* reset the last returns to GST_FLOW_OK. This is only set to something else
681 * while WavpackPackSamples() or more specific gst_wavpack_enc_push_block()
682 * so not valid anymore */
683 enc->srcpad_last_return = enc->wvcsrcpad_last_return = GST_FLOW_OK;
685 if (G_UNLIKELY (!buf))
686 return gst_wavpack_enc_drain (enc);
688 sample_count = gst_buffer_get_size (buf) / 4;
689 GST_DEBUG_OBJECT (enc, "got %u raw samples", sample_count);
691 /* check if we already have a valid WavpackContext, otherwise make one */
692 if (!enc->wp_context) {
693 /* create raw context */
695 WavpackOpenFileOutput (gst_wavpack_enc_push_block, &enc->wv_id,
696 (enc->correction_mode > 0) ? &enc->wvc_id : NULL);
697 if (!enc->wp_context)
700 /* set the WavpackConfig according to our parameters */
701 gst_wavpack_enc_set_wp_config (enc);
703 /* set the configuration to the context now that we know everything
704 * and initialize the encoder */
705 if (!WavpackSetConfiguration (enc->wp_context,
706 enc->wp_config, (uint32_t) (-1))
707 || !WavpackPackInit (enc->wp_context)) {
708 WavpackCloseFile (enc->wp_context);
711 GST_DEBUG_OBJECT (enc, "setup of encoding context successfull");
714 if (enc->need_channel_remap) {
715 buf = gst_buffer_make_writable (buf);
716 gst_buffer_map (buf, &map, GST_MAP_WRITE);
717 gst_wavpack_enc_fix_channel_order (enc, (gint32 *) map.data, sample_count);
718 gst_buffer_unmap (buf, &map);
721 gst_buffer_map (buf, &map, GST_MAP_READ);
723 /* if we want to append the MD5 sum to the stream update it here
724 * with the current raw samples */
726 g_checksum_update (enc->md5_context, map.data, map.size);
729 /* encode and handle return values from encoding */
730 if (WavpackPackSamples (enc->wp_context, (int32_t *) map.data,
731 sample_count / enc->channels)) {
732 GST_DEBUG_OBJECT (enc, "encoding samples successful");
733 gst_buffer_unmap (buf, &map);
736 gst_buffer_unmap (buf, &map);
737 if ((enc->srcpad_last_return == GST_FLOW_OK) ||
738 (enc->wvcsrcpad_last_return == GST_FLOW_OK)) {
740 } else if ((enc->srcpad_last_return == GST_FLOW_NOT_LINKED) &&
741 (enc->wvcsrcpad_last_return == GST_FLOW_NOT_LINKED)) {
742 ret = GST_FLOW_NOT_LINKED;
743 } else if ((enc->srcpad_last_return == GST_FLOW_FLUSHING) &&
744 (enc->wvcsrcpad_last_return == GST_FLOW_FLUSHING)) {
745 ret = GST_FLOW_FLUSHING;
747 goto encoding_failed;
757 GST_ELEMENT_ERROR (enc, LIBRARY, ENCODE, (NULL),
758 ("encoding samples failed"));
759 ret = GST_FLOW_ERROR;
764 GST_ELEMENT_ERROR (enc, LIBRARY, SETTINGS, (NULL),
765 ("error setting up wavpack encoding context"));
766 ret = GST_FLOW_ERROR;
771 GST_ELEMENT_ERROR (enc, LIBRARY, INIT, (NULL),
772 ("error creating Wavpack context"));
773 ret = GST_FLOW_ERROR;
779 gst_wavpack_enc_rewrite_first_block (GstWavpackEnc * enc)
784 gboolean seekable = FALSE;
786 g_return_if_fail (enc);
787 g_return_if_fail (enc->first_block);
789 /* update the sample count in the first block */
790 WavpackUpdateNumSamples (enc->wp_context, enc->first_block);
792 /* try to seek to the beginning of the output */
793 query = gst_query_new_seeking (GST_FORMAT_BYTES);
794 if (gst_pad_peer_query (GST_AUDIO_ENCODER_SRC_PAD (enc), query)) {
797 gst_query_parse_seeking (query, &format, &seekable, NULL, NULL);
798 if (format != GST_FORMAT_BYTES)
801 GST_LOG_OBJECT (enc, "SEEKING query not handled");
803 gst_query_unref (query);
806 GST_DEBUG_OBJECT (enc, "downstream not seekable; not rewriting");
810 gst_segment_init (&segment, GST_FORMAT_BYTES);
811 ret = gst_pad_push_event (GST_AUDIO_ENCODER_SRC_PAD (enc),
812 gst_event_new_segment (&segment));
814 /* try to rewrite the first block */
815 GST_DEBUG_OBJECT (enc, "rewriting first block ...");
816 enc->wv_id.passthrough = TRUE;
817 ret = gst_wavpack_enc_push_block (&enc->wv_id,
818 enc->first_block, enc->first_block_size);
819 enc->wv_id.passthrough = FALSE;
820 g_free (enc->first_block);
821 enc->first_block = NULL;
823 GST_WARNING_OBJECT (enc, "rewriting of first block failed. "
824 "Seeking to first block failed!");
829 gst_wavpack_enc_drain (GstWavpackEnc * enc)
831 if (!enc->wp_context)
834 GST_DEBUG_OBJECT (enc, "draining");
836 /* Encode all remaining samples and flush them to the src pads */
837 WavpackFlushSamples (enc->wp_context);
839 /* Drop all remaining data, this is no complete block otherwise
840 * it would've been pushed already */
841 if (enc->pending_buffer) {
842 gst_buffer_unref (enc->pending_buffer);
843 enc->pending_buffer = NULL;
844 enc->pending_offset = 0;
847 /* write the MD5 sum if we have to write one */
848 if ((enc->md5) && (enc->md5_context)) {
849 guint8 md5_digest[16];
850 gsize digest_len = sizeof (md5_digest);
852 g_checksum_get_digest (enc->md5_context, md5_digest, &digest_len);
853 if (digest_len == sizeof (md5_digest)) {
854 WavpackStoreMD5Sum (enc->wp_context, md5_digest);
855 WavpackFlushSamples (enc->wp_context);
857 GST_WARNING_OBJECT (enc, "Calculating MD5 digest failed");
860 /* Try to rewrite the first frame with the correct sample number */
861 if (enc->first_block)
862 gst_wavpack_enc_rewrite_first_block (enc);
864 /* close the context if not already happened */
865 if (enc->wp_context) {
866 WavpackCloseFile (enc->wp_context);
867 enc->wp_context = NULL;
874 gst_wavpack_enc_sink_event (GstAudioEncoder * benc, GstEvent * event)
876 GstWavpackEnc *enc = GST_WAVPACK_ENC (benc);
878 GST_DEBUG_OBJECT (enc, "Received %s event on sinkpad",
879 GST_EVENT_TYPE_NAME (event));
881 switch (GST_EVENT_TYPE (event)) {
882 case GST_EVENT_SEGMENT:
883 if (enc->wp_context) {
884 GST_WARNING_OBJECT (enc, "got NEWSEGMENT after encoding "
887 /* peek and hold NEWSEGMENT events for sending on correction pad */
888 if (enc->pending_segment)
889 gst_event_unref (enc->pending_segment);
890 enc->pending_segment = gst_event_ref (event);
896 /* baseclass handles rest */
897 return GST_AUDIO_ENCODER_CLASS (parent_class)->sink_event (benc, event);
901 gst_wavpack_enc_set_property (GObject * object, guint prop_id,
902 const GValue * value, GParamSpec * pspec)
904 GstWavpackEnc *enc = GST_WAVPACK_ENC (object);
908 enc->mode = g_value_get_enum (value);
911 guint val = g_value_get_uint (value);
913 if ((val >= 24000) && (val <= 9600000)) {
922 case ARG_BITSPERSAMPLE:{
923 gdouble val = g_value_get_double (value);
925 if ((val >= 2.0) && (val <= 24.0)) {
934 case ARG_CORRECTION_MODE:
935 enc->correction_mode = g_value_get_enum (value);
938 enc->md5 = g_value_get_boolean (value);
940 case ARG_EXTRA_PROCESSING:
941 enc->extra_processing = g_value_get_uint (value);
943 case ARG_JOINT_STEREO_MODE:
944 enc->joint_stereo_mode = g_value_get_enum (value);
947 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
953 gst_wavpack_enc_get_property (GObject * object, guint prop_id, GValue * value,
956 GstWavpackEnc *enc = GST_WAVPACK_ENC (object);
960 g_value_set_enum (value, enc->mode);
963 if (enc->bps == 0.0) {
964 g_value_set_uint (value, enc->bitrate);
966 g_value_set_uint (value, 0);
969 case ARG_BITSPERSAMPLE:
970 if (enc->bitrate == 0) {
971 g_value_set_double (value, enc->bps);
973 g_value_set_double (value, 0.0);
976 case ARG_CORRECTION_MODE:
977 g_value_set_enum (value, enc->correction_mode);
980 g_value_set_boolean (value, enc->md5);
982 case ARG_EXTRA_PROCESSING:
983 g_value_set_uint (value, enc->extra_processing);
985 case ARG_JOINT_STEREO_MODE:
986 g_value_set_enum (value, enc->joint_stereo_mode);
989 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
995 gst_wavpack_enc_plugin_init (GstPlugin * plugin)
997 if (!gst_element_register (plugin, "wavpackenc",
998 GST_RANK_NONE, GST_TYPE_WAVPACK_ENC))
1001 GST_DEBUG_CATEGORY_INIT (gst_wavpack_enc_debug, "wavpackenc", 0,