[Cmake-Android AV] ffmpeg3.4 audio resampling

[Cmake-Android] audio and video summary:

 

Audio flowchart resampling

Function Introduction

swr_alloc ()

Distribute audio resampling context

 

swr_alloc_set_opts(...)

Set audio resampling parameters can be changed through the sample rate of the audio playback speed, but generally sound distortion

struct SwrContext *swr_alloc_set_opts(struct SwrContext *s,
                                    int64_t out_ch_layout, 
                                    enum AVSampleFormat out_sample_fmt, 
                                    int out_sample_rate,
                                    int64_t  in_ch_layout, 
                                    enum AVSampleFormat  in_sample_fmt, 
                                    int  in_sample_rate,
                                    int log_offset, void *log_ctx);

Parameter Description:

  • Parameter 1: audio resampling context
  • Parameter 2: output layout, such as: 5.1 ...
  • Parameter 3: output sample format. Float, S16, S24, s16 is generally used in the vast majority of sound cards support
  • Parameter 4: output sample rate. You can not change.
  • Parameter 5: layout input.
  • Parameter 6: the input sample format.
  • 7 parameters: input sample rate.
  • Parameter 8, 9 parameters, log, not control, can be directly transmitted 0

 

swr_init(struct SwrContext *s)

Context Initialization audio resampling

 

swr_convert(...)

Resampling the audio frame

int swr_convert(struct SwrContext *s, uint8_t **out, int out_count,
                                const uint8_t **in , int in_count);

Parameter Description:

  • Parameter 1: context of audio resampling
  • Parameter 2: output pointer. Transmission output array
  • Number of samples of the output, not the number of bytes: 3 parameters. The number of samples of a single channel.
  • Parameter 4: the input array, AVFrame the decoded DATA
  • Parameter 5: the number of samples of a single channel input.

 

swr_free(struct SwrContext **s)

Release audio context space resampling

 

The key code reference

//音频重采样上下文初始化
SwrContext *swrContext = swr_alloc();
char *pcm = new char[480000*4*2];
swrContext = swr_alloc_set_opts(swrContext,
                                av_get_default_channel_layout(2),
                                AV_SAMPLE_FMT_S16,
                                ac->sample_rate,
                                av_get_default_channel_layout(ac->channels),
                                ac->sample_fmt,
                                ac->sample_rate,
                                0, 0);
re = swr_init(swrContext);
if (re != 0)
{
    LOGI("swr_init failed");
} else
{
    LOGI("swr_init success");
}
uint8_t *out[2] = {0};
out[0] = (uint8_t *)pcm;

//转换音频帧
int len = swr_convert(swrContext, out, frame->nb_samples,
                      (const uint8_t **)frame->data, frame->nb_samples);

 

note:

Resampling the audio related functions included in the library libswresample.so

It contains header #include <libswresample / swresample.h> code 

 

Guess you like

Origin blog.csdn.net/adolph_lu/article/details/92795081
Recommended