播放器实战18 xresample

采样率:
即每秒的采样次数。

采样点无限多即连成原来的模拟信号曲线。那什么样的采样率比较合适呢?

录制最终一定是为了播放。采样编码为了存储处理和传输,最终还是要还原出模拟信号来播放。

根据 “奈奎斯特采样理论”:当对被采样的模拟信号进行还原时,其最高频率只有采样频率的一半。
换句话说:要想重构完整的模拟信号,采样率要是模拟信号频率的两倍以上。

人听觉范围:20 Hz -20 KHz。所以对于给人听的音频,采样率要大于40 KHz 当从数字信号转换成模拟信号播放时才不会使人感觉到音质受损。因此,合适的采样率 一般大于 40 KHz,主流的采样率有44100HZ或者48000HZ

采样深度(采样格式):

即量化时纵坐标的量化粒度。

在上图模拟信号中,纵坐标代表的是声音的响度,即音量。

由上图量化过程可知,当量化的粒度越细,离散点取的值也就越接近模拟信号实际的值,如果量化时,采样深度为 1bit,即量化只能为 1 和 0,要么播放最大声音,要么没有声音。

只有采样深度越深,即量化的粒度越细,采样出来的数据,才更接近实际的音量。

现在一般用 16 位来表示一个采样数据的音量大小。(S16)

可以看到本媒体文件中的音频格式信息:
在这里插入图片描述
format=8
audio: the sample format, the value corresponds to enum AVSampleFormat.
对应的查表:

enum AVSampleFormat {
    AV_SAMPLE_FMT_NONE = -1,
    AV_SAMPLE_FMT_U8,          ///< unsigned 8 bits
    AV_SAMPLE_FMT_S16,         ///< signed 16 bits
    AV_SAMPLE_FMT_S32,         ///< signed 32 bits
    AV_SAMPLE_FMT_FLT,         ///< float
    AV_SAMPLE_FMT_DBL,         ///< double

    AV_SAMPLE_FMT_U8P,         ///< unsigned 8 bits, planar
    AV_SAMPLE_FMT_S16P,        ///< signed 16 bits, planar
    AV_SAMPLE_FMT_S32P,        ///< signed 32 bits, planar
    AV_SAMPLE_FMT_FLTP,        ///< float, planar
    AV_SAMPLE_FMT_DBLP,        ///< double, planar
    AV_SAMPLE_FMT_S64,         ///< signed 64 bits
    AV_SAMPLE_FMT_S64P,        ///< signed 64 bits, planar
    AV_SAMPLE_FMT_NB

为AV_SAMPLE_FMT_S32P, ///< signed 32 bits, planar
32位紧凑格式存放的,因为这里的任务是将其转换成16位的便于播放
1.xresample.h

#pragma once
struct AVCodecParameters;
struct AVFrame;
struct SwrContext;
#include <mutex>
class xresample
{
public:

	//输出参数和输入参数一致除了采样格式,输出为S16
	virtual bool Open(AVCodecParameters* para);
	virtual void Close();

	//返回重采样后大小,不管成功与否都释放AVFrame
	virtual int Resample(AVFrame* indata, unsigned char* outdata);
	xresample();
	~xresample();
protected:
	std::mutex mux;
	SwrContext* actx = NULL;
	int outformat = 1;//S16
};

采样率与通道数不变,要变的是输出格式,将要变更的输出格式放在头文件的protect中(outformat)

2.xresample.cpp

#include "xresample.h"
extern "C" {
#include <libswresample/swresample.h>
#include <libavcodec/avcodec.h>
}
#pragma comment(lib,"swresample.lib")
#include <iostream>

using namespace std;


//输出参数和输入参数一致除了采样格式,输出为S16
bool xresample::Open(AVCodecParameters* para)
{
	if (!para)return false;
	mux.lock();
	//音频重采样 上下文初始化
	//if(!actx)
	//	actx = swr_alloc();

	//如果actx为NULL会分配空间
	actx = swr_alloc_set_opts(actx,
		av_get_default_channel_layout(2),	//输出格式
		(AVSampleFormat)outformat,					//输出样本格式
		para->sample_rate,					//输出采样率
		av_get_default_channel_layout(para->channels),//输入格式
		(AVSampleFormat)para->format,
		para->sample_rate,
		0, 0
	);
	avcodec_parameters_free(&para);
	int re = swr_init(actx);
	mux.unlock();
	if (re != 0)
	{
		char buf[1024] = { 0 };
		av_strerror(re, buf, sizeof(buf) - 1);
		cout << "swr_init  failed! :" << buf << endl;
		return false;
	}
	//unsigned char *pcm = NULL;
	cout << "swr_int success!:" << endl;
	return true;
}
void xresample::Close()
{
	mux.lock();
	if (actx)
		swr_free(&actx);

	mux.unlock();
}

xresample::xresample()
{
}


xresample::~xresample()
{
}

int xresample::Resample(AVFrame* indata, unsigned char* outdata)
{
	if (!indata)return 0;
	if (!outdata)
	{
		av_frame_free(&indata);
		return 0;
	}
	uint8_t* data[2] = { 0 };
	data[0] = outdata;
	int re = swr_convert(actx, data, indata->nb_samples
		, (const uint8_t**)indata->data, indata->nb_samples);
	if (re <= 0)return re;
	int outSize = re * indata->channels * av_get_bytes_per_sample((AVSampleFormat)outformat);
	cout << "re :" << re << endl;
	cout << "indata->channels:" <<  indata->channels << endl;
	cout << "av_get_bytes_per_sample:" << av_get_bytes_per_sample((AVSampleFormat)outformat) << endl;
	return outSize;
}

re为swr_convert的返回值:每个通道的样本数量
return number of samples output per channel, negative value
indata->channels:为通道数
av_get_bytes_per_sample:为每个采样的比特数,即该格式(S16)所占空间大小(16位即两比特)
在这里插入图片描述
他们相乘即为每个音频通道每次采样的比特数
将此相乘的结果用在音频播放中(判断该结果与剩余缓存空间哪个大,如果剩余缓存空间大的话则可以播放)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值