filters.c
上传用户:kjfoods
上传日期:2020-07-06
资源大小:29949k
文件大小:15k
源码类别:

midi

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2.  * filters.c : audio output filters management
  3.  *****************************************************************************
  4.  * Copyright (C) 2002-2007 the VideoLAN team
  5.  * $Id: 2bf64e7ca8bf7eadefbdc5281a128ff9f7ec4f6e $
  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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  22.  *****************************************************************************/
  23. /*****************************************************************************
  24.  * Preamble
  25.  *****************************************************************************/
  26. #ifdef HAVE_CONFIG_H
  27. # include "config.h"
  28. #endif
  29. #include <vlc_common.h>
  30. #include <vlc_dialog.h>
  31. #ifdef HAVE_ALLOCA_H
  32. #   include <alloca.h>
  33. #endif
  34. #include <vlc_aout.h>
  35. #include "aout_internal.h"
  36. #include <libvlc.h>
  37. /*****************************************************************************
  38.  * FindFilter: find an audio filter for a specific transformation
  39.  *****************************************************************************/
  40. static aout_filter_t * FindFilter( aout_instance_t * p_aout,
  41.                              const audio_sample_format_t * p_input_format,
  42.                              const audio_sample_format_t * p_output_format )
  43. {
  44.     static const char typename[] = "audio output";
  45.     aout_filter_t * p_filter;
  46.     p_filter = vlc_custom_create( p_aout, sizeof(*p_filter),
  47.                                   VLC_OBJECT_GENERIC, typename );
  48.     if ( p_filter == NULL ) return NULL;
  49.     vlc_object_attach( p_filter, p_aout );
  50.     memcpy( &p_filter->input, p_input_format, sizeof(audio_sample_format_t) );
  51.     memcpy( &p_filter->output, p_output_format,
  52.             sizeof(audio_sample_format_t) );
  53.     p_filter->p_module = module_need( p_filter, "audio filter", NULL, false );
  54.     if ( p_filter->p_module == NULL )
  55.     {
  56.         vlc_object_detach( p_filter );
  57.         vlc_object_release( p_filter );
  58.         return NULL;
  59.     }
  60.     p_filter->b_continuity = false;
  61.     return p_filter;
  62. }
  63. /*****************************************************************************
  64.  * SplitConversion: split a conversion in two parts
  65.  *****************************************************************************
  66.  * Returns the number of conversions required by the first part - 0 if only
  67.  * one conversion was asked.
  68.  * Beware : p_output_format can be modified during this function if the
  69.  * developer passed SplitConversion( toto, titi, titi, ... ). That is legal.
  70.  * SplitConversion( toto, titi, toto, ... ) isn't.
  71.  *****************************************************************************/
  72. static int SplitConversion( const audio_sample_format_t * p_input_format,
  73.                             const audio_sample_format_t * p_output_format,
  74.                             audio_sample_format_t * p_middle_format )
  75. {
  76.     bool b_format =
  77.              (p_input_format->i_format != p_output_format->i_format);
  78.     bool b_rate = (p_input_format->i_rate != p_output_format->i_rate);
  79.     bool b_channels =
  80.         (p_input_format->i_physical_channels
  81.           != p_output_format->i_physical_channels)
  82.      || (p_input_format->i_original_channels
  83.           != p_output_format->i_original_channels);
  84.     int i_nb_conversions = b_format + b_rate + b_channels;
  85.     if ( i_nb_conversions <= 1 ) return 0;
  86.     memcpy( p_middle_format, p_output_format, sizeof(audio_sample_format_t) );
  87.     if ( i_nb_conversions == 2 )
  88.     {
  89.         if ( !b_format || !b_channels )
  90.         {
  91.             p_middle_format->i_rate = p_input_format->i_rate;
  92.             aout_FormatPrepare( p_middle_format );
  93.             return 1;
  94.         }
  95.         /* !b_rate */
  96.         p_middle_format->i_physical_channels
  97.              = p_input_format->i_physical_channels;
  98.         p_middle_format->i_original_channels
  99.              = p_input_format->i_original_channels;
  100.         aout_FormatPrepare( p_middle_format );
  101.         return 1;
  102.     }
  103.     /* i_nb_conversion == 3 */
  104.     p_middle_format->i_rate = p_input_format->i_rate;
  105.     aout_FormatPrepare( p_middle_format );
  106.     return 2;
  107. }
  108. static void ReleaseFilter( aout_filter_t * p_filter )
  109. {
  110.     module_unneed( p_filter, p_filter->p_module );
  111.     vlc_object_detach( p_filter );
  112.     vlc_object_release( p_filter );
  113. }
  114. /*****************************************************************************
  115.  * aout_FiltersCreatePipeline: create a filters pipeline to transform a sample
  116.  *                             format to another
  117.  *****************************************************************************
  118.  * pi_nb_filters must be initialized before calling this function
  119.  *****************************************************************************/
  120. int aout_FiltersCreatePipeline( aout_instance_t * p_aout,
  121.                                 aout_filter_t ** pp_filters_start,
  122.                                 int * pi_nb_filters,
  123.                                 const audio_sample_format_t * p_input_format,
  124.                                 const audio_sample_format_t * p_output_format )
  125. {
  126.     aout_filter_t** pp_filters = pp_filters_start + *pi_nb_filters;
  127.     audio_sample_format_t temp_format;
  128.     int i_nb_conversions;
  129.     if ( AOUT_FMTS_IDENTICAL( p_input_format, p_output_format ) )
  130.     {
  131.         msg_Dbg( p_aout, "no need for any filter" );
  132.         return 0;
  133.     }
  134.     aout_FormatsPrint( p_aout, "filter(s)", p_input_format, p_output_format );
  135.     if( *pi_nb_filters + 1 > AOUT_MAX_FILTERS )
  136.     {
  137.         msg_Err( p_aout, "max filter reached (%d)", AOUT_MAX_FILTERS );
  138.         dialog_Fatal( p_aout, _("Audio filtering failed"),
  139.                       _("The maximum number of filters (%d) was reached."),
  140.                       AOUT_MAX_FILTERS );
  141.         return -1;
  142.     }
  143.     /* Try to find a filter to do the whole conversion. */
  144.     pp_filters[0] = FindFilter( p_aout, p_input_format, p_output_format );
  145.     if ( pp_filters[0] != NULL )
  146.     {
  147.         msg_Dbg( p_aout, "found a filter for the whole conversion" );
  148.         ++*pi_nb_filters;
  149.         return 0;
  150.     }
  151.     /* We'll have to split the conversion. We always do the downmixing
  152.      * before the resampling, because the audio decoder can probably do it
  153.      * for us. */
  154.     i_nb_conversions = SplitConversion( p_input_format,
  155.                                         p_output_format, &temp_format );
  156.     if ( !i_nb_conversions )
  157.     {
  158.         /* There was only one conversion to do, and we already failed. */
  159.         msg_Err( p_aout, "couldn't find a filter for the conversion" );
  160.         return -1;
  161.     }
  162.     pp_filters[0] = FindFilter( p_aout, p_input_format, &temp_format );
  163.     if ( pp_filters[0] == NULL && i_nb_conversions == 2 )
  164.     {
  165.         /* Try with only one conversion. */
  166.         SplitConversion( p_input_format, &temp_format, &temp_format );
  167.         pp_filters[0] = FindFilter( p_aout, p_input_format, &temp_format );
  168.     }
  169.     if ( pp_filters[0] == NULL )
  170.     {
  171.         msg_Err( p_aout,
  172.               "couldn't find a filter for the first part of the conversion" );
  173.         return -1;
  174.     }
  175.     /* We have the first stage of the conversion. Find a filter for
  176.      * the rest. */
  177.     if( *pi_nb_filters + 2 > AOUT_MAX_FILTERS )
  178.     {
  179.         ReleaseFilter( pp_filters[0] );
  180.         msg_Err( p_aout, "max filter reached (%d)", AOUT_MAX_FILTERS );
  181.         dialog_Fatal( p_aout, _("Audio filtering failed"),
  182.                       _("The maximum number of filters (%d) was reached."),
  183.                       AOUT_MAX_FILTERS );
  184.         return -1;
  185.     }
  186.     pp_filters[1] = FindFilter( p_aout, &pp_filters[0]->output,
  187.                                 p_output_format );
  188.     if ( pp_filters[1] == NULL )
  189.     {
  190.         /* Try to split the conversion. */
  191.         i_nb_conversions = SplitConversion( &pp_filters[0]->output,
  192.                                            p_output_format, &temp_format );
  193.         if ( !i_nb_conversions )
  194.         {
  195.             ReleaseFilter( pp_filters[0] );
  196.             msg_Err( p_aout,
  197.               "couldn't find a filter for the second part of the conversion" );
  198.             return -1;
  199.         }
  200.         if( *pi_nb_filters + 3 > AOUT_MAX_FILTERS )
  201.         {
  202.             ReleaseFilter( pp_filters[0] );
  203.             msg_Err( p_aout, "max filter reached (%d)", AOUT_MAX_FILTERS );
  204.             dialog_Fatal( p_aout, _("Audio filtering failed"),
  205.                           _("The maximum number of filters (%d) was reached."),
  206.                           AOUT_MAX_FILTERS );
  207.             return -1;
  208.         }
  209.         pp_filters[1] = FindFilter( p_aout, &pp_filters[0]->output,
  210.                                     &temp_format );
  211.         pp_filters[2] = FindFilter( p_aout, &temp_format,
  212.                                     p_output_format );
  213.         if ( pp_filters[1] == NULL || pp_filters[2] == NULL )
  214.         {
  215.             ReleaseFilter( pp_filters[0] );
  216.             if ( pp_filters[1] != NULL )
  217.             {
  218.                 ReleaseFilter( pp_filters[1] );
  219.             }
  220.             if ( pp_filters[2] != NULL )
  221.             {
  222.                 ReleaseFilter( pp_filters[2] );
  223.             }
  224.             msg_Err( p_aout,
  225.                "couldn't find filters for the second part of the conversion" );
  226.             return -1;
  227.         }
  228.         *pi_nb_filters += 3;
  229.         msg_Dbg( p_aout, "found 3 filters for the whole conversion" );
  230.     }
  231.     else
  232.     {
  233.         *pi_nb_filters += 2;
  234.         msg_Dbg( p_aout, "found 2 filters for the whole conversion" );
  235.     }
  236.     return 0;
  237. }
  238. /*****************************************************************************
  239.  * aout_FiltersDestroyPipeline: deallocate a filters pipeline
  240.  *****************************************************************************/
  241. void aout_FiltersDestroyPipeline( aout_instance_t * p_aout,
  242.                                   aout_filter_t ** pp_filters,
  243.                                   int i_nb_filters )
  244. {
  245.     int i;
  246.     (void)p_aout;
  247.     for ( i = 0; i < i_nb_filters; i++ )
  248.     {
  249.         aout_filter_t *p_filter = pp_filters[i];
  250.         module_unneed( p_filter, p_filter->p_module );
  251.         free( p_filter->p_owner );
  252.         vlc_object_detach( p_filter );
  253.         vlc_object_release( p_filter );
  254.     }
  255. }
  256. /*****************************************************************************
  257.  * aout_FiltersHintBuffers: fill in aout_alloc_t structures to optimize
  258.  *                          buffer allocations
  259.  *****************************************************************************/
  260. void aout_FiltersHintBuffers( aout_instance_t * p_aout,
  261.                               aout_filter_t ** pp_filters,
  262.                               int i_nb_filters, aout_alloc_t * p_first_alloc )
  263. {
  264.     int i;
  265.     (void)p_aout; /* unused */
  266.     for ( i = i_nb_filters - 1; i >= 0; i-- )
  267.     {
  268.         aout_filter_t * p_filter = pp_filters[i];
  269.         int i_output_size = p_filter->output.i_bytes_per_frame
  270.                              * p_filter->output.i_rate * AOUT_MAX_INPUT_RATE
  271.                              / p_filter->output.i_frame_length;
  272.         int i_input_size = p_filter->input.i_bytes_per_frame
  273.                              * p_filter->input.i_rate * AOUT_MAX_INPUT_RATE
  274.                              / p_filter->input.i_frame_length;
  275.         p_first_alloc->i_bytes_per_sec = __MAX( p_first_alloc->i_bytes_per_sec,
  276.                                                 i_output_size );
  277.         if ( p_filter->b_in_place )
  278.         {
  279.             p_first_alloc->i_bytes_per_sec = __MAX(
  280.                                          p_first_alloc->i_bytes_per_sec,
  281.                                          i_input_size );
  282.             p_filter->output_alloc.i_alloc_type = AOUT_ALLOC_NONE;
  283.         }
  284.         else
  285.         {
  286.             /* We're gonna need a buffer allocation. */
  287.             memcpy( &p_filter->output_alloc, p_first_alloc,
  288.                     sizeof(aout_alloc_t) );
  289.             p_first_alloc->i_alloc_type = AOUT_ALLOC_STACK;
  290.             p_first_alloc->i_bytes_per_sec = i_input_size;
  291.         }
  292.     }
  293. }
  294. /*****************************************************************************
  295.  * aout_FiltersPlay: play a buffer
  296.  *****************************************************************************/
  297. void aout_FiltersPlay( aout_instance_t * p_aout,
  298.                        aout_filter_t ** pp_filters,
  299.                        int i_nb_filters, aout_buffer_t ** pp_input_buffer )
  300. {
  301.     int i;
  302.     for( i = 0; i < i_nb_filters; i++ )
  303.     {
  304.         aout_filter_t * p_filter = pp_filters[i];
  305.         aout_buffer_t * p_output_buffer;
  306.         /* Resamplers can produce slightly more samples than (i_in_nb *
  307.          * p_filter->output.i_rate / p_filter->input.i_rate) so we need
  308.          * slightly bigger buffers. */
  309.         aout_BufferAlloc( &p_filter->output_alloc,
  310.                           ((mtime_t)(*pp_input_buffer)->i_nb_samples + 2)
  311.                           * 1000000 / p_filter->input.i_rate,
  312.                           *pp_input_buffer, p_output_buffer );
  313.         if( p_output_buffer == NULL )
  314.             return;
  315.         /* Please note that p_output_buffer->i_nb_samples & i_nb_bytes
  316.          * shall be set by the filter plug-in. */
  317.         if( (*pp_input_buffer)->i_nb_samples > 0 )
  318.         {
  319.             p_filter->pf_do_work( p_aout, p_filter, *pp_input_buffer,
  320.                                   p_output_buffer );
  321.         }
  322.         else
  323.         {
  324.             p_output_buffer->i_nb_bytes = 0;
  325.             p_output_buffer->i_nb_samples = 0;
  326.         }
  327.         if( !p_filter->b_in_place )
  328.         {
  329.             aout_BufferFree( *pp_input_buffer );
  330.             *pp_input_buffer = p_output_buffer;
  331.         }
  332.     }
  333.     assert( (*pp_input_buffer) == NULL || (*pp_input_buffer)->i_alloc_type != AOUT_ALLOC_STACK );
  334. }
  335. /*****************************************************************************
  336.  * aout_filter_RequestVout
  337.  *****************************************************************************/
  338. vout_thread_t *aout_filter_RequestVout( aout_filter_t *p_filter,
  339.                                         vout_thread_t *p_vout, video_format_t *p_fmt )
  340. {
  341.     if( !p_filter->request_vout.pf_request_vout )
  342.         return NULL;
  343.     return p_filter->request_vout.pf_request_vout( p_filter->request_vout.p_private,
  344.                                                    p_vout, p_fmt, true );
  345. }