#include"getVideoThread.h"
#include <QTimer>
#include <QDateTime>
#include <QDebug>
GetVideoThread::GetVideoThread()
{
m_isRun = false;
pFormatCtx = NULL;
out_buffer = NULL;
pFrame = NULL;
pFrameYUV = NULL;
pCodecCtx = NULL;
m_pause = false;
mH264Encoder = new H264Encoder();
}
GetVideoThread::~GetVideoThread()
{
}
void GetVideoThread::setQuantity(int value)
{
mH264Encoder->setQuantity(value);
}
ErroCode GetVideoThread::init(char* videoDevName)
{
AVCodec *pCodec = NULL;
AVDictionary *options = NULL;
// av_register_all();
// avformat_network_init();
// avdevice_register_all(); //Register Device
pFormatCtx = avformat_alloc_context();
AVInputFormat *ifmt = av_find_input_format("video4linux2"); //使用video4linux2
//QString videoDevOption = QString("video=%1").arg("/dev/video1");
if(ifmt==NULL)
{
qDebug()<<"Couldn't find_input_format.\n";
return VideoOpenFailed;
}
av_dict_set(&options, "video_size", "320x240", 0);
if(avformat_open_input(&pFormatCtx,videoDevName,ifmt,&options)!=0){
qDebug()<<"Couldn't open input stream video.(无法打开输入流)\n";
return VideoOpenFailed;
}
videoindex=-1;
pCodecCtx = NULL;
for(i=0; i<pFormatCtx->nb_streams; i++)
if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO)
{
videoindex=i;
//pCodecCtx = pFormatCtx->streams[videoindex]->codec;
//qDebug("pCodecCtx->width=%d,pCodecCtx->height=%d\n",pCodecCtx->width,pCodecCtx->height);
break;
}
if(videoindex==-1)
{
printf("Didn't find a video stream.(没有找到视频流)\n");
return VideoOpenFailed;
}
pCodecCtx = pFormatCtx->streams[videoindex]->codec;
pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
if(pCodec == NULL)
{
printf("video Codec not found.\n");
return VideoDecoderOpenFailed;
}
if(avcodec_open2(pCodecCtx, pCodec,NULL)<0)
{
printf("Could not open video codec.\n");
return VideoDecoderOpenFailed;
}
pFrame=avcodec_alloc_frame();
pFrameYUV=avcodec_alloc_frame();
out_buffer=(uint8_t *)av_malloc(avpicture_get_size(PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height));
avpicture_fill((AVPicture *)pFrameYUV, out_buffer, PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height);
qDebug("pCodecCtx->width=%d,pCodecCtx->height=%d\n",pCodecCtx->width,pCodecCtx->height);
//***************
// int Screen_W = GetSystemMetrics(SM_CXSCREEN); //获取屏幕宽高
// int Screen_H = GetSystemMetrics(SM_CYSCREEN);
// mH264Encoder->setWidth(640,480); //设置编码器的宽高
mH264Encoder->setWidth(pCodecCtx->width,pCodecCtx->height);
return SUCCEED;
}
void GetVideoThread::deInit()
{
if (out_buffer)
{
av_free(out_buffer);
out_buffer = NULL;
}
if (pFrame)
{
av_free(pFrame);
pFrame = NULL;
}
if (pFrameYUV)
{
av_free(pFrameYUV);
pFrameYUV = NULL;
}
if (pCodecCtx)
avcodec_close(pCodecCtx);
avformat_close_input(&pFormatCtx);
avformat_free_context(pFormatCtx);
}
void GetVideoThread::startRecord()
{
m_isRun = true;
start();
mH264Encoder->startEncode();
}
void GetVideoThread::stopRecord()
{
m_isRun = false;
}
//FILE *fp = fopen("out.yuv","wb");
void GetVideoThread::run()
{
struct SwsContext *img_convert_ctx = NULL;
int y_size = 0;
int yuvSize = 0;
if (pCodecCtx)
{
int numBytes = avpicture_get_size(AV_PIX_FMT_YUV420P, pCodecCtx->width,pCodecCtx->height);
out_buffer = (uint8_t *) av_malloc(numBytes * sizeof(uint8_t));
avpicture_fill((AVPicture *) pFrameYUV, out_buffer, AV_PIX_FMT_YUV420P,pCodecCtx->width, pCodecCtx->height);
img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);
//------------------------------
y_size = pCodecCtx->width * pCodecCtx->height;
yuvSize = avpicture_get_size(pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height);
///理论上 这里的 size = y_size * 3 / 2
}
int ret, got_frame;
AVPacket *packet=(AVPacket *)av_malloc(sizeof(AVPacket));
qint64 firstTime = QDateTime::currentMSecsSinceEpoch();
m_getFirst = false;
qint64 timeIndex = 0;
bool m_saveVideoFileThread = true;
while(m_isRun)
{
if (av_read_frame(pFormatCtx, packet)<0)
{
qDebug()<<"read failed!";
msleep(10);
continue;
}
if (m_pause)
{
av_free_packet(packet);
msleep(10);
continue;
}
if(packet->stream_index==videoindex)
{
qint64 time = 0;
if (m_saveVideoFileThread)
{
if (m_getFirst)
{
qint64 secondTime = QDateTime::currentMSecsSinceEpoch();
time = secondTime - firstTime + timeIndex;
}
else
{
firstTime = QDateTime::currentMSecsSinceEpoch();
timeIndex = 0;
m_getFirst = true;
}
}
ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_frame, packet);
if(ret < 0)
{
printf("video Decode Error.(解码错误)\n");
return;
}
if(got_frame && pCodecCtx)
{
/// 转换成YUV420
sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameYUV->data, pFrameYUV->linesize);
if (m_saveVideoFileThread)
{
uint8_t * picture_buf = (uint8_t *)av_malloc(yuvSize);
memcpy(picture_buf,pFrameYUV->data[0],y_size);
memcpy(picture_buf+y_size,pFrameYUV->data[1],y_size/4);
memcpy(picture_buf+y_size+y_size/4,pFrameYUV->data[2],y_size/4);
// fwrite(picture_buf,1,y_size*3/2,fp);
// av_free(picture_buf);
mH264Encoder->inputYuvBuffer(picture_buf,yuvSize,time); //将yuv数据添加到h.264编码的线程
}
}
}
else
{
qDebug()<<"other"<<packet->stream_index;
}
av_free_packet(packet);
msleep(20);
}
sws_freeContext(img_convert_ctx);
qDebug()<<"record stopping...";
m_pause = false;
deInit();
mH264Encoder->stopEncode();
qDebug()<<"record finished!";
}
没有合适的资源?快使用搜索试试~ 我知道了~
利用ffmpeg使用v4l2读取视频,并用h264
共25个文件
o:7个
cpp:7个
h:4个
需积分: 22 30 下载量 54 浏览量
2018-04-19
09:41:18
上传
评论
收藏 2.58MB RAR 举报
温馨提示
这样的代码不应该需要太多积分,但时间久了积分上去了,特意来减少所需积分,希望大家喜欢。 linux下 ffmpeg使用的2.5.2的库 在编译时要下载x264的库 增加编译选项--enable libx264,否则会报264编码器未找到
资源推荐
资源详情
资源评论
收起资源包目录
VideoRecorderH264.rar (25个子文件)
VideoRecorderH264
VideoRecorderH264.pro 843B
moc_h264Encoder.cpp 2KB
mainwindow.ui 654B
moc_mainwindow.cpp 2KB
moc_h264Encoder.o 3KB
main.o 2KB
out.h264 1.7MB
mainwindow.cpp 964B
mainwindow.o 8KB
VideoRecorderH264.pro.user 17KB
moc_getVideoThread.o 3KB
video
getVideoThread.cpp 7KB
getVideoThread.h 1KB
h264Encoder.cpp 6KB
h264Encoder.h 1KB
zgy.h264 856KB
main.cpp 187B
Makefile 9KB
VideoRecorderH264 48KB
moc_mainwindow.o 6KB
h264Encoder.o 10KB
mainwindow.h 507B
ui_mainwindow.h 2KB
moc_getVideoThread.cpp 2KB
getVideoThread.o 14KB
共 25 条
- 1
资源评论
MINGTING1323
- 粉丝: 69
- 资源: 29
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功