1 /* GStreamer Wavpack encoder plugin
2 * Copyright (c) 2006 Sebastian Dröge <slomo@circular-chaos.org>
3 *
4 * gstwavpackdec.c: Wavpack audio encoder
5 *
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.
10 *
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.
15 *
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.
20 */
21
22 /**
23 * SECTION:element-wavpackenc
24 *
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.
28 *
29 * <refsect2>
30 * <title>Example launch line</title>
31 * |[
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.
35 * |[
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).
39 * |[
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).
43 * </refsect2>
44 */
45
46 /*
47 * TODO: - add 32 bit float mode. CONFIG_FLOAT_DATA
48 */
49
50 #include <string.h>
51 #include <gst/gst.h>
52 #include <glib/gprintf.h>
53
54 #include <wavpack/wavpack.h>
55 #include "gstwavpackenc.h"
56 #include "gstwavpackcommon.h"
57
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,
61 GstAudioInfo * info);
62 static GstFlowReturn gst_wavpack_enc_handle_frame (GstAudioEncoder * enc,
63 GstBuffer * in_buf);
64 static gboolean gst_wavpack_enc_sink_event (GstAudioEncoder * enc,
65 GstEvent * event);
66
67 static int gst_wavpack_enc_push_block (void *id, void *data, int32_t count);
68 static GstFlowReturn gst_wavpack_enc_drain (GstWavpackEnc * enc);
69
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);
74
75 enum
76 {
77 ARG_0,
78 ARG_MODE,
79 ARG_BITRATE,
80 ARG_BITSPERSAMPLE,
81 ARG_CORRECTION_MODE,
82 ARG_MD5,
83 ARG_EXTRA_PROCESSING,
84 ARG_JOINT_STEREO_MODE
85 };
86
87 GST_DEBUG_CATEGORY_STATIC (gst_wavpack_enc_debug);
88 #define GST_CAT_DEFAULT gst_wavpack_enc_debug
89
90 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
91 GST_PAD_SINK,
92 GST_PAD_ALWAYS,
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 ]")
97 );
98
99 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
100 GST_PAD_SRC,
101 GST_PAD_ALWAYS,
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")
106 );
107
108 static GstStaticPadTemplate wvcsrc_factory = GST_STATIC_PAD_TEMPLATE ("wvcsrc",
109 GST_PAD_SRC,
110 GST_PAD_SOMETIMES,
111 GST_STATIC_CAPS ("audio/x-wavpack-correction, " "framed = (boolean) TRUE")
112 );
113
114 enum
115 {
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
121 };
122
123 #define GST_TYPE_WAVPACK_ENC_MODE (gst_wavpack_enc_mode_get_type ())
124 static GType
gst_wavpack_enc_mode_get_type(void)125 gst_wavpack_enc_mode_get_type (void)
126 {
127 static GType qtype = 0;
128
129 if (qtype == 0) {
130 static const GEnumValue values[] = {
131 #if 0
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"},
135 #endif
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"},
140 {0, NULL, NULL}
141 };
142
143 qtype = g_enum_register_static ("GstWavpackEncMode", values);
144 }
145 return qtype;
146 }
147
148 enum
149 {
150 GST_WAVPACK_CORRECTION_MODE_OFF = 0,
151 GST_WAVPACK_CORRECTION_MODE_ON,
152 GST_WAVPACK_CORRECTION_MODE_OPTIMIZED
153 };
154
155 #define GST_TYPE_WAVPACK_ENC_CORRECTION_MODE (gst_wavpack_enc_correction_mode_get_type ())
156 static GType
gst_wavpack_enc_correction_mode_get_type(void)157 gst_wavpack_enc_correction_mode_get_type (void)
158 {
159 static GType qtype = 0;
160
161 if (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"},
167 {0, NULL, NULL}
168 };
169
170 qtype = g_enum_register_static ("GstWavpackEncCorrectionMode", values);
171 }
172 return qtype;
173 }
174
175 enum
176 {
177 GST_WAVPACK_JS_MODE_AUTO = 0,
178 GST_WAVPACK_JS_MODE_LEFT_RIGHT,
179 GST_WAVPACK_JS_MODE_MID_SIDE
180 };
181
182 #define GST_TYPE_WAVPACK_ENC_JOINT_STEREO_MODE (gst_wavpack_enc_joint_stereo_mode_get_type ())
183 static GType
gst_wavpack_enc_joint_stereo_mode_get_type(void)184 gst_wavpack_enc_joint_stereo_mode_get_type (void)
185 {
186 static GType qtype = 0;
187
188 if (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"},
193 {0, NULL, NULL}
194 };
195
196 qtype = g_enum_register_static ("GstWavpackEncJSMode", values);
197 }
198 return qtype;
199 }
200
201 #define gst_wavpack_enc_parent_class parent_class
202 G_DEFINE_TYPE (GstWavpackEnc, gst_wavpack_enc, GST_TYPE_AUDIO_ENCODER);
203
204 static void
gst_wavpack_enc_class_init(GstWavpackEncClass * klass)205 gst_wavpack_enc_class_init (GstWavpackEncClass * klass)
206 {
207 GObjectClass *gobject_class = (GObjectClass *) klass;
208 GstElementClass *element_class = (GstElementClass *) (klass);
209 GstAudioEncoderClass *base_class = (GstAudioEncoderClass *) (klass);
210
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);
215
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>");
221
222 /* set property handlers */
223 gobject_class->set_property = gst_wavpack_enc_set_property;
224 gobject_class->get_property = gst_wavpack_enc_get_property;
225
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);
231
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));
266 }
267
268 static void
gst_wavpack_enc_reset(GstWavpackEnc * enc)269 gst_wavpack_enc_reset (GstWavpackEnc * enc)
270 {
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;
275 }
276 if (enc->wp_config) {
277 g_free (enc->wp_config);
278 enc->wp_config = NULL;
279 }
280 if (enc->first_block) {
281 g_free (enc->first_block);
282 enc->first_block = NULL;
283 }
284 enc->first_block_size = 0;
285 if (enc->md5_context) {
286 g_checksum_free (enc->md5_context);
287 enc->md5_context = NULL;
288 }
289 if (enc->pending_segment)
290 gst_event_unref (enc->pending_segment);
291 enc->pending_segment = NULL;
292
293 if (enc->pending_buffer) {
294 gst_buffer_unref (enc->pending_buffer);
295 enc->pending_buffer = NULL;
296 enc->pending_offset = 0;
297 }
298
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;
303
304 /* reset stream information */
305 enc->samplerate = 0;
306 enc->depth = 0;
307 enc->channels = 0;
308 enc->channel_mask = 0;
309 enc->need_channel_remap = FALSE;
310
311 enc->timestamp_offset = GST_CLOCK_TIME_NONE;
312 enc->next_ts = GST_CLOCK_TIME_NONE;
313 }
314
315 static void
gst_wavpack_enc_init(GstWavpackEnc * enc)316 gst_wavpack_enc_init (GstWavpackEnc * enc)
317 {
318 GstAudioEncoder *benc = GST_AUDIO_ENCODER (enc);
319
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);
326
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;
333
334 /* set default values of params */
335 enc->mode = GST_WAVPACK_ENC_MODE_DEFAULT;
336 enc->bitrate = 0;
337 enc->bps = 0.0;
338 enc->correction_mode = GST_WAVPACK_CORRECTION_MODE_OFF;
339 enc->md5 = FALSE;
340 enc->extra_processing = 0;
341 enc->joint_stereo_mode = GST_WAVPACK_JS_MODE_AUTO;
342
343 /* require perfect ts */
344 gst_audio_encoder_set_perfect_timestamp (benc, TRUE);
345
346 GST_PAD_SET_ACCEPT_TEMPLATE (GST_AUDIO_ENCODER_SINK_PAD (enc));
347 }
348
349
350 static gboolean
gst_wavpack_enc_start(GstAudioEncoder * enc)351 gst_wavpack_enc_start (GstAudioEncoder * enc)
352 {
353 GST_DEBUG_OBJECT (enc, "start");
354
355 return TRUE;
356 }
357
358 static gboolean
gst_wavpack_enc_stop(GstAudioEncoder * enc)359 gst_wavpack_enc_stop (GstAudioEncoder * enc)
360 {
361 GstWavpackEnc *wpenc = GST_WAVPACK_ENC (enc);
362
363 GST_DEBUG_OBJECT (enc, "stop");
364 gst_wavpack_enc_reset (wpenc);
365
366 return TRUE;
367 }
368
369 static gboolean
gst_wavpack_enc_set_format(GstAudioEncoder * benc,GstAudioInfo * info)370 gst_wavpack_enc_set_format (GstAudioEncoder * benc, GstAudioInfo * info)
371 {
372 GstWavpackEnc *enc = GST_WAVPACK_ENC (benc);
373 GstAudioChannelPosition *pos;
374 GstAudioChannelPosition opos[64] = { GST_AUDIO_CHANNEL_POSITION_INVALID, };
375 GstCaps *caps;
376 guint64 mask = 0;
377
378 /* we may be configured again, but that change should have cleanup context */
379 g_assert (enc->wp_context == NULL);
380
381 enc->channels = GST_AUDIO_INFO_CHANNELS (info);
382 enc->depth = GST_AUDIO_INFO_DEPTH (info);
383 enc->samplerate = GST_AUDIO_INFO_RATE (info);
384
385 pos = info->position;
386 g_assert (pos);
387
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;
391 }
392
393 enc->channel_mask =
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);
398
399 /* wavpack caps hold gst mask, not wavpack mask */
400 gst_audio_channel_positions_to_mask (opos, enc->channels, FALSE, &mask);
401
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);
407
408 if (mask)
409 gst_caps_set_simple (caps, "channel-mask", GST_TYPE_BITMASK, mask, NULL);
410
411 if (!gst_audio_encoder_set_output_format (benc, caps))
412 goto setting_src_caps_failed;
413
414 gst_caps_unref (caps);
415
416 /* no special feedback to base class; should provide all available samples */
417
418 return TRUE;
419
420 /* ERRORS */
421 setting_src_caps_failed:
422 {
423 GST_DEBUG_OBJECT (enc,
424 "Couldn't set caps on source pad: %" GST_PTR_FORMAT, caps);
425 gst_caps_unref (caps);
426 return FALSE;
427 }
428 invalid_channels:
429 {
430 GST_DEBUG_OBJECT (enc, "input has invalid channel layout");
431 return FALSE;
432 }
433 }
434
435 static void
gst_wavpack_enc_set_wp_config(GstWavpackEnc * enc)436 gst_wavpack_enc_set_wp_config (GstWavpackEnc * enc)
437 {
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;
445
446 /*
447 * Set parameters in WavpackConfig
448 */
449
450 /* Encoding mode */
451 switch (enc->mode) {
452 #if 0
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;
456 break;
457 #endif
458 case GST_WAVPACK_ENC_MODE_FAST:
459 enc->wp_config->flags |= CONFIG_FAST_FLAG;
460 break;
461 case GST_WAVPACK_ENC_MODE_DEFAULT:
462 break;
463 case GST_WAVPACK_ENC_MODE_HIGH:
464 enc->wp_config->flags |= CONFIG_HIGH_FLAG;
465 break;
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;
469 break;
470 }
471
472 /* Bitrate, enables lossy mode */
473 if (enc->bitrate) {
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;
480 }
481
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);
487
488 enc->wvcsrcpad =
489 gst_pad_new_from_static_template (&wvcsrc_factory, "wvcsrc");
490
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");
497 } else {
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;
504 }
505 }
506 gst_caps_unref (caps);
507 }
508 } else {
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.");
513 }
514 }
515 gst_element_no_more_pads (GST_ELEMENT (enc));
516
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);
521 }
522
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;
527 }
528
529 /* Joint stereo mode */
530 switch (enc->joint_stereo_mode) {
531 case GST_WAVPACK_JS_MODE_AUTO:
532 break;
533 case GST_WAVPACK_JS_MODE_LEFT_RIGHT:
534 enc->wp_config->flags |= CONFIG_JOINT_OVERRIDE;
535 enc->wp_config->flags &= ~CONFIG_JOINT_STEREO;
536 break;
537 case GST_WAVPACK_JS_MODE_MID_SIDE:
538 enc->wp_config->flags |= (CONFIG_JOINT_OVERRIDE | CONFIG_JOINT_STEREO);
539 break;
540 }
541 }
542
543 static int
gst_wavpack_enc_push_block(void * id,void * data,int32_t count)544 gst_wavpack_enc_push_block (void *id, void *data, int32_t count)
545 {
546 GstWavpackEncWriteID *wid = (GstWavpackEncWriteID *) id;
547 GstWavpackEnc *enc = GST_WAVPACK_ENC (wid->wavpack_enc);
548 GstFlowReturn *flow;
549 GstBuffer *buffer;
550 GstPad *pad;
551 guchar *block = (guchar *) data;
552 gint samples = 0;
553
554 pad = (wid->correction) ? enc->wvcsrcpad : GST_AUDIO_ENCODER_SRC_PAD (enc);
555 flow =
556 (wid->correction) ? &enc->
557 wvcsrcpad_last_return : &enc->srcpad_last_return;
558
559 buffer = gst_buffer_new_and_alloc (count);
560 gst_buffer_fill (buffer, 0, data, count);
561
562 if (count > sizeof (WavpackHeader) && memcmp (block, "wvpk", 4) == 0) {
563 /* if it's a Wavpack block set buffer timestamp and duration, etc */
564 WavpackHeader wph;
565
566 GST_LOG_OBJECT (enc, "got %d bytes of encoded wavpack %sdata",
567 count, (wid->correction) ? "correction " : "");
568
569 gst_wavpack_read_header (&wph, block);
570
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
573 */
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);
581 } else {
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;
586 }
587
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))
591 return TRUE;
592
593 buffer = enc->pending_buffer;
594 enc->pending_buffer = NULL;
595 enc->pending_offset = 0;
596
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;
602 }
603
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) {
608 GstMapInfo map;
609
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);
614 }
615 }
616 }
617 samples = wph.block_samples;
618
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;
625 } else {
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);
628 }
629
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);
635 } else {
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,
639 samples);
640 }
641
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));
645 return FALSE;
646 }
647
648 return TRUE;
649 }
650
651 static void
gst_wavpack_enc_fix_channel_order(GstWavpackEnc * enc,gint32 * data,gint nsamples)652 gst_wavpack_enc_fix_channel_order (GstWavpackEnc * enc, gint32 * data,
653 gint nsamples)
654 {
655 gint i, j;
656 gint32 tmp[8];
657
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];
661 }
662 for (j = 0; j < enc->channels; j++) {
663 data[j] = tmp[j];
664 }
665 data += enc->channels;
666 }
667 }
668
669 static GstFlowReturn
gst_wavpack_enc_handle_frame(GstAudioEncoder * benc,GstBuffer * buf)670 gst_wavpack_enc_handle_frame (GstAudioEncoder * benc, GstBuffer * buf)
671 {
672 GstWavpackEnc *enc = GST_WAVPACK_ENC (benc);
673 uint32_t sample_count;
674 GstFlowReturn ret;
675 GstMapInfo map;
676
677 /* base class ensures configuration */
678 g_return_val_if_fail (enc->depth != 0, GST_FLOW_NOT_NEGOTIATED);
679
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;
684
685 if (G_UNLIKELY (!buf))
686 return gst_wavpack_enc_drain (enc);
687
688 sample_count = gst_buffer_get_size (buf) / 4;
689 GST_DEBUG_OBJECT (enc, "got %u raw samples", sample_count);
690
691 /* check if we already have a valid WavpackContext, otherwise make one */
692 if (!enc->wp_context) {
693 /* create raw context */
694 enc->wp_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)
698 goto context_failed;
699
700 /* set the WavpackConfig according to our parameters */
701 gst_wavpack_enc_set_wp_config (enc);
702
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);
709 goto config_failed;
710 }
711 GST_DEBUG_OBJECT (enc, "setup of encoding context successfull");
712 }
713
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);
719 }
720
721 gst_buffer_map (buf, &map, GST_MAP_READ);
722
723 /* if we want to append the MD5 sum to the stream update it here
724 * with the current raw samples */
725 if (enc->md5) {
726 g_checksum_update (enc->md5_context, map.data, map.size);
727 }
728
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);
734 ret = GST_FLOW_OK;
735 } else {
736 gst_buffer_unmap (buf, &map);
737 if ((enc->srcpad_last_return == GST_FLOW_OK) ||
738 (enc->wvcsrcpad_last_return == GST_FLOW_OK)) {
739 ret = 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;
746 } else {
747 goto encoding_failed;
748 }
749 }
750
751 exit:
752 return ret;
753
754 /* ERRORS */
755 encoding_failed:
756 {
757 GST_ELEMENT_ERROR (enc, LIBRARY, ENCODE, (NULL),
758 ("encoding samples failed"));
759 ret = GST_FLOW_ERROR;
760 goto exit;
761 }
762 config_failed:
763 {
764 GST_ELEMENT_ERROR (enc, LIBRARY, SETTINGS, (NULL),
765 ("error setting up wavpack encoding context"));
766 ret = GST_FLOW_ERROR;
767 goto exit;
768 }
769 context_failed:
770 {
771 GST_ELEMENT_ERROR (enc, LIBRARY, INIT, (NULL),
772 ("error creating Wavpack context"));
773 ret = GST_FLOW_ERROR;
774 goto exit;
775 }
776 }
777
778 static void
gst_wavpack_enc_rewrite_first_block(GstWavpackEnc * enc)779 gst_wavpack_enc_rewrite_first_block (GstWavpackEnc * enc)
780 {
781 GstSegment segment;
782 gboolean ret;
783 GstQuery *query;
784 gboolean seekable = FALSE;
785
786 g_return_if_fail (enc);
787 g_return_if_fail (enc->first_block);
788
789 /* update the sample count in the first block */
790 WavpackUpdateNumSamples (enc->wp_context, enc->first_block);
791
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)) {
795 GstFormat format;
796
797 gst_query_parse_seeking (query, &format, &seekable, NULL, NULL);
798 if (format != GST_FORMAT_BYTES)
799 seekable = FALSE;
800 } else {
801 GST_LOG_OBJECT (enc, "SEEKING query not handled");
802 }
803 gst_query_unref (query);
804
805 if (!seekable) {
806 GST_DEBUG_OBJECT (enc, "downstream not seekable; not rewriting");
807 return;
808 }
809
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));
813 if (ret) {
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;
822 } else {
823 GST_WARNING_OBJECT (enc, "rewriting of first block failed. "
824 "Seeking to first block failed!");
825 }
826 }
827
828 static GstFlowReturn
gst_wavpack_enc_drain(GstWavpackEnc * enc)829 gst_wavpack_enc_drain (GstWavpackEnc * enc)
830 {
831 if (!enc->wp_context)
832 return GST_FLOW_OK;
833
834 GST_DEBUG_OBJECT (enc, "draining");
835
836 /* Encode all remaining samples and flush them to the src pads */
837 WavpackFlushSamples (enc->wp_context);
838
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;
845 }
846
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);
851
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);
856 } else
857 GST_WARNING_OBJECT (enc, "Calculating MD5 digest failed");
858 }
859
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);
863
864 /* close the context if not already happened */
865 if (enc->wp_context) {
866 WavpackCloseFile (enc->wp_context);
867 enc->wp_context = NULL;
868 }
869
870 return GST_FLOW_OK;
871 }
872
873 static gboolean
gst_wavpack_enc_sink_event(GstAudioEncoder * benc,GstEvent * event)874 gst_wavpack_enc_sink_event (GstAudioEncoder * benc, GstEvent * event)
875 {
876 GstWavpackEnc *enc = GST_WAVPACK_ENC (benc);
877
878 GST_DEBUG_OBJECT (enc, "Received %s event on sinkpad",
879 GST_EVENT_TYPE_NAME (event));
880
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 "
885 "already started");
886 }
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);
891 break;
892 default:
893 break;
894 }
895
896 /* baseclass handles rest */
897 return GST_AUDIO_ENCODER_CLASS (parent_class)->sink_event (benc, event);
898 }
899
900 static void
gst_wavpack_enc_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)901 gst_wavpack_enc_set_property (GObject * object, guint prop_id,
902 const GValue * value, GParamSpec * pspec)
903 {
904 GstWavpackEnc *enc = GST_WAVPACK_ENC (object);
905
906 switch (prop_id) {
907 case ARG_MODE:
908 enc->mode = g_value_get_enum (value);
909 break;
910 case ARG_BITRATE:{
911 guint val = g_value_get_uint (value);
912
913 if ((val >= 24000) && (val <= 9600000)) {
914 enc->bitrate = val;
915 enc->bps = 0.0;
916 } else {
917 enc->bitrate = 0;
918 enc->bps = 0.0;
919 }
920 break;
921 }
922 case ARG_BITSPERSAMPLE:{
923 gdouble val = g_value_get_double (value);
924
925 if ((val >= 2.0) && (val <= 24.0)) {
926 enc->bps = val;
927 enc->bitrate = 0;
928 } else {
929 enc->bps = 0.0;
930 enc->bitrate = 0;
931 }
932 break;
933 }
934 case ARG_CORRECTION_MODE:
935 enc->correction_mode = g_value_get_enum (value);
936 break;
937 case ARG_MD5:
938 enc->md5 = g_value_get_boolean (value);
939 break;
940 case ARG_EXTRA_PROCESSING:
941 enc->extra_processing = g_value_get_uint (value);
942 break;
943 case ARG_JOINT_STEREO_MODE:
944 enc->joint_stereo_mode = g_value_get_enum (value);
945 break;
946 default:
947 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
948 break;
949 }
950 }
951
952 static void
gst_wavpack_enc_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)953 gst_wavpack_enc_get_property (GObject * object, guint prop_id, GValue * value,
954 GParamSpec * pspec)
955 {
956 GstWavpackEnc *enc = GST_WAVPACK_ENC (object);
957
958 switch (prop_id) {
959 case ARG_MODE:
960 g_value_set_enum (value, enc->mode);
961 break;
962 case ARG_BITRATE:
963 if (enc->bps == 0.0) {
964 g_value_set_uint (value, enc->bitrate);
965 } else {
966 g_value_set_uint (value, 0);
967 }
968 break;
969 case ARG_BITSPERSAMPLE:
970 if (enc->bitrate == 0) {
971 g_value_set_double (value, enc->bps);
972 } else {
973 g_value_set_double (value, 0.0);
974 }
975 break;
976 case ARG_CORRECTION_MODE:
977 g_value_set_enum (value, enc->correction_mode);
978 break;
979 case ARG_MD5:
980 g_value_set_boolean (value, enc->md5);
981 break;
982 case ARG_EXTRA_PROCESSING:
983 g_value_set_uint (value, enc->extra_processing);
984 break;
985 case ARG_JOINT_STEREO_MODE:
986 g_value_set_enum (value, enc->joint_stereo_mode);
987 break;
988 default:
989 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
990 break;
991 }
992 }
993
994 gboolean
gst_wavpack_enc_plugin_init(GstPlugin * plugin)995 gst_wavpack_enc_plugin_init (GstPlugin * plugin)
996 {
997 if (!gst_element_register (plugin, "wavpackenc",
998 GST_RANK_NONE, GST_TYPE_WAVPACK_ENC))
999 return FALSE;
1000
1001 GST_DEBUG_CATEGORY_INIT (gst_wavpack_enc_debug, "wavpackenc", 0,
1002 "Wavpack encoder");
1003
1004 return TRUE;
1005 }
1006