|
@@ -12,14 +12,57 @@ extern "C"
|
|
|
#include <libavutil/imgutils.h>
|
|
|
}
|
|
|
|
|
|
+/*=================================================================================================
|
|
|
+* @brief FFmpeg获取GPU硬件解码帧格式的回调函数
|
|
|
+===================================================================================================*/
|
|
|
+static enum AVPixelFormat g_hw_pix_fmt;
|
|
|
|
|
|
+/**
|
|
|
+ * @brief 回调函数,获取GPU硬件解码帧的格式
|
|
|
+ *
|
|
|
+ * @param ctx
|
|
|
+ * @param pix_fmts
|
|
|
+ * @return AVPixelFormat
|
|
|
+ */
|
|
|
+AVPixelFormat get_hw_format(AVCodecContext *ctx, const AVPixelFormat *pix_fmts)
|
|
|
+{
|
|
|
+ Q_UNUSED(ctx)
|
|
|
+ const AVPixelFormat* p;
|
|
|
+ for(p = pix_fmts; *p != -1; p++)
|
|
|
+ {
|
|
|
+ if(*p == g_hw_pix_fmt)
|
|
|
+ {
|
|
|
+ return *p;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ SPDLOG_WARN("无法获取硬件解码器表面格式。");
|
|
|
+ return AV_PIX_FMT_NONE;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * @brief Construct a new Decode Vedio:: Decode Vedio object
|
|
|
+ *
|
|
|
+ * @param thread
|
|
|
+ * @param parent
|
|
|
+ */
|
|
|
DecodeVedio::DecodeVedio(QThread* thread, QObject* parent) : QObject(parent) , m_thread(thread)
|
|
|
{
|
|
|
/* 在连接之前调用,移动到新的线程 */
|
|
|
this->moveToThread(thread);
|
|
|
connect(this, &DecodeVedio::signal_startDecode, this, &DecodeVedio::do_startDecodeVedio);
|
|
|
thread->start();
|
|
|
- getHWDecoder();
|
|
|
+ findHWDecoder();
|
|
|
+ // if(m_supportHWDecoder)
|
|
|
+ // {
|
|
|
+ // SPDLOG_DEBUG("支持的硬件解码器:");
|
|
|
+ // for(auto it : m_listDecoderName)
|
|
|
+ // {
|
|
|
+ // SPDLOG_DEBUG("{}", it.toStdString());
|
|
|
+ // }
|
|
|
+ // }else {
|
|
|
+ // SPDLOG_WARN("未找到可用的硬件解码器。");
|
|
|
+ // }
|
|
|
}
|
|
|
|
|
|
DecodeVedio::~DecodeVedio()
|
|
@@ -36,34 +79,7 @@ DecodeVedio::~DecodeVedio()
|
|
|
|
|
|
}
|
|
|
|
|
|
-/* 获取硬件解码器 */
|
|
|
-void DecodeVedio::getHWDecoder()
|
|
|
-{
|
|
|
- AVHWDeviceType type = AV_HWDEVICE_TYPE_NONE;
|
|
|
- QStringList strTypes;
|
|
|
- while( (type = av_hwdevice_iterate_types(type)) != AV_HWDEVICE_TYPE_NONE)
|
|
|
- {
|
|
|
- m_listHWDeviceType.append(type);
|
|
|
- /* 获取硬件解码器的名称 */
|
|
|
- const char* typeName = av_hwdevice_get_type_name(type);
|
|
|
- if(typeName)
|
|
|
- {
|
|
|
- strTypes.append(QString(typeName));
|
|
|
- }
|
|
|
- }
|
|
|
- FMTLOG_INFO("支持的硬件解码器:");
|
|
|
- for(auto type : strTypes)
|
|
|
- {
|
|
|
- FMTLOG_INFO_NON(" {}",type.toStdString());
|
|
|
- }
|
|
|
- FMTLOG_INFO_NON("\n");
|
|
|
- if(m_listHWDeviceType.isEmpty())
|
|
|
- {
|
|
|
- FMTLOG_WARN("没有找到硬件解码器...");
|
|
|
- }else {
|
|
|
- m_HWDecoder = true;
|
|
|
- }
|
|
|
-}
|
|
|
+
|
|
|
|
|
|
/* 开始解码视频 */
|
|
|
void DecodeVedio::startDecodeVedio()
|
|
@@ -215,10 +231,114 @@ QImage* DecodeVedio::getOneImageUntilHave(int timeOut)
|
|
|
return nullptr;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+/* 查找硬件解码器 */
|
|
|
+void DecodeVedio::findHWDecoder(QStringList& listDecoderName)
|
|
|
+{
|
|
|
+ AVHWDeviceType type = AV_HWDEVICE_TYPE_NONE;
|
|
|
+ listDecoderName.clear();
|
|
|
+ while( (type = av_hwdevice_iterate_types(type)) != AV_HWDEVICE_TYPE_NONE)
|
|
|
+ {
|
|
|
+ /* 获取硬件解码器的名称 */
|
|
|
+ const char* typeName = av_hwdevice_get_type_name(type);
|
|
|
+ if(typeName)
|
|
|
+ {
|
|
|
+ listDecoderName.append(QString(typeName));
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+/* 获取硬件解码器 */
|
|
|
+void DecodeVedio::findHWDecoder()
|
|
|
+{
|
|
|
+ AVHWDeviceType type = AV_HWDEVICE_TYPE_NONE;
|
|
|
+ // QStringList strTypes;
|
|
|
+ m_listDecoderName.clear();
|
|
|
+ while( (type = av_hwdevice_iterate_types(type)) != AV_HWDEVICE_TYPE_NONE)
|
|
|
+ {
|
|
|
+ m_listHWDeviceType.append(type);
|
|
|
+ /* 获取硬件解码器的名称 */
|
|
|
+ const char* typeName = av_hwdevice_get_type_name(type);
|
|
|
+ if(typeName)
|
|
|
+ {
|
|
|
+ m_listDecoderName.append(QString(typeName));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(m_listHWDeviceType.isEmpty())
|
|
|
+ {
|
|
|
+ m_supportHWDecoder = false;
|
|
|
+ }else {
|
|
|
+ m_supportHWDecoder = true;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
/* 初始化硬件解码器 */
|
|
|
void DecodeVedio::initHWDecoder(const AVCodec* codec)
|
|
|
{
|
|
|
+ if(codec == nullptr)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ for(int i = 0;;i++)
|
|
|
+ {
|
|
|
+ /* 获取编解码器支持的硬件配置 */
|
|
|
+ const AVCodecHWConfig* hwConfig = avcodec_get_hw_config(codec, 0);
|
|
|
+ if(hwConfig == nullptr)
|
|
|
+ {
|
|
|
+ SPDLOG_WARN("没有找到支持{}的硬件配置", codec->name);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ /* 判断是否是设备类型 */
|
|
|
+ if(hwConfig->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX)
|
|
|
+ {
|
|
|
+ /* 在已有的解码器列表中查找 */
|
|
|
+ for(auto i : m_listHWDeviceType)
|
|
|
+ {
|
|
|
+ if(hwConfig->device_type == AVHWDeviceType(i))
|
|
|
+ {
|
|
|
+ /* 获取像素格式 */
|
|
|
+ g_hw_pix_fmt = hwConfig->pix_fmt;
|
|
|
+
|
|
|
+ /* 打开指定类型的设备,并为其创建AVHWDeviceContext */
|
|
|
+ int ret = av_hwdevice_ctx_create(&m_hw_device_ctx, hwConfig->device_type, nullptr, nullptr, 0);
|
|
|
+ if(ret < 0)
|
|
|
+ {
|
|
|
+ SPDLOG_ERROR("打开硬件解码器失败!");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ SPDLOG_INFO("打开硬件解码器:{}", av_hwdevice_get_type_name(hwConfig->device_type));
|
|
|
+ m_pCodecContext->hw_device_ctx = av_buffer_ref(m_hw_device_ctx);
|
|
|
+ m_pCodecContext->get_format = get_hw_format;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
|
|
|
+
|
|
|
+/* 拷贝数据,从GPU显存拷贝到内存中 */
|
|
|
+bool DecodeVedio::copyDataFromGPU(AVFrame* pFrameHW, AVFrame* pFrameSRC)
|
|
|
+{
|
|
|
+ if(m_pFrameSRC->format != g_hw_pix_fmt)
|
|
|
+ {
|
|
|
+ av_frame_unref(m_pFrameSRC);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ /* 这一步可能会耗费较长时间 */
|
|
|
+ int ret = av_hwframe_transfer_data(pFrameHW, pFrameSRC, 0);
|
|
|
+ if(ret < 0)
|
|
|
+ {
|
|
|
+ SPDLOG_ERROR("从GPU拷贝数据失败!");
|
|
|
+ av_frame_unref(pFrameSRC);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ av_frame_copy_props(pFrameHW, pFrameSRC);
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
|
|
@@ -252,17 +372,25 @@ void DecodeVedio::openVedio(const QString& fileName)
|
|
|
m_queueImage.setQueueCapacity(30);
|
|
|
|
|
|
SPDLOG_DEBUG("开始初始化FFMPEG");
|
|
|
- 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 超时,如果等待时间过短,也可能会还没连接就返回了。
|
|
|
+ /* 设置网络缓冲区大小和错误恢复选项 */
|
|
|
+ AVDictionary *options = nullptr;
|
|
|
+ /* 设置接收缓冲区 */
|
|
|
+ av_dict_set(&options, "buffer_size", "1024000", 0);
|
|
|
+ /* 设置最大复用或解复用延迟(以微秒为单位)。当通过【UDP】 接收数据时,解复用器尝试重新排序接收到的数据包
|
|
|
+ * (因为它们可能无序到达,或者数据包可能完全丢失)。这可以通过将最大解复用延迟设置为零
|
|
|
+ * (通过max_delayAVFormatContext 字段)来禁用。 */
|
|
|
+ av_dict_set(&options, "max_delay", "500000", 0);
|
|
|
+ /* 设置rtsp流使用tcp打开,如果打开失败错误信息为【Error number -135 occurred】可以切换(UDP、tcp、udp_multicast、http),比如vlc推流就需要使用udp打开 */
|
|
|
+ av_dict_set(&options, "rtsp_transport", "tcp", 0);
|
|
|
+ /* 以微秒为单位设置套接字 TCP I/O 超时,如果等待时间过短,也可能会还没连接就返回了。 */
|
|
|
+ av_dict_set(&options, "stimeout", "20000000", 0);
|
|
|
|
|
|
/************ 存储文件格式信息 ************/
|
|
|
/* 打开文件,读取视频文件的头信息,放在第一个参数的结构体中 */
|
|
|
int ret = avformat_open_input( &m_pFormatContext, /* 解封装上下文 */
|
|
|
m_fileName.toStdString().data(), /* 打开视频地址 */
|
|
|
nullptr, /* 这个参数强制使用特定的输入格式,可以设置为null */
|
|
|
- &dict); /* 参数设置 */
|
|
|
+ &options); /* 参数设置 */
|
|
|
if(ret != 0)
|
|
|
{
|
|
|
SPDLOG_WARN("打开视频文件错误,错误代码:{}",ret);
|
|
@@ -270,9 +398,9 @@ void DecodeVedio::openVedio(const QString& fileName)
|
|
|
return;
|
|
|
}
|
|
|
/* 释放参数 */
|
|
|
- if(dict != nullptr)
|
|
|
+ if(options != nullptr)
|
|
|
{
|
|
|
- av_dict_free(&dict);
|
|
|
+ av_dict_free(&options);
|
|
|
}
|
|
|
|
|
|
/************ 找到视频流 ************/
|
|
@@ -301,11 +429,11 @@ void DecodeVedio::openVedio(const QString& fileName)
|
|
|
freeAll();
|
|
|
return;
|
|
|
}
|
|
|
- SPDLOG_INFO("找到视频流");
|
|
|
+ SPDLOG_DEBUG("找到视频流");
|
|
|
/* 获取视频流的编解码信息,主要是分辨率等信息 */
|
|
|
AVStream *pStream = m_pFormatContext->streams[m_videoStream]; /* 获取视频流 */
|
|
|
AVCodecParameters* pCodecParams = pStream->codecpar; /* 获取视频流的编解码信息 */
|
|
|
- SPDLOG_INFO("获取视频流参数成功!");
|
|
|
+ SPDLOG_DEBUG("获取视频流参数成功!");
|
|
|
/* 获取视频相关信息 */
|
|
|
m_srcSize.setWidth(pCodecParams->width);
|
|
|
m_srcSize.setHeight(pCodecParams->height);
|
|
@@ -333,12 +461,12 @@ void DecodeVedio::openVedio(const QString& fileName)
|
|
|
freeAll();
|
|
|
return;
|
|
|
}
|
|
|
- SPDLOG_INFO("设置解码器参数成功!");
|
|
|
+ SPDLOG_DEBUG("设置解码器参数成功!");
|
|
|
// m_pCodecContext->flags2 |= AV_CODEC_FLAG2_FAST; /* 使用快速解码(允许使用不符合规范的解码) */
|
|
|
m_pCodecContext->thread_count = 8; /* 使用8线程解码 */
|
|
|
|
|
|
/* 初始化硬件解码器 */
|
|
|
- if(m_HWDecoder)
|
|
|
+ if(m_supportHWDecoder)
|
|
|
{
|
|
|
initHWDecoder(pCodec);
|
|
|
}
|
|
@@ -364,13 +492,13 @@ void DecodeVedio::openVedio(const QString& fileName)
|
|
|
freeAll();
|
|
|
return;
|
|
|
}
|
|
|
- m_pFrameRGB = av_frame_alloc();
|
|
|
- if(m_pFrameRGB == nullptr)
|
|
|
- {
|
|
|
- SPDLOG_ERROR("创建pFrameRGB错误");
|
|
|
- freeAll();
|
|
|
- return;
|
|
|
- }
|
|
|
+ // m_pFrameRGB = av_frame_alloc();
|
|
|
+ // if(m_pFrameRGB == nullptr)
|
|
|
+ // {
|
|
|
+ // SPDLOG_ERROR("创建pFrameRGB错误");
|
|
|
+ // freeAll();
|
|
|
+ // return;
|
|
|
+ // }
|
|
|
m_pFrameHW = av_frame_alloc();
|
|
|
if(m_pFrameHW == nullptr)
|
|
|
{
|
|
@@ -553,7 +681,7 @@ void DecodeVedio::threadDecodeUsingCPU()
|
|
|
SPDLOG_INFO("创建SwsContext成功...");
|
|
|
}
|
|
|
/* 转换成RGBA格式 */
|
|
|
- uint8_t* data[1] = { m_buffer };
|
|
|
+ // uint8_t* data[1] = { m_buffer };
|
|
|
int lines[4];
|
|
|
/* 使用像素格式pix_fmt和宽度填充图像的平面线条的大小(一行大小?) */
|
|
|
av_image_fill_linesizes(lines, AV_PIX_FMT_RGBA, m_pFrameSRC->width);
|
|
@@ -563,7 +691,7 @@ void DecodeVedio::threadDecodeUsingCPU()
|
|
|
m_pFrameSRC->linesize, /* 包含源图像每个平面步幅的数组 */
|
|
|
0, /* 开始位置 */
|
|
|
m_pFrameSRC->height, /* 行数 */
|
|
|
- data, /* 目标图像数组 */
|
|
|
+ &m_buffer, /* 目标图像数组 */
|
|
|
lines); /* 目标图像行数 */
|
|
|
if(m_buffer != nullptr)
|
|
|
{
|
|
@@ -571,7 +699,7 @@ void DecodeVedio::threadDecodeUsingCPU()
|
|
|
auto image = new QImage(m_buffer, m_srcSize.width(), m_srcSize.height(), QImage::Format_RGBA8888);
|
|
|
/* 如果队列满,线程会阻塞在这里 */
|
|
|
m_queueImage.push(image);
|
|
|
- av_frame_unref(m_pFrameRGB);
|
|
|
+ // av_frame_unref(m_pFrameRGB);
|
|
|
// SPDLOG_DEBUG("一帧视频入队");
|
|
|
}
|
|
|
av_frame_unref(m_pFrameSRC);
|
|
@@ -615,6 +743,206 @@ void DecodeVedio::exitThread()
|
|
|
m_queueImage.exit();
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ * @brief 硬件解码线程,使用GPU解码,使用环境队列存储解码的数据
|
|
|
+ *
|
|
|
+ */
|
|
|
+void DecodeVedio::threadDecodeUsingGPU()
|
|
|
+{
|
|
|
+ /******** 初始化局部变量 ********/
|
|
|
+ bool isEnd = false;
|
|
|
+ int ret = 0;
|
|
|
+ int retFrame = 0;
|
|
|
+ int retPacket = 0;
|
|
|
+ m_pauseDecode = false;
|
|
|
+ m_decodeStatus = true;
|
|
|
+ m_decodeState.store(DecodeState::DecodeRun);
|
|
|
+
|
|
|
+ /* 开始解码 */
|
|
|
+ SPDLOG_DEBUG("开始解码...");
|
|
|
+ while(m_threadRuning)
|
|
|
+ {
|
|
|
+ /******** 判断是否在暂停状态 ********/
|
|
|
+ while(m_pauseDecode)
|
|
|
+ {
|
|
|
+ m_decodeState.store(DecodeState::DecodePause);
|
|
|
+ std::this_thread::sleep_for(std::chrono::microseconds(100));
|
|
|
+ }
|
|
|
+ m_decodeState.store(DecodeState::DecodeRun);
|
|
|
+ /* 刷新解码器缓冲区,清除掉里面残留的解码文件 */
|
|
|
+ if(m_flushDecoder.load())
|
|
|
+ {
|
|
|
+ avcodec_flush_buffers(m_pCodecContext);
|
|
|
+ m_flushDecoder.store(false);
|
|
|
+ }
|
|
|
+ /******** 读取数据包 av_read_frame ********/
|
|
|
+ int retRead = av_read_frame(m_pFormatContext, m_packet);
|
|
|
+ if(retRead == AVERROR_EOF)
|
|
|
+ {
|
|
|
+ /* 读取到末尾后,需要传入一个空的packet,才能读取到最后几帧 */
|
|
|
+ avcodec_send_packet(m_pCodecContext, m_packet);
|
|
|
+ }
|
|
|
+ else if(retRead < 0)
|
|
|
+ {
|
|
|
+ SPDLOG_ERROR("读取帧错误...");
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ if(m_packet->stream_index == m_videoStream)
|
|
|
+ {
|
|
|
+ // SPDLOG_DEBUG("源pts:{}", m_packet->pts);
|
|
|
+ /* pts 表示显示时间戳(Presentation Timestamp),它指示解码后的帧应该在什么时候显示。pts 是用于同步音视频的关键时间戳。
|
|
|
+ dts 表示解码时间戳(Decoding Timestamp),它指示解码器应该在什么时候解码这个数据包。dts 用于确保解码器按照正确的顺序解码数据包。 */
|
|
|
+ #if 1
|
|
|
+ /* 计算当前帧时间,两种方法,第一种适用于所有场景,但是存在一定的误差 */
|
|
|
+ m_packet->pts = qRound64(m_packet->pts * (1000 * rationalToDouble(&m_pFormatContext->streams[m_videoStream]->time_base)));
|
|
|
+ m_packet->dts = qRound64(m_packet->dts * (1000 * rationalToDouble(&m_pFormatContext->streams[m_videoStream]->time_base)));
|
|
|
+ #else
|
|
|
+ /* 适用于本地视频,直接计算本地视频文件的每一帧时间 */
|
|
|
+ m_currentFrame++;
|
|
|
+ // m_packet->pts = qRound64(m_currentFrame * (qreal(m_duration) / m_totalFrame));
|
|
|
+ #endif
|
|
|
+
|
|
|
+ /* 将数据传给解码器 */
|
|
|
+ int ret = avcodec_send_packet(m_pCodecContext, m_packet);
|
|
|
+ if(ret < 0)
|
|
|
+ {
|
|
|
+ SPDLOG_ERROR("发送数据包错误...");
|
|
|
+ av_packet_unref(m_packet);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ // SPDLOG_INFO("不是视频流。");
|
|
|
+ av_packet_unref(m_packet);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // SPDLOG_DEBUG("读取到数据包packet,pts:{}",m_packet->pts);
|
|
|
+ /* 解码packet包的内容,一个packet包内可能包含好多帧视频 */
|
|
|
+ while(m_threadRuning)
|
|
|
+ {
|
|
|
+ /* 读取出解码器返回的帧 avcodec_receive_frame */
|
|
|
+ int ret = avcodec_receive_frame(m_pCodecContext, m_pFrameSRC);
|
|
|
+ if(ret == AVERROR_EOF)
|
|
|
+ {
|
|
|
+ SPDLOG_INFO("读取到视频流末尾。");
|
|
|
+ isEnd = true;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ else if (ret == AVERROR(EAGAIN))
|
|
|
+ {
|
|
|
+ /* packet中的内容无法解码成一帧,需要更多的输入包,可能已经取出了几帧,也肯能就不够一帧
|
|
|
+ * 这时候就需要往解码器送数据包了 */
|
|
|
+ // SPDLOG_WARN("packet无法解码成一帧,需要更多的输入包");
|
|
|
+ av_frame_unref(m_pFrameSRC);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ else if(ret < 0)
|
|
|
+ {
|
|
|
+ av_frame_unref(m_pFrameSRC);
|
|
|
+ if(retRead < 0)
|
|
|
+ {
|
|
|
+ SPDLOG_ERROR("读取错误,错误值:{}",ret);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /* 硬件解码,上面读取到的帧m_pFrameSRC->data[0] = nullptr
|
|
|
+ * 数据要从GPU中拷贝出来 */
|
|
|
+ if(!copyDataFromGPU(m_pFrameHW, m_pFrameSRC))
|
|
|
+ {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* 解码成功,获取当前时间,现在已经是准的时间了
|
|
|
+ * 如果在跳转状态,在这里判断是否到了目标位置 */
|
|
|
+ m_pts = m_pFrameHW->pts;
|
|
|
+ if(m_isSeek.load())
|
|
|
+ {
|
|
|
+ if(m_pts < m_targetPos)
|
|
|
+ {
|
|
|
+ SPDLOG_DEBUG("目标位置:{} 当前位置:{}",m_targetPos, m_pts.load());
|
|
|
+ av_frame_unref(m_pFrameHW);
|
|
|
+ continue;
|
|
|
+ }else {
|
|
|
+ m_isSeek = false;
|
|
|
+ m_targetPos = -1;
|
|
|
+ SPDLOG_INFO("跳转结束。");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // SPDLOG_DEBUG("当前帧的pts:{}", m_pts.load());
|
|
|
+
|
|
|
+ /* 转换解码后的帧格式,转换成RGBA格式,Qt可以识别 */
|
|
|
+ if(m_sws_ctx == nullptr)
|
|
|
+ {
|
|
|
+ /* 选择在这里创建,为了兼容硬件解码,硬件解码出来的格式可能和解码器传出的不一样 */
|
|
|
+ m_sws_ctx = sws_getCachedContext(m_sws_ctx,
|
|
|
+ m_pFrameHW->width, m_pFrameHW->height, /* 原图像大小和格式 */
|
|
|
+ m_pCodecContext->pix_fmt, /* 输入图像的像素格式 */
|
|
|
+ m_srcSize.width(), m_srcSize.height(), /* 目标图像的大小 */
|
|
|
+ AV_PIX_FMT_RGBA, /* 目标图像的格式 */
|
|
|
+ SWS_BILINEAR, /* 图像缩放算法,双线性 */
|
|
|
+ nullptr, /* 输入图像的滤波器信息,不需要传NULL */
|
|
|
+ nullptr, /* 输出图像的滤波器信息,不需要传NULL */
|
|
|
+ nullptr); /* 特定缩放算法需要的参数,不需要传NULL */
|
|
|
+ if(m_sws_ctx == nullptr)
|
|
|
+ {
|
|
|
+ SPDLOG_ERROR("创建SwsContext错误...");
|
|
|
+ goto label_ThreadDecodeExit;
|
|
|
+ }
|
|
|
+ SPDLOG_INFO("创建SwsContext成功...");
|
|
|
+ }
|
|
|
+ /* 转换成RGBA格式 */
|
|
|
+ // uint8_t* data[1] = { m_buffer };
|
|
|
+ int lines[4];
|
|
|
+ /* 使用像素格式pix_fmt和宽度填充图像的平面线条的大小(一行大小?) */
|
|
|
+ av_image_fill_linesizes(lines, AV_PIX_FMT_RGBA, m_pFrameHW->width);
|
|
|
+
|
|
|
+ sws_scale( m_sws_ctx, /* 缩放的上下文 */
|
|
|
+ m_pFrameHW->data, /* 源图像数组 */
|
|
|
+ m_pFrameHW->linesize, /* 包含源图像每个平面步幅的数组 */
|
|
|
+ 0, /* 开始位置 */
|
|
|
+ m_pFrameHW->height, /* 行数 */
|
|
|
+ &m_buffer, /* 目标图像数组 */
|
|
|
+ lines); /* 目标图像行数 */
|
|
|
+ if(m_buffer != nullptr)
|
|
|
+ {
|
|
|
+ /* 将数据拷贝到QImage中 */
|
|
|
+ auto image = new QImage(m_buffer, m_srcSize.width(), m_srcSize.height(), QImage::Format_RGBA8888);
|
|
|
+ /* 如果队列满,线程会阻塞在这里 */
|
|
|
+ m_queueImage.push(image);
|
|
|
+ // av_frame_unref(m_pFrameRGB);
|
|
|
+ // SPDLOG_DEBUG("一帧视频入队");
|
|
|
+ }
|
|
|
+ av_frame_unref(m_pFrameSRC);
|
|
|
+ /* 如果在跳转过程中,直接退出,防止一个packet中有多个视频帧,再次阻塞在上面 */
|
|
|
+ if(m_isSeek)
|
|
|
+ {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ av_packet_unref(m_packet); /* 释放数据包,引用计数-1,为0时释放空间 */
|
|
|
+ if(isEnd)
|
|
|
+ {
|
|
|
+ emit signal_playCompleted();
|
|
|
+ m_decodeState.store(DecodeState::DecodeStop);
|
|
|
+ /* 读取到结尾,但是不退出解码线程,可能还会使用倒退功能,后退读取 */
|
|
|
+ while(m_decodeState.load() != DecodeState::DecodeRun)
|
|
|
+ {
|
|
|
+ std::this_thread::sleep_for(std::chrono::milliseconds(2));
|
|
|
+ }
|
|
|
+ isEnd = false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+label_ThreadDecodeExit:
|
|
|
+ /* 释放空间 */
|
|
|
+ av_packet_free(&m_packet);
|
|
|
+ m_decodeState.store(DecodeState::DecodeExit);
|
|
|
+ m_threadRuning = false;
|
|
|
+}
|
|
|
+
|
|
|
/* 暂停解码,会阻塞到线程暂停为止 */
|
|
|
void DecodeVedio::pauseDecode()
|
|
|
{
|
|
@@ -694,10 +1022,10 @@ void DecodeVedio::freeAll()
|
|
|
sws_freeContext(m_sws_ctx);
|
|
|
m_sws_ctx = nullptr;
|
|
|
}
|
|
|
- if(m_pFrameRGB)
|
|
|
- {
|
|
|
- av_frame_free(&m_pFrameRGB);
|
|
|
- }
|
|
|
+ // if(m_pFrameRGB)
|
|
|
+ // {
|
|
|
+ // av_frame_free(&m_pFrameRGB);
|
|
|
+ // }
|
|
|
if(m_pFrameSRC)
|
|
|
{
|
|
|
av_frame_free(&m_pFrameSRC);
|