ffmepg音频重采样

 

1.概述

 

在进行音频播放时,有时视频流不能知足播放要求,须要对声音的相关属性如:通道数,采样率,样本存储方式进行变动播放,也就是音频重采样。ffmpeg提供了SwrContext进行转换。ide

 
typedef struct SwrContext SwrContext;  

2.基本概念

 

2.1通道数

 

声音在录制时在不一样空间位置用不一样录音设备采样的声音信号,声音在播放时采用相应个数的扬声器播放。采用多通道的方式是为了丰富声音的现场感。经常使用的立体声有2个通道,环绕立体声3个通道。数字音频就是有一连串的样本流组成,立体声每次采用要采两次。有点相似视频中的YUV各个份量。函数

 

2.2采样率

 

把模拟信号转换成数字信号在计算机中处理,须要按照必定的采样率采样,样本值就是声音波形中的一个值。音频在播放时按照采样率进行,采样率越高声音的连续性就越好,因为人的听觉器官分辨能力的局限,每每这些数值达到某种程度就能够知足人对“连续”性的需求了。例如22050和44100的采样率就是电台和CD 经常使用的采样率。相似视频中的帧率。布局

 

2.3比特率(bps或kbps)

 

单位时间所需的空间存储。比特率反应的是视频或者音频一个样本全部的信息量,越大含有的信息量就大。视频中,图像分辨率越大,一帧就越大,实时解码就容易饥渴,传输带宽须要越大,存储空间就越大。音频中描述一个样本就越准确。学习

 

2.4帧

 

视频中帧就是一个图片采样。音频中一帧通常包含多个样本,如AAC格式会包含1024个样本。ui

 

2.5样本格式

 

音频中一个样本存储方式。列举ffmpeg中的样本格式spa

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_NB           ///< Number of sample formats. DO NOT USE if linking dynamically
};

 

讲一下AV_SAMPLE_FMT_S16和AV_SAMPLE_FMT_S16P格式,AV_SAMPLE_FMT_S16保存一个样本采用有符号16bit交叉存储的方式,AV_SAMPLE_FMT_S16P保存一个样本采用有符号16bit平面存储的方式。举例有两个通道,通道1数据流 c1 c1 c1c1... , 通道2数据流 c2 c2 c2 c2....net

 

平面存储方式:c1 c1 c1c1... c2 c2 c2 c2...code

交叉存储方式:c1, c2,c1, c2, c1, c2, ...orm

AVFrame中平面方式planar每一个通道数据存储在data[0], data[1]中,长度为linesize[0],linesize[1],交叉方式则全部的数据都存储在data[0],长度为linesize[0]。视频

 

3.ffmepg实例

 

 

例程是ffmpeg2.4源代码目录下的doc/examples/resampling_audio.c文件,为便于学习作部分修改。

 

3.1 根据样本格式返回样本格式字符串

 

 static int get_format_from_sample_fmt(const char **fmt,
	enum AVSampleFormat sample_fmt)
{
	int i;
	struct sample_fmt_entry 
	{
		enum AVSampleFormat sample_fmt; const char *fmt_be, *fmt_le;
	} sample_fmt_entries[] = 
	{
		{ AV_SAMPLE_FMT_U8,  "u8",    "u8"    },
		{ AV_SAMPLE_FMT_S16, "s16be", "s16le" },
		{ AV_SAMPLE_FMT_S32, "s32be", "s32le" },
		{ AV_SAMPLE_FMT_FLT, "f32be", "f32le" },
		{ AV_SAMPLE_FMT_DBL, "f64be", "f64le" },
	};
	*fmt = NULL;

	for (i = 0; i < FF_ARRAY_ELEMS(sample_fmt_entries); i++) 
	{
		struct sample_fmt_entry *entry = &sample_fmt_entries[i];
		if (sample_fmt == entry->sample_fmt) 
		{
			*fmt = AV_NE(entry->fmt_be, entry->fmt_le);
			return 0;
		}
	}

	fprintf(stderr,
		"Sample format %s not supported as output format\n",
		av_get_sample_fmt_name(sample_fmt));
	return AVERROR(EINVAL);
}

 

 

3.2获取声音

 

/**
* Fill dst buffer with nb_samples, generated starting from t.
*至关因而声源 产生一个正弦波形的声波
* dst 保存声音数据返回个调用者 nb_samples 采用的样本数 nb_channels 声音通道数,代表单次采样的样本数 t采用开始时间
*正弦波形就是一个生源,实际中复杂的声音都是经过波形叠加成的。
*以 sample_rate采样率,从时间t开始采样,采样通道为2,每一个通道的数据相同,从频率为440HZ的波形上采样,造成声源
*/
static void fill_samples(double *dst, int nb_samples, int nb_channels, int sample_rate, double *t)
{
	int i, j;
	//采样时间间隔    tincr
	double tincr = 1.0 / sample_rate, *dstp = dst;
	//正弦波y=Asin(ωx+φ)+h 最小正周期T=2π/|ω| 因此440HZ是正弦波的频率
	const double c = 2 * M_PI * 440.0;

	/* generate sin tone with 440Hz frequency and duplicated channels */
	//填充每一个通道数据 采用交叉存储
	for (i = 0; i < nb_samples; i++) 
	{
		*dstp = sin(c * *t);
		for (j = 1; j < nb_channels; j++)
		{
			dstp[j] = dstp[0];
		}
		dstp += nb_channels;
		*t += tincr;
	}
}

3.2主函数

 

int main(int argc, char **argv)
{
	// AV_CH_LAYOUT_STEREO 声音布局立体声 	  AV_CH_LAYOUT_SURROUND 声音布局环绕立体声
	int64_t src_ch_layout = AV_CH_LAYOUT_STEREO, dst_ch_layout = AV_CH_LAYOUT_SURROUND;
	//声音采样率
	int src_rate = 48000, dst_rate = 44100;
	uint8_t **src_data = NULL, **dst_data = NULL;
	int src_nb_channels = 0, dst_nb_channels = 0;
	int src_linesize, dst_linesize;
	//每次采用样本数
	int src_nb_samples = 1024, dst_nb_samples, max_dst_nb_samples;
	//样本存储格式
	enum AVSampleFormat src_sample_fmt = AV_SAMPLE_FMT_DBL, dst_sample_fmt = AV_SAMPLE_FMT_S16;
	const char *dst_filename = NULL;
	FILE *dst_file;
	int dst_bufsize;
	const char *fmt;
	//重采样上下文
	struct SwrContext *swr_ctx;
	double t;
	int ret;

	if (argc != 2) 
	{
		fprintf(stderr, "Usage: %s output_file\n"
			"API example program to show how to resample an audio stream with libswresample.\n"
			"This program generates a series of audio frames, resamples them to a specified "
			"output format and rate and saves them to an output file named output_file.\n",
			argv[0]);
		exit(1);
	}
	dst_filename = argv[1];

	dst_file = fopen(dst_filename, "wb");
	if (!dst_file) 
	{
		fprintf(stderr, "Could not open destination file %s\n", dst_filename);
		exit(1);
	}

	/* create resampler context */
	//初始化常采样上下文
	swr_ctx = swr_alloc();
	if (!swr_ctx) 
	{
		fprintf(stderr, "Could not allocate resampler context\n");
		ret = AVERROR(ENOMEM);
		goto end;
	}

	/* set options */
	//设置源通道布局
	av_opt_set_int(swr_ctx, "in_channel_layout",    src_ch_layout, 0);
	//设置源通道采样率
	av_opt_set_int(swr_ctx, "in_sample_rate",       src_rate, 0);
	//设置源通道样本格式
	av_opt_set_sample_fmt(swr_ctx, "in_sample_fmt", src_sample_fmt, 0);

	//目标通道布局
	av_opt_set_int(swr_ctx, "out_channel_layout",    dst_ch_layout, 0);
	//目标采用率
	av_opt_set_int(swr_ctx, "out_sample_rate",       dst_rate, 0);
	//目标样本格式
	av_opt_set_sample_fmt(swr_ctx, "out_sample_fmt", dst_sample_fmt, 0);

	/* initialize the resampling context */
	if ((ret = swr_init(swr_ctx)) < 0) 
	{
		fprintf(stderr, "Failed to initialize the resampling context\n");
		goto end;
	}

	/* allocate source and destination samples buffers */
	//获取源通道数
	src_nb_channels = av_get_channel_layout_nb_channels(src_ch_layout);
	//分配源声音所须要空间  src_linesize=	 src_nb_channels× src_nb_samples×sizeof(double)
	ret = av_samples_alloc_array_and_samples(&src_data, &src_linesize, src_nb_channels,
		src_nb_samples, src_sample_fmt, 0);
	if (ret < 0) 
	{
		fprintf(stderr, "Could not allocate source samples\n");
		goto end;
	}

	/* compute the number of converted samples: buffering is avoided
	* ensuring that the output buffer will contain at least all the
	* converted input samples */
	//计算目标样本数  转换先后的样本数不同  抓住一点 采样时间相等
	//src_nb_samples/src_rate=dst_nb_samples/dst_rate
	max_dst_nb_samples = dst_nb_samples = av_rescale_rnd(src_nb_samples, dst_rate, src_rate, AV_ROUND_UP);

	/* buffer is going to be directly written to a rawaudio file, no alignment */
	dst_nb_channels = av_get_channel_layout_nb_channels(dst_ch_layout);
	ret = av_samples_alloc_array_and_samples(&dst_data, &dst_linesize, dst_nb_channels,
		dst_nb_samples, dst_sample_fmt, 0);
	if (ret < 0) 
	{
		fprintf(stderr, "Could not allocate destination samples\n");
		goto end;
	}

	t = 0;
	do {
		/* generate synthetic audio */
		fill_samples((double *)src_data[0], src_nb_samples, src_nb_channels, src_rate, &t);

		/* compute destination number of samples */
		//swr_get_delay(swr_ctx, src_rate)延迟时间 源采样率为单位的样本数
		dst_nb_samples = av_rescale_rnd(swr_get_delay(swr_ctx, src_rate) +
			src_nb_samples, dst_rate, src_rate, AV_ROUND_UP);
		if (dst_nb_samples > max_dst_nb_samples) 
		{
			av_freep(&dst_data[0]);
			ret = av_samples_alloc(dst_data, &dst_linesize, dst_nb_channels,
				dst_nb_samples, dst_sample_fmt, 1);
			if (ret < 0)
				break;
			max_dst_nb_samples = dst_nb_samples;
		}

		/* convert to destination format */
		//ret 实际转换获得的样本数
		ret = swr_convert(swr_ctx, dst_data, dst_nb_samples, (const uint8_t **)src_data, src_nb_samples);
		if (ret < 0) 
		{
			fprintf(stderr, "Error while converting\n");
			goto end;
		}
		dst_bufsize = av_samples_get_buffer_size(&dst_linesize, dst_nb_channels,
			ret, dst_sample_fmt, 1);
		if (dst_bufsize < 0) 
		{
			fprintf(stderr, "Could not get sample buffer size\n");
			goto end;
		}
		printf("t:%f in:%d out:%d\n", t, src_nb_samples, ret);
		fwrite(dst_data[0], 1, dst_bufsize, dst_file);
	} while (t < 10);

	if ((ret = get_format_from_sample_fmt(&fmt, dst_sample_fmt)) < 0)
		goto end;

	fprintf(stderr, "Resampling succeeded. Play the output file with the command:\n"
		"ffplay -f %s -channel_layout %lld -channels %d -ar %d %s\n",
		fmt, dst_ch_layout, dst_nb_channels, dst_rate, dst_filename);
	while(1)
	{
		Sleep(50);
	}

end:
	fclose(dst_file);

	if (src_data)
		av_freep(&src_data[0]);
	av_freep(&src_data);

	if (dst_data)
		av_freep(&dst_data[0]);
	av_freep(&dst_data);

	swr_free(&swr_ctx);
	return ret < 0;
}

 

编译环境:Win7_32bit+VS2010

FFMPEG版本:ffmpeg-2.4

源代码下载地址:http://download.csdn.net/detail/hiwubihe/9593005

 

技术在于交流、沟通,转载请注明出处并保持做品的完整性。

原文://http://blog.csdn.net/hiwubihe/article/details/52059378