/**
 * Initialize the audio resampler based on the input and output codec settings.
 * If the input and output sample formats differ, a conversion is required
 * libswresample takes care of this, but requires initialization.
 * @param      input_codec_context  Codec context of the input file
 * @param      output_codec_context Codec context of the output file
 * @param[out] resample_context     Resample context for the required conversion
 * @return Error code (0 if successful)
 */
    int init_resampler(AVCodecContext *input_codec_context,
                       AVCodecContext *output_codec_context,
                       SwrContext **resample_context)
    {
        int error;

        /*
         * Create a resampler context for the conversion.
         * Set the conversion parameters.
         * Default channel layouts based on the number of channels
         * are assumed for simplicity (they are sometimes not detected
         * properly by the demuxer and/or decoder).
         */
        *resample_context = swr_alloc_set_opts(null,
                                               av_get_default_channel_layout(output_codec_context->channels),
                                               output_codec_context->sample_fmt,
                                               output_codec_context->sample_rate,
                                               av_get_default_channel_layout(input_codec_context->channels),
                                               input_codec_context->sample_fmt,
                                               input_codec_context->sample_rate,
                                               0, null);

        if (*resample_context == null)
        {
            Console.WriteLine("error: Could not allocate resample context");
            return(AVERROR(ENOMEM));
        }

        /*
         * Perform a sanity check so that the number of converted samples is
         * not greater than the number of samples to be converted.
         * If the sample rates differ, this case has to be handled differently
         */
        Debug.Assert(output_codec_context->sample_rate == input_codec_context->sample_rate);

        /* Open the resampler with the specified parameters. */
        if ((error = swr_init(*resample_context)) < 0)
        {
            Console.WriteLine("error: Could not open resample context");
            swr_free(resample_context);
            return(error);
        }

        return(0);
    }
 public static extern void swr_free(SwrContext ** @s);
Exemplo n.º 3
0
 internal static extern void swr_free(SwrContext ** @s);