From 62de6d18b8c09b644daa83a5b483281ba6452c2d Mon Sep 17 00:00:00 2001 From: mhf <1603291350@qq.com> Date: Wed, 19 Oct 2022 19:20:35 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9A=E5=AE=8C=E6=88=90=E4=BD=BF?= =?UTF-8?q?=E7=94=A8ffmpeg=E9=9F=B3=E8=A7=86=E9=A2=91=E5=BA=93=E3=80=90?= =?UTF-8?q?=E8=BD=AF/=E7=A1=AC=E8=A7=A3=E7=A0=81=20+=20OpenGL=E3=80=91?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E7=9A=84=E8=A7=86=E9=A2=91=E6=92=AD=E6=94=BE?= =?UTF-8?q?=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1、使用ffmpeg音视频库【软/硬解码】实现的视频播放器; 2、支持打开本地视频文件(如mp4、mov、avi等)、网络视频流(rtsp、rtmp、http等); 3、支持视频匀速播放; 4、采用【OpenGL显示YUV、NV12】图像,支持自适应窗口缩放,支持使用QOpenGLWidget、QOpenGLWindow显 示; 5、将YUV/NV12转RGB的步骤由CPU转换改为使用GPU转换,降低CPU占用率; 6、使用av_hwframe_map替代av_hwframe_transfer_data,可将【耗时降低1/3】; 7、视频播放支持实时开始/关闭、暂停/继续播放; 8、视频解码、线程控制、显示各部分功能分离,低耦合度。 9、采用最新的【5.1.2版本】ffmpeg库进行开发,超详细注释信息,将所有踩过的坑、解决办法、注意事项都得很写清楚。 --- FFmpegDemo/FFmpegDemo.pro | 9 +- .../VideoPlayHWGL/VideoPlay/VideoPlay.pri | 31 + .../VideoPlayHWGL/VideoPlay/readthread.cpp | 133 +++++ .../VideoPlayHWGL/VideoPlay/readthread.h | 56 ++ .../VideoPlayHWGL/VideoPlay/videodecode.cpp | 543 ++++++++++++++++++ .../VideoPlayHWGL/VideoPlay/videodecode.h | 76 +++ FFmpegDemo/VideoPlayHWGL/VideoPlayHWGL.pro | 67 +++ FFmpegDemo/VideoPlayHWGL/fragment.fsh | 35 ++ FFmpegDemo/VideoPlayHWGL/main.cpp | 11 + FFmpegDemo/VideoPlayHWGL/playimage.cpp | 399 +++++++++++++ FFmpegDemo/VideoPlayHWGL/playimage.h | 75 +++ FFmpegDemo/VideoPlayHWGL/rc.qrc | 6 + FFmpegDemo/VideoPlayHWGL/vertex.vsh | 9 + FFmpegDemo/VideoPlayHWGL/widget.cpp | 111 ++++ FFmpegDemo/VideoPlayHWGL/widget.h | 36 ++ FFmpegDemo/VideoPlayHWGL/widget.ui | 150 +++++ 16 files changed, 1743 insertions(+), 4 deletions(-) create mode 100644 FFmpegDemo/VideoPlayHWGL/VideoPlay/VideoPlay.pri create mode 100644 FFmpegDemo/VideoPlayHWGL/VideoPlay/readthread.cpp create mode 100644 FFmpegDemo/VideoPlayHWGL/VideoPlay/readthread.h create mode 100644 FFmpegDemo/VideoPlayHWGL/VideoPlay/videodecode.cpp create mode 100644 FFmpegDemo/VideoPlayHWGL/VideoPlay/videodecode.h create mode 100644 FFmpegDemo/VideoPlayHWGL/VideoPlayHWGL.pro create mode 100644 FFmpegDemo/VideoPlayHWGL/fragment.fsh create mode 100644 FFmpegDemo/VideoPlayHWGL/main.cpp create mode 100644 FFmpegDemo/VideoPlayHWGL/playimage.cpp create mode 100644 FFmpegDemo/VideoPlayHWGL/playimage.h create mode 100644 FFmpegDemo/VideoPlayHWGL/rc.qrc create mode 100644 FFmpegDemo/VideoPlayHWGL/vertex.vsh create mode 100644 FFmpegDemo/VideoPlayHWGL/widget.cpp create mode 100644 FFmpegDemo/VideoPlayHWGL/widget.h create mode 100644 FFmpegDemo/VideoPlayHWGL/widget.ui diff --git a/FFmpegDemo/FFmpegDemo.pro b/FFmpegDemo/FFmpegDemo.pro index 25c81c9..691da36 100644 --- a/FFmpegDemo/FFmpegDemo.pro +++ b/FFmpegDemo/FFmpegDemo.pro @@ -10,7 +10,8 @@ #--------------------------------------------------------------------------------------- TEMPLATE = subdirs -SUBDIRS += VideoPlay # 使用软解码实现的视频播放器 -SUBDIRS += VideoPlayGL1 # 使用软解码实现的视频播放器 使用OpenGL显示RGB图像 -SUBDIRS += VideoPlayGL2 # 使用软解码实现的视频播放器 使用OpenGL显示YUV图像 -SUBDIRS += VideoPlayHW # 使用硬件解码实现的视频播放器 +SUBDIRS += VideoPlay # 使用软解码实现的视频播放器 +SUBDIRS += VideoPlayGL1 # 使用软解码实现的视频播放器 使用OpenGL显示RGB图像 +SUBDIRS += VideoPlayGL2 # 使用软解码实现的视频播放器 使用OpenGL显示YUV图像 +SUBDIRS += VideoPlayHW # 使用硬件解码实现的视频播放器 +SUBDIRS += VideoPlayHWGL # 使用硬件解码实现的视频播放器 使用OpenGL显示YUV/NV12图像 diff --git a/FFmpegDemo/VideoPlayHWGL/VideoPlay/VideoPlay.pri b/FFmpegDemo/VideoPlayHWGL/VideoPlay/VideoPlay.pri new file mode 100644 index 0000000..a1396dc --- /dev/null +++ b/FFmpegDemo/VideoPlayHWGL/VideoPlay/VideoPlay.pri @@ -0,0 +1,31 @@ +#--------------------------------------------------------------------------------------- +# @功能: ffmpeg读取视频图像模块 +# @编译器: Desktop Qt 5.12.5 MSVC2017 64bit(也支持其它编译器) +# @Qt IDE: D:/Qt/Qt5.12.5/Tools/QtCreator/share/qtcreator +# +# @开发者 mhf +# @邮箱 1603291350@qq.com +# @时间 2022-09-15 14:27:25 +# @备注 +#--------------------------------------------------------------------------------------- + +# 加载库,ffmpeg n5.1.2版本 +win32{ +LIBS += -LE:/lib/ffmpeg5-1-2/lib/ -lavcodec -lavfilter -lavformat -lswscale -lavutil -lswresample -lavdevice +INCLUDEPATH += E:/lib/ffmpeg5-1-2/include +DEPENDPATH += E:/lib/ffmpeg5-1-2/include +} + +unix:!macx{ +LIBS += -L/home/mhf/lib/ffmpeg/ffmpeg-5-1-2/lib -lavcodec -lavfilter -lavformat -lswscale -lavutil -lswresample -lavdevice +INCLUDEPATH += /home/mhf/lib/ffmpeg/ffmpeg-5-1-2/include +DEPENDPATH += /home/mhf/lib/ffmpeg/ffmpeg-5-1-2/include +} + +HEADERS += \ + $$PWD/readthread.h \ + $$PWD/videodecode.h + +SOURCES += \ + $$PWD/readthread.cpp \ + $$PWD/videodecode.cpp diff --git a/FFmpegDemo/VideoPlayHWGL/VideoPlay/readthread.cpp b/FFmpegDemo/VideoPlayHWGL/VideoPlay/readthread.cpp new file mode 100644 index 0000000..bd333fb --- /dev/null +++ b/FFmpegDemo/VideoPlayHWGL/VideoPlay/readthread.cpp @@ -0,0 +1,133 @@ +#include "readthread.h" +#include "videodecode.h" + +#include + +#include +#include +#include +#include +#include + +ReadThread::ReadThread(QObject *parent) : QThread(parent) +{ + m_videoDecode = new VideoDecode(); + + // 注册自定义枚举类型,否则信号槽无法发送 + qRegisterMetaType("PlayState"); +} + +ReadThread::~ReadThread() +{ + if(m_videoDecode) + { + delete m_videoDecode; + } +} + +/** + * @brief 传入播放的视频地址并开启线程 + * @param url + */ +void ReadThread::open(const QString &url) +{ + if(!this->isRunning()) + { + m_url = url; + emit this->start(); + } +} + +/** + * @brief 控制暂停、继续 + * @param flag true:暂停 fasle:继续 + */ +void ReadThread::pause(bool flag) +{ + m_pause = flag; +} + +/** + * @brief 关闭播放 + */ +void ReadThread::close() +{ + m_play = false; + m_pause = false; +} + +/** + * @brief 返回当前播放的地址 + * @return + */ +const QString &ReadThread::url() +{ + return m_url; +} + +void ReadThread::setHWDecoder(bool flag) +{ + m_videoDecode->setHWDecoder(flag); +} + +/** + * @brief 非阻塞延时 + * @param msec 延时毫秒 + */ +void sleepMsec(int msec) +{ + if(msec <= 0) return; + QEventLoop loop; //定义一个新的事件循环 + QTimer::singleShot(msec, &loop, SLOT(quit()));//创建单次定时器,槽函数为事件循环的退出函数 + loop.exec(); //事件循环开始执行,程序会卡在这里,直到定时时间到,本循环被退出 +} + +void ReadThread::run() +{ + bool ret = m_videoDecode->open(m_url); // 打开网络流时会比较慢,如果放到Ui线程会卡 + if(ret) + { + m_play = true; + m_etime1.start(); + m_etime2.start(); + emit playState(play); + } + else + { + qWarning() << "打开失败!"; + } + // 循环读取视频图像 + while (m_play) + { + // 暂停 + while (m_pause) + { + sleepMsec(200); + } + + AVFrame* frame = m_videoDecode->read(); // 读取视频图像 + if(frame) + { + // 1倍速播放 +#if 0 + sleepMsec(int(m_decodeVideo->pts() - m_etime1.elapsed())); // 不支持后退 +#else + sleepMsec(int(m_videoDecode->pts() - m_etime2.elapsed())); // 支持后退 +#endif + emit repaint(frame); + } + else + { + // 当前读取到无效图像时判断是否读取完成 + if(m_videoDecode->isEnd()) + { + break; + } + sleepMsec(1); // 这里不能使用QThread::msleep()延时,否则会很不稳定 + } + } + + qDebug() << "播放结束!"; + m_videoDecode->close(); + emit playState(end); +} diff --git a/FFmpegDemo/VideoPlayHWGL/VideoPlay/readthread.h b/FFmpegDemo/VideoPlayHWGL/VideoPlay/readthread.h new file mode 100644 index 0000000..56c51fc --- /dev/null +++ b/FFmpegDemo/VideoPlayHWGL/VideoPlay/readthread.h @@ -0,0 +1,56 @@ +/****************************************************************************** + * @文件名 readthread.h + * @功能 读取视频图像数据线程,在线程中解码视频 + * + * @开发者 mhf + * @邮箱 1603291350@qq.com + * @时间 2022/09/15 + * @备注 + *****************************************************************************/ +#ifndef READTHREAD_H +#define READTHREAD_H + +#include +#include +#include + +class VideoDecode; +class PlayImage; +struct AVFrame; + +class ReadThread : public QThread +{ + Q_OBJECT +public: + enum PlayState // 视频播放状态 + { + play, + end + }; +public: + explicit ReadThread(QObject *parent = nullptr); + ~ReadThread() override; + + void open(const QString& url = QString()); // 打开视频 + void pause(bool flag); // 暂停视频 + void close(); // 关闭视频 + const QString& url(); // 获取打开的视频地址 + void setHWDecoder(bool flag); // 是否使用硬件解码器 + +protected: + void run() override; + +signals: + void repaint(AVFrame* frame); // 重绘 + void playState(PlayState state); // 视频播放状态发送改变时触发 + +private: + VideoDecode* m_videoDecode = nullptr; // 视频解码类 + QString m_url; // 打开的视频地址 + bool m_play = false; // 播放控制 + bool m_pause = false; // 暂停控制 + QElapsedTimer m_etime1; // 控制视频播放速度(更精确,但不支持视频后退) + QTime m_etime2; // 控制视频播放速度(支持视频后退) +}; + +#endif // READTHREAD_H diff --git a/FFmpegDemo/VideoPlayHWGL/VideoPlay/videodecode.cpp b/FFmpegDemo/VideoPlayHWGL/VideoPlay/videodecode.cpp new file mode 100644 index 0000000..f841705 --- /dev/null +++ b/FFmpegDemo/VideoPlayHWGL/VideoPlay/videodecode.cpp @@ -0,0 +1,543 @@ +#include "videodecode.h" +#include +#include +#include +#include + + +extern "C" { // 用C规则编译指定的代码 +#include "libavcodec/avcodec.h" +#include "libavformat/avformat.h" +#include "libavutil/avutil.h" +#include "libswscale/swscale.h" +#include "libavutil/imgutils.h" +} + +#define ERROR_LEN 1024 // 异常信息数组长度 +#define PRINT_LOG 1 + +VideoDecode::VideoDecode() +{ +// initFFmpeg(); // 5.1.2版本不需要调用了 + + m_error = new char[ERROR_LEN]; + + /*************************************** 获取当前环境支持的硬件解码器 *********************************************/ + AVHWDeviceType type = AV_HWDEVICE_TYPE_NONE; // ffmpeg支持的硬件解码器 + QStringList strTypes; + while ((type = av_hwdevice_iterate_types(type)) != AV_HWDEVICE_TYPE_NONE) // 遍历支持的设备类型。 + { + m_HWDeviceTypes.append(type); + const char* ctype = av_hwdevice_get_type_name(type); // 获取AVHWDeviceType的字符串名称。 + if(ctype) + { + strTypes.append(QString(ctype)); + } + } + qDebug() << "支持的硬件解码器:" << strTypes; + /************************************************ END ******************************************************/ +} + +VideoDecode::~VideoDecode() +{ + close(); +} + +/** + * @brief 初始化ffmpeg库(整个程序中只需加载一次) + * 旧版本的ffmpeg需要注册各种文件格式、解复用器、对网络库进行全局初始化。 + * 在新版本的ffmpeg中纷纷弃用了,不需要注册了 + */ +void VideoDecode::initFFmpeg() +{ + static bool isFirst = true; + static QMutex mutex; + QMutexLocker locker(&mutex); + if(isFirst) + { + // av_register_all(); // 已经从源码中删除 + /** + * 初始化网络库,用于打开网络流媒体,此函数仅用于解决旧GnuTLS或OpenSSL库的线程安全问题。 + * 一旦删除对旧GnuTLS和OpenSSL库的支持,此函数将被弃用,并且此函数不再有任何用途。 + */ + avformat_network_init(); + isFirst = false; + } +} + +/*********************************** FFmpeg获取GPU硬件解码帧格式的回调函数 *****************************************/ +static enum AVPixelFormat g_pixelFormat; +/** + * @brief 回调函数,获取GPU硬件解码帧的格式 + * @param s + * @param fmt + * @return + */ +AVPixelFormat get_hw_format(AVCodecContext* s, const enum AVPixelFormat* fmt) +{ + Q_UNUSED(s) + const enum AVPixelFormat* p; + + for (p = fmt; *p != -1; p++) + { + if(*p == g_pixelFormat) + { + return *p; + } + } + + qDebug() << "无法获取硬件表面格式."; // 当同时打开太多路视频时,如果超过了GPU的能力,可能会返回找不到解码帧格式 + return AV_PIX_FMT_NONE; +} +/************************************************ END ******************************************************/ + +/**************************************** FFmpeg初始化硬件解码器 **********************************************/ +/** + * @brief 初始化硬件解码器 + * @param codec + */ +void VideoDecode::initHWDecoder(const AVCodec *codec) +{ + if(!codec) return; + + for(int i = 0; ; i++) + { + const AVCodecHWConfig* config = avcodec_get_hw_config(codec, i); // 检索编解码器支持的硬件配置。 + if(!config) + { + qDebug() << "打开硬件解码器失败!"; + return; // 没有找到支持的硬件配置 + } + + if (config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX) // 判断是否是设备类型 + { + for(auto i : m_HWDeviceTypes) + { + if(config->device_type == AVHWDeviceType(i)) // 判断设备类型是否是支持的硬件解码器 + { + g_pixelFormat = config->pix_fmt; + + // 打开指定类型的设备,并为其创建AVHWDeviceContext。 + int ret = av_hwdevice_ctx_create(&hw_device_ctx, config->device_type, nullptr, nullptr, 0); + if(ret < 0) + { + showError(ret); + free(); + return ; + } + qDebug() << "打开硬件解码器:" << av_hwdevice_get_type_name(config->device_type); + m_codecContext->hw_device_ctx = av_buffer_ref(hw_device_ctx); // 创建一个对AVBuffer的新引用。 + m_codecContext->get_format = get_hw_format; // 由一些解码器调用,以选择将用于输出帧的像素格式 + return; + } + } + } + } +} + +/************************************************ END ******************************************************/ + +/** + * @brief 打开媒体文件,或者流媒体,例如rtmp、strp、http + * @param url 视频地址 + * @return true:成功 false:失败 + */ +bool VideoDecode::open(const QString &url) +{ + if(url.isNull()) return false; + + AVDictionary* dict = nullptr; + av_dict_set(&dict, "rtsp_transport", "tcp", 0); // 设置rtsp流使用tcp打开,如果打开失败错误信息为【Error number -135 occurred】可以切换(UDP、tcp、udp_multicast、http),比如vlc推流就需要使用udp打开 + av_dict_set(&dict, "max_delay", "3", 0); // 设置最大复用或解复用延迟(以微秒为单位)。当通过【UDP】 接收数据时,解复用器尝试重新排序接收到的数据包(因为它们可能无序到达,或者数据包可能完全丢失)。这可以通过将最大解复用延迟设置为零(通过max_delayAVFormatContext 字段)来禁用。 + av_dict_set(&dict, "timeout", "1000000", 0); // 以微秒为单位设置套接字 TCP I/O 超时,如果等待时间过短,也可能会还没连接就返回了。 + + // 打开输入流并返回解封装上下文 + int ret = avformat_open_input(&m_formatContext, // 返回解封装上下文 + url.toStdString().data(), // 打开视频地址 + nullptr, // 如果非null,此参数强制使用特定的输入格式。自动选择解封装器(文件格式) + &dict); // 参数设置 + // 释放参数字典 + if(dict) + { + av_dict_free(&dict); + } + // 打开视频失败 + if(ret < 0) + { + showError(ret); + free(); + return false; + } + + // 读取媒体文件的数据包以获取流信息。 + ret = avformat_find_stream_info(m_formatContext, nullptr); + if(ret < 0) + { + showError(ret); + free(); + return false; + } + m_totalTime = m_formatContext->duration / (AV_TIME_BASE / 1000); // 计算视频总时长(毫秒) +#if PRINT_LOG + qDebug() << QString("视频总时长:%1 ms,[%2]").arg(m_totalTime).arg(QTime::fromMSecsSinceStartOfDay(int(m_totalTime)).toString("HH:mm:ss zzz")); +#endif + + // 通过AVMediaType枚举查询视频流ID(也可以通过遍历查找),最后一个参数无用 + m_videoIndex = av_find_best_stream(m_formatContext, AVMEDIA_TYPE_VIDEO, -1, -1, nullptr, 0); + if(m_videoIndex < 0) + { + showError(m_videoIndex); + free(); + return false; + } + + AVStream* videoStream = m_formatContext->streams[m_videoIndex]; // 通过查询到的索引获取视频流 + + // 获取视频图像分辨率(AVStream中的AVCodecContext在新版本中弃用,改为使用AVCodecParameters) + m_size.setWidth(videoStream->codecpar->width); + m_size.setHeight(videoStream->codecpar->height); + m_frameRate = rationalToDouble(&videoStream->avg_frame_rate); // 视频帧率 + + // 通过解码器ID获取视频解码器(新版本返回值必须使用const) + const AVCodec* codec = avcodec_find_decoder(videoStream->codecpar->codec_id); + m_totalFrames = videoStream->nb_frames; + +#if PRINT_LOG + qDebug() << QString("分辨率:[w:%1,h:%2] 帧率:%3 总帧数:%4 解码器:%5") + .arg(m_size.width()).arg(m_size.height()).arg(m_frameRate).arg(m_totalFrames).arg(codec->name); +#endif + + // 分配AVCodecContext并将其字段设置为默认值。 + m_codecContext = avcodec_alloc_context3(codec); + if(!m_codecContext) + { +#if PRINT_LOG + qWarning() << "创建视频解码器上下文失败!"; +#endif + free(); + return false; + } + + // 使用视频流的codecpar为解码器上下文赋值 + ret = avcodec_parameters_to_context(m_codecContext, videoStream->codecpar); + if(ret < 0) + { + showError(ret); + free(); + return false; + } + + m_codecContext->flags2 |= AV_CODEC_FLAG2_FAST; // 允许不符合规范的加速技巧。 + m_codecContext->thread_count = 8; // 使用8线程解码 + + if(m_HWDecoder) + { + initHWDecoder(codec); // 初始化硬件解码器(在avcodec_open2前调用) + } + + // 初始化解码器上下文,如果之前avcodec_alloc_context3传入了解码器,这里设置NULL就可以 + ret = avcodec_open2(m_codecContext, nullptr, nullptr); + if(ret < 0) + { + showError(ret); + free(); + return false; + } + + return initObject(); +} + +/** + * @brief 初始化需要用到的对象 + * @return + */ +bool VideoDecode::initObject() +{ + // 分配AVPacket并将其字段设置为默认值。 + m_packet = av_packet_alloc(); + if(!m_packet) + { +#if PRINT_LOG + qWarning() << "av_packet_alloc() Error!"; +#endif + free(); + return false; + } + // 分配AVFrame并将其字段设置为默认值。 + m_frame = av_frame_alloc(); + if(!m_frame) + { +#if PRINT_LOG + qWarning() << "av_frame_alloc() Error!"; +#endif + free(); + return false; + } + m_frameHW = av_frame_alloc(); + if(!m_frameHW) + { +#if PRINT_LOG + qWarning() << "av_frame_alloc() Error!"; +#endif + free(); + return false; + } + + + // 由于传递时是浅拷贝,可能显示类还没处理完成,所以如果播放完成就释放可能会崩溃; + if(m_buffer) + { + delete [] m_buffer; + m_buffer = nullptr; + } + // 分配图像空间 + int size = av_image_get_buffer_size(AV_PIX_FMT_RGBA, m_size.width(), m_size.height(), 4); + /** + * 【注意:】这里可以多分配一些,否则如果只是安装size分配,大部分视频图像数据拷贝没有问题, + * 但是少部分视频图像在使用sws_scale()拷贝时会超出数组长度,在使用使用msvc debug模式时delete[] m_buffer会报错(HEAP CORRUPTION DETECTED: after Normal block(#32215) at 0x000001AC442830370.CRT delected that the application wrote to memory after end of heap buffer) + * 特别是这个视频流http://vfx.mtime.cn/Video/2019/02/04/mp4/190204084208765161.mp4 + */ + m_buffer = new uchar[size + 1000]; // 这里多分配1000个字节就基本不会出现拷贝超出的情况了,反正不缺这点内存 +// m_image = new QImage(m_buffer, m_size.width(), m_size.height(), QImage::Format_RGBA8888); // 这种方式分配内存大部分情况下也可以,但是因为存在拷贝超出数组的情况,delete时也会报错 + m_end = false; + + return true; +} + + +/** + * @brief 读取并返回视频图像 + * @return + */ +AVFrame* VideoDecode::read() +{ + // 如果没有打开则返回 + if(!m_formatContext) + { + return nullptr; + } + + // 读取下一帧数据 + int readRet = av_read_frame(m_formatContext, m_packet); + if(readRet < 0) + { + avcodec_send_packet(m_codecContext, m_packet); // 读取完成后向解码器中传如空AVPacket,否则无法读取出最后几帧 + } + else + { + if(m_packet->stream_index == m_videoIndex) // 如果是图像数据则进行解码 + { + // 计算当前帧时间(毫秒) +#if 1 // 方法一:适用于所有场景,但是存在一定误差 + m_packet->pts = qRound64(m_packet->pts * (1000 * rationalToDouble(&m_formatContext->streams[m_videoIndex]->time_base))); + m_packet->dts = qRound64(m_packet->dts * (1000 * rationalToDouble(&m_formatContext->streams[m_videoIndex]->time_base))); +#else // 方法二:适用于播放本地视频文件,计算每一帧时间较准,但是由于网络视频流无法获取总帧数,所以无法适用 + m_obtainFrames++; + m_packet->pts = qRound64(m_obtainFrames * (qreal(m_totalTime) / m_totalFrames)); +#endif + // 将读取到的原始数据包传入解码器 + int ret = avcodec_send_packet(m_codecContext, m_packet); + if(ret < 0) + { + showError(ret); + } + } + } + av_packet_unref(m_packet); // 释放数据包,引用计数-1,为0时释放空间 + av_frame_unref(m_frame); + av_frame_unref(m_frameHW); + int ret = avcodec_receive_frame(m_codecContext, m_frame); + if(ret < 0) + { + av_frame_unref(m_frame); + if(readRet < 0) + { + m_end = true; // 当无法读取到AVPacket并且解码器中也没有数据时表示读取完成 + } + return nullptr; + } + + // 这样写是为了兼容软解码或者硬件解码打开失败情况 + AVFrame* m_frameTemp = m_frame; + if(!m_frame->data[0]) // 如果是硬件解码就进入 + { + m_frameTemp = m_frameHW; + // 将解码后的数据从GPU拷贝到CPU + if(!dataCopy()) + { + return nullptr; + } + } + + m_pts = m_frameTemp->pts; + + return m_frameTemp; +} + +/********************************* FFmpeg初始化硬件后将图像数据从GPU拷贝到CPU *************************************/ +/** + * @brief 硬件解码完成需要将数据从GPU复制到CPU + * @return + */ +bool VideoDecode::dataCopy() +{ + if(m_frame->format != g_pixelFormat) + { + av_frame_unref(m_frame); + return false; + } +#if 1 // av_hwframe_map在ffmpeg3.3以后才有,经过测试av_hwframe_transfer_data的耗时大概是av_hwframe_map的【1.5倍】 + int ret = av_hwframe_map(m_frameHW, m_frame, 0); // 映射硬件数据帧 + if(ret < 0) + { + showError(ret); + av_frame_unref(m_frame); + return false; + } + m_frameHW->width = m_frame->width; + m_frameHW->height = m_frame->height; +#else + int ret = av_hwframe_transfer_data(m_frameHW, m_frame, 0); // 将解码后的数据从GPU复制到CPU(m_frameHW) 这一步比较耗时,在这一步之前硬解码速度比软解码快很多 + if(ret < 0) + { + showError(ret); + av_frame_unref(m_frame); + return false; + } + av_frame_copy_props(m_frameHW, m_frame); // 仅将“metadata”字段从src复制到dst。 +#endif + return true; +} + +/************************************************ END ******************************************************/ + +/** + * @brief 关闭视频播放并释放内存 + */ +void VideoDecode::close() +{ + clear(); + free(); + + m_totalTime = 0; + m_videoIndex = 0; + m_totalFrames = 0; + m_obtainFrames = 0; + m_pts = 0; + m_frameRate = 0; + m_size = QSize(0, 0); +} + +/** + * @brief 视频是否读取完成 + * @return + */ +bool VideoDecode::isEnd() +{ + return m_end; +} + +/** + * @brief 返回当前帧图像播放时间 + * @return + */ +const qint64 &VideoDecode::pts() +{ + return m_pts; +} + +/** + * @brief 设置是否使用硬件解码 + * @param flag true:使用 false:不使用 + */ +void VideoDecode::setHWDecoder(bool flag) +{ + m_HWDecoder = flag; +} + +/** + * @brief 返回当前是否使用硬件解码 + * @return + */ +bool VideoDecode::isHWDecoder() +{ + return m_HWDecoder; +} + +/** + * @brief 显示ffmpeg函数调用异常信息 + * @param err + */ +void VideoDecode::showError(int err) +{ +#if PRINT_LOG + memset(m_error, 0, ERROR_LEN); // 将数组置零 + av_strerror(err, m_error, ERROR_LEN); + qWarning() << "DecodeVideo Error:" << m_error; +#else + Q_UNUSED(err) +#endif +} + +/** + * @brief 将AVRational转换为double,用于计算帧率 + * @param rational + * @return + */ +qreal VideoDecode::rationalToDouble(AVRational* rational) +{ + qreal frameRate = (rational->den == 0) ? 0 : (qreal(rational->num) / rational->den); + return frameRate; +} + +/** + * @brief 清空读取缓冲 + */ +void VideoDecode::clear() +{ + // 因为avformat_flush不会刷新AVIOContext (s->pb)。如果有必要,在调用此函数之前调用avio_flush(s->pb)。 + if(m_formatContext && m_formatContext->pb) + { + avio_flush(m_formatContext->pb); + } + if(m_formatContext) + { + avformat_flush(m_formatContext); // 清理读取缓冲 + } +} + +void VideoDecode::free() +{ + // 释放上下文swsContext。 + if(m_swsContext) + { + sws_freeContext(m_swsContext); + m_swsContext = nullptr; // sws_freeContext不会把上下文置NULL + } + // 释放编解码器上下文和与之相关的所有内容,并将NULL写入提供的指针 + if(m_codecContext) + { + avcodec_free_context(&m_codecContext); + } + // 关闭并失败m_formatContext,并将指针置为null + if(m_formatContext) + { + avformat_close_input(&m_formatContext); + } + if(hw_device_ctx) + { + av_buffer_unref(&hw_device_ctx); + } + if(m_packet) + { + av_packet_free(&m_packet); + } + if(m_frame) + { + av_frame_free(&m_frame); + } + if(m_frameHW) + { + av_frame_free(&m_frameHW); + } +} diff --git a/FFmpegDemo/VideoPlayHWGL/VideoPlay/videodecode.h b/FFmpegDemo/VideoPlayHWGL/VideoPlay/videodecode.h new file mode 100644 index 0000000..d9199f1 --- /dev/null +++ b/FFmpegDemo/VideoPlayHWGL/VideoPlay/videodecode.h @@ -0,0 +1,76 @@ +/****************************************************************************** + * @文件名 videodecode.h + * @功能 视频解码类,在这个类中调用ffmpeg打开视频进行解码; + * 使用av_hwframe_map替代av_hwframe_transfer_data,可将【耗时降低1/3】; + * + * @开发者 mhf + * @邮箱 1603291350@qq.com + * @时间 2022/09/15 + * @备注 + *****************************************************************************/ +#ifndef VIDEODECODE_H +#define VIDEODECODE_H + +#include +#include +#include +#include + +struct AVFormatContext; +struct AVCodecContext; +struct AVRational; +struct AVPacket; +struct AVFrame; +struct AVCodec; +struct SwsContext; +struct AVBufferRef; +class QImage; + +class VideoDecode +{ +public: + VideoDecode(); + ~VideoDecode(); + + bool open(const QString& url = QString()); // 打开媒体文件,或者流媒体rtmp、strp、http + AVFrame* read(); // 读取视频图像 + void close(); // 关闭 + bool isEnd(); // 是否读取完成 + const qint64& pts(); // 获取当前帧显示时间 + void setHWDecoder(bool flag); // 是否使用硬件解码器 + bool isHWDecoder(); + +private: + void initFFmpeg(); // 初始化ffmpeg库(整个程序中只需加载一次) + void initHWDecoder(const AVCodec* codec); // 初始化硬件解码器 + bool initObject(); // 初始化对象 + bool dataCopy(); // 硬件解码完成需要将数据从GPU复制到CPU + void showError(int err); // 显示ffmpeg执行错误时的错误信息 + qreal rationalToDouble(AVRational* rational); // 将AVRational转换为double + void clear(); // 清空读取缓冲 + void free(); // 释放 + +private: + AVFormatContext* m_formatContext = nullptr; // 解封装上下文 + AVCodecContext* m_codecContext = nullptr; // 解码器上下文 + SwsContext* m_swsContext = nullptr; // 图像转换上下文 + AVPacket* m_packet = nullptr; // 数据包 + AVFrame* m_frame = nullptr; // 解码后的视频帧 + AVFrame* m_frameHW = nullptr; // 硬件解码后的视频帧 + int m_videoIndex = 0; // 视频流索引 + qint64 m_totalTime = 0; // 视频总时长 + qint64 m_totalFrames = 0; // 视频总帧数 + qint64 m_obtainFrames = 0; // 视频当前获取到的帧数 + qint64 m_pts = 0; // 图像帧的显示时间 + qreal m_frameRate = 0; // 视频帧率 + QSize m_size; // 视频分辨率大小 + char* m_error = nullptr; // 保存异常信息 + bool m_end = false; // 视频读取完成 + uchar* m_buffer = nullptr; + + QList m_HWDeviceTypes; // 保存当前环境支持的硬件解码器 + AVBufferRef* hw_device_ctx = nullptr; // 对数据缓冲区的引用 + bool m_HWDecoder = false; // 记录是否使用硬件解码 +}; + +#endif // VIDEODECODE_H diff --git a/FFmpegDemo/VideoPlayHWGL/VideoPlayHWGL.pro b/FFmpegDemo/VideoPlayHWGL/VideoPlayHWGL.pro new file mode 100644 index 0000000..66edf0b --- /dev/null +++ b/FFmpegDemo/VideoPlayHWGL/VideoPlayHWGL.pro @@ -0,0 +1,67 @@ +#--------------------------------------------------------------------------------------- +# @功能: 使用ffmpeg音视频库【软/硬解码 + OpenGL】实现的视频播放器; +# @编译器: Desktop Qt 5.12.5 MSVC2017 64bit 32bit(也支持其它编译器) +# @Qt IDE: D:/Qt/Qt5.12.5/Tools/QtCreator/share/qtcreator +# +# @开发者 mhf +# @邮箱 1603291350@qq.com +# @时间 2022-09-14 14:13:52 +# @备注 1、使用ffmpeg音视频库【软/硬解码】实现的视频播放器; +# 2、支持打开本地视频文件(如mp4、mov、avi等)、网络视频流(rtsp、rtmp、http等); +# 3、支持视频匀速播放; +# 4、采用【OpenGL显示YUV、NV12】图像,支持自适应窗口缩放,支持使用QOpenGLWidget、QOpenGLWindow显示; +# 5、将YUV/NV12转RGB的步骤由CPU转换改为使用GPU转换,降低CPU占用率; +# 6、使用av_hwframe_map替代av_hwframe_transfer_data,可将【耗时降低1/3】; +# 7、视频播放支持实时开始/关闭、暂停/继续播放; +# 8、视频解码、线程控制、显示各部分功能分离,低耦合度。 +# 9、采用最新的【5.1.2版本】ffmpeg库进行开发,超详细注释信息,将所有踩过的坑、解决办法、注意事项都得很写清楚。 +#--------------------------------------------------------------------------------------- +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets +CONFIG += c++11 +DEFINES += QT_DEPRECATED_WARNINGS + +SOURCES += \ + playimage.cpp \ + main.cpp \ + widget.cpp + +HEADERS += \ + playimage.h \ + widget.h + +FORMS += \ + widget.ui + +# Default rules for deployment. +qnx: target.path = /tmp/$${TARGET}/bin +else: unix:!android: target.path = /opt/$${TARGET}/bin +!isEmpty(target.path): INSTALLS += target + +# ffmpeg读取视频图像模块 +include(./VideoPlay/VideoPlay.pri) +INCLUDEPATH += ./VideoPlay + +# 定义程序版本号 +VERSION = 1.0.0 +DEFINES += APP_VERSION=\\\"$$VERSION\\\" +TARGET = VideoPlayHWGL + +contains(QT_ARCH, i386){ # 使用32位编译器 +DESTDIR = $$PWD/../bin # 程序输出路径 +}else{ +DESTDIR = $$PWD/../bin64 # 使用64位编译器 +} +# msvc 编译器使用utf-8编码 +msvc { +QMAKE_CFLAGS += /utf-8 +QMAKE_CXXFLAGS += /utf-8 +} + +DISTFILES += \ + fragment.fsh \ + vertex.vsh + +RESOURCES += \ + rc.qrc diff --git a/FFmpegDemo/VideoPlayHWGL/fragment.fsh b/FFmpegDemo/VideoPlayHWGL/fragment.fsh new file mode 100644 index 0000000..841ae7e --- /dev/null +++ b/FFmpegDemo/VideoPlayHWGL/fragment.fsh @@ -0,0 +1,35 @@ +#version 330 core +in vec2 TexCord; // 纹理坐标 +uniform int format = -1; // 像素格式 +uniform sampler2D tex_y; +uniform sampler2D tex_u; +uniform sampler2D tex_v; +uniform sampler2D tex_uv; + +void main() +{ + vec3 yuv; + vec3 rgb; + + + if(format == 0) // YUV420P转RGB + { + yuv.x = texture2D(tex_y, TexCord).r; + yuv.y = texture2D(tex_u, TexCord).r-0.5; + yuv.z = texture2D(tex_v, TexCord).r-0.5; + } + else if(format == 23) // NV12转RGB + { + yuv.x = texture2D(tex_y, TexCord.st).r; + yuv.y = texture2D(tex_uv, TexCord.st).r - 0.5; + yuv.z = texture2D(tex_uv, TexCord.st).g - 0.5; + } + else + { + } + + rgb = mat3(1.0, 1.0, 1.0, + 0.0, -0.39465, 2.03211, + 1.13983, -0.58060, 0.0) * yuv; + gl_FragColor = vec4(rgb, 1.0); +} diff --git a/FFmpegDemo/VideoPlayHWGL/main.cpp b/FFmpegDemo/VideoPlayHWGL/main.cpp new file mode 100644 index 0000000..b0a4ec2 --- /dev/null +++ b/FFmpegDemo/VideoPlayHWGL/main.cpp @@ -0,0 +1,11 @@ +#include "widget.h" + +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + Widget w; + w.show(); + return a.exec(); +} diff --git a/FFmpegDemo/VideoPlayHWGL/playimage.cpp b/FFmpegDemo/VideoPlayHWGL/playimage.cpp new file mode 100644 index 0000000..4daaede --- /dev/null +++ b/FFmpegDemo/VideoPlayHWGL/playimage.cpp @@ -0,0 +1,399 @@ +#include "playimage.h" + +extern "C" { // 用C规则编译指定的代码 +#include "libavcodec/avcodec.h" +} + +#if USE_WINDOW +PlayImage::PlayImage(QOpenGLWindow::UpdateBehavior updateBehavior, QWindow *parent):QOpenGLWindow(updateBehavior, parent) +{ + // 初始化视图大小,由于Shader里面有YUV转RGB的代码,会初始化显示为绿色,这里通过将视图大小设置为0避免显示绿色背景 + m_pos = QPointF(0, 0); + m_zoomSize = QSize(0, 0); +} +#else +PlayImage::PlayImage(QWidget *parent, Qt::WindowFlags f): QOpenGLWidget(parent, f) +{ + // 初始化视图大小,由于Shader里面有YUV转RGB的代码,会初始化显示为绿色,这里通过将视图大小设置为0避免显示绿色背景 + m_pos = QPointF(0, 0); + m_zoomSize = QSize(0, 0); +} +#endif + + +PlayImage::~PlayImage() +{ + if(!isValid()) return; // 如果控件和OpenGL资源(如上下文)已成功初始化,则返回true。 + this->makeCurrent(); // 通过将相应的上下文设置为当前上下文并在该上下文中绑定帧缓冲区对象,为呈现此小部件的OpenGL内容做准备。 + + freeTexYUV420P(); + freeTexNV12(); + this->doneCurrent(); // 释放上下文 + // 释放 + glDeleteBuffers(1, &VBO); + glDeleteBuffers(1, &EBO); + glDeleteVertexArrays(1, &VAO); +} + +void PlayImage::repaint(AVFrame *frame) +{ + if(!frame) return; + + m_format = frame->format; + switch (m_format) + { + case AV_PIX_FMT_YUV420P: // ffmpeg软解码的像素格式为YUV420P + { + repaintTexYUV420P(frame); + break; + } + case AV_PIX_FMT_NV12: // 由于ffmpeg硬件解码的像素格式为NV12,不是YUV,所以需要单独处理 + { + repaintTexNV12(frame); + break; + } + default: break; + } + + av_frame_unref(frame); // 取消引用帧引用的所有缓冲区并重置帧字段。 + + this->update(); +} + +/** + * @brief 更新YUV420P图像数据纹理 + * @param frame + */ +void PlayImage::repaintTexYUV420P(AVFrame *frame) +{ + // 当切换显示的视频后,如果分辨率不同则需要重新创建纹理,否则会崩溃 + if(frame->width != m_size.width() || frame->height != m_size.height()) + { + freeTexYUV420P(); + } + initTexYUV420P(frame); + + m_options.setImageHeight(frame->height); + m_options.setRowLength(frame->linesize[0]); + m_texY->setData(QOpenGLTexture::Red, QOpenGLTexture::UInt8, static_cast(frame->data[0]), &m_options); // 设置图像数据 Y + m_options.setRowLength(frame->linesize[1]); + m_texU->setData(QOpenGLTexture::Red, QOpenGLTexture::UInt8, static_cast(frame->data[1]), &m_options); // 设置图像数据 U + m_options.setRowLength(frame->linesize[2]); + m_texV->setData(QOpenGLTexture::Red, QOpenGLTexture::UInt8, static_cast(frame->data[2]), &m_options); // 设置图像数据 V +} + +/** + * @brief 初始化YUV420P图像纹理 + * @param frame + */ +void PlayImage::initTexYUV420P(AVFrame *frame) +{ + + if(!m_texY) // 初始化纹理 + { + // 创建2D纹理 + m_texY = new QOpenGLTexture(QOpenGLTexture::Target2D); + + // 设置纹理大小 + m_texY->setSize(frame->width, frame->height); + + // 设置放大、缩小过滤器 + m_texY->setMinMagFilters(QOpenGLTexture::LinearMipMapLinear,QOpenGLTexture::Linear); + + // 设置图像格式 + m_texY->setFormat(QOpenGLTexture::R8_UNorm); + + // 分配内存 + m_texY->allocateStorage(); + + // 记录图像分辨率 + m_size.setWidth(frame->width); + m_size.setHeight(frame->height); + resizeGL(this->width(), this->height()); + + } + if(!m_texU) + { + m_texU = new QOpenGLTexture(QOpenGLTexture::Target2D); + m_texU->setSize(frame->width / 2, frame->height / 2); + m_texU->setMinMagFilters(QOpenGLTexture::LinearMipMapLinear,QOpenGLTexture::Linear); + m_texU->setFormat(QOpenGLTexture::R8_UNorm); + m_texU->allocateStorage(); + } + if(!m_texV) // 初始化纹理 + { + m_texV = new QOpenGLTexture(QOpenGLTexture::Target2D); + m_texV->setSize(frame->width / 2, frame->height / 2); + m_texV->setMinMagFilters(QOpenGLTexture::LinearMipMapLinear,QOpenGLTexture::Linear); + m_texV->setFormat(QOpenGLTexture::R8_UNorm); + m_texV->allocateStorage(); + } +} + +/** + * @brief 释放YUV420P图像纹理 + */ +void PlayImage::freeTexYUV420P() +{ + // 释放纹理 + if(m_texY) + { + m_texY->destroy(); + delete m_texY; + m_texY = nullptr; + } + if(m_texU) + { + m_texU->destroy(); + delete m_texU; + m_texU = nullptr; + } + if(m_texV) + { + m_texV->destroy(); + delete m_texV; + m_texV = nullptr; + } +} + +/** + * @brief 更新NV12图像数据纹理 + * @param frame + */ +void PlayImage::repaintTexNV12(AVFrame *frame) +{ + // 当切换显示的视频后,如果分辨率不同则需要重新创建纹理,否则会崩溃 + if(frame->width != m_size.width() || frame->height != m_size.height()) + { + freeTexNV12(); + } + initTexNV12(frame); + + m_options.setImageHeight(frame->height); + m_options.setRowLength(frame->linesize[0]); + m_texY->setData(QOpenGLTexture::Red, QOpenGLTexture::UInt8, static_cast(frame->data[0]), &m_options); // 设置图像数据 Y + m_options.setImageHeight(frame->height / 2); + m_options.setRowLength(frame->linesize[1] / 2); + m_texUV->setData(QOpenGLTexture::RG, QOpenGLTexture::UInt8, static_cast(frame->data[1]), &m_options); // 设置图像数据 UV +} + +/** + * @brief 初始化NV12图像纹理 + * @param frame + */ +void PlayImage::initTexNV12(AVFrame *frame) +{ + if(!m_texY) // 初始化纹理 + { + // 创建2D纹理 + m_texY = new QOpenGLTexture(QOpenGLTexture::Target2D); + + // 设置纹理大小 + m_texY->setSize(frame->width, frame->height); + + // 设置放大、缩小过滤器 + m_texY->setMinMagFilters(QOpenGLTexture::LinearMipMapLinear,QOpenGLTexture::Linear); + + // 设置图像格式 + m_texY->setFormat(QOpenGLTexture::R8_UNorm); + + // 分配内存 + m_texY->allocateStorage(); + + // 记录图像分辨率 + m_size.setWidth(frame->width); + m_size.setHeight(frame->height); + resizeGL(this->width(), this->height()); + + } + if(!m_texUV) + { + m_texUV = new QOpenGLTexture(QOpenGLTexture::Target2D); + m_texUV->setSize(frame->width / 2, frame->height / 2); + m_texUV->setMinMagFilters(QOpenGLTexture::LinearMipMapLinear,QOpenGLTexture::Linear); + m_texUV->setFormat(QOpenGLTexture::RG8_UNorm); + m_texUV->allocateStorage(); + } +} + +/** + * @brief 释放NV12图像纹理 + */ +void PlayImage::freeTexNV12() +{ + // 释放纹理 + if(m_texY) + { + m_texY->destroy(); + delete m_texY; + m_texY = nullptr; + } + if(m_texUV) + { + m_texUV->destroy(); + delete m_texUV; + m_texUV = nullptr; + } +} + +// 三个顶点坐标XYZ,VAO、VBO数据播放,范围时[-1 ~ 1]直接 +static GLfloat vertices[] = { // 前三列点坐标,后两列为纹理坐标 + 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, // 右上角 + 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, // 右下 + -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, // 左下 + -1.0f, 1.0f, 0.0f, 0.0f, 1.0f // 左上 +}; +static GLuint indices[] = { + 0, 1, 3, + 1, 2, 3 +}; +void PlayImage::initializeGL() +{ + initializeOpenGLFunctions(); + + // 加载shader脚本程序 + m_program = new QOpenGLShaderProgram(this); + m_program->addShaderFromSourceFile(QOpenGLShader::Vertex, ":/vertex.vsh"); + m_program->addShaderFromSourceFile(QOpenGLShader::Fragment, ":/fragment.fsh"); + m_program->link(); + + // 绑定YUV 变量值 + m_program->bind(); + m_program->setUniformValue("tex_y", 0); + m_program->setUniformValue("tex_u", 1); + m_program->setUniformValue("tex_v", 2); + m_program->setUniformValue("tex_uv", 3); + + + // 返回属性名称在此着色器程序的参数列表中的位置。如果名称不是此着色器程序的有效属性,则返回-1。 + GLuint posAttr = GLuint(m_program->attributeLocation("aPos")); + GLuint texCord = GLuint(m_program->attributeLocation("aTexCord")); + + glGenVertexArrays(1, &VAO); + glBindVertexArray(VAO); + + glGenBuffers(1, &VBO); + glBindBuffer(GL_ARRAY_BUFFER, VBO); + glGenBuffers(1, &EBO); // 创建一个EBO + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); + + + // 为当前绑定到的缓冲区对象创建一个新的数据存储target。任何预先存在的数据存储都将被删除。 + glBufferData(GL_ARRAY_BUFFER, // 为VBO缓冲绑定顶点数据 + sizeof (vertices), // 数组字节大小 + vertices, // 需要绑定的数组 + GL_STATIC_DRAW); // 指定数据存储的预期使用模式,GL_STATIC_DRAW: 数据几乎不会改变 + glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); // 将顶点索引数组传入EBO缓存 + // 设置顶点坐标数据 + glVertexAttribPointer(posAttr, // 指定要修改的通用顶点属性的索引 + 3, // 指定每个通用顶点属性的组件数(如vec3:3,vec4:4) + GL_FLOAT, // 指定数组中每个组件的数据类型(数组中一行有几个数) + GL_FALSE, // 指定在访问定点数据值时是否应规范化 ( GL_TRUE) 或直接转换为定点值 ( GL_FALSE),如果vertices里面单个数超过-1或者1可以选择GL_TRUE + 5 * sizeof(GLfloat), // 指定连续通用顶点属性之间的字节偏移量。 + nullptr); // 指定当前绑定到目标的缓冲区的数据存储中数组中第一个通用顶点属性的第一个组件的偏移量。初始值为0 (一个数组从第几个字节开始读) + // 启用通用顶点属性数组 + glEnableVertexAttribArray(posAttr); // 属性索引是从调用glGetAttribLocation接收的,或者传递给glBindAttribLocation。 + + // 设置纹理坐标数据 + glVertexAttribPointer(texCord, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), reinterpret_cast(3 * sizeof (GLfloat))); // 指定当前绑定到目标的缓冲区的数据存储中数组中第一个通用顶点属性的第一个组件的偏移量。初始值为0 (一个数组从第几个字节开始读) + // 启用通用顶点属性数组 + glEnableVertexAttribArray(texCord); // 属性索引是从调用glGetAttribLocation接收的,或者传递给glBindAttribLocation。 + + // 释放 + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindVertexArray(0); // 设置为零以破坏现有的顶点数组对象绑定 + + glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // 指定颜色缓冲区的清除值(背景色) + + +} + +void PlayImage::resizeGL(int w, int h) +{ + if(m_size.width() < 0 || m_size.height() < 0) return; + + // 计算需要显示图片的窗口大小,用于实现长宽等比自适应显示 + if((double(w) / h) < (double(m_size.width()) / m_size.height())) + { + m_zoomSize.setWidth(w); + m_zoomSize.setHeight(((double(w) / m_size.width()) * m_size.height())); // 这里不使用QRect,使用QRect第一次设置时有误差bug + } + else + { + m_zoomSize.setHeight(h); + m_zoomSize.setWidth((double(h) / m_size.height()) * m_size.width()); + } + m_pos.setX(double(w - m_zoomSize.width()) / 2); + m_pos.setY(double(h - m_zoomSize.height()) / 2); + this->update(QRect(0, 0, w, h)); +} + +void PlayImage::paintGL() +{ + glClear(GL_COLOR_BUFFER_BIT); // 将窗口的位平面区域(背景)设置为先前由glClearColor、glClearDepth和选择的值 + glViewport(m_pos.x(), m_pos.y(), m_zoomSize.width(), m_zoomSize.height()); // 设置视图大小实现图片自适应 + + m_program->bind(); // 绑定着色器 + m_program->setUniformValue("format", m_format); + + // 绑定纹理 + switch (m_format) + { + case AV_PIX_FMT_YUV420P: + { + if(m_texY && m_texU && m_texV) + { + m_texY->bind(0); + m_texU->bind(1); + m_texV->bind(2); + } + break; + } + case AV_PIX_FMT_NV12: + { + if(m_texY && m_texUV) + { + m_texY->bind(0); + m_texUV->bind(3); + } + break; + } + default: break; + } + + + glBindVertexArray(VAO); // 绑定VAO + + glDrawElements(GL_TRIANGLES, // 绘制的图元类型 + 6, // 指定要渲染的元素数(点数) + GL_UNSIGNED_INT, // 指定索引中值的类型(indices) + nullptr); // 指定当前绑定到GL_ELEMENT_array_buffer目标的缓冲区的数据存储中数组中第一个索引的偏移量。 + glBindVertexArray(0); + + // 释放纹理 + switch (m_format) + { + case AV_PIX_FMT_YUV420P: + { + if(m_texY && m_texU && m_texV) + { + m_texY->release(); + m_texU->release(); + m_texV->release(); + } + break; + } + case AV_PIX_FMT_NV12: + { + if(m_texY && m_texUV) + { + m_texY->release(); + m_texUV->release(); + } + break; + } + default: break; + } + m_program->release(); +} + diff --git a/FFmpegDemo/VideoPlayHWGL/playimage.h b/FFmpegDemo/VideoPlayHWGL/playimage.h new file mode 100644 index 0000000..74efc05 --- /dev/null +++ b/FFmpegDemo/VideoPlayHWGL/playimage.h @@ -0,0 +1,75 @@ +/****************************************************************************** + * @文件名 playimage.h + * @功能 使用OpenGL实现YUV图像的绘制,可通过USE_WINDOW宏切换使用QOpenGLWindow还是QOpenGLWidget + * + * @开发者 mhf + * @邮箱 1603291350@qq.com + * @时间 2022/10/14 + * @备注 + *****************************************************************************/ +#ifndef PLAYIMAGE_H +#define PLAYIMAGE_H + +#include +#include +#include +#include +#include + +struct AVFrame; + +#define USE_WINDOW 0 // 1:使用QOpenGLWindow显示, 0:使用QOpenGLWidget显示 + +#if USE_WINDOW +#include +class PlayImage : public QOpenGLWindow, public QOpenGLFunctions_3_3_Core +#else +#include +class PlayImage : public QOpenGLWidget, public QOpenGLFunctions_3_3_Core +#endif +{ + Q_OBJECT +public: +#if USE_WINDOW + explicit PlayImage(UpdateBehavior updateBehavior = NoPartialUpdate, QWindow *parent = nullptr); +#else + explicit PlayImage(QWidget* parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags()); +#endif + ~PlayImage() override; + + void repaint(AVFrame* frame); // 设置需要绘制的图像帧 + + +protected: + void initializeGL() override; // 初始化gl + void resizeGL(int w, int h) override; // 窗口尺寸变化 + void paintGL() override; // 刷新显示 + +private: + // YUV420图像数据更新 + void repaintTexYUV420P(AVFrame* frame); + void initTexYUV420P(AVFrame* frame); + void freeTexYUV420P(); + // NV12图像数据更新 + void repaintTexNV12(AVFrame* frame); + void initTexNV12(AVFrame* frame); + void freeTexNV12(); + +private: + QOpenGLShaderProgram* m_program = nullptr; + QOpenGLTexture* m_texY = nullptr; + QOpenGLTexture* m_texU = nullptr; + QOpenGLTexture* m_texV = nullptr; + QOpenGLTexture* m_texUV = nullptr; + QOpenGLPixelTransferOptions m_options; + + GLuint VBO = 0; // 顶点缓冲对象,负责将数据从内存放到缓存,一个VBO可以用于多个VAO + GLuint VAO = 0; // 顶点数组对象,任何随后的顶点属性调用都会储存在这个VAO中,一个VAO可以有多个VBO + GLuint EBO = 0; // 元素缓冲对象,它存储 OpenGL 用来决定要绘制哪些顶点的索引 + QSize m_size; + QSizeF m_zoomSize; + QPointF m_pos; + int m_format; // 像素格式 +}; + +#endif // PLAYIMAGE_H diff --git a/FFmpegDemo/VideoPlayHWGL/rc.qrc b/FFmpegDemo/VideoPlayHWGL/rc.qrc new file mode 100644 index 0000000..2829308 --- /dev/null +++ b/FFmpegDemo/VideoPlayHWGL/rc.qrc @@ -0,0 +1,6 @@ + + + fragment.fsh + vertex.vsh + + diff --git a/FFmpegDemo/VideoPlayHWGL/vertex.vsh b/FFmpegDemo/VideoPlayHWGL/vertex.vsh new file mode 100644 index 0000000..fb7430e --- /dev/null +++ b/FFmpegDemo/VideoPlayHWGL/vertex.vsh @@ -0,0 +1,9 @@ +#version 330 core +layout (location = 0) in vec3 aPos; +layout (location = 1) in vec2 aTexCord; +out vec2 TexCord; // 纹理坐标 +void main() +{ + gl_Position = vec4(aPos.x, -aPos.y, aPos.z, 1.0); // 图像坐标和OpenGL坐标Y轴相反, + TexCord = aTexCord; +} diff --git a/FFmpegDemo/VideoPlayHWGL/widget.cpp b/FFmpegDemo/VideoPlayHWGL/widget.cpp new file mode 100644 index 0000000..0f85954 --- /dev/null +++ b/FFmpegDemo/VideoPlayHWGL/widget.cpp @@ -0,0 +1,111 @@ +#include "widget.h" +#include "ui_widget.h" + +#include + +extern "C" { // 用C规则编译指定的代码 +#include "libavcodec/avcodec.h" +} +Q_DECLARE_METATYPE(AVFrame) //注册结构体,否则无法通过信号传递AVFrame + +Widget::Widget(QWidget *parent) + : QWidget(parent) + , ui(new Ui::Widget) +{ + ui->setupUi(this); + this->setWindowTitle(QString("Qt+ffmpeg视频播放(软/硬解码 + OpenGL显示YUV/NV12)Demo V%1").arg(APP_VERSION)); + + + // 使用QOpenGLWindow绘制 + playImage = new PlayImage; +#if USE_WINDOW + ui->verticalLayout->addWidget(QWidget::createWindowContainer(playImage)); // 这一步加载速度要比OpenGLWidget慢一点 +#else + ui->verticalLayout->addWidget(playImage); +#endif + + m_readThread = new ReadThread(); + connect(m_readThread, &ReadThread::repaint, playImage, &PlayImage::repaint, Qt::BlockingQueuedConnection); + connect(m_readThread, &ReadThread::playState, this, &Widget::on_playState); +} + +Widget::~Widget() +{ + // 释放视频读取线程 + if(m_readThread) + { + m_readThread->close(); + m_readThread->wait(); + delete m_readThread; + } + delete ui; +} + +/** + * @brief 获取本地视频路径 + */ +void Widget::on_but_file_clicked() +{ + QString strName = QFileDialog::getOpenFileName(this, "选择播放视频~!", "/", "视频 (*.mp4 *.m4v *.mov *.avi *.flv);; 其它(*)"); + if(strName.isEmpty()) + { + return; + } + ui->com_url->setCurrentText(strName); +} + +/** + * @brief 视频播放/停止 + */ +void Widget::on_but_open_clicked() +{ + if(ui->but_open->text() == "开始播放") + { + m_readThread->open(ui->com_url->currentText()); + } + else + { + m_readThread->close(); + } +} + +/** + * @brief 视频暂停/继续 + */ +void Widget::on_but_pause_clicked() +{ + if(ui->but_pause->text() == "暂停") + { + m_readThread->pause(true); + ui->but_pause->setText("继续"); + } + else + { + m_readThread->pause(false); + ui->but_pause->setText("暂停"); + } +} + +/** + * @brief 根据视频播放状态切换界面设置 + * @param state + */ +void Widget::on_playState(ReadThread::PlayState state) +{ + if(state == ReadThread::play) + { + this->setWindowTitle(QString("正在播放:%1").arg(m_readThread->url())); + ui->but_open->setText("停止播放"); + } + else + { + ui->but_open->setText("开始播放"); + ui->but_pause->setText("暂停"); + this->setWindowTitle(QString("Qt+ffmpeg视频播放(软/硬解码 + OpenGL显示YUV/NV12)Demo V%1").arg(APP_VERSION)); + } +} + +void Widget::on_check_HW_clicked(bool checked) +{ + m_readThread->setHWDecoder(checked); +} diff --git a/FFmpegDemo/VideoPlayHWGL/widget.h b/FFmpegDemo/VideoPlayHWGL/widget.h new file mode 100644 index 0000000..0a7b0ad --- /dev/null +++ b/FFmpegDemo/VideoPlayHWGL/widget.h @@ -0,0 +1,36 @@ +#ifndef WIDGET_H +#define WIDGET_H + +#include +#include "readthread.h" +#include "playimage.h" + +QT_BEGIN_NAMESPACE +namespace Ui { class Widget; } +QT_END_NAMESPACE + +class Widget : public QWidget +{ + Q_OBJECT +public: + Widget(QWidget *parent = nullptr); + ~Widget(); + +private slots: + void on_but_file_clicked(); + + void on_but_open_clicked(); + + void on_but_pause_clicked(); + + void on_playState(ReadThread::PlayState state); + + void on_check_HW_clicked(bool checked); + +private: + Ui::Widget *ui; + + PlayImage* playImage = nullptr; + ReadThread* m_readThread = nullptr; +}; +#endif // WIDGET_H diff --git a/FFmpegDemo/VideoPlayHWGL/widget.ui b/FFmpegDemo/VideoPlayHWGL/widget.ui new file mode 100644 index 0000000..b3189e0 --- /dev/null +++ b/FFmpegDemo/VideoPlayHWGL/widget.ui @@ -0,0 +1,150 @@ + + + Widget + + + + 0 + 0 + 800 + 600 + + + + Widget + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + + + true + + + QComboBox::InsertAtBottom + + + QComboBox::AdjustToMinimumContentsLengthWithIcon + + + false + + + + rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mp4 + + + + + rtmp://ns8.indexforce.com/home/mystream + + + + + rtmp://58.200.131.2:1935/livetv/cctv1 + + + + + http://vfx.mtime.cn/Video/2019/02/04/mp4/190204084208765161.mp4 + + + + + http://playertest.longtailvideo.com/adaptive/bipbop/gear4/prog_index.m3u8 + + + + + http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4 + + + + + http://vjs.zencdn.net/v/oceans.mp4 + + + + + https://media.w3.org/2010/05/sintel/trailer.mp4 + + + + + https://sf1-hscdn-tos.pstatp.com/obj/media-fe/xgplayer_doc_video/flv/xgplayer-demo-360p.flv + + + + + + + + + 0 + 0 + + + + 硬解码 + + + + + + + + 0 + 0 + + + + 选择 + + + + + + + + 0 + 0 + + + + 开始播放 + + + + + + + + 0 + 0 + + + + 暂停 + + + + + + + + + + -- GitLab