一.包含头文件和库文件

修改CMakeLists

# swresample
add_library(swresample SHARED IMPORTED)
set_target_properties(swresample PROPERTIES IMPORTED_LOCATION ${FF}/libswresample.so)

更新target_link_libraries

target_link_libraries( # Specifies the target library.
native-lib
avcodec
avformat
avutil
swscale
swresample # Links the target library to the log library
# included in the NDK.
${log-lib} )

在代码中包含头文件 #include <libswresample/swresample.h>

二.相关函数说明

a)

SwrContext *swr_alloc(void);  // 分配重采样的上下文。

SwrContext *swr_alloc_set_opts(struct SwrContext *s, int64_t out_ch_layout, AVSampleFormat out_sample_fmt, int out_sample_rate

, int64_t in_ch_layout, AVSampleFormat in_sample_fmt, int in_sample_rate, int log_offset, void *log_ctx

);

参数1:重采样上下文

参数2:输出的layout, 如:5.1声道…

参数3:输出的样本格式。Float, S16, S24

参数4:输出的样本率。可以不变。

参数5:输入的layout。

参数6:输入的样本格式。

参数7:输入的样本率。

参数8,参数9,日志,不用管,可直接传0

针对音频的播放速度,可以通过样本率的改变而改变。

int swr_init(struct SwrContext *s);                       // 初始化上下文

void swr_free(struct SwrContext **s);                  // 释放上下文空间。

b)

swr_convert()

针对每一帧音频的处理。把一帧帧的音频作相应的重采样

int swr_convert(struct SwrContext *s, uint8_t **out, int out_count, const uint8_t **in, int in_count);

参数1:音频重采样的上下文

参数2:输出的指针。传递的输出的数组

参数3:输出的样本数量,不是字节数。单通道的样本数量。

参数4:输入的数组,AVFrame解码出来的DATA

参数5:输入的单通道的样本数量。

三.示例代码

// 初始化像素格式转换上下文
SwsContext *vctx = NULL;
int outWidth = ;
int outHeight = ;
char *rgb = new char[**];
char *pcm = new char[**]; // 初始化音频重采样上下文
SwrContext *actx = swr_alloc();
actx = swr_alloc_set_opts(
actx,
av_get_default_channel_layout(),
AV_SAMPLE_FMT_S16,
ac->sample_rate,
av_get_default_channel_layout(ac->channels),
ac->sample_fmt,
ac->sample_rate,
,
); ret = swr_init(actx);
if (ret != ) {
LOGE("swr_init failed");
} else {
LOGI("swr_init success");
} for (;;) { if (getNowMs() - start >= ) {
LOGI("now decoder fps is: %d", frameCount / );
start = getNowMs();
frameCount = ;
}
int ret = av_read_frame(ic, packet);
if (ret != ) {
LOGE("读取到结尾处");
int pos = * r2d(ic->streams[videoStream]->time_base);
// 改变播放进度
av_seek_frame(ic, videoStream, pos, AVSEEK_FLAG_BACKWARD | AVSEEK_FLAG_FRAME);
continue;
} // LOGI("Read a Packet. streamIndex=%d, size=%d, pts=%lld, flag=%d",
// packet->stream_index,
// packet->size,
// packet->pts,
// packet->flags
// ); AVCodecContext *cc = vc;
if (packet->stream_index == audioStream) cc = ac;
// 发送到线程中去解码(将packet写入到解码队列当中去)
ret = avcodec_send_packet(cc, packet);
// 清理
int p = packet->pts;
av_packet_unref(packet);
if (ret != ) {
// LOGE("avcodec_send_packet failed.");
continue;
} for(;;) {
// 从已解码成功的数据当中取出一个frame, 要注意send一次,receive不一定是一次
ret = avcodec_receive_frame(cc, frame);
if (ret != ) {
break;
}
if (cc == vc) {
frameCount++;
vctx = sws_getCachedContext(
vctx,
frame->width,
frame->height,
(AVPixelFormat)frame->format,
outWidth,
outHeight,
AV_PIX_FMT_RGBA,
SWS_FAST_BILINEAR,
, ,
);
if (!vctx) {
LOGE("sws_getCachedContext failed!");
} else {
// 开始像素格式转换
uint8_t *data[AV_NUM_DATA_POINTERS] = {};
data[] = (uint8_t *)rgb;
int lines[AV_NUM_DATA_POINTERS] = {};
lines[] = outWidth * ;
int h = sws_scale(
vctx,
(const uint8_t **)frame->data,
frame->linesize,
,
frame->height,
data, lines
);
LOGI("sws_scale = %d", h);
}
} else { // 音频部分
uint8_t *out[] = {};
out[] = (uint8_t *)pcm;
// 音频重采样
int len = swr_convert(
actx,
out,
frame->nb_samples,
(const uint8_t **)frame->data,
frame->nb_samples
);
LOGI("swr_convert = %d", len);
}
// LOGI("Receive a frame.........");
}
} delete rgb;
delete pcm;

FFmpeg(11)-基于FFmpeg进行音频重采样(swr_init(), swr_convert())的更多相关文章

  1. 基于傅里叶变换的音频重采样算法 (附完整c代码)

    前面有提到音频采样算法: WebRTC 音频采样算法 附完整C++示例代码 简洁明了的插值音频重采样算法例子 (附完整C代码) 近段时间有不少朋友给我写过邮件,说了一些他们使用的情况和问题. 坦白讲, ...

  2. 基于sinc的音频重采样(二):实现

    上篇(基于sinc的音频重采样(一):原理)讲了基于sinc方法的重采样原理,并给出了数学表达式,如下:                  (1) 本文讲如何基于这个数学表达式来做软件实现.软件实现的 ...

  3. FFmpeg进行视频帧提取&音频重采样-Process.waitFor()引发的阻塞超时

    由于产品需要对视频做一系列的解析操作,利用FFmpeg命令来完成视频的音频提取.第一帧提取作为封面图片.音频重采样.字幕压缩等功能: 前一篇文章已经记录了FFmpeg在JAVA中的使用-音频提取&am ...

  4. 基于sinc的音频重采样(一):原理

    我在前面的文章<音频开源代码中重采样算法的评估与选择>中说过sinc方法是较好的音频重采样方法,缺点是运算量大.https://ccrma.stanford.edu/~jos/resamp ...

  5. 最简单的基于FFMPEG+SDL的音频播放器 ver2 (采用SDL2.0)

    ===================================================== 最简单的基于FFmpeg的音频播放器系列文章列表: <最简单的基于FFMPEG+SDL ...

  6. FFMpeg笔记(三) 音频处理基本概念及音频重采样

    Android放音的采样率固定为44.1KHz,录音的采样率固定为8KHz,因此底层的音频设备驱动需要设置好这两个固定的采样率.如果上层传过来的采样率不符的话,需要进行resample重采样处理. 几 ...

  7. 最简单的基于FFMPEG+SDL的音频播放器 ver2 (採用SDL2.0)

    ===================================================== 最简单的基于FFmpeg的音频播放器系列文章列表: <最简单的基于FFMPEG+SDL ...

  8. EasyDarwin开源音频解码项目EasyAudioDecoder:基于ffmpeg的安卓音频(AAC、G726)解码库(第一部分,ffmpeg-android的编译)

    ffmpeg是一套开源的,完整的流媒体解决方案.基于它可以很轻松构建一些强大的应用程序.对于流媒体这个行业,ffmpeg就像圣经一样的存在.为了表达敬意,在这里把ffmpeg官网的一段简介搬过来,ff ...

  9. 最简单的基于FFMPEG的音频编码器(PCM编码为AAC)

    http://blog.csdn.net/leixiaohua1020/article/details/25430449 本文介绍一个最简单的基于FFMPEG的音频编码器.该编码器实现了PCM音频采样 ...

随机推荐

  1. Inno Setup入门(三)——指定压缩方式

    Setup段中的compression指定了采用的压缩方式,较高的压缩率需要较多的时间或者需要更大的内存空间,可用的值如下: zip zip/1到zip/9 bzip bzip/1 到bzip/9 l ...

  2. Android自定义控件进阶-打造Android自定义的下拉列表框控件

    技术:Android+java   概述 Android中的有个原生的下拉列表控件Spinner,但是这个控件有时候不符合我们自己的要求, 比如有时候我们需要类似windows 或者web网页中常见的 ...

  3. PL/SQL Developer 中文乱码问题的解决

    分三个步骤解决: 1.检查server编码:         运行SQL语法: select * from v$nls_parameters;   2.设置本地client编码:        进入 ...

  4. $(document).ready() 和 window.onload 方法比较

    说明 页面加载文档完毕后,浏览器会通过 Javascript 为 DOM 元素添加事件. Javascript 使用 window.onload 方法,而 jQuery 使用 $(document). ...

  5. 【mysql】使用xtrabackup在线增量备份及恢复数据库

    一.Percona Xtrabackup 简介 1.Xtrabackup  bin目录文件 介绍 1)innobackupex innobackupex 是xtrabackup的一个符号链接 . in ...

  6. NYOJ------汉诺塔(一)

    汉诺塔(一) 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 在印度,有这么一个古老的传说:在世界中心贝拿勒斯(在印度北部)的圣庙里,一块黄铜板上插着三根宝石针.印度 ...

  7. fwrite()的返回值随着格式的不同返回值也不同;

    常用地函数fwrite fwrite()的返回值随着格式的不同返回值也不同: 也是最近涉及到代码才注意到的,汗!!! 转载了一篇文章来说明这个问题:文章地址:http://blog.csdn.net/ ...

  8. Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 2611816 bytes)

    一段PHP程序执行报错: Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 261181 ...

  9. 解决编译Apache出现的问题:configure: error: APR not found

    今日编译apache时出错: #./configure --prefix……检查编辑环境时出现: checking for APR... noconfigure: error: APR not fou ...

  10. Linux安装最新版Mono,Jexus(截至2015年12月30日)

    安装系统必备: yum -y install gcc gcc-c++ bison pkgconfig glib2-devel gettext make libpng-devel libjpeg-dev ...