dolby.c 8.12 KB
Newer Older
1 2 3
/*****************************************************************************
 * dolby.c : simple decoder for dolby surround encoded streams
 *****************************************************************************
4
 * Copyright (C) 2005, 2006 the VideoLAN team
Antoine Cellerier's avatar
Antoine Cellerier committed
5
 * $Id$
6
 *
7
 * Authors: Boris Dorès <babal@via.ecp.fr>
8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * 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
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 23 24 25 26 27 28
 *****************************************************************************/

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

#include <vlc/vlc.h>
Clément Stenac's avatar
Clément Stenac committed
29
#include <vlc_aout.h>
30 31 32 33 34 35 36 37 38 39 40 41 42 43

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int  Create    ( vlc_object_t * );
static void Destroy   ( vlc_object_t * );

static void DoWork    ( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
                        aout_buffer_t * );

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
vlc_module_begin();
44
    set_description( _("Simple decoder for Dolby Surround encoded streams") );
45
    set_shortname( _("Dolby Surround decoder") );
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    set_category( CAT_INPUT );
    set_subcategory( SUBCAT_INPUT_ACODEC );
    set_capability( "audio filter", 5 );
    set_callbacks( Create, Destroy );
vlc_module_end();

/*****************************************************************************
 * Internal data structures
 *****************************************************************************/
struct aout_filter_sys_t
{
    int i_left;
    int i_center;
    int i_right;
    int i_rear_left;
    int i_rear_center;
    int i_rear_right;
};

/* our internal channel order (WG-4 order) */
static const uint32_t pi_channels[] =
{ AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,
  AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, AOUT_CHAN_REARCENTER,
  AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0 };

/*****************************************************************************
 * Create: allocate headphone downmixer
 *****************************************************************************/
static int Create( vlc_object_t *p_this )
{
    int i = 0;
    int i_offset = 0;
    aout_filter_t * p_filter = (aout_filter_t *)p_this;

    /* Validate audio filter format */
81 82 83 84 85
    if ( p_filter->input.i_physical_channels != (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT)
       || ! ( p_filter->input.i_original_channels & AOUT_CHAN_DOLBYSTEREO )
       || aout_FormatNbChannels( &p_filter->output ) <= 2
       || ( p_filter->input.i_original_channels & ~AOUT_CHAN_DOLBYSTEREO )
          != ( p_filter->output.i_original_channels & ~AOUT_CHAN_DOLBYSTEREO ) )
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
    {
        return VLC_EGENERIC;
    }

    if ( p_filter->input.i_rate != p_filter->output.i_rate )
    {
        return VLC_EGENERIC;
    }

    if ( p_filter->input.i_format != VLC_FOURCC('f','l','3','2')
          || p_filter->output.i_format != VLC_FOURCC('f','l','3','2') )
    {
        return VLC_EGENERIC;
    }

    /* Allocate the memory needed to store the module's structure */
    p_filter->p_sys = malloc( sizeof(struct aout_filter_sys_t) );
    if ( p_filter->p_sys == NULL )
    {
105
        msg_Err( p_filter, "out of memory" );
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
        return VLC_EGENERIC;
    }
    p_filter->p_sys->i_left = -1;
    p_filter->p_sys->i_center = -1;
    p_filter->p_sys->i_right = -1;
    p_filter->p_sys->i_rear_left = -1;
    p_filter->p_sys->i_rear_center = -1;
    p_filter->p_sys->i_rear_right = -1;

    while ( pi_channels[i] )
    {
        if ( p_filter->output.i_physical_channels & pi_channels[i] )
        {
            switch ( pi_channels[i] )
            {
                case AOUT_CHAN_LEFT:
                    p_filter->p_sys->i_left = i_offset;
                    break;
                case AOUT_CHAN_CENTER:
                    p_filter->p_sys->i_center = i_offset;
                    break;
                case AOUT_CHAN_RIGHT:
                    p_filter->p_sys->i_right = i_offset;
                    break;
                case AOUT_CHAN_REARLEFT:
                    p_filter->p_sys->i_rear_left = i_offset;
                    break;
                case AOUT_CHAN_REARCENTER:
                    p_filter->p_sys->i_rear_center = i_offset;
                    break;
                case AOUT_CHAN_REARRIGHT:
                    p_filter->p_sys->i_rear_right = i_offset;
                    break;
            }
            ++i_offset;
        }
        ++i;
    }

    p_filter->pf_do_work = DoWork;
    p_filter->b_in_place = 0;

    return VLC_SUCCESS;
}

/*****************************************************************************
 * Destroy: deallocate resources associated with headphone downmixer
 *****************************************************************************/
static void Destroy( vlc_object_t *p_this )
{
    aout_filter_t * p_filter = (aout_filter_t *)p_this;

    if ( p_filter->p_sys != NULL )
    {
        free ( p_filter->p_sys );
        p_filter->p_sys = NULL;
    }
}

/*****************************************************************************
 * DoWork: convert a buffer
 *****************************************************************************/
static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
                    aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
{
    float * p_in = (float*) p_in_buf->p_buffer;
    float * p_out = (float*) p_out_buf->p_buffer;
    size_t i_nb_samples = p_in_buf->i_nb_samples;
    size_t i_nb_channels = aout_FormatNbChannels( &p_filter->output );

    p_out_buf->i_nb_samples = i_nb_samples;
    p_out_buf->i_nb_bytes = sizeof(float) * i_nb_samples
                            * aout_FormatNbChannels( &p_filter->output );
    memset ( p_out , 0 , p_out_buf->i_nb_bytes );

    if ( p_filter->p_sys != NULL )
    {
        struct aout_filter_sys_t * p_sys = p_filter->p_sys;
        size_t i_nb_rear = 0;
        size_t i;

        if ( p_sys->i_rear_left >= 0 )
        {
            ++i_nb_rear;
        }
        if ( p_sys->i_rear_center >= 0 )
        {
            ++i_nb_rear;
        }
        if ( p_sys->i_rear_right >= 0 )
        {
            ++i_nb_rear;
        }

        for ( i = 0; i < i_nb_samples; ++i )
        {
            float f_left = p_in[ i * 2 ];
            float f_right = p_in[ i * 2 + 1 ];
            float f_rear = ( f_left - f_right ) / i_nb_rear;

            if ( p_sys->i_center >= 0 )
            {
                float f_center = f_left + f_right;
                f_left -= f_center / 2;
                f_right -= f_center / 2;

                p_out[ i * i_nb_channels + p_sys->i_center ] = f_center;
            }

            if ( p_sys->i_left >= 0 )
            {
                p_out[ i * i_nb_channels + p_sys->i_left ] = f_left;
            }
            if ( p_sys->i_right >= 0 )
            {
                p_out[ i * i_nb_channels + p_sys->i_right ] = f_right;
            }
            if ( p_sys->i_rear_left >= 0 )
            {
                p_out[ i * i_nb_channels + p_sys->i_rear_left ] = f_rear;
            }
            if ( p_sys->i_rear_center >= 0 )
            {
                p_out[ i * i_nb_channels + p_sys->i_rear_center ] = f_rear;
            }
            if ( p_sys->i_rear_right >= 0 )
            {
                p_out[ i * i_nb_channels + p_sys->i_rear_right ] = f_rear;
            }
        }
    }
}