#include "xvideothread2.h"
XVideoThread2::XVideoThread2(QObject *parent) : QObject(parent)
{
qDebug() << "XVideoThread2";
}
XVideoThread2::~XVideoThread2()
{
}
void XVideoThread2::setRunflag(int flag)
{
runFlag = flag;
}
void XVideoThread2::handleResults()
{
}
std::string XVideoThread2::strPath() const
{
return m_strPath;
}
void XVideoThread2::setStrPath(const std::string &strPath)
{
runFlag = 0;
m_strPath = strPath;
}
void XVideoThread2::start()
{
qDebug() << "start()";
int nGotPic = 0;
av_register_all();
avformat_network_init();
m_pInFmtCtx = avformat_alloc_context();
runFlag = 1;
if (runFlag == 0)
{
return;
}
qDebug() << "m_strPath" << QString::fromStdString(m_strPath);
if(avformat_open_input(&m_pInFmtCtx, m_strPath.c_str(), NULL, NULL))// TODO 测试
// if(avformat_open_input(&m_pInFmtCtx, "D:/27a434687f7d420ab46057ba377621610.wmv", NULL, NULL))
{
return;
}
if(avformat_find_stream_info(m_pInFmtCtx, NULL) < 0)
{
return;
}
int nVideoIndex = -1;
for(int i = 0; i < m_pInFmtCtx->nb_streams; i++)
{
if(m_pInFmtCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
{
nVideoIndex = i;
break;
}
}
if(nVideoIndex == -1)
{
return;
}
m_pCodecCtx = m_pInFmtCtx->streams[nVideoIndex]->codec;
m_pCodec = avcodec_find_decoder(m_pCodecCtx->codec_id);
if(!m_pCodec)
{
return;
}
if (avcodec_open2(m_pCodecCtx, m_pCodec, NULL) < 0) {
return;
}
m_pFrame = av_frame_alloc();
m_pFrameRGB = av_frame_alloc();
m_pOutBuf = (uint8_t*)av_malloc(avpicture_get_size(AV_PIX_FMT_RGB32, m_pCodecCtx->width, m_pCodecCtx->height));
avpicture_fill((AVPicture*)m_pFrameRGB, m_pOutBuf, AV_PIX_FMT_RGB32, m_pCodecCtx->width, m_pCodecCtx->height);
pImgCtx = sws_getContext(m_pCodecCtx->width, m_pCodecCtx->height, m_pCodecCtx->pix_fmt,
m_pCodecCtx->width, m_pCodecCtx->height, AV_PIX_FMT_RGB32, SWS_BICUBIC, NULL, NULL, NULL);
int nSize = m_pCodecCtx->width * m_pCodecCtx->height;
m_pPacket = (AVPacket *)av_malloc(sizeof(AVPacket));
if(av_new_packet(m_pPacket, nSize) != 0)
{
}
while (runFlag == 1)
{
if(av_read_frame(m_pInFmtCtx, m_pPacket) >= 0)
{
if(m_pPacket->stream_index == nVideoIndex)
{
nGotPic = 0;
if(avcodec_decode_video2(m_pCodecCtx, m_pFrame, &nGotPic, m_pPacket) < 0)
{
return;
}
if(nGotPic)
{
sws_scale(pImgCtx, (const uint8_t* const*)m_pFrame->data,
m_pFrame->linesize, 0, m_pCodecCtx->height, m_pFrameRGB->data,
m_pFrameRGB->linesize);
pImage = QImage((uchar*)m_pOutBuf, m_pCodecCtx->width, m_pCodecCtx->height, QImage::Format_RGB32);
// qDebug() << "width" << QString::number(m_pCodecCtx->width) << "height"<< QString::number(m_pCodecCtx->height);
if (runFlag == 1)
{
emit sig_SendOneFrame(pImage);
}
}
}
}
av_packet_unref(m_pPacket);
QThread::msleep(20);
}
qDebug() << "av_free_packet(m_pPacket)111";
av_packet_unref(m_pPacket);
av_free_packet(m_pPacket);
av_free(m_pOutBuf);
av_frame_free(&m_pFrame);
av_frame_free(&m_pFrameRGB);
av_free(m_pCodec);
avcodec_close(m_pCodecCtx);
avcodec_free_context(&m_pCodecCtx);
avformat_close_input(&m_pInFmtCtx);
avformat_free_context(m_pInFmtCtx);
sws_freeContext(pImgCtx);
qDebug() << "av_free_packet(m_pPacket)000";
}
QImage XVideoThread2::Mat2QImage(const Mat &mat)
{
if(mat.type()==CV_8UC1)
{
// cout<<"1"<<endl;
// Set the color table (used to translate colour indexes to qRgb values)
QVector<QRgb> colorTable;
for (int i=0; i<256; i++)
colorTable.push_back(qRgb(i,i,i));
// Copy input Mat
const uchar *qImageBuffer = (const uchar*)mat.data;
// Create QImage with same dimensions as input Mat
QImage img(qImageBuffer, mat.cols, mat.rows, mat.step, QImage::Format_Indexed8);
img.setColorTable(colorTable);
return img;
}
// 8-bits unsigned, NO. OF CHANNELS=3
if(mat.type()==CV_8UC3)
{
// cout<<"3"<<endl;
// Copy input Mat
const uchar *qImageBuffer = (const uchar*)mat.data;
// Create QImage with same dimensions as input Mat
QImage img(qImageBuffer, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);
return img.rgbSwapped();
}
else
{
qDebug() << "ERROR: Mat could not be converted to QImage.";
return QImage();
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
qt ffmpeg 视频文件 (509个子文件)
untitled121.pro.user.22a7442 23KB
libavutil.dll.a 275KB
libavcodec.dll.a 142KB
libavformat.dll.a 103KB
libavfilter.dll.a 54KB
libswscale.dll.a 23KB
libswresample.dll.a 14KB
libavdevice.dll.a 11KB
libpostproc.dll.a 7KB
xvideothread2.cpp 5KB
mainwindow.cpp 2KB
xvideo2.cpp 2KB
main.cpp 177B
avformat.cpp 69B
wc.db 272KB
wc.db-journal 0B
avutil-54.def 10KB
avcodec-56.def 6KB
avformat-56.def 4KB
avfilter-5.def 2KB
swscale-3.def 781B
avdevice-56.def 491B
swresample-1.def 433B
postproc-53.def 233B
avcodec-56.dll 18.74MB
avformat-56.dll 5.57MB
avfilter-5.dll 2.21MB
avdevice-56.dll 1.3MB
avutil-54.dll 442KB
swscale-3.dll 437KB
swresample-1.dll 264KB
postproc-53.dll 119KB
entries 3B
ffplay.exe 464KB
ffmpeg.exe 317KB
ffprobe.exe 149KB
format 3B
avcodec.h 206KB
avcodec.h 176KB
avformat.h 112KB
avformat.h 100KB
avfilter.h 56KB
avfilter.h 40KB
opt.h 37KB
opt.h 35KB
pixfmt.h 32KB
pixfmt.h 31KB
avio.h 29KB
frame.h 24KB
frame.h 23KB
mem.h 23KB
swresample.h 21KB
swresample.h 19KB
avio.h 18KB
intreadwrite.h 18KB
intreadwrite.h 18KB
avdevice.h 18KB
avdevice.h 16KB
hwcontext.h 16KB
common.h 15KB
mem.h 14KB
pixdesc.h 14KB
old_pix_fmts.h 14KB
pixdesc.h 14KB
common.h 14KB
avstring.h 14KB
avstring.h 13KB
swscale.h 12KB
swscale.h 12KB
log.h 11KB
buffer.h 10KB
old_codec_ids.h 10KB
log.h 10KB
samplefmt.h 10KB
samplefmt.h 10KB
buffer.h 10KB
channel_layout.h 9KB
channel_layout.h 9KB
avutil.h 9KB
avutil.h 8KB
hash.h 8KB
imgutils.h 8KB
imgutils.h 8KB
dict.h 8KB
mathematics.h 8KB
vdpau.h 8KB
version.h 8KB
dict.h 8KB
bprint.h 8KB
bprint.h 8KB
parseutils.h 7KB
buffersink.h 7KB
parseutils.h 7KB
vdpau.h 7KB
version.h 6KB
buffersrc.h 6KB
xvmc.h 6KB
xvmc.h 6KB
vda.h 6KB
audio_fifo.h 6KB
共 509 条
- 1
- 2
- 3
- 4
- 5
- 6
资源评论
zck0911
- 粉丝: 1
- 资源: 13
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功