临时显示出图像,延迟大,cpu占用太高

This commit is contained in:
Barry 2018-10-17 23:16:38 +08:00
commit fa7cf8c2b1
6 changed files with 44 additions and 6 deletions

View file

@ -73,7 +73,6 @@ bool Convert::convert(AVFrame* srcFrame, AVFrame* dstFrame)
return false; return false;
} }
qint32 ret = sws_scale(m_convertCtx, (const uint8_t* const*)srcFrame->data, srcFrame->linesize, 0, m_srcHeight, dstFrame->data, dstFrame->linesize); qint32 ret = sws_scale(m_convertCtx, (const uint8_t* const*)srcFrame->data, srcFrame->linesize, 0, m_srcHeight, dstFrame->data, dstFrame->linesize);
qDebug() << "Convert::convert sws_scale return " << ret;
if (0 == ret) { if (0 == ret) {
return false; return false;
} }

View file

@ -4,6 +4,7 @@
extern "C" extern "C"
{ {
#include "libavcodec/avcodec.h"
#include "libswscale/swscale.h" #include "libswscale/swscale.h"
#include "libavutil/frame.h" #include "libavutil/frame.h"
} }

View file

@ -93,6 +93,7 @@ void Decoder::run()
AVFrame* rgbDecoderFrame = Q_NULLPTR; AVFrame* rgbDecoderFrame = Q_NULLPTR;
yuvDecoderFrame = av_frame_alloc(); yuvDecoderFrame = av_frame_alloc();
rgbDecoderFrame = av_frame_alloc(); rgbDecoderFrame = av_frame_alloc();
quint8 *outBuffer = Q_NULLPTR;
bool isFormatCtxOpen = false; bool isFormatCtxOpen = false;
bool isCodecCtxOpen = false; bool isCodecCtxOpen = false;
@ -164,12 +165,19 @@ void Decoder::run()
if (!ret) { if (!ret) {
// a frame was received // a frame was received
if (!m_conver.isInit()) { if (!m_conver.isInit()) {
qDebug() << "decoder frame format" << yuvDecoderFrame->format;
m_conver.setSrcFrameInfo(codecCtx->width, codecCtx->height, AV_PIX_FMT_YUV420P); m_conver.setSrcFrameInfo(codecCtx->width, codecCtx->height, AV_PIX_FMT_YUV420P);
m_conver.setDstFrameInfo(codecCtx->width, codecCtx->height, AV_PIX_FMT_RGB24); m_conver.setDstFrameInfo(codecCtx->width, codecCtx->height, AV_PIX_FMT_RGB32);
m_conver.init(); m_conver.init();
} }
if (!outBuffer) {
outBuffer=new quint8[avpicture_get_size(AV_PIX_FMT_RGB32, codecCtx->width, codecCtx->height)];
avpicture_fill((AVPicture *)rgbDecoderFrame, outBuffer, AV_PIX_FMT_RGB32, codecCtx->width, codecCtx->height);
}
m_conver.convert(yuvDecoderFrame, rgbDecoderFrame); m_conver.convert(yuvDecoderFrame, rgbDecoderFrame);
//push_frame(decoder); QImage tmpImg((uchar *)outBuffer, codecCtx->width, codecCtx->height, QImage::Format_RGB32);
QImage image = tmpImg.copy(); //把图像复制一份 传递给界面显示
emit getOneImage(image);
} else if (ret != AVERROR(EAGAIN)) { } else if (ret != AVERROR(EAGAIN)) {
qCritical("Could not receive video frame: %d", ret); qCritical("Could not receive video frame: %d", ret);
av_packet_unref(&packet); av_packet_unref(&packet);
@ -215,6 +223,9 @@ runQuit:
avcodec_free_context(&codecCtx); avcodec_free_context(&codecCtx);
} }
if (outBuffer) {
delete[] outBuffer;
}
if (yuvDecoderFrame) { if (yuvDecoderFrame) {
av_free(yuvDecoderFrame); av_free(yuvDecoderFrame);
} }

View file

@ -4,6 +4,7 @@
#include <QThread> #include <QThread>
#include <QTcpSocket> #include <QTcpSocket>
#include <QPointer> #include <QPointer>
#include <QImage>
#include "convert.h" #include "convert.h"
extern "C" extern "C"
@ -28,6 +29,8 @@ public:
bool startDecode(); bool startDecode();
void stopDecode(); void stopDecode();
signals:
void getOneImage(QImage img);
protected: protected:
void run(); void run();

View file

@ -23,6 +23,14 @@ Dialog::Dialog(QWidget *parent) :
decoder.startDecode(); decoder.startDecode();
} }
}); });
// must be Qt::QueuedConnection, ui update must be main thread
connect(&decoder, &Decoder::getOneImage, this, [this](QImage img){
// 将图像按比例缩放成和窗口一样大小
QImage img2 = img.scaled(ui->imgLabel->size(), Qt::IgnoreAspectRatio);
ui->imgLabel->setPixmap(QPixmap::fromImage(img2));
qDebug() << "getOneImage";
}, Qt::QueuedConnection);
} }
Dialog::~Dialog() Dialog::~Dialog()

View file

@ -6,8 +6,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>400</width> <width>716</width>
<height>300</height> <height>757</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
@ -52,6 +52,22 @@
<string>stopServer</string> <string>stopServer</string>
</property> </property>
</widget> </widget>
<widget class="QLabel" name="imgLabel">
<property name="geometry">
<rect>
<x>230</x>
<y>20</y>
<width>451</width>
<height>701</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(0, 0, 0);</string>
</property>
<property name="text">
<string/>
</property>
</widget>
</widget> </widget>
<layoutdefault spacing="6" margin="11"/> <layoutdefault spacing="6" margin="11"/>
<resources/> <resources/>