D
D
Dmitry Zhilko2015-03-19 10:34:17
Delphi
Dmitry Zhilko, 2015-03-19 10:34:17

How to remove crackling sound when downsampling using ffmpeg?

Hello. I'm having a problem changing sample rate and sample format after decoding using ffmpeg. More precisely, with a decrease in it from 48000 to 44100. A background crack appears in the sound. (There is no crackling when increasing from 44100 to 48000.)
After calling the function:

ErrorCode := avcodec_decode_audio4(AudioCodecContext, AudioFrame,
      @FrameFinished, @Packet);

The following is happening to me:
if FrameFinished = 1 then
    begin
      swr := swr_alloc;
      try
        av_opt_set_int(swr, 'in_channel_layout', AudioFrame.channel_layout, 0);
        av_opt_set_int(swr, 'out_channel_layout', AV_CH_LAYOUT_STEREO, 0);

        av_opt_set_int(swr, 'in_sample_rate', AudioFrame.sample_rate, 0);
        av_opt_set_int(swr, 'out_sample_rate', 44100, 0);

        av_opt_set_sample_fmt(swr, 'in_sample_fmt', TAVSampleFormat(AudioFrame.format), 0);
        av_opt_set_sample_fmt(swr, 'out_sample_fmt', AV_SAMPLE_FMT_S16, 0);

        swr_init(swr);

        OutSample := av_rescale_rnd(
          swr_get_delay(swr, AudioFrame.sample_rate) + AudioFrame.nb_samples,
          44100, AudioFrame.sample_rate, AV_ROUND_UP );

        av_samples_alloc(@OutputBuffer, nil, 2, OutSample,
          AV_SAMPLE_FMT_S16, 0);

        ConvertSize := swr_convert(swr, @OutputBuffer, OutSample,
          @AudioFrame.data[0], AudioFrame.nb_samples);

      finally
        swr_free(@swr);
      end;

      ConvertSize := ConvertSize * av_get_bytes_per_sample(AV_SAMPLE_FMT_S16) * 2 {channel numbers};

      BufferDest.Write(OutputBuffer, ConvertSize);
      av_free(outputBuffer);

My OutputBuffer is declared as PByte.
The format I'm trying to get as output is:
Stereo (2 Channels), Signed 16 bit, 44100

Further, my data is sent to the DirectSound already configured for these parameters , but cod is heard there. I tried saving the converted audio stream to a file and feeding it to Audacity - the result didn't change. In the program, I use the ffmpeg-20150312-git-3bedc99-win32-shared build from the site.
What could be the problem and where can I find a solution? (I omitted the error checks in the code)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Shadow_ru, 2015-03-19
@Shadow_ru

filter_type = cubic add

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question