fixed.c 7.87 KB
Newer Older
1
/*****************************************************************************
2
 * fixed.c: Fixed-point audio format conversions
3
 *****************************************************************************
4
 * Copyright (C) 2002, 2006 the VideoLAN team
Clément Stenac's avatar
Clément Stenac committed
5
 * $Id$
6
 *
Jean-Paul Saman's avatar
Jean-Paul Saman committed
7
 * Authors: Jean-Paul Saman <jpsaman _at_ videolan _dot_ org>
8 9
 *          Marc Ariberti <marcari@videolan.org>
 *          Samuel Hocevar <sam@zoy.org>
10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
Antoine Cellerier's avatar
Antoine Cellerier committed
23
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 25 26 27 28 29
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/

30 31 32 33
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

34
#include <vlc_common.h>
35
#include <vlc_plugin.h>
Clément Stenac's avatar
Clément Stenac committed
36
#include <vlc_aout.h>
37 38 39 40

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
41 42 43
static int  Create_F32ToS16    ( vlc_object_t * );
static void Do_F32ToS16( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
                         aout_buffer_t * );
44

45 46 47 48 49 50 51
static int  Create_S16ToF32    ( vlc_object_t * );
static void Do_S16ToF32( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
                         aout_buffer_t * );

static int  Create_U8ToF32    ( vlc_object_t * );
static void Do_U8ToF32( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
                         aout_buffer_t * );
52 53 54 55

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
56 57 58 59 60 61 62 63 64 65 66 67
vlc_module_begin ()
    set_description( N_("Fixed point audio format conversions") )
    add_submodule ()
        set_callbacks( Create_F32ToS16, NULL )
        set_capability( "audio filter", 10 )
    add_submodule ()
        set_callbacks( Create_S16ToF32, NULL )
        set_capability( "audio filter", 15 )
    add_submodule ()
        set_callbacks( Create_U8ToF32, NULL )
        set_capability( "audio filter", 1 )
vlc_module_end ()
68 69

/*****************************************************************************
70
 * F32 to S16
71
 *****************************************************************************/
72
static int Create_F32ToS16( vlc_object_t *p_this )
73 74 75
{
    aout_filter_t * p_filter = (aout_filter_t *)p_this;

76
    if ( p_filter->input.i_format != VLC_CODEC_FI32
77
          || p_filter->output.i_format != VLC_CODEC_S16N )
78 79 80 81
    {
        return -1;
    }

82
    if ( !AOUT_FMTS_SIMILAR( &p_filter->input, &p_filter->output ) )
83 84 85 86
    {
        return -1;
    }

87
    p_filter->pf_do_work = Do_F32ToS16;
88 89
    p_filter->b_in_place = 1;

90
    return VLC_SUCCESS;;
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
}

/*****************************************************************************
 * support routines borrowed from mpg321 (file: mad.c), which is distributed
 * under GPL license
 *
 * mpg321 was written by Joe Drew <drew@debian.org>, and based upon 'plaympeg'
 * from the smpeg sources, which was written by various people from Loki Software
 * (http://www.lokigames.com).
 *
 * It also incorporates some source from mad, written by Robert Leslie
 *****************************************************************************/

/* The following two routines and data structure are from the ever-brilliant
     Rob Leslie.
*/

#define VLC_F_FRACBITS  28

# if VLC_F_FRACBITS == 28
Sam Hocevar's avatar
Sam Hocevar committed
111
#  define VLC_F(x) ((vlc_fixed_t) (x##L))
112 113
# endif

Sam Hocevar's avatar
Sam Hocevar committed
114
# define VLC_F_ONE VLC_F(0x10000000)
115 116 117 118

/*****************************************************************************
 * s24_to_s16_pcm: Scale a 24 bit pcm sample to a 16 bit pcm sample.
 *****************************************************************************/
Sam Hocevar's avatar
Sam Hocevar committed
119
static inline int16_t s24_to_s16_pcm(vlc_fixed_t sample)
120 121 122 123 124 125 126 127 128 129 130 131 132 133
{
  /* round */
  sample += (1L << (VLC_F_FRACBITS - 16));

  /* clip */
  if (sample >= VLC_F_ONE)
    sample = VLC_F_ONE - 1;
  else if (sample < -VLC_F_ONE)
    sample = -VLC_F_ONE;

  /* quantize */
  return (sample >> (VLC_F_FRACBITS + 1 - 16));
}

134 135
static void Do_F32ToS16( aout_instance_t * p_aout, aout_filter_t * p_filter,
                         aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
136
{
Rafaël Carré's avatar
Rafaël Carré committed
137
    VLC_UNUSED(p_aout);
138 139
    int i;
    vlc_fixed_t * p_in = (vlc_fixed_t *)p_in_buf->p_buffer;
Sam Hocevar's avatar
Sam Hocevar committed
140
    int16_t * p_out = (int16_t *)p_out_buf->p_buffer;
141

142 143
    for ( i = p_in_buf->i_nb_samples
               * aout_FormatNbChannels( &p_filter->input ) ; i-- ; )
144 145
    {
        /* Fast Scaling */
146
        *p_out++ = s24_to_s16_pcm(*p_in++);
147 148
    }
    p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
149
    p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes / 2;
150
}
151 152 153 154 155 156 157 158 159


/*****************************************************************************
 * S16 to F32
 *****************************************************************************/
static int Create_S16ToF32( vlc_object_t *p_this )
{
    aout_filter_t * p_filter = (aout_filter_t *)p_this;

160
    if ( p_filter->output.i_format != VLC_CODEC_FI32
161
          || p_filter->input.i_format != VLC_CODEC_S16N )
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
    {
        return -1;
    }

    if ( !AOUT_FMTS_SIMILAR( &p_filter->input, &p_filter->output ) )
    {
        return -1;
    }

    p_filter->pf_do_work = Do_S16ToF32;
    p_filter->b_in_place = 1;

    return 0;
}

static void Do_S16ToF32( aout_instance_t * p_aout, aout_filter_t * p_filter,
                         aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
{
Rafaël Carré's avatar
Rafaël Carré committed
180
    VLC_UNUSED(p_aout);
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
    int i = p_in_buf->i_nb_samples * aout_FormatNbChannels( &p_filter->input );

    /* We start from the end because b_in_place is true */
    int16_t * p_in = (int16_t *)p_in_buf->p_buffer + i - 1;
    vlc_fixed_t * p_out = (vlc_fixed_t *)p_out_buf->p_buffer + i - 1;

    while( i-- )
    {
        *p_out = (vlc_fixed_t)( (int32_t)(*p_in) * (FIXED32_ONE >> 16) );
        p_in--; p_out--;
    }

    p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
    p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes
            * sizeof(vlc_fixed_t) / sizeof(int16_t);
}


/*****************************************************************************
 * U8 to F32
 *****************************************************************************/
static int Create_U8ToF32( vlc_object_t *p_this )
{
    aout_filter_t * p_filter = (aout_filter_t *)p_this;

206 207
    if ( p_filter->input.i_format != VLC_CODEC_U8
          || p_filter->output.i_format != VLC_CODEC_FI32 )
208 209 210 211 212 213 214 215 216 217
    {
        return -1;
    }

    if ( !AOUT_FMTS_SIMILAR( &p_filter->input, &p_filter->output ) )
    {
        return -1;
    }

    p_filter->pf_do_work = Do_U8ToF32;
218
    p_filter->b_in_place = true;
219 220 221 222 223 224 225

    return 0;
}

static void Do_U8ToF32( aout_instance_t * p_aout, aout_filter_t * p_filter,
                        aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
{
Rafaël Carré's avatar
Rafaël Carré committed
226
    VLC_UNUSED(p_aout);
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
    int i = p_in_buf->i_nb_samples * aout_FormatNbChannels( &p_filter->input );

    /* We start from the end because b_in_place is true */
    uint8_t * p_in = (uint8_t *)p_in_buf->p_buffer + i - 1;
    vlc_fixed_t * p_out = (vlc_fixed_t *)p_out_buf->p_buffer + i - 1;

    while( i-- )
    {
        *p_out = (vlc_fixed_t)( (int32_t)(*p_in - 128) * (FIXED32_ONE / 128) );
        p_in--; p_out--;
    }

    p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
    p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes * sizeof(vlc_fixed_t);
}