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>
- }
- static enum AVPixelFormat g_hw_pix_fmt;
- 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;
- }
- 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();
-
-
-
-
-
-
-
-
-
-
- }
- 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;
-
-
- 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;
- }
- 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)));
-
- 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();
- }
- qint64 DecodeVedio::getCurrentPos()
- {
- return m_pts.load() - m_startPos;
- }
- qint64 DecodeVedio::getDuration()
- {
- return m_duration;
- }
- QImage* DecodeVedio::getOneImage()
- {
- if(!m_threadRuning)
- {
- return nullptr;
- }
- QImage* image = nullptr;
- if(!m_queueImage.front_pop_NoBlock(image))
- {
- return nullptr;
- }
- return image;
- }
- 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;
-
- 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;
-
- 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;
- }
- }
- }
- }
-
- }
- 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);
-
- av_dict_set(&options, "max_delay", "500000", 0);
-
- av_dict_set(&options, "rtsp_transport", "tcp", 0);
-
- av_dict_set(&options, "stimeout", "20000000", 0);
-
-
- int ret = avformat_open_input( &m_pFormatContext,
- m_fileName.toStdString().data(),
- nullptr,
- &options);
- if(ret != 0)
- {
- SPDLOG_WARN("打开视频文件错误,错误代码:{}",ret);
- freeAll();
- return;
- }
-
- if(options != nullptr)
- {
- av_dict_free(&options);
- }
-
-
- 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);
-
-
-
- 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;
-
-
- m_pCodecContext = avcodec_alloc_context3(pCodec);
-
- if(avcodec_parameters_to_context(m_pCodecContext, pCodecParams) != 0)
- {
- SPDLOG_WARN("复制上下文错误");
- freeAll();
- return;
- }
- SPDLOG_DEBUG("设置解码器参数成功!");
-
- m_pCodecContext->thread_count = 8;
-
- if(m_supportHWDecoder)
- {
- initHWDecoder(pCodec);
- }
-
- 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);
-
- m_pFrameSRC = av_frame_alloc();
- if(m_pFrameSRC == nullptr)
- {
- SPDLOG_ERROR("创建pFrame错误");
- 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);
-
- m_buffer = (uint8_t *)av_malloc(numBytes + 1000);
- m_initFFmpeg = true;
- SPDLOG_INFO("FFMPEG初始化完成!");
-
-
- if(m_fps == 0)
- {
- if((m_duration > 0) && (m_totalFrame > 0))
- {
- m_fps = m_totalFrame / (m_duration / 1000.0);
- }
-
- if(m_fps == 0)
- {
- m_fps = 25;
- }
- }
- }
- 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);
- }
-
- int retRead = av_read_frame(m_pFormatContext, m_packet);
- if(retRead == AVERROR_EOF)
- {
-
- avcodec_send_packet(m_pCodecContext, m_packet);
- }
- else if(retRead < 0)
- {
- SPDLOG_ERROR("读取帧错误...");
- break;
- }
- else
- {
- if(m_packet->stream_index == m_videoStream)
- {
-
-
- #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++;
-
- #endif
-
-
- int ret = avcodec_send_packet(m_pCodecContext, m_packet);
- if(ret < 0)
- {
- SPDLOG_ERROR("发送数据包错误...");
- av_packet_unref(m_packet);
- continue;
- }
- }else {
-
- av_packet_unref(m_packet);
- continue;
- }
- }
-
-
- while(m_threadRuning)
- {
-
- int ret = avcodec_receive_frame(m_pCodecContext, m_pFrameSRC);
- if(ret == AVERROR_EOF)
- {
- SPDLOG_INFO("读取到视频流末尾。");
- isEnd = true;
- break;
- }
- else if (ret == AVERROR(EAGAIN))
- {
-
-
- 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("跳转结束。");
- }
- }
-
-
- 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,
- nullptr,
- nullptr);
- if(m_sws_ctx == nullptr)
- {
- SPDLOG_ERROR("创建SwsContext错误...");
- goto label_ThreadDecodeExit;
- }
- SPDLOG_INFO("创建SwsContext成功...");
- }
-
-
- int lines[4];
-
- 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)
- {
-
- auto image = new QImage(m_buffer, m_srcSize.width(), m_srcSize.height(), QImage::Format_RGBA8888);
-
- m_queueImage.push(image);
-
-
- }
- av_frame_unref(m_pFrameSRC);
-
- if(m_isSeek)
- {
- break;
- }
- }
- av_packet_unref(m_packet);
- 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();
- }
- 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);
- }
-
- int retRead = av_read_frame(m_pFormatContext, m_packet);
- if(retRead == AVERROR_EOF)
- {
-
- avcodec_send_packet(m_pCodecContext, m_packet);
- }
- else if(retRead < 0)
- {
- SPDLOG_ERROR("读取帧错误...");
- break;
- }
- else
- {
- if(m_packet->stream_index == m_videoStream)
- {
-
-
- #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++;
-
- #endif
-
-
- int ret = avcodec_send_packet(m_pCodecContext, m_packet);
- if(ret < 0)
- {
- SPDLOG_ERROR("发送数据包错误...");
- av_packet_unref(m_packet);
- continue;
- }
- }else {
-
- av_packet_unref(m_packet);
- continue;
- }
- }
-
-
- while(m_threadRuning)
- {
-
- int ret = avcodec_receive_frame(m_pCodecContext, m_pFrameSRC);
- if(ret == AVERROR_EOF)
- {
- SPDLOG_INFO("读取到视频流末尾。");
- isEnd = true;
- break;
- }
- else if (ret == AVERROR(EAGAIN))
- {
-
-
- av_frame_unref(m_pFrameSRC);
- break;
- }
- else if(ret < 0)
- {
- av_frame_unref(m_pFrameSRC);
- if(retRead < 0)
- {
- SPDLOG_ERROR("读取错误,错误值:{}",ret);
- break;
- }
- }
-
-
- 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("跳转结束。");
- }
- }
-
-
- 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,
- nullptr,
- nullptr);
- if(m_sws_ctx == nullptr)
- {
- SPDLOG_ERROR("创建SwsContext错误...");
- goto label_ThreadDecodeExit;
- }
- SPDLOG_INFO("创建SwsContext成功...");
- }
-
-
- int lines[4];
-
- 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)
- {
-
- auto image = new QImage(m_buffer, m_srcSize.width(), m_srcSize.height(), QImage::Format_RGBA8888);
-
- m_queueImage.push(image);
-
-
- }
- av_frame_unref(m_pFrameSRC);
-
- if(m_isSeek)
- {
- break;
- }
- }
- av_packet_unref(m_packet);
- 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);
- }
- qreal DecodeVedio::rationalToDouble(AVRational* rational)
- {
- return ( (rational->den == 0) ? 0 : (qreal(rational->num) / rational->den) );
- }
- void DecodeVedio::do_startDecodeVedio()
- {
- SPDLOG_DEBUG("解码线程ID:{}",QThread::currentThreadId());
-
-
-
-
- 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_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();
- }
|