12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079 |
- #include "DecodeVedio.h"
- #include "spdlog/spdlog.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)
- {
-
- m_threadRuning = false;
- m_initFFmpeg = false;
- m_pauseDecode = false;
- m_decodeStatus = false;
- m_isSeek = false;
- m_flushDecoder = false;
- m_decodeState = DecodeState::NONE;
- m_pts.store(0);
-
- 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_DEBUG("跳转结束");
- }
- }
-
-
- 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();
- }
|