ffmpeg音频转码,采用swr_convert重取样

1、概述:

和上一篇音频转码流程一样,只是把filter重取样改为了swr_convert重取样,理解本例子对AVFrame中data如何存储音频数据有更好的理解。

2、代码:


/*
*最简单的音频转码器(只处理音频)
*缪国凯 Mickel
*821486004@qq.com
*本程序实现从一个视频格式转码到另一个视频格式,只处理音频,视频忽略,若有多个音频流,只处理第一个,采用swr_convert重取样
*2015-5-8
*/


#include "stdafx.h"

#ifdef __cplusplus
extern"C"
{
#endif
#include <libavformat/avformat.h>
#include "libavcodec/avcodec.h"
#include "libavfilter/avfiltergraph.h"
#include "libavfilter/buffersink.h"
#include "libavfilter/buffersrc.h"
#include "libavutil/avutil.h"
#include "libavutil/opt.h"
#include "libavutil/pixdesc.h"
#include "libswresample\swresample.h"
#include "libavutil\fifo.h"


#pragma comment(lib, "avcodec.lib")
#pragma comment(lib, "avformat.lib")
#pragma comment(lib, "avutil.lib")
	//#pragma comment(lib, "avdevice.lib")
#pragma comment(lib, "avfilter.lib")
	//#pragma comment(lib, "postproc.lib")
#pragma comment(lib, "swresample.lib")
	//#pragma comment(lib, "swscale.lib")
#ifdef __cplusplus
};
#endif

#define BUF_SIZE_20K 2048000
#define BUF_SIZE_1K 1024000

SwrContext* pSwrCtx = NULL;
AVStream *out_stream = NULL;
AVFormatContext *in_fmt_ctx = NULL, *out_fmt_ctx = NULL;
int audio_index = -1;

void initSwr()
{
	if (out_fmt_ctx->streams[0]->codec->channels != in_fmt_ctx->streams[audio_index]->codec->channels
		|| out_fmt_ctx->streams[0]->codec->sample_rate != in_fmt_ctx->streams[audio_index]->codec->sample_rate
		|| out_fmt_ctx->streams[0]->codec->sample_fmt != in_fmt_ctx->streams[audio_index]->codec->sample_fmt)
	{
		if ( NULL == pSwrCtx ) 
		{
			pSwrCtx = swr_alloc();
		}
#if LIBSWRESAMPLE_VERSION_MINOR >= 17	// 根据版本不同,选用适当函数
		av_opt_set_int(pSwrCtx, "ich", in_fmt_ctx->streams[audio_index]->codec->channels, 0);
		av_opt_set_int(pSwrCtx, "och", out_fmt_ctx->streams[0]->codec->channels, 0);
		av_opt_set_int(pSwrCtx, "in_sample_rate",  in_fmt_ctx->streams[audio_index]->codec->sample_rate, 0);
		av_opt_set_int(pSwrCtx, "out_sample_rate",  out_fmt_ctx->streams[0]->codec->sample_rate, 0);
		av_opt_set_sample_fmt(pSwrCtx, "in_sample_fmt", in_fmt_ctx->streams[audio_index]->codec->sample_fmt, 0);
		av_opt_set_sample_fmt(pSwrCtx, "out_sample_fmt", out_fmt_ctx->streams[0]->codec->sample_fmt, 0);

#else
		pSwrCtx = swr_alloc_set_opts(pSwrCtx,			
			out_fmt_ctx->streams[0]->codec->channel_layout, 
			out_fmt_ctx->streams[0]->codec->sample_fmt, 
			out_fmt_ctx->streams[0]->codec->->sample_rate,
			in_fmt_ctx->streams[audio_index]->codec->channel_layout, 
			in_fmt_ctx->streams[audio_index]->codec->sample_fmt, 
			in_fmt_ctx->streams[audio_index]->codec->sample_rate,
			0, NULL);
#endif
		swr_init(pSwrCtx);
	}
}

//setup_array函数摘自ffmpeg例程
static void setup_array(uint8_t* out[SWR_CH_MAX], AVFrame* in_frame, int format, int samples)
{
	if (av_sample_fmt_is_planar((AVSampleFormat)format)) 
	{
		int i;int plane_size = av_get_bytes_per_sample((AVSampleFormat)(format & 0xFF)) * samples;format &= 0xFF;
		
		//从decoder出来的frame中的data数据不是连续分布的,所以不能这样写:
		in_frame->data[0]+i*plane_size;
		for (i = 0; i < in_frame->channels; i++)
		{
			out[i] = in_frame->data[i];
		}
	} 
	else
	{
		out[0] = in_frame->data[0];
	}
}

int TransSample(AVFrame *in_frame, AVFrame *out_frame)
{
	int ret;
	int max_dst_nb_samples = 4096;
	//int64_t dst_nb_samples;
	int64_t src_nb_samples = in_frame->nb_samples;
	out_frame->pts = in_frame->pts;
	uint8_t* paudiobuf;
	int decode_size, input_size, len;
	if (pSwrCtx != NULL) 
	{
		out_frame->nb_samples = av_rescale_rnd(swr_get_delay(pSwrCtx, out_fmt_ctx->streams[0]->codec->sample_rate) + src_nb_samples,
			out_fmt_ctx->streams[0]->codec->sample_rate, in_fmt_ctx->streams[audio_index]->codec->sample_rate, AV_ROUND_UP);

		ret = av_samples_alloc(out_frame->data, 
			&out_frame->linesize[0], 
			out_fmt_ctx->streams[0]->codec->channels, 
			out_frame->nb_samples, 
			out_fmt_ctx->streams[0]->codec->sample_fmt, 0);
		
		if (ret < 0)
		{
			av_log(NULL, AV_LOG_WARNING, "[%s.%d %s() Could not allocate samples Buffer\n", __FILE__, __LINE__, __FUNCTION__);
			return -1;
		}
		
		max_dst_nb_samples = out_frame->nb_samples;
		//输入也可能是分平面的,所以要做如下处理
		uint8_t* m_ain[SWR_CH_MAX];
		setup_array(m_ain, in_frame, in_fmt_ctx->streams[audio_index]->codec->sample_fmt, src_nb_samples);
		
		//注意这里,out_count和in_count是samples单位,不是byte
		//所以这样av_get_bytes_per_sample(in_fmt_ctx->streams[audio_index]->codec->sample_fmt) * src_nb_samples是错的
		len = swr_convert(pSwrCtx, out_frame->data, out_frame->nb_samples, (const uint8_t**)m_ain, src_nb_samples);
		if (len < 0)
		{
			char errmsg[BUF_SIZE_1K];
			av_strerror(len, errmsg, sizeof(errmsg));
			av_log(NULL, AV_LOG_WARNING, "[%s:%d] swr_convert!(%d)(%s)", __FILE__, __LINE__, len, errmsg);
			return -1;
		}
	}
	else
	{
		printf("pSwrCtx with out init!\n");
		return -1;
	}
	return 0;
}

int flush_encoder(AVFormatContext *fmt_ctx, unsigned int stream_index)
{
	int ret;
	int got_frame;
	AVPacket enc_pkt;
	if (!(fmt_ctx->streams[stream_index]->codec->codec->capabilities &CODEC_CAP_DELAY))
	{
		return 0;
	}
	
	int i = 0;
	while(1)
	{
		enc_pkt.data = NULL;
		enc_pkt.size = 0;
		av_init_packet(&enc_pkt);
		ret = avcodec_encode_audio2(out_fmt_ctx->streams[stream_index]->codec, &enc_pkt,NULL, &got_frame);
		if (ret < 0)
			break;
		
		if (!got_frame)
			break;
		
		/* prepare packet for muxing */
		enc_pkt.stream_index = stream_index;
		
		enc_pkt.dts = av_rescale_q_rnd(enc_pkt.dts,out_fmt_ctx->streams[stream_index]->codec->time_base,
			out_fmt_ctx->streams[stream_index]->time_base,(AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
		
		enc_pkt.pts = av_rescale_q_rnd(enc_pkt.pts,out_fmt_ctx->streams[stream_index]->codec->time_base,
			out_fmt_ctx->streams[stream_index]->time_base,(AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
		
		enc_pkt.duration = av_rescale_q(enc_pkt.duration,out_fmt_ctx->streams[stream_index]->codec->time_base,
			out_fmt_ctx->streams[stream_index]->time_base);
		
		/* mux encoded frame */
		ret = av_interleaved_write_frame(out_fmt_ctx, &enc_pkt);
		
		av_free_packet(&enc_pkt);
		
		if (ret < 0)
		{
			break;
		}
		i++;
	}
	
	printf("flusher write %d frame", i);
	
	return ret;
}

int _tmain(int argc, _TCHAR* argv[])
{
	AVFrame *frame, *frame_out;
	AVPacket pkt_in, pkt_out;
	int ret;
	
	if (argc < 3)
	{
		printf("error in input param");
		getchar();
		return -1;
	}
	
	av_register_all();
	avfilter_register_all();
	
	//input
	if (avformat_open_input(&in_fmt_ctx, argv[1], NULL, NULL) < 0)
	{
		printf("can not open input file context");
		goto end;
	}
	
	if (avformat_find_stream_info(in_fmt_ctx, NULL) < 0)
	{
		printf("can not find input stream info!\n");
		goto end;
	}
	
	//output
	avformat_alloc_output_context2(&out_fmt_ctx, NULL, NULL, argv[2]);
	if (!out_fmt_ctx)
	{
		printf("can not alloc output context!\n");
		goto end;
	}
	
	//open decoder & new out stream & open encoder
	for (int i = 0; i < in_fmt_ctx->nb_streams; i++)
	{
		if (in_fmt_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
		{
			//open decoder
			if(0 > avcodec_open2(in_fmt_ctx->streams[i]->codec, avcodec_find_decoder(in_fmt_ctx->streams[i]->codec->codec_id), NULL))
			{
				printf("can not find or open decoder!\n");
				goto end;
			}
			audio_index = i;
			
			//new stream
			out_stream = avformat_new_stream(out_fmt_ctx, NULL);
			if (!out_stream)
			{
				printf("can not new stream for output!\n");
				goto end;
			}
			//set codec context param
			//use default audio encoder
			out_stream->codec->codec = avcodec_find_encoder(out_fmt_ctx->oformat->audio_codec);
			//use the input audio encoder
			//
			out_stream->codec->codec = avcodec_find_encoder(in_fmt_ctx->streams[i]->codec->codec_id);
			out_stream->codec->sample_rate = in_fmt_ctx->streams[i]->codec->sample_rate;
			out_stream->codec->channel_layout = in_fmt_ctx->streams[i]->codec->channel_layout;
			out_stream->codec->channels = av_get_channel_layout_nb_channels(out_stream->codec->channel_layout);
			
			// take first format from list of supported formats
			out_stream->codec->sample_fmt = out_stream->codec->codec->sample_fmts[0];
			AVRational time_base={1, out_stream->codec->sample_rate};
			out_stream->codec->time_base = time_base;
			
			//open encoder
			if (!out_stream->codec->codec)
			{
				printf("can not find the encoder!\n");
				goto end;
			}
			if ((avcodec_open2(out_stream->codec, out_stream->codec->codec, NULL)) < 0)
			{
				printf("can not open the encoder\n");
				goto end;
			}
			if (out_fmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
				out_stream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
			break;
		}
	}
	//dump input info
	av_dump_format(in_fmt_ctx, 0, argv[1], 0);
	//dump output info
	av_dump_format(out_fmt_ctx, 0, argv[2], 1);
	if (-1 == audio_index)
	{
		printf("found no audio stream in input file!\n");
		goto end;
	}
	if (!(out_fmt_ctx->oformat->flags & AVFMT_NOFILE))
	{
		if(avio_open(&out_fmt_ctx->pb, argv[2], AVIO_FLAG_WRITE) < 0)
		{
			printf("can not open output file handle!\n");
			goto end;
		}
	}
	
	if(avformat_write_header(out_fmt_ctx, NULL) < 0)
	{
		printf("can not write the header of the output file!\n");
		goto end;
	}
	//
	initSwr();
	int got_frame, got_picture;
	int frame_index = 0;
	int i = 0;
	
	for(int i = 0; ;i++)
	{
		pkt_in.data = NULL;
		pkt_in.size = 0;
		got_frame = -1;
		got_picture = -1;
		if (av_read_frame(in_fmt_ctx, &pkt_in) < 0)
		{
			break;
		}

		if (pkt_in.stream_index != audio_index)
		{
			continue;
		}
		
		frame = av_frame_alloc();
		
		if ((ret = avcodec_decode_audio4(in_fmt_ctx->streams[audio_index]->codec, frame, &got_frame, &pkt_in)) < 0)
		{
			av_frame_free(&frame);
			printf("can not decoder a frame");
			break;
		}
		
		av_free_packet(&pkt_in);
		
		if (got_frame)
		{
			frame->pts = av_frame_get_best_effort_timestamp(frame);
			frame_out = av_frame_alloc();
			if (0 != TransSample(frame, frame_out))
			{
				printf("can not swr the audio data!\n");
				break;
			}
			
			av_init_packet(&pkt_out);
			int ret = avcodec_encode_audio2(out_fmt_ctx->streams[0]->codec, &pkt_out, frame_out, &got_picture);
			
			av_free(frame_out->data[0]);
			av_frame_free(&frame_out);
			
			if (got_picture ) 
			{
				pkt_out.stream_index = out_stream->index;
				pkt_out.dts = av_rescale_q_rnd(pkt_out.dts,
					out_fmt_ctx->streams[out_stream->index]->codec->time_base,
					out_fmt_ctx->streams[out_stream->index]->time_base,
					(AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
				pkt_out.pts = av_rescale_q_rnd(pkt_out.pts,
					out_fmt_ctx->streams[out_stream->index]->codec->time_base,
					out_fmt_ctx->streams[out_stream->index]->time_base,
					(AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
				pkt_out.duration = av_rescale_q(pkt_out.duration,
					out_fmt_ctx->streams[out_stream->index]->codec->time_base,
					out_fmt_ctx->streams[out_stream->index]->time_base);
				
				av_log(NULL, AV_LOG_DEBUG, "Muxing frame\n");
				
				/* mux encoded frame */
				ret = av_interleaved_write_frame(out_fmt_ctx, &pkt_out);
				av_free_packet(&pkt_out);
				
				if (ret < 0)
				{
					printf("write a frame failed!\n");
					break;
				}
				
				printf("success write a frame:index %d\n", frame_index++);
			}
		}
	}

	ret = flush_encoder(out_fmt_ctx, out_stream->index);
	if (ret < 0)
	{
		printf("Flushing encoder failed");
		return -1;
	}
	
	//write file trailer	
	av_write_trailer(out_fmt_ctx);
	
	//clean
	avcodec_close(out_stream->codec);
	avcodec_close(in_fmt_ctx->streams[audio_index]->codec);

end:
	avformat_close_input(&in_fmt_ctx);
	if (out_fmt_ctx && !(out_fmt_ctx->oformat->flags & AVFMT_NOFILE))
	{
		avio_close(out_fmt_ctx->pb);
	}
	avformat_free_context(out_fmt_ctx);
	getchar();
	return 0;
}



3、工程下载地址:

http://download.csdn.net/detail/dancing_night/8685669

`swr_convert` 是 FFmpeg 库中的一个函数,用于在不同的音频格式之间进行采样转换。如果你想在 C++ 程序中调用 `swr_convert` 来处理 WAV 格式的音频并播放它,你需要首先确保你已经正确安装了 FFmpeg 库,并且配置了相应的编译环境。 在 C++ 中使用 `swr_convert` 处理并播放 WAV 格式的音频大致可以分为以下几个步骤: 1. 初始化源音频流和目标音频流的参数(比如采样率、通道数、采样格式等)。 2. 创建一个音频采样上下文 `SwrContext`。 3. 使用 `swr_alloc_set_opts` 初始化采样上下文,并设置源和目标的参数。 4. 打开源音频文件,读取源音频数据。 5. 使用 `swr_convert` 进行采样转换。 6. 播放转换后的音频数据,这通常需要使用 FFmpeg 的其他音视频处理函数或集成第三方库,如 ALSA、PulseAudio 等,或者将数据写入文件进行播放。 7. 清理资源,释放采样上下文和关闭音频文件。 以下是使用 `swr_convert` 的一个简化的示例代码框架: ```cpp #include <libswresample/swresample.h> int main() { // 初始化源音频流和目标音频流参数 AVSampleFormat src_sample_fmt = AV_SAMPLE_FMT_S16; // 源音频采样格式 int src_sample_rate = 44100; // 源音频采样率 int src_channels = 2; // 源音频通道数 AVSampleFormat dst_sample_fmt = AV_SAMPLE_FMT_FLTP; // 目标音频采样格式 int dst_sample_rate = 44100; // 目标音频采样率 int dst_channels = 2; // 目标音频通道数 // 创建采样上下文 SwrContext *swr_ctx = swr_alloc_set_opts(NULL, dst_channels, dst_sample_fmt, dst_sample_rate, src_channels, src_sample_fmt, src_sample_rate, 0, NULL); if (!swr_ctx) { // 错误处理 } // 初始化采样上下文 swr_init(swr_ctx); // 读取源音频数据,进行采样转换,并播放处理后的音频数据 // ... // 清理资源 swr_free(&swr_ctx); return 0; } ``` 请注意,这只是一个非常基础的示例,实际使用时你需要添加更多的代码来处理文件读取、错误检查、数据处理以及音频播放等。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dancing_night

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值