libswresample库功能主要包括高度优化的音频重采样、rematrixing和样本格式转换操作。

以下是测试代码(test_ffmpeg_libswresample.cpp),对音频了解较少,测试代码是参考examples中的:

#include "funset.hpp"
#include <iostream>#ifdef __cplusplus
extern "C" {
#endif#include <libavutil/opt.h>
#include <libavutil/channel_layout.h>
#include <libavutil/samplefmt.h>
#include <libswresample/swresample.h>#ifdef __cplusplus
}
#endifnamespace {int get_format_from_sample_fmt(const char** fmt, enum AVSampleFormat sample_fmt)
{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 = nullptr;for (int 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);
}// Fill dst buffer with nb_samples, generated starting from t.
void fill_samples(double *dst, int nb_samples, int nb_channels, int sample_rate, double *t)
{double tincr = 1.0 / sample_rate, *dstp = dst;const double c = 2 * M_PI * 440.0;// generate sin tone with 440Hz frequency and duplicated channelsfor (int i = 0; i < nb_samples; i++) {*dstp = sin(c * *t);for (int j = 1; j < nb_channels; j++)dstp[j] = dstp[0];dstp += nb_channels;*t += tincr;}
}} // namespaceint test_ffmpeg_libswresample_resample()
{// reference: doc/examples/resample_audio.cfprintf(stdout, "swresample version: %d\n", swresample_version());fprintf(stdout, "swresample configuration: %s\n", swresample_configuration());fprintf(stdout, "swresample license: %s\n", swresample_license());    //create resampler contextstruct SwrContext* swr_ctx = swr_alloc();if (!swr_ctx) {fprintf(stderr, "fail to swr_alloc\n");return -1;}int64_t src_ch_layout = AV_CH_LAYOUT_STEREO, dst_ch_layout = AV_CH_LAYOUT_SURROUND;int src_rate = 48000, dst_rate = 44100;enum AVSampleFormat src_sample_fmt = AV_SAMPLE_FMT_DBL, dst_sample_fmt = AV_SAMPLE_FMT_S16;// set optionsav_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 contextif (swr_init(swr_ctx) < 0) {fprintf(stderr, "fail to swr_init\n");return -1;}uint8_t **src_data = nullptr, **dst_data = nullptr;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;// allocate source and destination samples bufferssrc_nb_channels = av_get_channel_layout_nb_channels(src_ch_layout);int 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, "fail to av_samples_alloc_array_and_samples\n");return -1;}// compute the number of converted samples: buffering is avoided// ensuring that the output buffer will contain at least all the converted input samplesmax_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 alignmentdst_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, "fail to av_samples_alloc_array_and_samples\n");return -1;}#ifdef _MSC_VERconst char* file_name = "E:/GitCode/OpenCV_Test/test_images/xxx";
#elseconst char* file_name = "test_images/xxx";
#endifFILE* dst_file = fopen(file_name, "wb");if (!dst_file) {fprintf(stderr, "fail to open file: %s\n", file_name);return -1;}double t = 0;do {// generate synthetic audiofill_samples((double *)src_data[0], src_nb_samples, src_nb_channels, src_rate, &t);// compute destination number of samplesdst_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 formatret = swr_convert(swr_ctx, dst_data, dst_nb_samples, (const uint8_t **)src_data, src_nb_samples);if (ret < 0) {fprintf(stderr, "fail to swr_convert\n");return -1;}int dst_bufsize = av_samples_get_buffer_size(&dst_linesize, dst_nb_channels, ret, dst_sample_fmt, 1);if (dst_bufsize < 0) {fprintf(stderr, "fail to av_samples_get_buffer_size\n");return -1;}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);const char* fmt = nullptr;if ((ret = get_format_from_sample_fmt(&fmt, dst_sample_fmt)) < 0) {fprintf(stderr, "fail to get_format_from_sample_fmt");return -1;}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 0;
}

执行结果如下所示:

GitHub: https://github.com/fengbingchun/OpenCV_Test

FFmpeg中libswresample库简介及测试代码相关推荐

  1. FFmpeg中libswscale库简介及测试代码

    libswscale库功能主要包括高度优化的图像缩放.颜色空间和像素格式转换操作. 以下是测试代码(test_ffmpeg_libswscale.cpp): #include "funset ...

  2. FFmpeg中libavutil库简介及测试代码

    libavutil是一个实用库,用于辅助多媒体编程.此库包含安全的可移植字符串函数.随机数生成器.数据结构.附加数学函数.加密和多媒体相关功能(如像素和样本格式的枚举).libavcodec和liba ...

  3. FFmpeg中RTSP客户端拉流测试代码

    之前在https://blog.csdn.net/fengbingchun/article/details/91355410中给出了通过LIVE555实现拉流的测试代码,这里通过FFmpeg来实现,代 ...

  4. FFmpeg实现获取USB摄像头视频流测试代码

    通过USB摄像头(注:windows7/10下使用内置摄像头,linux下接普通的usb摄像头(Logitech))获取视频流用到的模块包括avformat和avdevice.头文件仅include ...

  5. 人脸识别引擎SeetaFaceEngine中Detection模块使用的测试代码

    人脸识别引擎SeetaFaceEngine中Detection模块用于人脸检测,以下是测试代码: int test_detection() {std::vector<std::string> ...

  6. Windows/Linux TCP Socket网络编程简介及测试代码

    典型的网络应用是由一对程序(即客户程序和服务器程序)组成的,它们位于两个不同的端系统中.当运行这两个程序时,创建了一个客户进程和一个服务器进程,同时它们通过从套接字(socket)读出和写入数据在彼此 ...

  7. 人脸识别引擎SeetaFaceEngine中Identification模块使用的测试代码

    人脸识别引擎SeetaFaceEngine中Identification模块用于比较两幅人脸图像的相似度,以下是测试代码: int test_recognize() {const std::strin ...

  8. 人脸识别引擎SeetaFaceEngine中Alignment模块使用的测试代码

    人脸识别引擎SeetaFaceEngine中Alignment模块用于检测人脸关键点,包括5个点,两个眼的中心.鼻尖.两个嘴角,以下是测试代码: int test_alignment() {std:: ...

  9. c++中STL库 简介 及 使用说明

    致谢:http://www.cppblog.com/bhjjkg/articles/94514.html 该文章讲的很清楚! 作为C++标准不可缺少的一部分,STL应该是渗透在C++程序的角角落落里的 ...

最新文章

  1. matlab函数 size()函数和waterfal()l函数,三维图形,矩阵
  2. 测试眉形的有哪个软件_心理测试:你的眉形是下面的哪种?测你生来命运如何!超准...
  3. 机器学习在热门微博推荐中的应用
  4. 49. Leetcode 117. 填充每个节点的下一个右侧节点指针 II (二叉树-二叉树遍历)
  5. Ubuntu没有/etc/apt/sources.list文件的解决办法
  6. 盘点程序猿经常说的那些行话,你了解吗?
  7. 根据谁创建资源授权资源
  8. 为什么说去中心化很重要
  9. python java混合编程_详解java调用python的几种用法(看这篇就够了)
  10. java 钩子 64位 操作系统_Java与系统钩子
  11. python中不被定义_一日一技:在Python中双下划线私有方法不能被调用的原理
  12. shell script语法高亮和自动缩进的配置
  13. UDP中recvfrom函数与sendto函数分析
  14. DP9637汽车故障诊断仪的原理与作用
  15. C语言实现通讯录(动态版)
  16. video 标签 autoplay 实现自动播放
  17. 51单片机IIC通信协议
  18. Echarts实现省级到市级地图下钻
  19. 【谦川解读】如何保证汽车“黑匣子”—EDR数据安全
  20. java版红石电路修改_我的世界红石数模互换电路【红石小课堂】

热门文章

  1. Python装饰器的神奇功能:自动打印每个方法耗时
  2. 一文搞懂TCP的三次握手和四次挥手
  3. (C++)数组作为函数参数
  4. opencv函数copyto的用法
  5. opencv4.0棋盘格标定c++
  6. keras 的 example 文件 pretrained_word_embeddings.py 解析
  7. 判断文件或文件夹(目录)是否存在 C/C++ win/linux通用
  8. mysql怎么创建表视频教程_mySQL学习入门教程——2.创建表
  9. DLL: 一种直接法的激光雷达定位方案
  10. 【从零开始的ROS四轴机械臂控制】(二) - ROS与Gazebo连接,Gazebo仿真及urdf文件修改