]> git.sesse.net Git - vlc/blob - modules/audio_filter/resampler/src.c
Print an errorr if a resampler skipped input frames
[vlc] / modules / audio_filter / resampler / src.c
1 /*****************************************************************************
2  * src.c : Secret Rabbit Code (a.k.a. libsamplerate) resampler
3  *****************************************************************************
4  * Copyright (C) 2011 RĂ©mi Denis-Courmont
5  *
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.
10  *
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.
15  *
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  *****************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include <vlc_common.h>
26 #include <vlc_plugin.h>
27 #include <vlc_aout.h>
28 #include <vlc_filter.h>
29 #include <samplerate.h>
30 #include <math.h>
31
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,
39 };
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)",
43 };
44
45 static int Open (vlc_object_t *);
46 static void Close (vlc_object_t *);
47
48 vlc_module_begin ()
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)
58 vlc_module_end ()
59
60 static block_t *Resample (filter_t *, block_t *);
61
62 static int Open (vlc_object_t *obj)
63 {
64     filter_t *filter = (filter_t *)obj;
65
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)
76         return VLC_EGENERIC;
77
78     int type = var_InheritInteger (obj, "src-converter-type");
79     int channels = aout_FormatNbChannels (&filter->fmt_in.audio);
80     int err;
81
82     SRC_STATE *s = src_new (type, channels, &err);
83     if (s == NULL)
84     {
85         msg_Err (obj, "cannot initialize resampler: %s", src_strerror (err));
86         return VLC_EGENERIC;
87     }
88
89     filter->p_sys = (filter_sys_t *)s;
90     filter->pf_audio_filter = Resample;
91     return VLC_SUCCESS;
92 }
93
94 static void Close (vlc_object_t *obj)
95 {
96     filter_t *filter = (filter_t *)obj;
97     SRC_STATE *s = (SRC_STATE *)filter->p_sys;
98
99     src_delete (s);
100 }
101
102 static block_t *Resample (filter_t *filter, block_t *in)
103 {
104     block_t *out = NULL;
105     const size_t framesize = filter->fmt_out.audio.i_bytes_per_frame;
106
107     SRC_STATE *s = (SRC_STATE *)filter->p_sys;
108     SRC_DATA src;
109
110     src.src_ratio = (double)filter->fmt_out.audio.i_rate
111                   / (double)filter->fmt_in.audio.i_rate;
112
113     int err = src_set_ratio (s, src.src_ratio);
114     if (err != 0)
115     {
116         msg_Err (filter, "cannot update resampling ratio: %s",
117                  src_strerror (err));
118         goto error;
119     }
120
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;
124
125     out = block_Alloc (src.output_frames * framesize);
126     if (unlikely(out == NULL))
127         goto error;
128
129     src.data_in = (float *)in->p_buffer;
130     src.data_out = (float *)out->p_buffer;
131
132     err = src_process (s, &src);
133     if (err != 0)
134     {
135         msg_Err (filter, "cannot resample: %s", src_strerror (err));
136         block_Release (out);
137         out = NULL;
138         goto error;
139     }
140
141     if (src.input_frames_used < src.input_frames)
142         msg_Err (filter, "lost %ld of %ld input frames",
143                  src.input_frames - src.input_frames_used, src.input_frames);
144
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;
150 error:
151     block_Release (in);
152     return out;
153 }