概述
本示例基于ffmpeg的resampling_audio.c示例,增加了Plane模式的支持以及将程序接口化。Plane格式的介绍可以参照博文:音频格式解析:交错模式 vs Plane模式。
基于下文中代码,可实现AV_SAMPLE_FMT_S16转AV_SAMPLE_FMT_S16P,当然稍作修改,也可实现AV_SAMPLE_FMT_S16转AV_SAMPLE_FMT_FLTP。
非Plane模式转码后的数据保存在程序当前路径下的channel_all.pcm文件中,Plane模式则将每个通道单独保存:channel_x.pcm。
代码
#include <libavutil/opt.h>
#include <libavutil/channel_layout.h>
#include <libavutil/samplefmt.h>
#include <libswresample/swresample.h>
#define DEBUG_ON 1
typedef struct _convertParam {
int64_t src_ch_layout;
int src_rate;
enum AVSampleFormat src_sample_fmt;
int64_t dst_ch_layout;
int dst_rate;
enum AVSampleFormat dst_sample_fmt;
struct SwrContext *swr_ctx;
} ConvertParam;
int audio_convert_init(ConvertParam *param);
void audio_convert_deinit(ConvertParam *param);
int audio_convert_process(ConvertParam *param,
int src_nb_samples, uint8_t **src_data,
int *result_nb_samples, uint8_t ***result_data);
/**
* Fill dst buffer with nb_samples, generated starting from t.
*/
static void fill_samples_S16(int16_t *dst, int nb_samples, int nb_channels, int sample_rate, double *t)
{
int i, j;
double tincr = 1.0 / sample_rate;
int16_t *dstp = dst;
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 = (int16_t)(sin(c * *t) * 10000);
for (j = 1; j < nb_channels; j++)
dstp[j] = dstp[0];
dstp += nb_channels;
*t += tincr;
}
}
//double
static void fill_samples_DBL(double *dst, int nb_samples, int nb_channels, int sample_rate, double *t)
{
int i, j;
double tincr = 1.0 / sample_rate, *dstp = dst;
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;
}
}
static int check_param(ConvertParam *param)
{
if (!param)
return -1;
if (!param->src_ch_layout ||
!param->src_rate ||
(param->src_sample_fmt == AV_SAMPLE_FMT_NONE) ||
(param->src_sample_fmt == AV_SAMPLE_FMT_NB))
return -1;
if (!param->dst_ch_layout ||
!param->dst_rate ||
(param->dst_sample_fmt == AV_SAMPLE_FMT_NONE) ||
(param->dst_sample_fmt == AV_SAMPLE_FMT_NB))
return -1;
return 0;
}
int audio_convert_init(ConvertParam *param)
{
int64_t src_ch_layout, dst_ch_layout;
int src_rate, dst_rate;
enum AVSampleFormat src_sample_fmt, dst_sample_fmt;
struct SwrContext *swr_ctx;
int ret = 0;
if (check_param(param) < 0) {
printf("ERROR: Invalid param!\n");
return -1;
}
src_ch_layout = param->src_ch_layout;
dst_ch_layout = param->dst_ch_layout;
src_rate = param->src_rate;
dst_rate = param->dst_rate;
src_sample_fmt = param->src_sample_fmt;
dst_sample_fmt = param->dst_sample_fmt;
/* create resampler context */
swr_ctx = swr_alloc();
if (!swr_ctx) {
fprintf(stderr, "Could not allocate resampler context\n");
return -1;
}
/* 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");
swr_free(&swr_ctx);
return -1;
}
param->swr_ctx = swr_ctx;
return 0;
}
void audio_convert_deinit(ConvertParam *param)
{
if (param->swr_ctx) {
swr_free(¶m->swr_ctx);
param->swr_ctx = NULL;
}
}
int audio_convert_process(ConvertParam *param,
int src_nb_samples, uint8_t **src_data,
int *result_nb_samples, uint8_t ***result_data)
{
int dst_linesize, dst_nb_channels;
int dst_nb_samples, max_dst_nb_samples;
int ret;
int src_rate, dst_rate;
enum AVSampleFormat dst_sample_fmt;
int64_t dst_ch_layout;
struct SwrContext *swr_ctx;
uint8_t **dst_data;
int dst_bufsize = 0;
if (!param->swr_ctx) {
printf("ERROR: audio convert not be inited.\n");
return -1;
}
swr_ctx = param->swr_ctx;
dst_sample_fmt = param->dst_sample_fmt;
dst_ch_layout = param->dst_ch_layout;
src_rate = param->src_rate;
dst_rate = param->dst_rate;
/* compute the number of converted samples: buffering is avoided
* ensuring that the output buffer will contain at least all the
* converted input samples */
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");
return -1;
}
/* compute destination number of samples */
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) {
fprintf(stderr, "Could not reallocate destination samples\n");
return -1;
}
max_dst_nb_samples = dst_nb_samples;
}
/* convert to destination format */
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");
av_freep(&dst_data[0]);
return -1;
}
dst_bufsize = av_samples_get_buffer_size(&dst_linesize, dst_nb_channels,
ret, dst_sample_fmt, 1);
if (dst_bufsize < 0)
printf("WARNING: Could not get sample buffer size\n");
*result_nb_samples = dst_nb_samples;
*result_data = dst_data;
return dst_bufsize;
}
int main(int argc, char **argv)
{
int ret = 0;
ConvertParam param;
int src_nb_samples = 1024;
int dst_nb_samples = 0;
int src_nb_channels;
int dst_nb_channels;
uint8_t **src_data = NULL;
uint8_t **dst_data = NULL;
double t = 0;
int i = 0;
#if DEBUG_ON
FILE *debug_file[8] = {NULL};
#endif
memset(¶m, 0, sizeof(param));
param.src_ch_layout = AV_CH_LAYOUT_STEREO;
param.src_rate = 48000;
param.src_sample_fmt = AV_SAMPLE_FMT_S16;
param.dst_ch_layout = AV_CH_LAYOUT_STEREO;
param.dst_rate = 48000;
param.dst_sample_fmt = AV_SAMPLE_FMT_S16P;
#if DEBUG_ON
dst_nb_channels = av_get_channel_layout_nb_channels(param.dst_sample_fmt);
if ((param.dst_sample_fmt >= AV_SAMPLE_FMT_U8P) &&
(param.dst_sample_fmt <= AV_SAMPLE_FMT_S64P) &&
dst_nb_channels > 1) {
for (i = 0; i < dst_nb_channels; i++) {
char f_name[64] = {0};
sprintf(f_name, "channel_%d.pcm", i);
debug_file[i] = fopen(f_name, "wb");
if (!debug_file[i])
printf("WARNING: Could not open debug file %s\n", f_name);
}
} else {
debug_file[0] = fopen("channel_all.pcm", "wb");
if (!debug_file[0])
printf("WARNING: Could not open debug file channel_all.pcm\n");
}
#endif
ret = audio_convert_init(¶m);
if (ret < 0)
return -1;
src_nb_channels = av_get_channel_layout_nb_channels(param.src_ch_layout);
ret = av_samples_alloc_array_and_samples(&src_data, NULL, src_nb_channels,
src_nb_samples, param.src_sample_fmt, 0);
if (ret < 0) {
fprintf(stderr, "Could not allocate source samples\n");
return -1;
}
do {
fill_samples_S16((int16_t *)src_data[0], src_nb_samples, src_nb_channels, param.src_rate, &t);
ret = audio_convert_process(¶m, src_nb_samples, src_data, &dst_nb_samples, &dst_data);
if (ret < 0)
break;
printf("### FLC-DBG: convert %d frames to %d frames\n", src_nb_samples, dst_nb_samples);
#if DEBUG_ON
if ((param.dst_sample_fmt >= AV_SAMPLE_FMT_U8P) &&
(param.dst_sample_fmt <= AV_SAMPLE_FMT_S64P)) {
for (i = 0; i < dst_nb_channels; i++) {
if (debug_file[i])
fwrite(dst_data[i], 1, ret / dst_nb_channels, debug_file[i]);
}
} else if (debug_file[0])
fwrite(dst_data[0], 1, ret, debug_file[0]);
#endif
av_freep(&dst_data);
} while(t < 10);
av_freep(&src_data);
audio_convert_deinit(¶m);
return 0;
}