【Android FFMPEG 开发】FFMPEG 音频重采样 ( 初始化音频重采样上下文 SwrContext | 计算音频延迟 | 计算输出样本个数 | 音频重采样 swr_convert )



I . FFMPEG 播放视频流程



FFMPEG 播放视频流程 : 视频中包含图像和音频 ;


① FFMPEG 初始化 : 参考博客 【Android FFMPEG 开发】FFMPEG 初始化 ( 网络初始化 | 打开音视频 | 查找音视频流 )

② FFMPEG 获取 AVStream 音视频流 : 参考博客 【Android FFMPEG 开发】FFMPEG 获取 AVStream 音视频流 ( AVFormatContext 结构体 | 获取音视频流信息 | 获取音视频流个数 | 获取音视频流 )

③ FFMPEG 获取 AVCodec 编解码器 : 参考博客 【Android FFMPEG 开发】FFMPEG 获取编解码器 ( 获取编解码参数 | 查找编解码器 | 获取编解码器上下文 | 设置上下文参数 | 打开编解码器 )

④ FFMPEG 读取音视频流中的数据到 AVPacket : 参考博客 【Android FFMPEG 开发】FFMPEG 读取音视频流中的数据到 AVPacket ( 初始化 AVPacket 数据 | 读取 AVPacket )

⑤ FFMPEG 解码 AVPacket 数据到 AVFrame ( 音频 / 视频数据解码 ) : 参考博客 【Android FFMPEG 开发】FFMPEG 解码 AVPacket 数据到 AVFrame ( AVPacket->解码器 | 初始化 AVFrame | 解码为 AVFrame 数据 )

⑥ FFMPEG AVFrame 图像格式转换 YUV -> RGBA : 参考博客 【Android FFMPEG 开发】FFMPEG AVFrame 图像格式转换 YUV -> RGBA ( 获取 SwsContext | 初始化图像数据存储内存 | 图像格式转换 )

⑦ FFMPEG ANativeWindow 原生绘制 准备 : 参考博客 【Android FFMPEG 开发】FFMPEG ANativeWindow 原生绘制 ( Java 层获取 Surface | 传递画布到本地 | 创建 ANativeWindow )



II . FFMPEG 音频重采样流程



FFMPEG 音频重采样流程 :


〇 视频播放操作 : FFMPEG 环境初始化 , 获取 AVStream 音视频流 , 获取 AVCodec 编解码器 , 读取音视频流中的数据到 AVPacket , 解码 AVPacket 数据到 AVFrame , AVFrame 图像格式转换 YUV -> RGBA , ANativeWindow 原生绘制 ;


〇 音频播放操作 : FFMPEG 环境初始化 , 获取 AVStream 音视频流 , 获取 AVCodec 编解码器 , 读取音视频流中的数据到 AVPacket , 解码 AVPacket 数据到 AVFrame , 然后进行下面的操作 , 音频重采样 ;


① 初始化音频重采样上下文 : struct SwrContext *swr_alloc_set_opts( … ) , int swr_init(struct SwrContext *s)

SwrContext *swrContext = swr_alloc_set_opts(
        0 ,                     //现在还没有 SwrContext 上下文 , 先传入 0
        //输出的音频参数
        AV_CH_LAYOUT_STEREO ,   //双声道立体声
        AV_SAMPLE_FMT_S16 ,     //采样位数 16 位
        44100 ,                 //输出的采样率
        //从编码器中获取输入音频格式
        avCodecContext->channel_layout, //输入的声道数
        avCodecContext->sample_fmt,     //输入的采样位数
        avCodecContext->sample_rate,    //输入的采样率
        0, 0    //日志参数 设置 0 即可
        );
        
swr_init(swrContext);

② 计算积压的延迟数据 : int64_t swr_get_delay(struct SwrContext *s, int64_t base)

int64_t delay = swr_get_delay(swrContext , avFrame->sample_rate);

③ 计算本次重采样后的样本个数 : int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd) av_const

int64_t out_count = av_rescale_rnd(
        avFrame->nb_samples + delay, //本次要处理的数据个数
        44100,
        avFrame->sample_rate ,
        AV_ROUND_UP );

④ 音频重采样 : int swr_convert(struct SwrContext *s, uint8_t **out, int out_count, const uint8_t **in , int in_count)

int samples_per_channel_count = swr_convert(
        swrContext ,
        &data,
        out_count ,
        (const uint8_t **)avFrame->data, //普通指针转为 const 指针需要使用 const_cast 转换
        avFrame->nb_samples
        );

⑤ 计算音频重采样字节数 : 音频重采样 swr_convert ( ) 返回值 samples_per_channel_count 是 每个通道的样本数 ;

pcm_data_bit_size = samples_per_channel_count * 2 * 2;


III . FFMPEG 音频重采样



1 . 音频解码 : FFMPEG 从 AVStream 音频流中读取 AVPacket 压缩的编码数据包 , 然后进行解码 , 获得解码后的数据 , 封装在 AVFrame 中 ;


2 . 音频重采样 : 解码后的 AVFrame 的音频 采样率 , 采样位数 , 声道数 ( 左声道 / 右声道 / 立体声 ) 都是不确定的 , 但是在 Android 中的播放器 , 需要播放指定的 采样率 , 采样位数 , 声道数 参数的音频 , 因此需要将 AVFrame 中的音频数据 , 进行重采样 , 将其转换为我们创建的 Android 播放器可以播放的音频数据 ;


3 . 参考视频解码 : 视频播放的时候也是从 AVStream 中读取 AVPacket 数据 , 然后解码为 AVFrame 数据 , 但是其图像大部分是 YUV 像素格式的 , 需要转成 ARGB 像素格式才能再 Android 的 SurfaceView 中进行绘制 ;


4 . 重采样 与 像素格式转换 : 这个 音频重采样 与 图像的像素格式转换作用相同 , 都是将读取的不确定的音频图像格式 , 转成可以在 Android 中播放或显示的固定的音频图像格式 ;


5 . OpenSL ES 播放参数举例 : 我们设置的 OpenSLES 播放器设定播放的音频格式是 立体声 , 44100 Hz 采样 , 16位采样位数 , 要将 AVFrame 中的解码后的音频转为上面的格式要求 , 才能再 OpenSLES 播放器中播放 ;



IV . FFMPEG 初始化音频重采样上下文 SwrContext



1 . 初始化音频重采样上下文 : 音频重采样需要先初始化 音频重采样上下文 SwrContext , 首先要调用 swr_alloc_set_opts ( ) 初始化内存 并 设置 SwrContext 参数 , 再调用 swr_init(swrContext) 方法初始化 ;


2 . swr_alloc_set_opts ( ) 函数原型 : 为 SwrContext 音频重采样上下文 结构体分配内存 , 并设置相关参数 ;


① struct SwrContext *s 参数 : 音频重采样上下文 结构体指针 , 这里还没有 , 传入 0 即可 ;


输出相关参数 :

② int64_t out_ch_layout 参数 : 输出通道参数 , 左声道 / 右声道 / 立体声 ;

③ enum AVSampleFormat out_sample_fmt 参数 : 输出采样位数 , 每个样本的大小 , 8 位 或 16 位 ;

④ int out_sample_rate 参数 : 输出的采样率 , 单位 Hz , 如 44100 Hz , 代表一秒钟有 44100 个采样 ;


输入相关参数 :

⑤ int64_t in_ch_layout 参数 : 输入通道参数 , 左声道 / 右声道 / 立体声 ;

⑥ enum AVSampleFormat in_sample_fmt 参数 : 输入采样位数 , 每个样本的大小 , 8 位 或 16 位 ;

⑦ int in_sample_rate 参数 : 输入的采样率 , 单位 Hz , 如 44100 Hz , 代表一秒钟有 44100 个采样 ;


⑧ int log_offset 参数 : 日志相关参数 ; 0 即可 ;

⑨ void *log_ctx 参数 : 日志相关参数 ; 0 即可 ;

/**
 * Allocate SwrContext if needed and set/reset common parameters.
 *
 * This function does not require s to be allocated with swr_alloc(). On the
 * other hand, swr_alloc() can use swr_alloc_set_opts() to set the parameters
 * on the allocated context.
 *
 * @param s               existing Swr context if available, or NULL if not
 * @param out_ch_layout   output channel layout (AV_CH_LAYOUT_*)
 * @param out_sample_fmt  output sample format (AV_SAMPLE_FMT_*).
 * @param out_sample_rate output sample rate (frequency in Hz)
 * @param in_ch_layout    input channel layout (AV_CH_LAYOUT_*)
 * @param in_sample_fmt   input sample format (AV_SAMPLE_FMT_*).
 * @param in_sample_rate  input sample rate (frequency in Hz)
 * @param log_offset      logging level offset
 * @param log_ctx         parent logging context, can be NULL
 *
 * @see swr_init(), swr_free()
 * @return NULL on error, allocated context otherwise
 */
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);

3 . swr_init ( ) 函数原型 : 在用户设置完音频重采样上下文参数后 , 调用该方法可以初始化该上下文 ;


① int 返回值 : 如果初始化失败 , 会返回 AVERROR 错误码 ;

/**
 * Initialize context after user parameters have been set.
 * @note The context must be configured using the AVOption API.
 *
 * @see av_opt_set_int()
 * @see av_opt_set_dict()
 *
 * @param[in,out]   s Swr context to initialize
 * @return AVERROR error code in case of failure.
 */
int swr_init(struct SwrContext *s);

4 . FFMPEG 初始化音频重采样上下文 SwrContext 代码示例 :

/*
 设置音频重采样的上下文参数
 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);
 */
swrContext = swr_alloc_set_opts(
        0 ,                     //现在还没有 SwrContext 上下文 , 先传入 0
        //输出的音频参数
        AV_CH_LAYOUT_STEREO ,   //双声道立体声
        AV_SAMPLE_FMT_S16 ,     //采样位数 16 位
        44100 ,                 //输出的采样率
        //从编码器中获取输入音频格式
        avCodecContext->channel_layout, //输入的声道数
        avCodecContext->sample_fmt,     //输入的采样位数
        avCodecContext->sample_rate,    //输入的采样率
        0, 0    //日志参数 设置 0 即可
        );
//注意创建完之后初始化
swr_init(swrContext);


V . FFMPEG 计算音频延迟样本数



1 . 音频延迟情况 : FFMPEG 转码的过程中 , 可能没有一次性将一帧数据处理完毕 , 如输入了 20 个数据 , 一般情况下 20 个数据都能处理完毕 , 有时还会出现只处理了 19 个 , 剩余的 1 个数据就积压在了缓冲区中的情况 , 如果这种积压在缓冲区中的数据过大 , 会造成很大的音频延迟 , 甚至内存崩溃 ;


2 . 延迟数据处理方案 : 每次音频处理时 , 都尝试将上一次积压的音频采样数据加入到本次处理的数据中 , 防止出现音频延迟的情况 ;


3 . 获取音频数据积压个数 : 调用 swr_get_delay ( ) 方法 , 可以获取当前积压的音频采样数 , 或播放延迟时间 ;


4 . 对延迟的理解 : swr_get_delay ( ) 获取的是下一次的样本数据 A 输入 经过多长时间延迟后 , 才能将样本 A 播放出来 , 这个延迟就是积压的数据的播放时间 , 因此每次处理时将少部分积压数据进行处理 , 可以有效降低音频延迟 ;


5 . swr_get_delay ( ) 函数原型 : 获取下一次输入的样本 , 到对应的样本输出时 , 需要经历的延迟 , 即获取延迟的数据播放时长或样本个数 ( 二选一 ) ;


① struct SwrContext *s 参数 : 音频重采样上下文 结构体指针 ;

② int64_t base 参数 : 设置成 1 / 1000 获取延迟的时间 秒 / 毫秒 , 设置采样率 获取延迟的样本个数 ;

/**
 * Gets the delay the next input sample will experience relative to the next output sample.
 *
 * Swresample can buffer data if more input has been provided than available
 * output space, also converting between sample rates needs a delay.
 * This function returns the sum of all such delays.
 * The exact delay is not necessarily an integer value in either input or
 * output sample rate. Especially when downsampling by a large value, the
 * output sample rate may be a poor choice to represent the delay, similarly
 * for upsampling and the input sample rate.
 *
 * @param s     swr context
 * @param base  timebase in which the returned delay will be:
 *              @li if it's set to 1 the returned delay is in seconds
 *              @li if it's set to 1000 the returned delay is in milliseconds
 *              @li if it's set to the input sample rate then the returned
 *                  delay is in input samples
 *              @li if it's set to the output sample rate then the returned
 *                  delay is in output samples
 *              @li if it's the least common multiple of in_sample_rate and
 *                  out_sample_rate then an exact rounding-free delay will be
 *                  returned
 * @returns     the delay in 1 / @c base units.
 */
int64_t swr_get_delay(struct SwrContext *s, int64_t base);

6 . FFMPEG 计算音频延迟样本数 swr_get_delay ( ) 函数使用示例 : 这里传入样本采样率 , 获取的是样本个数 ;

//OpenSLES 播放器设定播放的音频格式是 立体声 , 44100 Hz 采样 , 16位采样位数
//  解码出来的 AVFrame 中的数据格式不确定 , 需要进行重采样
/*
    int64_t swr_get_delay(
    struct SwrContext *s,
    int64_t base
    );
    转码的过程中 , 输入 10 个数据 , 并不一定都能处理完毕并输出 10 个数据 , 可能处理输出了 8 个数据
    还剩余 2 个数据没有处理
    那么在下一次处理的时候 , 需要将上次没有处理完的两个数据处理了 ;
    如果不处理上次的2个数据 , 那么数据会一直积压 , 如果积压数据过多 , 最终造成很大的延迟 , 甚至崩溃
    因此每次处理的时候 , 都要尝试将上次剩余没有处理的数据加入到本次处理的数据中
    如果计算出的 delay 一直等于 0 , 说明没有积压数据
 */
int64_t delay = swr_get_delay(swrContext , avFrame->sample_rate);


VI . FFMPEG 计算音频重采样输出样本个数



1 . FFMPEG 音频重采样 : 音频重采样操作 , 需要指定一个输出样本个数, 目前已知的是 输入音频采样个数 , 输出音频采样率 , 输入音频采样率 , 需要计算出输出的音频采样个数 ;


2 . 计算公式如下 :

= 音频播放时间 = \frac{输入音频采样个数}{输入音频采样率}


= × 输出音频采样个数= 音频播放时间 \times 输出音频采样率


= × 输出音频采样个数= \frac{输入音频采样个数}{输入音频采样率} \times 输出音频采样率


3 . 计算溢出问题 : 上面涉及到的计算数据过大 , 音频采样率 与 采样个数 相乘 , 如 44100 Hz 采样率 , 10 万采样 , 相乘结果为 4,410,000,000 , 这个数量级有溢出的风险 , 为了解决计算溢出问题 , FFMPEG 给出了专门的函数 av_rescale_rnd ( ) 来处理这个计算 ;


4 . av_rescale_rnd ( ) 函数原型 : 该函数传入上述 输入音频采样个数 , 输入音频采样率 , 输出音频采样率 参数 , 进行上述计算 , 没有溢出问题 ; 计算公式是 a * b / c ;


① int64_t a 参数 : 输入音频采样个数 ;

② int64_t b 参数 : 输出音频采样率 ;

③ int64_t c 参数 : 输入音频采样率 ;

④ enum AVRounding rnd 参数 : 小数转为整数的方式 , 如四舍五入 , 向上取整 , 或向下取整 等 ;

/**
 * Rescale a 64-bit integer with specified rounding.
 *
 * The operation is mathematically equivalent to `a * b / c`, but writing that
 * directly can overflow, and does not support different rounding methods.
 *
 * @see av_rescale(), av_rescale_q(), av_rescale_q_rnd()
 */
int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd) av_const;

5 . FFMPEG 计算音频重采样输出缓冲区大小 代码示例 :

/*
    将 a 个数据 , 由 c 采样率转换成 b 采样率后 , 返回多少数据
    int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd) av_const;
    下面的方法时将 avFrame->nb_samples 个数据 , 由 avFrame->sample_rate 采样率转为 44100 采样率
    返回的数据个数
    AV_ROUND_UP : 向上取整
 */
int64_t out_count = av_rescale_rnd(
        avFrame->nb_samples + delay, //本次要处理的数据个数
        44100,
        avFrame->sample_rate ,
        AV_ROUND_UP );


VII . FFMPEG 输出样本缓冲区初始化



音频重采样后 , 需要初始化一段内存 , 用于保存重采样后的样本数据 ; 为其分配内存 , 并初始化内存数据 ;

/**
 * 存放重采样后的数据缓冲区 , 这个缓冲区存储 1 秒的数据 
 * 44100 Hz 采样率 , 16 位采样位数 , 双声道立体声 , 占用内存 44100 * 2 * 2 字节 
 */
uint8_t *data = static_cast<uint8_t *>(malloc(44100 * 2 * 2)); 

//初始化内存数据
memset(data, 0, 44100 * 2 * 2);


VIII . FFMPEG 音频重采样



1 . 音频重采样 : 上面准备好了音频重采样的所有参数 , 音频重采样上下文 SwrContext , 输出样本个数 , 输出缓冲区 uint8_t *data , AVFrame 中封装了输入音频的数据内容 , 采样率 , 采样位数 等信息 , 调用 swr_convert ( ) 函数 , 传入上述参数 , 即可进行音频重采样 ;


2 . swr_convert ( ) 函数原型 : FFMPEG 音频重采样的核心方法 ;


① struct SwrContext *s 参数 : 音频重采样上下文结构体指针 ;

② uint8_t **out 参数 : 输出的缓冲区 , 二维指针 ;

③ int out_count 参数 : 输出的缓冲区最大可接受的样本个数

④ const uint8_t **in 参数 : 输入的音频数据 ;

⑤ int in_count 参数 : 输入的样本个数

⑥ int 返回值 : 返回值是每个通道的样本个数 , 这里注意 , 如果是立体声 ,实际 样本数 是返回值 * 2 ;

/** Convert audio.
 *
 * in and in_count can be set to 0 to flush the last few samples out at the
 * end.
 *
 * If more input is provided than output space, then the input will be buffered.
 * You can avoid this buffering by using swr_get_out_samples() to retrieve an
 * upper bound on the required number of output samples for the given number of
 * input samples. Conversion will run directly without copying whenever possible.
 *
 * @param s         allocated Swr context, with parameters set
 * @param out       output buffers, only the first one need be set in case of packed audio
 * @param out_count amount of space available for output in samples per channel
 * @param in        input buffers, only the first one need to be set in case of packed audio
 * @param in_count  number of input samples available in one channel
 *
 * @return number of samples output per channel, negative value on error
 */
int swr_convert(struct SwrContext *s, uint8_t **out, int out_count,
                                const uint8_t **in , int in_count);

//参数注释
int swr_convert(
       struct SwrContext *s,   //上下文
       uint8_t **out,          //输出的缓冲区 ( 需要计算 )
       int out_count,          //输出的缓冲区最大可接受的样本个数 ( 需要计算 )
       const uint8_t **in ,    //输入的数据
       int in_count);          //输入的数据大小

3 . FFMPEG 音频重采样 swr_convert ( ) 函数 代码示例 :

/*
 int swr_convert(
        struct SwrContext *s,   //上下文
        uint8_t **out,          //输出的缓冲区 ( 需要计算 )
        int out_count,          //输出的缓冲区最大可接受的样本个数 ( 需要计算 )
        const uint8_t **in ,    //输入的数据
        int in_count);          //输入的样本个数
返回值 : 转换后的采样个数 , 是样本个数 , 每个样本是 16 位 , 两个字节 ;
        samples_out_count 是每个通道的样本数 , samples_out_count * 2 是立体声双声道样本个数
        samples_out_count * 2 * 2 是字节个数
 */
int samples_per_channel_count = swr_convert(
        swrContext ,
        &data,
        out_count ,
        (const uint8_t **)avFrame->data, //普通指针转为 const 指针需要使用 const_cast 转换
        avFrame->nb_samples
        );


IX . FFMPEG 音频重采样输出的重采样数据字节数计算



1 . 初始值 : 上述调用 swr_convert ( ) 方法 , 进行音频重采样 , 返回值 samples_per_channel_count 是每个通道的样本个数 ;


2 . 立体声样本数 : 如果该音频是立体声音频数据 , 其样本个数是 samples_per_channel_count * 2 ;


3 . 16 位立体声样本个数 : 如果该音频是 16 位立体声数据 , 其数据字节大小是 samples_per_channel_count * 2 * 2 字节 ;


4 . 计算字节数代码示例 :

//根据样本个数计算样本的字节数
pcm_data_bit_size = samples_per_channel_count * 2 * 2;


X . FFMPEG 音频重采样部分代码总结



// I . 音频重采样输出缓冲区准备


/**
 * 存放重采样后的数据缓冲区 , 这个缓冲区存储 1 秒的数据 
 * 44100 Hz 采样率 , 16 位采样位数 , 双声道立体声 , 占用内存 44100 * 2 * 2 字节 
 */
uint8_t *data = static_cast<uint8_t *>(malloc(44100 * 2 * 2)); 

//初始化内存数据
memset(data, 0, 44100 * 2 * 2);


// II . 音频重采样上下文 初始化


/*
 设置音频重采样的上下文参数
 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);
 */
swrContext = swr_alloc_set_opts(
        0 ,                     //现在还没有 SwrContext 上下文 , 先传入 0
        //输出的音频参数
        AV_CH_LAYOUT_STEREO ,   //双声道立体声
        AV_SAMPLE_FMT_S16 ,     //采样位数 16 位
        44100 ,                 //输出的采样率
        //从编码器中获取输入音频格式
        avCodecContext->channel_layout, //输入的声道数
        avCodecContext->sample_fmt,     //输入的采样位数
        avCodecContext->sample_rate,    //输入的采样率
        0, 0    //日志参数 设置 0 即可
        );
        
//注意创建完之后初始化
swr_init(swrContext);


// III . 获取延迟数据


//OpenSLES 播放器设定播放的音频格式是 立体声 , 44100 Hz 采样 , 16位采样位数
//  解码出来的 AVFrame 中的数据格式不确定 , 需要进行重采样
/*
    int64_t swr_get_delay(
    struct SwrContext *s,
    int64_t base
    );
    转码的过程中 , 输入 10 个数据 , 并不一定都能处理完毕并输出 10 个数据 , 可能处理输出了 8 个数据
    还剩余 2 个数据没有处理
    那么在下一次处理的时候 , 需要将上次没有处理完的两个数据处理了 ;
    如果不处理上次的2个数据 , 那么数据会一直积压 , 如果积压数据过多 , 最终造成很大的延迟 , 甚至崩溃
    因此每次处理的时候 , 都要尝试将上次剩余没有处理的数据加入到本次处理的数据中
    如果计算出的 delay 一直等于 0 , 说明没有积压数据
 */
int64_t delay = swr_get_delay(swrContext , avFrame->sample_rate);


// IV . 计算输出样本个数


/*
    将 a 个数据 , 由 c 采样率转换成 b 采样率后 , 返回多少数据
    int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd) av_const;
    下面的方法时将 avFrame->nb_samples 个数据 , 由 avFrame->sample_rate 采样率转为 44100 采样率
    返回的数据个数
    AV_ROUND_UP : 向上取整
 */
int64_t out_count = av_rescale_rnd(
        avFrame->nb_samples + delay, //本次要处理的数据个数
        44100,
        avFrame->sample_rate ,
        AV_ROUND_UP );


// V . 音频重采样


/*
 int swr_convert(
        struct SwrContext *s,   //上下文
        uint8_t **out,          //输出的缓冲区 ( 需要计算 )
        int out_count,          //输出的缓冲区最大可接受的样本个数 ( 需要计算 )
        const uint8_t **in ,    //输入的数据
        int in_count);          //输入的样本个数
返回值 : 转换后的采样个数 , 是样本个数 , 每个样本是 16 位 , 两个字节 ;
        samples_out_count 是每个通道的样本数 , samples_out_count * 2 是立体声双声道样本个数
        samples_out_count * 2 * 2 是字节个数
 */
int samples_per_channel_count = swr_convert(
        swrContext ,
        &data,
        out_count ,
        (const uint8_t **)avFrame->data, //普通指针转为 const 指针需要使用 const_cast 转换
        avFrame->nb_samples
        );


// VI . 最终重采样后的数据字节大小


//根据样本个数计算样本的字节数
pcm_data_bit_size = samples_per_channel_count * 2 * 2;
发布了307 篇原创文章 · 获赞 1043 · 访问量 170万+

猜你喜欢

转载自blog.csdn.net/han1202012/article/details/104871223
今日推荐