#include "videoplayer.h"
/***
***DecodeVideo类的成员
***/
static QMutex decodeVideoMutex;
#if 0
DecodeVideo::DecodeVideo()
{
bufferRGB = NULL;
}
DecodeVideo::~DecodeVideo()
{
}
void DecodeVideo::setAVCodecContext(AVCodecContext*ctx)
{
pCodecCtx = ctx;
width = pCodecCtx->width;
height= pCodecCtx->height;
pix_fmt = pCodecCtx->pix_fmt;
if (bufferRGB != NULL)
{
av_free(bufferRGB);
bufferRGB = NULL;
}
int numBytes = avpicture_get_size(PIX_FMT_RGB24, pCodecCtx->width,pCodecCtx->height);
bufferRGB = (uint8_t *)av_malloc(numBytes*sizeof(uint8_t));
}
void DecodeVideo::run()
{
int frameFinished = 0;
AVFrame *pFrame = avcodec_alloc_frame();
decodeVideoMutex.lock();
avcodec_decode_video(pCodecCtx, pFrame, &frameFinished,packet.data,packet.size);
decodeVideoMutex.unlock();
AVFrame *pFrameRGB;
pFrameRGB = avcodec_alloc_frame();
avpicture_fill((AVPicture *)pFrameRGB, bufferRGB, PIX_FMT_RGB24,pCodecCtx->width, pCodecCtx->height);
SwsContext *convert_ctx = sws_getContext(width,height,pix_fmt,width,height,PIX_FMT_RGB24,SWS_BICUBIC, NULL,NULL,NULL);
sws_scale(convert_ctx,(const uint8_t* const*)pFrame->data,pFrame->linesize,0,height,pFrameRGB->data,pFrameRGB->linesize);
QImage tmpImage((uchar *)bufferRGB,width,height,QImage::Format_RGB888);
QImage image = tmpImage.copy();
av_free(pFrameRGB);
sws_freeContext(convert_ctx);
emit readOneFrame(image);
av_free_packet(&packet);
}
#endif
/***
***VideoPlayer类的成员
***/
VideoPlayer::VideoPlayer()
{
initAvcodec();
aCodecCtx = NULL;
pFormatCtx = NULL;
eventloop = NULL;
curState = StoppedState;
curType = NoneType;
// decodeVideoThread = new DecodeVideo;
// connect(decodeVideoThread,SIGNAL(readOneFrame(QImage)),this,SIGNAL(readOneFrame(QImage)));
}
VideoPlayer::~VideoPlayer()
{
}
void VideoPlayer::run()
{
eventloop = new QEventLoop;
QTimer playtimer; //控制播放的定时器
connect(&playtimer,SIGNAL(timeout()),this,SLOT(readPacket()),Qt::DirectConnection);
playtimer.start(50);
eventloop->exec();
delete eventloop;
eventloop = NULL;
}
void VideoPlayer::initAvcodec()
{
av_register_all();
avcodec_init();
avcodec_register_all();
avdevice_register_all();
}
bool VideoPlayer::openVideo(char *filename)
{
videoStream = -1;
audioStream = -1;
if(av_open_input_file(&pFormatCtx, filename, NULL, 0, NULL)!=0)
{
fprintf(stderr, "Couldn't open file\n");
return false; //Couldn't open file
}
if(av_find_stream_info(pFormatCtx)<0)
{
fprintf(stderr, "Couldn't find stream information\n");
return false ; // Couldn't find stream information
}
//dump_format(pFormatCtx, 0, filename, 0); //输出视频信息到终端
int i;
for(i=0; i<pFormatCtx->nb_streams; i++)
{
if(pFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_VIDEO && videoStream < 0)
{
videoStream=i;
}
if(pFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_AUDIO && audioStream < 0)
{
audioStream=i;
}
}
if(audioStream==-1 && videoStream==-1)
{
closeVideo();
fprintf(stderr, "Didn't find a audio stream\n");
return false; // Didn't find a audio stream
}
if (videoStream != -1)
{
// Get a pointer to the codec context for the video stream
pCodecCtx=pFormatCtx->streams[videoStream]->codec;
// Find the decoder for the video stream
AVCodec *pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
if(pCodec==NULL)
{
fprintf(stderr, "Unsupported codec!\n");
return false; // Codec not found
}
// Open codec
if(avcodec_open(pCodecCtx, pCodec)<0)
{
fprintf(stderr, "Could not open audio codec!\n");
return false; // Could not open audio codec
}
curType = VideoType;
}
else
{
curType = AudioType;
}
if (audioStream != -1)
{
aCodecCtx = pFormatCtx->streams[audioStream]->codec;
AVCodec *aCodec = avcodec_find_decoder(aCodecCtx->codec_id);
if(!aCodec)
{
fprintf(stderr, "Unsupported codec!\n");
return false;
}
if(avcodec_open(aCodecCtx, aCodec)<0)
{
fprintf(stderr, "Could not open video codec!\n");
return false; // Could not open video codec
}
}
totaltime = pFormatCtx->duration;
return true;
}
void VideoPlayer::closeVideo()
{
if (aCodecCtx != NULL)
{
avcodec_close(aCodecCtx);
}
if (pFormatCtx != NULL)
{
av_close_input_file(pFormatCtx);
}
aCodecCtx = NULL;
pFormatCtx = NULL;
curType = NoneType;
}
//extern QQueue<QByteArray> queueFrame;
void VideoPlayer::readPacket()
{
if (pFormatCtx == NULL) return;
mutex.lock();
currenttime+=10;
if (currenttime >= nextPacket.dts)
{
// if(nextPacket.stream_index == videoStream)
// if(!queueFrame.isEmpty())
{
// QByteArray getFram = queueFrame.dequeue();
packet = nextPacket;
// qDebug()<<packet.size<<packet.date[0]<<packet.date[1]<<packet.date[2]<<packet.date[3]<<packet.date[4]<<packet.date[5];
qDebug("%d:[%02x][%02x][%02x][%02x][%02x][%02x][%02x][%02x][%02x][%02x][%02x][%02x][%02x][%02x][%02x][%02x][%02x][%02x][%02x][%02x][%02x][%02x][%02x][%02x]",
packet.size,
packet.data[0],
packet.data[1],
packet.data[2],
packet.data[3],
packet.data[4],
packet.data[5],
packet.data[6],
packet.data[7],
packet.data[8],
packet.data[9],
packet.data[10],
packet.data[11],
packet.data[12],
packet.data[13],
packet.data[14],
packet.data[15],
packet.data[16],
packet.data[17],
packet.data[18],
packet.data[19],
packet.data[20],
packet.data[21],
packet.data[22],
packet.data[23]
);
int frameFinished = 0;
AVFrame *pFrame = avcodec_alloc_frame();
decodeVideoMutex.lock();
avcodec_decode_video(pCodecCtx, pFrame, &frameFinished,packet.data,packet.size);
// avcodec_decode_video(pCodecCtx, pFrame, &frameFinished,(uint8_t *)getFram.data(),getFram.size());
decodeVideoMutex.unlock();
AVFrame *pFrameRGB;
pFrameRGB = avcodec_alloc_frame();
avpicture_fill((AVPicture *)pFrameRGB, bufferRGB, PIX_FMT_RGB24,pCodecCtx->width, pCodecCtx->height);
SwsContext *convert_ctx = sws_getContext(width,height,pix_fmt,width,height,PIX_FMT_RGB24,SWS_BICUBIC, NULL,NULL,NULL);
sws_scale(convert_ctx,(const uint8_t* const*)pFrame->data,pFrame->linesize,0,height,pFrameRGB->data,pFrameRGB->linesize);
QImage tmpImage((uchar *)bufferRGB,width,height,QImage::Format_RGB888);
QImage image = tmpImage.copy();
av_free(pFrameRGB);
sws_freeContext(convert_ctx);
emit readOneFrame(image);
// av_free_packet(&packet);
}
if(av_read_frame(pFormatCtx, &nextPacket) < 0)
{//整个视频文件读取完毕
stop();
currenttime =
没有合适的资源?快使用搜索试试~ 我知道了~
QT视频播放器
共82个文件
h:57个
dll:7个
lib:7个
4星 · 超过85%的资源 需积分: 50 965 下载量 153 浏览量
2016-04-13
15:43:08
上传
评论 19
收藏 6.5MB ZIP 举报
温馨提示
QT界面 和 ffmpeg解码 的视频播放器, 无音频. 供H.264视频解码学习之用
资源推荐
资源详情
资源评论
收起资源包目录
VideoPlayer_noSDL.zip (82个子文件)
VideoPlayer_noSDL
VideoPlayer.pro 785B
PlayWindow.cpp 7KB
ffmpeg
lib
avformat.lib 182KB
avcodec.lib 1.46MB
postproc.lib 3KB
libavdevice.dll.a 3KB
libavutil.dll.a 109KB
libavfilter.dll.a 74KB
avdevice.lib 2KB
swscale.lib 12KB
libpostproc.dll.a 6KB
libswscale.dll.a 34KB
avutil.lib 36KB
libavformat.dll.a 555KB
avfilter.lib 27KB
libavcodec.dll.a 4.38MB
include
libavcodec
opt.h 2KB
avcodec.h 142KB
vdpau.h 3KB
dxva2.h 2KB
version.h 4KB
xvmc.h 7KB
vaapi.h 4KB
avfft.h 3KB
libpostproc
postprocess.h 3KB
libavfilter
avfiltergraph.h 5KB
vsrc_buffer.h 2KB
vsink_buffer.h 2KB
avfilter.h 35KB
avcodec.h 2KB
libavdevice
avdevice.h 2KB
libswscale
swscale.h 12KB
libavutil
cpu.h 2KB
pixdesc.h 7KB
lzo.h 2KB
base64.h 2KB
mathematics.h 4KB
intreadwrite.h 14KB
sha1.h 2KB
log.h 5KB
pixfmt.h 13KB
avstring.h 5KB
mem.h 5KB
samplefmt.h 5KB
fifo.h 4KB
imgutils.h 5KB
common.h 11KB
avutil.h 4KB
file.h 2KB
random_seed.h 1KB
opt.h 9KB
eval.h 6KB
aes.h 2KB
md5.h 1KB
rational.h 4KB
lfg.h 2KB
dict.h 3KB
bswap.h 3KB
audioconvert.h 4KB
sha.h 2KB
avconfig.h 170B
intfloat_readwrite.h 1KB
parseutils.h 4KB
error.h 4KB
attributes.h 3KB
crc.h 1KB
avassert.h 2KB
adler32.h 1KB
libavformat
avio.h 25KB
avformat.h 62KB
version.h 5KB
PlayWindow.h 1012B
dll
avcodec-52.dll 11.89MB
avutil-50.dll 133KB
avfilter-1.dll 780KB
avformat-52.dll 1.24MB
avdevice-52.dll 54KB
postproc-51.dll 164KB
swscale-0.dll 229KB
main.cpp 529B
videoplayer.h 3KB
videoplayer.cpp 9KB
共 82 条
- 1
阳光柠檬_
- 粉丝: 426
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页