1 /*****************************************************************************
2 * src.c : Secret Rabbit Code (a.k.a. libsamplerate) resampler
3 *****************************************************************************
4 * Copyright (C) 2011 RĂ©mi Denis-Courmont
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program 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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
25 #include <vlc_common.h>
26 #include <vlc_plugin.h>
28 #include <vlc_filter.h>
29 #include <samplerate.h>
32 #define SRC_CONV_TYPE_TEXT N_("Sample rate converter type")
33 #define SRC_CONV_TYPE_LONGTEXT N_( \
34 "Different resampling algorithm are supported. " \
35 "The best one is slower, while the fast one exhibits low quality.")
36 static const int conv_type_values[] = {
37 SRC_SINC_BEST_QUALITY, SRC_SINC_MEDIUM_QUALITY, SRC_SINC_FASTEST,
38 SRC_ZERO_ORDER_HOLD, SRC_LINEAR,
40 static const char *const conv_type_texts[] = {
41 "Sinc function (best quality)", "Sinc function (medium quality)",
42 "Sinc function (fast)", "Zero Order Hold (fastest)", "Linear (fastest)",
45 static int Open (vlc_object_t *);
46 static void Close (vlc_object_t *);
49 set_shortname (N_("SRC resampler"))
50 set_description (N_("Secret Rabbit Code (libsamplerate) resampler") )
51 set_category (CAT_AUDIO)
52 set_subcategory (SUBCAT_AUDIO_MISC)
53 add_integer ("src-converter-type", SRC_SINC_MEDIUM_QUALITY,
54 SRC_CONV_TYPE_TEXT, SRC_CONV_TYPE_LONGTEXT, true)
55 change_integer_list (conv_type_values, conv_type_texts)
56 set_capability ("audio filter", 50)
57 set_callbacks (Open, Close)
60 static block_t *Resample (filter_t *, block_t *);
62 static int Open (vlc_object_t *obj)
64 filter_t *filter = (filter_t *)obj;
66 /* Only float->float */
67 if (filter->fmt_in.audio.i_format != VLC_CODEC_FL32
68 || filter->fmt_out.audio.i_format != VLC_CODEC_FL32
69 /* No channels remapping */
70 || filter->fmt_in.audio.i_physical_channels
71 != filter->fmt_out.audio.i_physical_channels
72 || filter->fmt_in.audio.i_original_channels
73 != filter->fmt_out.audio.i_original_channels
74 /* Different sample rate */
75 || filter->fmt_in.audio.i_rate == filter->fmt_out.audio.i_rate)
78 int type = var_InheritInteger (obj, "src-converter-type");
79 int channels = aout_FormatNbChannels (&filter->fmt_in.audio);
82 SRC_STATE *s = src_new (type, channels, &err);
85 msg_Err (obj, "cannot initialize resampler: %s", src_strerror (err));
89 filter->p_sys = (filter_sys_t *)s;
90 filter->pf_audio_filter = Resample;
94 static void Close (vlc_object_t *obj)
96 filter_t *filter = (filter_t *)obj;
97 SRC_STATE *s = (SRC_STATE *)filter->p_sys;
102 static block_t *Resample (filter_t *filter, block_t *in)
105 const size_t framesize = filter->fmt_out.audio.i_bytes_per_frame;
107 SRC_STATE *s = (SRC_STATE *)filter->p_sys;
110 src.src_ratio = (double)filter->fmt_out.audio.i_rate
111 / (double)filter->fmt_in.audio.i_rate;
113 int err = src_set_ratio (s, src.src_ratio);
116 msg_Err (filter, "cannot update resampling ratio: %s",
121 src.input_frames = in->i_nb_samples;
122 src.output_frames = ceil (src.src_ratio * src.input_frames);
123 src.end_of_input = 0;
125 out = block_Alloc (src.output_frames * framesize);
126 if (unlikely(out == NULL))
129 src.data_in = (float *)in->p_buffer;
130 src.data_out = (float *)out->p_buffer;
132 err = src_process (s, &src);
135 msg_Err (filter, "cannot resample: %s", src_strerror (err));
141 if (src.input_frames_used < src.input_frames)
142 msg_Warn (filter, "lost %ld of %ld input frames",
143 src.input_frames - src.input_frames_used, src.input_frames);
145 out->i_buffer = src.output_frames_gen * framesize;
146 out->i_nb_samples = src.output_frames_gen;
147 out->i_pts = in->i_pts;
148 out->i_length = src.output_frames_gen * CLOCK_FREQ
149 / filter->fmt_out.audio.i_rate;