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

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

View file

@ -72,8 +72,7 @@ bool Convert::convert(AVFrame* srcFrame, AVFrame* dstFrame)
if(!m_convertCtx || !srcFrame || !dstFrame) {
return false;
}
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;
qint32 ret = sws_scale(m_convertCtx, (const uint8_t* const*)srcFrame->data, srcFrame->linesize, 0, m_srcHeight, dstFrame->data, dstFrame->linesize);
if (0 == ret) {
return false;
}

View file

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

View file

@ -93,6 +93,7 @@ void Decoder::run()
AVFrame* rgbDecoderFrame = Q_NULLPTR;
yuvDecoderFrame = av_frame_alloc();
rgbDecoderFrame = av_frame_alloc();
quint8 *outBuffer = Q_NULLPTR;
bool isFormatCtxOpen = false;
bool isCodecCtxOpen = false;
@ -164,12 +165,19 @@ void Decoder::run()
if (!ret) {
// a frame was received
if (!m_conver.isInit()) {
qDebug() << "decoder frame format" << yuvDecoderFrame->format;
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();
}
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);
//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)) {
qCritical("Could not receive video frame: %d", ret);
av_packet_unref(&packet);
@ -215,6 +223,9 @@ runQuit:
avcodec_free_context(&codecCtx);
}
if (outBuffer) {
delete[] outBuffer;
}
if (yuvDecoderFrame) {
av_free(yuvDecoderFrame);
}

View file

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

View file

@ -23,6 +23,14 @@ Dialog::Dialog(QWidget *parent) :
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()

View file

@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
<width>716</width>
<height>757</height>
</rect>
</property>
<property name="windowTitle">
@ -52,6 +52,22 @@
<string>stopServer</string>
</property>
</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>
<layoutdefault spacing="6" margin="11"/>
<resources/>