qt: playlist: use item title if available
[vlc.git] / modules / audio_filter / resampler / ugly.c
1 /*****************************************************************************
2  * ugly.c : zero-order hold "ugly" resampler
3  *****************************************************************************
4  * Copyright (C) 2002, 2006 VLC authors and VideoLAN
5  *
6  * Authors: Samuel Hocevar <sam@zoy.org>
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation; either version 2.1 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21  *****************************************************************************/
22
23 /*****************************************************************************
24  * Preamble
25  *****************************************************************************/
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_aout.h>
34 #include <vlc_filter.h>
35
36 /*****************************************************************************
37  * Local prototypes
38  *****************************************************************************/
39 static int Create (vlc_object_t *);
40 static int CreateResampler (vlc_object_t *);
41
42 static block_t *DoWork( filter_t *, block_t * );
43
44 /*****************************************************************************
45  * Module descriptor
46  *****************************************************************************/
47 vlc_module_begin ()
48     set_description( N_("Nearest-neighbor audio resampler") )
49     set_capability( "audio converter", 2 )
50     set_category( CAT_AUDIO )
51     set_subcategory( SUBCAT_AUDIO_RESAMPLER )
52     set_callback( Create )
53
54     add_submodule()
55     set_capability( "audio resampler", 2 )
56     set_callback( CreateResampler )
57 vlc_module_end ()
58
59 /*****************************************************************************
60  * Create: allocate ugly resampler
61  *****************************************************************************/
62 static int Create( vlc_object_t *p_this )
63 {
64     filter_t * p_filter = (filter_t *)p_this;
65
66     if( p_filter->fmt_in.audio.i_rate == p_filter->fmt_out.audio.i_rate )
67         return VLC_EGENERIC;
68     return CreateResampler( p_this );
69 }
70
71 static int CreateResampler( vlc_object_t *p_this )
72 {
73     filter_t * p_filter = (filter_t *)p_this;
74
75     if( p_filter->fmt_in.audio.i_format != p_filter->fmt_out.audio.i_format
76      || p_filter->fmt_in.audio.i_channels != p_filter->fmt_out.audio.i_channels
77      || !AOUT_FMT_LINEAR( &p_filter->fmt_in.audio ) )
78         return VLC_EGENERIC;
79
80     static const struct vlc_filter_operations filter_ops =
81     {
82         .filter_audio = DoWork,
83     };
84     p_filter->ops = &filter_ops;
85     return VLC_SUCCESS;
86 }
87
88 /*****************************************************************************
89  * DoWork: convert a buffer
90  *****************************************************************************/
91 static block_t *DoWork( filter_t * p_filter, block_t * p_in_buf )
92 {
93     /* Check if we really need to run the resampler */
94     if( p_filter->fmt_out.audio.i_rate == p_filter->fmt_in.audio.i_rate )
95         return p_in_buf;
96
97     block_t *p_out_buf = p_in_buf;
98     unsigned int i_out_nb = p_in_buf->i_nb_samples
99         * p_filter->fmt_out.audio.i_rate / p_filter->fmt_in.audio.i_rate;
100     const unsigned framesize = (p_filter->fmt_in.audio.i_bitspersample / 8)
101         * aout_FormatNbChannels( &p_filter->fmt_in.audio );
102
103     if( p_filter->fmt_out.audio.i_rate > p_filter->fmt_in.audio.i_rate )
104     {
105         p_out_buf = block_Alloc( i_out_nb * framesize );
106         if( !p_out_buf )
107             goto out;
108     }
109
110     unsigned char *p_out = p_out_buf->p_buffer;
111     unsigned char *p_in = p_in_buf->p_buffer;
112     unsigned int i_remainder = 0;
113
114     p_out_buf->i_nb_samples = i_out_nb;
115     p_out_buf->i_buffer = i_out_nb * framesize;
116     p_out_buf->i_pts = p_in_buf->i_pts;
117     p_out_buf->i_length = vlc_tick_from_samples(p_out_buf->i_nb_samples, p_filter->fmt_out.audio.i_rate);
118
119     while( i_out_nb )
120     {
121         if( p_out != p_in )
122             memcpy( p_out, p_in, framesize );
123         p_out += framesize;
124         i_out_nb--;
125
126         i_remainder += p_filter->fmt_in.audio.i_rate;
127         while( i_remainder >= p_filter->fmt_out.audio.i_rate )
128         {
129             p_in += framesize;
130             i_remainder -= p_filter->fmt_out.audio.i_rate;
131         }
132     }
133
134     if( p_in_buf != p_out_buf )
135 out:
136         block_Release( p_in_buf );
137     return p_out_buf;
138 }