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

多媒体

开发平台:

MultiPlatform

  1. /*****************************************************************************
  2.  * trivial.c : trivial mixer plug-in (1 input, no downmixing)
  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_buffer_t * );
  36. /*****************************************************************************
  37.  * Module descriptor
  38.  *****************************************************************************/
  39. vlc_module_begin();
  40.     set_description( _("Trivial audio mixer") );
  41.     set_capability( "audio mixer", 1 );
  42.     set_callbacks( Create, NULL );
  43. vlc_module_end();
  44. /*****************************************************************************
  45.  * Create: allocate trivial mixer
  46.  *****************************************************************************/
  47. static int Create( vlc_object_t *p_this )
  48. {
  49.     aout_instance_t * p_aout = (aout_instance_t *)p_this;
  50.     if ( p_aout->mixer.mixer.i_format != VLC_FOURCC('f','l','3','2')
  51.           && p_aout->mixer.mixer.i_format != VLC_FOURCC('f','i','3','2') )
  52.     {
  53.         return -1;
  54.     }
  55.     p_aout->mixer.pf_do_work = DoWork;
  56.     return 0;
  57. }
  58. /*****************************************************************************
  59.  * DoWork: mix a new output buffer
  60.  *****************************************************************************/
  61. static void DoWork( aout_instance_t * p_aout, aout_buffer_t * p_buffer )
  62. {
  63.     int i = 0;
  64.     aout_input_t * p_input = p_aout->pp_inputs[i];
  65.     int i_nb_channels = aout_FormatNbChannels( &p_aout->mixer.mixer );
  66.     int i_nb_bytes = p_buffer->i_nb_samples * sizeof(int32_t)
  67.                       * i_nb_channels;
  68.     byte_t * p_in;
  69.     byte_t * p_out;
  70.     while ( p_input->b_error )
  71.     {
  72.         p_input = p_aout->pp_inputs[++i];
  73.         /* This can't crash because if no input has b_error == 0, the
  74.          * audio mixer cannot run and we can't be here. */
  75.     }
  76.     p_in = p_input->p_first_byte_to_mix;
  77.     p_out = p_buffer->p_buffer;
  78.     for ( ; ; )
  79.     {
  80.         ptrdiff_t i_available_bytes = (p_input->fifo.p_first->p_buffer
  81.                                         - p_in)
  82.                                         + p_input->fifo.p_first->i_nb_samples
  83.                                            * sizeof(int32_t)
  84.                                            * i_nb_channels;
  85.         if ( i_available_bytes < i_nb_bytes )
  86.         {
  87.             aout_buffer_t * p_old_buffer;
  88.             if ( i_available_bytes > 0 )
  89.                 p_aout->p_vlc->pf_memcpy( p_out, p_in, i_available_bytes );
  90.             i_nb_bytes -= i_available_bytes;
  91.             p_out += i_available_bytes;
  92.             /* Next buffer */
  93.             p_old_buffer = aout_FifoPop( p_aout, &p_input->fifo );
  94.             aout_BufferFree( p_old_buffer );
  95.             if ( p_input->fifo.p_first == NULL )
  96.             {
  97.                 msg_Err( p_aout, "internal amix error" );
  98.                 return;
  99.             }
  100.             p_in = p_input->fifo.p_first->p_buffer;
  101.         }
  102.         else
  103.         {
  104.             if ( i_nb_bytes > 0 )
  105.                 p_aout->p_vlc->pf_memcpy( p_out, p_in, i_nb_bytes );
  106.             p_input->p_first_byte_to_mix = p_in + i_nb_bytes;
  107.             break;
  108.         }
  109.     }
  110.     /* Empty other FIFOs to avoid a memory leak. */
  111.     for ( i++; i < p_aout->i_nb_inputs; i++ )
  112.     {
  113.         aout_fifo_t * p_fifo;
  114.         aout_buffer_t * p_deleted;
  115.         p_input = p_aout->pp_inputs[i];
  116.         if ( p_input->b_error ) continue;
  117.         p_fifo = &p_input->fifo;
  118.         p_deleted = p_fifo->p_first;
  119.         while ( p_deleted != NULL )
  120.         {
  121.             aout_buffer_t * p_next = p_deleted->p_next;
  122.             aout_BufferFree( p_deleted );
  123.             p_deleted = p_next;
  124.         }
  125.         p_fifo->p_first = NULL;
  126.         p_fifo->pp_last = &p_fifo->p_first;
  127.     }
  128. }