#include "worker.h"
extern "C"
{
#include "avcodec.h"
#include "avdevice.h"
#include "avformat.h"
#include "swscale.h"
}
#include <qDebug>
#include <QThread>
#include <QImage>
#include "logger.h"
Worker::Worker()
{
}
Worker::~Worker()
{
}
void Worker::setCameraName(QString name)
{
CameraName = QString::fromLocal8Bit("video=%1").arg(name);
}
void Worker::ffmpeg_test()
{
AVFormatContext *pFormatCtx;//FFMPEG所有的操作都要通过这个AVFormatContext来进行
AVInputFormat *ifmt;//使用libavdevice的时候,唯一的不同在于需要首先查找用于输入的设备
int i, videoindex;
int numBytes;
int ret, got_picture;
AVCodecContext *pCodecCtx;
AVCodec *pCodec;
AVFrame *pFrame, *pFrameRGB;
AVPacket *packet;
uint8_t *out_buffer;
av_register_all(); //初始化FFMPEG 调用了这个才能正常适用编码器和解码器
avformat_network_init();//初始化FFmpeg网络模块
avdevice_register_all();//初始化libavdevice并注册所有输入和输出设备
pFormatCtx = avformat_alloc_context();//分配一个AVFormatContext,查找用于输入的设备
qDebug() << QDateTime::currentDateTime();
//使用libavdevice读取数据,和直接打开视频文件比较类似,
//因为系统的设备也被FFmpeg认为是一种输入的格式(即AVInputFormat)
AVDictionary* pOptions = NULL;
pFormatCtx->probesize = 100 *1024;
pFormatCtx->max_analyze_duration = 5 * AV_TIME_BASE;
//ifmt=av_find_input_format("dshow");
//if(int a = avformat_open_input(&pFormatCtx,CameraName.toUtf8().constData(),ifmt,&pOptions)!=0){
ifmt=av_find_input_format("vfwcap");
if(avformat_open_input(&pFormatCtx,"0",ifmt,&pOptions)!=0){
qDebug() << "Couldn't open input stream.\n";
Logger::s_add("Couldn't open input stream");
emit sig_returnStauts(false);
}else{
qDebug() << "Success open input stream.\n";
qDebug() << QDateTime::currentDateTime();
Logger::s_add(QDateTime::currentDateTime().toString("yyyy/mm/dd hh:MM:ss"));
//读取一个媒体文件的数据包以获取流信息
if(avformat_find_stream_info(pFormatCtx,&pOptions) < 0){
qDebug() << "Couldn't find stream information.\n";
}else{
qDebug() << "Success find stream information!\n";
}
qDebug() << QDateTime::currentDateTime();
//循环查找数据包包含的流信息,直到找到视频类型的流
// 便将其记录下来 保存到videoStream变量中
videoindex = -1;
for(i = 0; i < pFormatCtx->nb_streams; i++)
{
if(pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
{
videoindex=i;
break;
}
}
if(videoindex==-1){
qDebug() << "Couldn't find a video stream.\n";
}else{
qDebug() << "Success find a video stream!\n";
}
//查找对应的解码器并打开
pCodecCtx = pFormatCtx->streams[videoindex]->codec;
pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
// AVCodec *codec = avcodec_find_encoder(AV_CODEC_ID_H264);//软编码
// AVCodec * codec = avcodec_find_encoder_by_name("nvenc_h264");//硬编码
if(pCodec == NULL){
qDebug() << ("Codec not found.\n");
}else{
qDebug() << "Codec found Successfuly!\n";
}
// pCodecCtx->bit_rate =0; //初始化为0
// pCodecCtx->time_base.num=1; //下面两行:一秒钟25帧
// pCodecCtx->time_base.den=10;
// pCodecCtx->frame_number=1; //每包一个视频帧
//打开解码器
if(avcodec_open2(pCodecCtx, pCodec,NULL)<0){
qDebug() << ("Could not open codec.\n");
}else{
qDebug() << "Success open codec!\n";
}
//6_开始准备读取视频
pFrame = av_frame_alloc();//分配一个AVFrame并将其字段设置为默认值
pFrameRGB = av_frame_alloc();
//分配和返回一个SwsContext你需要它来执行使用swsscale()的缩放/转换操作
struct SwsContext *img_convert_ctx;
img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height,
pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height,
AV_PIX_FMT_RGB32, SWS_BICUBIC, NULL, NULL, NULL);
numBytes = avpicture_get_size(AV_PIX_FMT_RGB32, pCodecCtx->width,pCodecCtx->height);
qDebug() << numBytes;
out_buffer = (uint8_t *) av_malloc(numBytes * sizeof(uint8_t));
//根据指定的图像参数和提供的图像数据缓冲区设置图像域
avpicture_fill((AVPicture *) pFrameRGB, out_buffer, AV_PIX_FMT_RGB32,
pCodecCtx->width, pCodecCtx->height);
int y_size = pCodecCtx->width * pCodecCtx->height;
packet = (AVPacket *) malloc(sizeof(AVPacket)); //分配一个packet
av_new_packet(packet, y_size); //分配packet的数据
av_dump_format(pFormatCtx, 0, QApplication::applicationDirPath().toLatin1(), 0); //输出视频信息
//7_解码压缩
while (1)
{
if (av_read_frame(pFormatCtx, packet) < 0)
{
break; //这里认为视频读取完了
}
if (packet->stream_index == videoindex) {
ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture,packet);//解码一帧视频数据
if (ret < 0) {
qDebug() << ("decode error.");
}
if (got_picture) {
//在 pFrame->data 中缩放图像切片,并将得到的缩放切片放在pFrameRGB->data图像中
sws_scale(img_convert_ctx,
(uint8_t const * const *) pFrame->data,
pFrame->linesize, 0, pCodecCtx->height, pFrameRGB->data,
pFrameRGB->linesize);
//把这个RGB数据 用QImage加载
QImage tmpImg((uchar *)out_buffer,pCodecCtx->width,pCodecCtx->height,QImage::Format_RGB32);
QImage image = tmpImg.copy(); //把图像复制一份 传递给界面显示
emit sig_GetOneFrame(image); //发送信号
QThread::msleep(10);
}
}
av_free_packet(packet); //释放资源,否则内存会一直上升
QThread::msleep(10);
}
sws_freeContext(img_convert_ctx);
av_free(out_buffer);
av_free(pFrameRGB);
avcodec_close(pCodecCtx);
avformat_close_input(&pFormatCtx);
}
}
void Worker::run()
{
ffmpeg_test();
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Qt 使用ffmpeg库打开usb摄像头 (142个子文件)
worker.cpp 6KB
mainwindow.cpp 1KB
logger.cpp 877B
config.cpp 803B
main.cpp 224B
avcodec-58.dll 30.64MB
avfilter-7.dll 6.78MB
avformat-58.dll 5.96MB
avdevice-58.dll 1.33MB
avutil-56.dll 642KB
swscale-5.dll 505KB
swresample-3.dll 305KB
postproc-55.dll 112KB
testFFmpeg.exe 9.86MB
avcodec.h 207KB
avformat.h 115KB
avfilter.h 41KB
opt.h 35KB
pixfmt.h 33KB
avio.h 31KB
frame.h 29KB
mem.h 23KB
hwcontext.h 22KB
swresample.h 21KB
intreadwrite.h 18KB
avdevice.h 17KB
common.h 16KB
pixdesc.h 16KB
avstring.h 14KB
swscale.h 12KB
imgutils.h 11KB
log.h 11KB
buffer.h 10KB
samplefmt.h 10KB
channel_layout.h 9KB
avutil.h 9KB
hash.h 8KB
dict.h 8KB
spherical.h 8KB
mathematics.h 8KB
bprint.h 8KB
parseutils.h 7KB
encryption_info.h 7KB
buffersrc.h 6KB
buffersink.h 6KB
hwcontext_d3d11va.h 6KB
xvmc.h 6KB
audio_fifo.h 6KB
fifo.h 6KB
rational.h 6KB
vdpau.h 6KB
cpu.h 6KB
error.h 5KB
tree.h 5KB
timecode.h 5KB
eval.h 5KB
stereo3d.h 5KB
version.h 5KB
attributes.h 5KB
hwcontext_drm.h 5KB
version.h 5KB
version.h 4KB
dirac.h 4KB
videotoolbox.h 4KB
mastering_display_metadata.h 4KB
threadmessage.h 4KB
hwcontext_vaapi.h 4KB
qsv.h 4KB
dv_profile.h 4KB
murmur3.h 4KB
display.h 3KB
mediacodec.h 3KB
downmix_info.h 3KB
crc.h 3KB
avfft.h 3KB
postprocess.h 3KB
hmac.h 3KB
bswap.h 3KB
d3d11va.h 3KB
xtea.h 3KB
file.h 3KB
timestamp.h 3KB
avdct.h 3KB
cast5.h 3KB
sha512.h 3KB
sha.h 2KB
hwcontext_dxva2.h 2KB
blowfish.h 2KB
dxva2.h 2KB
avassert.h 2KB
version.h 2KB
des.h 2KB
md5.h 2KB
vaapi.h 2KB
vorbis_parser.h 2KB
base64.h 2KB
ripemd.h 2KB
aes_ctr.h 2KB
twofish.h 2KB
lfg.h 2KB
共 142 条
- 1
- 2
资源评论
徵东
- 粉丝: 1
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功