trivial.c
上传用户:riyaled888
上传日期:2009-03-27
资源大小:7338k
文件大小:6k
源码类别:

多媒体

开发平台:

MultiPlatform

  1. /*****************************************************************************
  2.  * trivial.c : trivial channel mixer plug-in (drops unwanted channels)
  3.  *****************************************************************************
  4.  * Copyright (C) 2002 VideoLAN
  5.  * $Id: trivial.c 6961 2004-03-05 17:34:23Z sam $
  6.  *
  7.  * Authors: Christophe Massiot <massiot@via.ecp.fr>
  8.  *
  9.  * This program is free software; you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation; either version 2 of the License, or
  12.  * (at your option) any later version.
  13.  *
  14.  * This program is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with this program; if not, write to the Free Software
  21.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  22.  *****************************************************************************/
  23. /*****************************************************************************
  24.  * Preamble
  25.  *****************************************************************************/
  26. #include <stdlib.h>                                      /* malloc(), free() */
  27. #include <string.h>
  28. #include <vlc/vlc.h>
  29. #include "audio_output.h"
  30. #include "aout_internal.h"
  31. /*****************************************************************************
  32.  * Local prototypes
  33.  *****************************************************************************/
  34. static int  Create    ( vlc_object_t * );
  35. static void DoWork    ( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
  36.                         aout_buffer_t * );
  37. /*****************************************************************************
  38.  * Module descriptor
  39.  *****************************************************************************/
  40. vlc_module_begin();
  41.     set_description( _("audio filter for trivial channel mixing") );
  42.     set_capability( "audio filter", 1 );
  43.     set_callbacks( Create, NULL );
  44. vlc_module_end();
  45. /*****************************************************************************
  46.  * Create: allocate trivial channel mixer
  47.  *****************************************************************************/
  48. static int Create( vlc_object_t *p_this )
  49. {
  50.     aout_filter_t * p_filter = (aout_filter_t *)p_this;
  51.     if ( (p_filter->input.i_physical_channels
  52.            == p_filter->output.i_physical_channels
  53.            && p_filter->input.i_original_channels
  54.                == p_filter->output.i_original_channels)
  55.           || p_filter->input.i_format != p_filter->output.i_format
  56.           || p_filter->input.i_rate != p_filter->output.i_rate
  57.           || (p_filter->input.i_format != VLC_FOURCC('f','l','3','2')
  58.                && p_filter->input.i_format != VLC_FOURCC('f','i','3','2')) )
  59.     {
  60.         return -1;
  61.     }
  62.     p_filter->pf_do_work = DoWork;
  63.     if ( aout_FormatNbChannels( &p_filter->input )
  64.            > aout_FormatNbChannels( &p_filter->output ) )
  65.     {
  66.         /* Downmixing */
  67.         p_filter->b_in_place = 1;
  68.     }
  69.     else
  70.     {
  71.         /* Upmixing */
  72.         p_filter->b_in_place = 0;
  73.     }
  74.     return 0;
  75. }
  76. /*****************************************************************************
  77.  * SparseCopy: trivially downmix or upmix a buffer
  78.  *****************************************************************************/
  79. static void SparseCopy( int32_t * p_dest, const int32_t * p_src, size_t i_len,
  80.                         int i_output_stride, int i_input_stride )
  81. {
  82.     int i;
  83.     for ( i = i_len; i--; )
  84.     {
  85.         int j;
  86.         for ( j = 0; j < i_output_stride; j++ )
  87.         {
  88.             p_dest[j] = p_src[j % i_input_stride];
  89.         }
  90.         p_src += i_input_stride;
  91.         p_dest += i_output_stride;
  92.     }
  93. }
  94. /*****************************************************************************
  95.  * DoWork: convert a buffer
  96.  *****************************************************************************/
  97. static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
  98.                     aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
  99. {
  100.     int i_input_nb = aout_FormatNbChannels( &p_filter->input );
  101.     int i_output_nb = aout_FormatNbChannels( &p_filter->output );
  102.     int32_t * p_dest = (int32_t *)p_out_buf->p_buffer;
  103.     int32_t * p_src = (int32_t *)p_in_buf->p_buffer;
  104.     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
  105.     p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes * i_output_nb / i_input_nb;
  106.     if ( (p_filter->output.i_original_channels & AOUT_CHAN_PHYSMASK)
  107.                 != (p_filter->input.i_original_channels & AOUT_CHAN_PHYSMASK)
  108.            && (p_filter->input.i_original_channels & AOUT_CHAN_PHYSMASK)
  109.                 == (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT) )
  110.     {
  111.         int i;
  112.         /* This is a bit special. */
  113.         if ( !(p_filter->output.i_original_channels & AOUT_CHAN_LEFT) )
  114.         {
  115.             p_src++;
  116.         }
  117.         if ( p_filter->output.i_physical_channels == AOUT_CHAN_CENTER )
  118.         {
  119.             /* Mono mode */
  120.             for ( i = p_in_buf->i_nb_samples; i--; )
  121.             {
  122.                 *p_dest = *p_src;
  123.                 p_dest++;
  124.                 p_src += 2;
  125.             }
  126.         }
  127.         else
  128.         {
  129.             /* Fake-stereo mode */
  130.             for ( i = p_in_buf->i_nb_samples; i--; )
  131.             {
  132.                 *p_dest = *p_src;
  133.                 p_dest++;
  134.                 *p_dest = *p_src;
  135.                 p_dest++;
  136.                 p_src += 2;
  137.             }
  138.         }
  139.     }
  140.     else if ( p_filter->output.i_original_channels
  141.                                     & AOUT_CHAN_REVERSESTEREO )
  142.     {
  143.         /* Reverse-stereo mode */
  144.         int i;
  145.         for ( i = p_in_buf->i_nb_samples; i--; )
  146.         {
  147.             *p_dest = p_src[1];
  148.             p_dest++;
  149.             *p_dest = p_src[0];
  150.             p_dest++;
  151.             p_src += 2;
  152.         }
  153.     }
  154.     else
  155.     {
  156.         SparseCopy( p_dest, p_src, p_in_buf->i_nb_samples, i_output_nb,
  157.                     i_input_nb );
  158.     }
  159. }