1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069 |
- #include "DecodeVedio.h"
- #include "spdlog/spdlog.h"
- #include "FmtLog/fmtlog.h"
- #include <QThread>
- extern "C"
- {
- #include <libavcodec/avcodec.h>
- #include <libavformat/avformat.h>
- #include <libswscale/swscale.h>
- #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();
- findHWDecoder();
- // if(m_supportHWDecoder)
- // {
- // SPDLOG_DEBUG("支持的硬件解码器:");
- // for(auto it : m_listDecoderName)
- // {
- // SPDLOG_DEBUG("{}", it.toStdString());
- // }
- // }else {
- // SPDLOG_WARN("未找到可用的硬件解码器。");
- // }
- }
- DecodeVedio::~DecodeVedio()
- {
- stopDecodeVedio();
- if(m_thread != nullptr)
- {
- if(m_thread->isRunning())
- {
- m_thread->quit();
- m_thread->wait();
- }
- }
- }
- /* 开始解码视频 */
- void DecodeVedio::startDecodeVedio()
- {
- if(m_threadRuning)
- {
- return;
- }
- if(!m_initFFmpeg)
- {
- SPDLOG_WARN("未初始化FFMPEG...");
- return;
- }
- m_threadRuning = true;
- // decodeUsingCPU();
- /* 发送信号,开启新线程 */
- emit signal_startDecode();
- }
- /* 停止解码视频,退出工作函数,线程未停止 */
- void DecodeVedio::stopDecodeVedio()
- {
- if(!m_threadRuning)
- {
- return;
- }
- exitThread();
- /* 唤醒阻塞住的解码线程 */
- // /* 等待线程执行结束 */
- while(m_decodeState.load() != DecodeState::DecodeExit)
- {
- std::this_thread::sleep_for(std::chrono::milliseconds(5));
- }
- freeAll();
- m_threadRuning = false;
- }
- /**
- * @brief 设置当前播放位置,单位是毫秒
- * 这里需要去掉队列中已有的图片数目对应的时长
- *
- * @param pos 要跳转的位置,范围从0~duration
- */
- void DecodeVedio::setCurrentPos(qint64 pos)
- {
- if(!m_threadRuning)
- {
- return;
- }
- m_isSeek = true;
- /* 先暂停解码 */
- pauseDecode();
-
- SPDLOG_DEBUG("跳转到:{}ms",pos);
- /*在环形队列中有已解码的视频帧数,需要去掉已有的帧数所占的时间 */
- pos = pos - m_queueImage.QueueSize() * (1000 / m_fps);
- if(pos < 0) {
- pos = 0;
- }
- if(pos > m_duration) {
- pos = m_duration;
- }
- pos = pos + m_startPos;
- qint64 targetPos = qRound64((double)pos / (1000 * rationalToDouble(&m_pFormatContext->streams[m_videoStream]->time_base)));
- /* 开始跳转,这里设置标志为AVSEEK_FLAG_BACKWARD,跳转到目标位置的前一个关键帧中,然后开始解码,直到到达目标位置为止 */
- int ret = av_seek_frame(m_pFormatContext, m_videoStream, targetPos, AVSEEK_FLAG_BACKWARD);
- if(ret < 0)
- {
- SPDLOG_ERROR("跳转失败!");
- }
- m_targetPos = pos;
- /* 刷新解码器缓冲区 */
- m_flushDecoder.store(true);
- /* 清空环形队列中的视频 */
- SPDLOG_DEBUG("清空环形队列中的视频。");
- QImage* image = 0;
- while (m_queueImage.QueueSize() > 0)
- {
- image = nullptr;
- m_queueImage.front_pop_NoBlock(image);
- if(image != nullptr)
- {
- delete image;
- }
- }
-
- /* 继续解码 */
- continueDecode();
- }
- /* 获取当前播放位置,单位ms */
- qint64 DecodeVedio::getCurrentPos()
- {
- return m_pts.load() - m_startPos;
- }
- /* 获取视频时长 */
- qint64 DecodeVedio::getDuration()
- {
- return m_duration;
- }
- /**
- * @brief 获取一帧图像,队列为空就返回nullptr,这个函数应该是运行在UI线程中的
- * @warning 传出这个指针后,队列就出队了,内存需要外面获取的实例释放
- * @return QImage* 一帧图像的指针
- */
- QImage* DecodeVedio::getOneImage()
- {
- if(!m_threadRuning)
- {
- return nullptr;
- }
- QImage* image = nullptr;
- if(!m_queueImage.front_pop_NoBlock(image))
- {
- return nullptr;
- }
- return image;
- }
- /**
- * @brief 获取一帧图像,直到有图像为止
- *
- * @param timeOut 设为-1是一直等待,设置正整数是等待的时间,单位ms
- * @return QImage*
- */
- QImage* DecodeVedio::getOneImageUntilHave(int timeOut)
- {
- if(!m_threadRuning)
- {
- return nullptr;
- }
- if(timeOut < 0)
- {
- QImage* image = m_queueImage.front_pop();
- return image;
- }
- for(int i = 0; i < timeOut; i++)
- {
- QImage* image = nullptr;
- if(m_queueImage.front_pop_NoBlock(image))
- {
- return image;
- }
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- }
- 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;
- }
- /* 打开视频,同时初始化解码器) */
- void DecodeVedio::openVedio(const QString& fileName)
- {
- if(fileName.isEmpty())
- {
- SPDLOG_WARN("文件名为空...");
- return;
- }
- m_fileName = fileName;
- if(m_initFFmpeg)
- {
- freeAll();
- }
- /* 清空队列 */
- if(m_queueImage.QueueSize() > 0)
- {
- for(int i = 0; i < m_queueImage.QueueSize(); i++)
- {
- QImage* image = nullptr;
- if (m_queueImage.front_pop_NoBlock(image))
- {
- delete image;
- }
- }
- m_queueImage.clearQueue();
- }
- m_queueImage.setQueueCapacity(30);
- SPDLOG_DEBUG("开始初始化FFMPEG");
- /* 设置网络缓冲区大小和错误恢复选项 */
- 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 */
- &options); /* 参数设置 */
- if(ret != 0)
- {
- SPDLOG_WARN("打开视频文件错误,错误代码:{}",ret);
- freeAll();
- return;
- }
- /* 释放参数 */
- if(options != nullptr)
- {
- av_dict_free(&options);
- }
- /************ 找到视频流 ************/
- /* 检查视频容器内部的流信息,将所有流存储到了pFormatContext->streams中
- * 查找到视频流,并获取视频时长相关的信息 */
- ret = 0;
- ret = avformat_find_stream_info(m_pFormatContext, nullptr);
- if(ret < 0)
- {
- SPDLOG_WARN("获取视频流错误,错误代码:{}",ret);
- freeAll();
- return;
- }
- m_duration = m_pFormatContext->duration / (AV_TIME_BASE / 1000); /* 获取视频时长,单位是毫秒 */
- m_startPos = m_pFormatContext->start_time / (AV_TIME_BASE / 1000); /* 获取视频开始时间,单位是毫秒 */
- // SPDLOG_DEBUG("开始时间:{} 时长:{}",m_startPos, m_duration);
-
- /* 一个调试函数,将流信息输出到控制台 */
- av_dump_format(m_pFormatContext, 0, m_fileName.toStdString().c_str(), 0);
- /* 找到视频流 */
- m_videoStream = av_find_best_stream(m_pFormatContext, AVMEDIA_TYPE_VIDEO, -1, -1, nullptr, 0);
- if(m_videoStream < 0)
- {
- SPDLOG_WARN("没有找到视频流");
- freeAll();
- return;
- }
- SPDLOG_DEBUG("找到视频流");
- /* 获取视频流的编解码信息,主要是分辨率等信息 */
- AVStream *pStream = m_pFormatContext->streams[m_videoStream]; /* 获取视频流 */
- AVCodecParameters* pCodecParams = pStream->codecpar; /* 获取视频流的编解码信息 */
- SPDLOG_DEBUG("获取视频流参数成功!");
- /* 获取视频相关信息 */
- m_srcSize.setWidth(pCodecParams->width);
- m_srcSize.setHeight(pCodecParams->height);
- m_fps = rationalToDouble(&pStream->avg_frame_rate);
- m_totalFrame = m_pFormatContext->streams[m_videoStream]->nb_frames;
- m_pts.store(0);
- /************ 查找并设置解码器 ************/
- /* 找到解码器 */
- const AVCodec* pCodec = avcodec_find_decoder(pCodecParams->codec_id);
- if(pCodec == nullptr)
- {
- SPDLOG_WARN("没有找到解码器");
- freeAll();
- return;
- }
- m_decoderName = pCodec->name;
- // SPDLOG_INFO("找到解码器:{}",pCodec->name);
- /* 获取视频信息的上下文,先分配空间,后面记得释放空间 */
- m_pCodecContext = avcodec_alloc_context3(pCodec);
- /* 将视频流中的编码器参数拷贝下来,这个函数不是线程安全的 */
- if(avcodec_parameters_to_context(m_pCodecContext, pCodecParams) != 0)
- {
- SPDLOG_WARN("复制上下文错误");
- freeAll();
- return;
- }
- SPDLOG_DEBUG("设置解码器参数成功!");
- // m_pCodecContext->flags2 |= AV_CODEC_FLAG2_FAST; /* 使用快速解码(允许使用不符合规范的解码) */
- m_pCodecContext->thread_count = 8; /* 使用8线程解码 */
- /* 初始化硬件解码器 */
- if(m_supportHWDecoder)
- {
- initHWDecoder(pCodec);
- }
- /* 打开解码器,(初始化解码器上下文,如果调用了avcodec_alloc_context3,第二个参数可以设置为nullptr) */
- if(avcodec_open2(m_pCodecContext, nullptr, nullptr) < 0)
- {
- SPDLOG_ERROR("打开解码器错误");
- freeAll();
- return;
- }
- SPDLOG_DEBUG("打开解码器成功!");
- /************ 初始化数据包 ************/
- m_packet = av_packet_alloc();
- av_new_packet(m_packet, m_pCodecContext->width * m_pCodecContext->height);
- /* 创建两个pFrame,一个存放原始数据,一个存放转换后的RGB数据 */
- m_pFrameSRC = av_frame_alloc();
- if(m_pFrameSRC == nullptr)
- {
- SPDLOG_ERROR("创建pFrame错误");
- 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)
- {
- SPDLOG_ERROR("创建pFrameHW错误");
- freeAll();
- return;
- }
- if(m_buffer != nullptr)
- {
- av_free(m_buffer);
- m_buffer = nullptr;
- }
-
- /* 分配图像空间 */
- int numBytes = av_image_get_buffer_size(AV_PIX_FMT_RGBA, m_pCodecContext->width, m_pCodecContext->height, 1);
- /* 这里多分配了一些空间,防止某些视频图像在使用sws_scale()拷贝后超出数组长度 */
- m_buffer = (uint8_t *)av_malloc(numBytes + 1000);
- m_initFFmpeg = true;
- SPDLOG_INFO("FFMPEG初始化完成!");
- // SPDLOG_INFO("视频宽度:{} 高度:{} 帧率:{} 总时长:{} 总帧数:{}",m_srcSize.width(), m_srcSize.height(), m_fps, m_duration, m_totalFrame);
- /* 再次判断帧数是否正常,如果没读取到,就使用 总帧数 / 时长 */
- if(m_fps == 0)
- {
- if((m_duration > 0) && (m_totalFrame > 0))
- {
- m_fps = m_totalFrame / (m_duration / 1000.0);
- }
- /* 到这一步,无法确定帧数了,就按照25帧来计算了 */
- if(m_fps == 0)
- {
- m_fps = 25;
- }
- }
- }
- /**
- * @brief 软解码线程,使用CPU解码,使用环境队列存储解码的数据
- *
- */
- void DecodeVedio::threadDecodeUsingCPU()
- {
- /******** 初始化局部变量 ********/
- 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_pts = m_pFrameSRC->pts;
- if(m_isSeek.load())
- {
- if(m_pts < m_targetPos)
- {
- SPDLOG_DEBUG("目标位置:{} 当前位置:{}",m_targetPos, m_pts.load());
- av_frame_unref(m_pFrameSRC);
- 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_pFrameSRC->width, m_pFrameSRC->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_pFrameSRC->width);
- sws_scale( m_sws_ctx, /* 缩放的上下文 */
- m_pFrameSRC->data, /* 源图像数组 */
- m_pFrameSRC->linesize, /* 包含源图像每个平面步幅的数组 */
- 0, /* 开始位置 */
- m_pFrameSRC->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::exitThread()
- {
- if(m_threadRuning)
- {
- m_threadRuning = false;
- }
- /* 设置成运行状态,唤醒可能阻塞在了解码结束的位置 */
- m_decodeState.store(DecodeState::DecodeRun);
- m_pauseDecode = false;
- /* 先退出可能阻塞住的解码线程 */
- 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()
- {
- if(!m_threadRuning)
- {
- return;
- }
- if( (m_decodeState.load() == DecodeState::DecodeExit)
- || (m_decodeState.load() == DecodeState::DecodePause) )
- {
- return;
- }
- /* 设置成运行状态,唤醒可能阻塞在了解码结束的位置 */
- m_decodeState.store(DecodeState::DecodeRun);
- m_pauseDecode = true;
- /* 队列出队两张图片,防止解码线程阻塞到环形队列满上面 */
- QImage* image = nullptr;
- m_queueImage.front_pop_NoBlock(image);
- if(image != nullptr)
- {
- delete image;
- image = nullptr;
- }
- m_queueImage.front_pop_NoBlock(image);
- if(image != nullptr)
- {
- delete image;
- image = nullptr;
- }
- /* 等待线程状态变为暂停为止 */
- while (m_decodeState.load() != DecodeState::DecodePause)
- {
- std::this_thread::sleep_for(std::chrono::microseconds(100));
- }
- }
- /* 继续解码 */
- void DecodeVedio::continueDecode()
- {
- m_pauseDecode = false;
- m_decodeState.store(DecodeState::DecodeRun);
- }
- /**
- * @brief AVRational存储的是分子和分母,这里相除转换为double,用于计算帧率
- * 这个函数就等同于av_q2d()
- * @param rational
- * @return
- */
- qreal DecodeVedio::rationalToDouble(AVRational* rational)
- {
- return ( (rational->den == 0) ? 0 : (qreal(rational->num) / rational->den) );
- }
- /* 开启解码 */
- void DecodeVedio::do_startDecodeVedio()
- {
- SPDLOG_DEBUG("解码线程ID:{}",QThread::currentThreadId());
- // if(!m_initFFmpeg)
- // {
- // initFFmpeg();
- // }
- m_threadRuning = true;
- m_pauseDecode = false;
- /* 进入解码,直到播放完成或者手动退出 */
- threadDecodeUsingCPU();
- SPDLOG_TRACE("Decode解码结束。");
- }
- /* 释放所有资源 */
- void DecodeVedio::freeAll()
- {
- if(m_sws_ctx)
- {
- sws_freeContext(m_sws_ctx);
- m_sws_ctx = nullptr;
- }
- // if(m_pFrameRGB)
- // {
- // av_frame_free(&m_pFrameRGB);
- // }
- if(m_pFrameSRC)
- {
- av_frame_free(&m_pFrameSRC);
- }
- if(m_pFrameHW)
- {
- av_frame_free(&m_pFrameHW);
- }
- if(m_packet)
- {
- av_packet_free(&m_packet);
- }
- if(m_pCodecContext)
- {
- avcodec_free_context(&m_pCodecContext);
- }
- if(m_pFormatContext)
- {
- avformat_close_input(&m_pFormatContext);
- }
- if(m_buffer)
- {
- av_free(m_buffer);
- m_buffer = nullptr;
- }
- if(m_hw_device_ctx)
- {
- av_buffer_unref(&m_hw_device_ctx);
- }
- for(int i = 0; i < m_queueImage.QueueSize(); i++)
- {
- QImage* image = nullptr;
- if (m_queueImage.front_pop_NoBlock(image))
- {
- delete image;
- }
- }
- m_queueImage.clearQueue();
- }
|