当前位置: 首页 > news >正文

公司网站链接建设网站主题

公司网站链接,建设网站主题,磐安网站建设公司,建程网是干嘛的目录 学习课题#xff1a;逐步构建开发播放器【QT5 FFmpeg6 SDL2】 步骤 AudioOutPut模块 1、初始化【分配缓存、读取信息】 2、开始线程工作【从队列读帧-重采样-SDL回调-写入音频播放数据-SDL进行播放】 主要代码 分配缓存 // 对于样本队列 av_audio_…目录 学习课题逐步构建开发播放器【QT5 FFmpeg6 SDL2】 步骤 AudioOutPut模块 1、初始化【分配缓存、读取信息】 2、开始线程工作【从队列读帧-重采样-SDL回调-写入音频播放数据-SDL进行播放】 主要代码 分配缓存 // 对于样本队列 av_audio_fifo_alloc(playSampleFmt, playChannels, spec.samples * 5);// 对于帧的音频字节数据 // 首次计算帧大小并且开辟缓冲区 maxOutSamples (int) av_rescale_rnd(decCtxSamples, playSampleRate, srcSampleRate, AV_ROUND_UP); audioBufferSize av_samples_get_buffer_size(nullptr, srcChannels, maxOutSamples, playSampleFmt, 0); audioBuffer (uint8_t *) av_malloc(audioBufferSize); 重采样相关 //配置重采样器参数 swr_alloc_set_opts2(swrContext,srcChannelLayout, playSampleFmt, playSampleRate,srcChannelLayout, AVSampleFormat(srcSampleFmt), srcSampleRate,0, nullptr); //初始化重采样器 swr_init(swrContext);//重采样流程 // 计算重采样后要输出多少样本数delay swr_get_delay(swrContext, sample_rate);out_samples (int) av_rescale_rnd(nb_samples delay,playSampleRate,sample_rate,AV_ROUND_DOWN);// 判断预测的输出样本数是否本次任务的最大样本数if (out_samples maxOutSamples) {// 释放缓冲区重新初始化缓冲区大小av_freep(audioBuffer);audioBufferSize av_samples_get_buffer_size(nullptr, srcChannels, out_samples, playSampleFmt, 0);audioBuffer (uint8_t *) av_malloc(audioBufferSize);maxOutSamples out_samples;}playSamples swr_convert(swrContext, audioBuffer, out_samples, (const uint8_t **) frame-data, nb_samples); SDL的音频回调 // SDL音频回调函数提供了一个回调接口可以让我们在音频设备需要数据的时候向里面写入数据 // 从而进行声音播放 // 回调函数示例 函数名自定义放在类中需要加静态(static) void AudioOutPut::AudioCallBackFunc(void *userdata, Uint8 *stream, int len) {//userdata 是在初始化时赋值的有时候会把类中this传进去//stream 是音频流在回调函数中需要把音频数据写入到stream就可以实现声音播放//len是由SDL传入的SDL缓冲区的大小如果这个缓冲未满我们就一直往里填充数据... } 完整模块 AudioOutPut //AudioOutPut.h #include FFmpegHeader.h #include SDL.h #include queue/AVFrameQueue.h #include QDebug #include QObject #include QtGui #include QtWidgets #include threadclass AudioOutPut { private:std::thread *m_thread;bool isStopped true; // 是否已经停止 停止时退出线程bool isPlaying false;// 是否正在播放bool isPause false; // 是否暂停void run();int resampleFrame(AVFrame *frame);int sdlCallBackMode 1;QString url; //视频地址uint8_t *audioBuffer; //存储解码后音频bufferint audioBufferSize 0;//buffer大小int audioBufferIndex 0;SDL_mutex *mtx nullptr;// 队列锁SDL_AudioDeviceID audioDevice;AVAudioFifo *fifo nullptr;//Audio BufferAVFrameQueue *frameQueue; //解码后的帧队列SwrContext *swrContext; //重采样上下文// 解码器上下文AVCodecContext *decCtx; // 音频解码器上下文int srcChannels; // 源通道数AVChannelLayout srcChannelLayout;// 源通道布局enum AVSampleFormat srcSampleFmt;// 源采样格式int srcSampleRate; // 源音频采样率// playerint maxOutSamples; // 最大样本数用于计算缓存区大小int playSamples; // 最终播放的样本数int playSampleRate;// 最终播放的音频采样率enum AVSampleFormat playSampleFmt;int playChannels;// 源通道数 public:AudioOutPut(AVCodecContext *dec_ctx, AVFrameQueue *frame_queue);int init(int mode 1);static void AudioCallBackFunc(void *userdata, Uint8 *stream, int len);//SDL音频回调函数实体普通版void AudioCallBack(Uint8 *stream, int len);//SDL音频回调函数实体队列版void AudioCallBackFromQueue(Uint8 *stream, int len);int start(); };//AudioOutPut.cpp #include AudioOutPut.h AudioOutPut::AudioOutPut(AVCodecContext *dec_ctx, AVFrameQueue *frame_queue): decCtx(dec_ctx), frameQueue(frame_queue) {srcSampleFmt decCtx-sample_fmt;srcSampleRate decCtx-sample_rate;srcChannelLayout decCtx-ch_layout;srcChannels srcChannelLayout.nb_channels; } int AudioOutPut::init(int mode) {sdlCallBackMode mode;// SDL initif (SDL_Init(SDL_INIT_AUDIO) ! 0) {qDebug() SDL_INIT_AUDIO error;return -1;}SDL_AudioSpec wanted_spec, spec;wanted_spec.channels decCtx-ch_layout.nb_channels;wanted_spec.freq decCtx-sample_rate;SDL_AudioFormat sample_type;switch (srcSampleFmt) {case AV_SAMPLE_FMT_FLTP:case AV_SAMPLE_FMT_FLT:sample_type AUDIO_F32SYS;break;case AV_SAMPLE_FMT_U8P:case AV_SAMPLE_FMT_U8:sample_type AUDIO_U8;break;case AV_SAMPLE_FMT_S64P:case AV_SAMPLE_FMT_S64:case AV_SAMPLE_FMT_S32P:case AV_SAMPLE_FMT_S32:sample_type AUDIO_S32SYS;break;case AV_SAMPLE_FMT_S16P:case AV_SAMPLE_FMT_S16:sample_type AUDIO_S16SYS;break;default:sample_type AUDIO_S16SYS;qDebug() 不支持的采样格式:AVSampleFormat( srcSampleFmt );}wanted_spec.format sample_type;wanted_spec.silence 0;wanted_spec.callback AudioCallBackFunc;wanted_spec.userdata this;wanted_spec.samples decCtx-frame_size;int ret;// ret SDL_OpenAudio(wanted_spec, spec);audioDevice SDL_OpenAudioDevice(NULL, 0, wanted_spec, spec, SDL_AUDIO_ALLOW_ANY_CHANGE);if (audioDevice 0) {qDebug() SDL_OpenAudio error;return -1;}playChannels spec.channels;playSampleRate spec.freq;playSampleFmt av_get_packed_sample_fmt(srcSampleFmt);if (mode 1) {fifo av_audio_fifo_alloc(playSampleFmt, playChannels, spec.samples * 5);}ret swr_alloc_set_opts2(swrContext,srcChannelLayout, playSampleFmt, playSampleRate,srcChannelLayout, AVSampleFormat(srcSampleFmt), srcSampleRate,0, nullptr);if (ret ! 0) {qDebug() swr_alloc_set_opts2错误;return -1;}if (!swrContext) {qDebug() 创建音频重采样上下文错误 swr_alloc;return -1;}ret swr_init(swrContext);if (ret 0) {qDebug() 初始化音频重采样上下文错误 swr_init;return -1;}// 解码器上下文保存的帧样本数int decCtxSamples 1024;if (decCtx-frame_size 1024) {decCtxSamples decCtx-frame_size;}// 首次计算帧大小并且开辟缓冲区maxOutSamples (int) av_rescale_rnd(decCtxSamples, playSampleRate, srcSampleRate, AV_ROUND_UP);audioBufferSize av_samples_get_buffer_size(nullptr, srcChannels, maxOutSamples, playSampleFmt, 0);audioBuffer (uint8_t *) av_malloc(audioBufferSize);return 1; }void AudioOutPut::AudioCallBackFunc(void *userdata, Uint8 *stream, int len) {AudioOutPut *player (AudioOutPut *) userdata;if (player-sdlCallBackMode 1) {player-AudioCallBackFromQueue(stream, len);} else {player-AudioCallBack(stream, len);} }void AudioOutPut::AudioCallBack(Uint8 *stream, int len) {int len1;// sdl的内部stream可用空间/* len是由SDL传入的SDL缓冲区的大小如果这个缓冲未满我们就一直往里填充数据 */while (len 0) {/* audioBufferIndex 和 audioBufferSize 标示我们自己用来放置解码出来的数据的缓冲区*//* 这些数据待copy到SDL缓冲区 当audioBufferIndex audioBufferSize的时候意味着我*//* 们的缓冲为空没有数据可供copy这时候需要调用audio_decode_frame来解码出更多的桢数据 */if (audioBufferIndex audioBufferSize) {AVFrame *frame frameQueue-pop(10);if (frame) {audioBufferSize resampleFrame(frame);/* audioBufferSize 0 标示没能解码出数据我们默认播放静音 */if (audioBufferSize 0) {/* silence */audioBufferSize 1024;/* 清零静音 */memset(audioBuffer, 0, audioBufferSize);}}audioBufferIndex 0;}/* 当audioBufferIndex audioBufferSize 查看stream可用空间决定一次copy多少数据剩下的下次继续copy */len1 audioBufferSize - audioBufferIndex;// 可用空间if (len1 len) {len1 len;}if (audioBuffer nullptr) return;memcpy(stream, (uint8_t *) audioBuffer audioBufferIndex, len1);len - len1;stream len1;audioBufferIndex len1;} }void AudioOutPut::AudioCallBackFromQueue(Uint8 *stream, int len) {//由于AVAudioFifo非线程安全且是子线程触发此回调所以需要加锁SDL_LockMutex(mtx);//读取队列中的音频数据av_audio_fifo_read(fifo, (void **) stream, playSamples);SDL_UnlockMutex(mtx); } int AudioOutPut::start() {SDL_PauseAudioDevice(audioDevice, 0);// SDL_PauseAudio(0);if (sdlCallBackMode 1) {m_thread new std::thread(AudioOutPut::run, this);if (!m_thread-joinable()) {qDebug() AudioOutPut音频帧处理线程创建失败;return -1;}}isStopped false;isPlaying true;return 0; } void AudioOutPut::run() {AVFrame *frame;while (!isStopped) {frame frameQueue-pop(10);if (frame) {audioBufferSize resampleFrame(frame);while (true) {SDL_LockMutex(mtx);if (av_audio_fifo_space(fifo) playSamples) {av_audio_fifo_write(fifo, (void **) audioBuffer, playSamples);SDL_UnlockMutex(mtx);av_frame_unref(frame);break;}SDL_UnlockMutex(mtx);//队列可用空间不足则延时等待SDL_Delay((double) playSamples / playSampleRate);}}} } int AudioOutPut::resampleFrame(AVFrame *frame) {int64_t delay; // 重采样后延迟int out_samples;// 预测的重采样后的输出样本数int sample_rate;// 帧原采样率int nb_samples; // 帧原样本数sample_rate frame-sample_rate;nb_samples frame-nb_samples;// 计算重采样后要输出多少样本数delay swr_get_delay(swrContext, sample_rate);out_samples (int) av_rescale_rnd(nb_samples delay,playSampleRate,sample_rate,AV_ROUND_DOWN);// 判断预测的输出样本数是否本次任务的最大样本数if (out_samples maxOutSamples) {// 释放缓冲区重新初始化缓冲区大小av_freep(audioBuffer);audioBufferSize av_samples_get_buffer_size(nullptr, srcChannels, out_samples, playSampleFmt, 0);audioBuffer (uint8_t *) av_malloc(audioBufferSize);maxOutSamples out_samples;}playSamples swr_convert(swrContext, audioBuffer, out_samples, (const uint8_t **) frame-data, nb_samples);if (playSamples 0) {return -1;}return av_samples_get_buffer_size(nullptr, srcChannels, playSamples, playSampleFmt, 1); } PlayerMain 添加音频输出代码 AudioOutPut *audioOutPut; audioOutPut new AudioOutPut(audioDecodeThread-dec_ctx, audioFrameQueue); audioOutPut-init(1); audioOutPut-start(); 测试运行结果 如果需要同时执行视频和音频的输出记得要在解复用模块那把限制队列大小的位置把视频队列的大小限制给去掉。 目前只是实现了音频播放和视频渲染显示画面但是可以看到音频和视频是不同步的下一章我们就要让音频和视频同步起来。 播放器开发(六)音频帧处理并用SDL播放结果
http://www.dnsts.com.cn/news/185131.html

相关文章:

  • 长春网站上排名网站系统怎么做
  • 建设网站什么语言比较合适太原市建设工程招标投标网站
  • 响应式网站建设的好处wordpress 旅游网站
  • 自己开外销网站怎么做科研网站怎么建设
  • 阿里云怎么做淘客网站绍兴网站建设 微德福
  • 遂宁网站开发免费网站空间 推荐
  • 福建远方建设有限公司网站在哪儿可以找到网站开发的需求
  • 成都住房和城乡建设部网站查询whcms wordpress
  • 企业网站建立意义何在wordpress直排主题
  • wordpress支持多个站点地址中国商品价格网
  • app与微网站的区别是什么意思建站之家官网
  • h5 网站开发流程图百度官方网站登录
  • 外包网站价格黑马培训是正规学校吗
  • 永久免费无代码开发平台网站wordpress文章索引
  • 苏州企业网站建设制作服务手机商城系统总结
  • 如何知道网站流量班级建设网站设计方案
  • 方正网站制作新手用什么框架做网站比较好
  • 安徽省网站建设wordpress 可爱插件
  • 知名排版网站wordpress没有分类模板
  • 运动鞋网站建设目的优化关键词的步骤
  • 凡客做网站公司建设网站的服务费
  • jsp怎么做网站的删除比较出名的wordpress网站
  • 二手车网站设计河北建设集团官方网站
  • 东莞如何建网站费用wordpress默认模版在哪
  • 网站建设陆金手指下拉壹玖北京市建设工程信息网有哪些
  • 网站如何防止攻击营销型网站 案例
  • 聊城seo优化hyein seo是什么牌子
  • 怎样创建个人视频号杭州网站建设方案优化
  • 机床网站建设微信小程序界面设计
  • 苏州网站制作开发公司网站后台管理系统制作