实现功能:提取rgb波段并通过h264编码;
This commit is contained in:
@ -101,7 +101,7 @@ void XimeaImager::openImger()
|
||||
if (ret)
|
||||
{
|
||||
m_imager.setEffectiveWindow(offsetx, width, offsety, height);
|
||||
m_rgbImage->SetRgbImageWidthAndHeight(height, width, 20);
|
||||
m_rgbImage->SetRgbImageWidthAndHeight(height, width, 720);
|
||||
std::cout<<"height:"<< height <<std::endl;
|
||||
std::cout<<"width:"<< width <<std::endl;
|
||||
std::cout<<"每帧字节数:"<< width * height * 2 <<std::endl;
|
||||
@ -592,6 +592,124 @@ void XimeaImager::startRecord(double TimeDifferenceBetweensOSAndSbg,QString base
|
||||
FILE *hFile=fopen(imageFileName.toStdString().c_str(),"w+b");
|
||||
double * imageBuffer = new double[number_WriteDisk];
|
||||
|
||||
//tc+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
// 创建输出视频的AVFormatContext
|
||||
const char* outputVideoPath = "/media/nvme/delete/300tc.h264";
|
||||
int width = 1368;
|
||||
int height = 720;
|
||||
int framerateVideo = 10;
|
||||
|
||||
AVFormatContext* formatContext = nullptr;
|
||||
avformat_alloc_output_context2(&formatContext, nullptr, "mp4", outputVideoPath);
|
||||
if (!formatContext)
|
||||
{
|
||||
qDebug() << "Error: Failed to allocate output context";
|
||||
return;
|
||||
}
|
||||
|
||||
// 查找H.264编码器
|
||||
const AVCodec* codec = avcodec_find_encoder(AV_CODEC_ID_H264);
|
||||
if (!codec)
|
||||
{
|
||||
qDebug() << "Error: Codec not found";
|
||||
avformat_free_context(formatContext);
|
||||
return;
|
||||
}
|
||||
|
||||
// 创建输出视频流
|
||||
AVStream* videoStream = avformat_new_stream(formatContext, codec);
|
||||
if (!videoStream)
|
||||
{
|
||||
qDebug() << "Error: Failed to create video stream";
|
||||
avformat_free_context(formatContext);
|
||||
return;
|
||||
}
|
||||
// 设置视频流的参数,例如分辨率、帧率等
|
||||
videoStream->codecpar->width = width;
|
||||
videoStream->codecpar->height = height;
|
||||
videoStream->codecpar->codec_id = AV_CODEC_ID_H264; // 设置为H.264编解码器
|
||||
videoStream->codecpar->format = AV_PIX_FMT_YUV420P; // 设置为YUV420P像素格式
|
||||
|
||||
// 配置视频流的参数
|
||||
AVCodecContext* codecContext = avcodec_alloc_context3(codec);
|
||||
if (!codecContext)
|
||||
{
|
||||
qDebug() << "Error: Failed to allocate codec context";
|
||||
avformat_free_context(formatContext);
|
||||
return;
|
||||
}
|
||||
|
||||
// 设置视频流的参数,例如分辨率、帧率等
|
||||
codecContext->width = width;
|
||||
codecContext->height = height;
|
||||
codecContext->time_base = {1, framerateVideo}; // 30 frames per second
|
||||
codecContext->pix_fmt = AV_PIX_FMT_YUV420P; // 设置为YUV420P格式
|
||||
codecContext->gop_size = 1;//多少帧产生一组关键帧
|
||||
codecContext->max_b_frames = 1;//b帧,参考帧
|
||||
// codecContext->bit_rate = 1000000; // 设置比特率为 1000000
|
||||
|
||||
// 打开视频编码器
|
||||
if (avcodec_open2(codecContext, codec, nullptr) < 0)
|
||||
{
|
||||
qDebug() << "Error: Failed to open codec";
|
||||
avcodec_free_context(&codecContext);
|
||||
avformat_free_context(formatContext);
|
||||
return;
|
||||
}
|
||||
|
||||
// 打开输出文件
|
||||
if (avio_open(&formatContext->pb, outputVideoPath, AVIO_FLAG_WRITE) < 0)
|
||||
{
|
||||
qDebug() << "Error: Failed to open output file";
|
||||
avcodec_close(codecContext);
|
||||
avcodec_free_context(&codecContext);
|
||||
avformat_free_context(formatContext);
|
||||
return;
|
||||
}
|
||||
|
||||
// 写入文件头
|
||||
avformat_write_header(formatContext, nullptr);
|
||||
|
||||
// 使用sws_scale进行颜色空间转换
|
||||
SwsContext* swsContext = sws_getContext(width, height, AV_PIX_FMT_BGR24,
|
||||
width, height, AV_PIX_FMT_YUV420P,
|
||||
SWS_BICUBIC, nullptr, nullptr, nullptr);
|
||||
if (!swsContext)
|
||||
{
|
||||
qDebug() << "Error: Failed to create sws context";
|
||||
avio_closep(&formatContext->pb);
|
||||
avcodec_close(codecContext);
|
||||
avcodec_free_context(&codecContext);
|
||||
avformat_free_context(formatContext);
|
||||
return;
|
||||
}
|
||||
|
||||
// 创建 AVFrame 作为目标图像
|
||||
AVFrame* dstFrame = av_frame_alloc();
|
||||
av_image_alloc(dstFrame->data, dstFrame->linesize, width, height, AV_PIX_FMT_YUV420P, 1);
|
||||
|
||||
// 设置目标图像参数
|
||||
dstFrame->width = width;
|
||||
dstFrame->height = height;
|
||||
dstFrame->format = AV_PIX_FMT_YUV420P;
|
||||
|
||||
AVFrame* frame = av_frame_alloc();
|
||||
av_image_alloc(frame->data, frame->linesize, width, height, AV_PIX_FMT_BGR24, 1);
|
||||
|
||||
double framerate = getFramerate();
|
||||
int pushFlowFactor = framerate/framerateVideo;
|
||||
|
||||
FILE * fp= fopen("/media/nvme/delete/300tc_fp.h264","wb");
|
||||
|
||||
QUdpSocket * m_udpSocket = new QUdpSocket();
|
||||
m_udpSocket->bind(PUSH_FLOW_PORT, QUdpSocket::ShareAddress);
|
||||
QHostAddress m_clientIpAddress=QHostAddress(QHostAddress::LocalHost);
|
||||
// QHostAddress m_clientIpAddress("192.168.1.21");
|
||||
// QHostAddress m_clientIpAddress("192.168.111.1");
|
||||
int udpSendCounter=0;
|
||||
int encodeCounter=0;
|
||||
//tc--------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
m_imager.start();
|
||||
struct timeval timeStart, timeEnd;
|
||||
double runTime=0;
|
||||
@ -606,6 +724,48 @@ void XimeaImager::startRecord(double TimeDifferenceBetweensOSAndSbg,QString base
|
||||
timeDifferenceBetweenSbgAndXimea = calculateTimeDifferenceBetweenSbgAndximea(&m_imager.m_image, TimeDifferenceBetweensOSAndSbg);
|
||||
}
|
||||
fwrite(m_imager.m_image.bp,1,m_iFrameSizeInByte, hFile);
|
||||
//构造rgb图像,用于推流到m300遥控器
|
||||
m_rgbImage->FillRgbImage((unsigned short *)m_imager.m_image.bp);
|
||||
|
||||
|
||||
if (m_iFrameCounter % pushFlowFactor == 0)
|
||||
{
|
||||
memcpy(frame->data[0], m_rgbImage->m_matRgbImage->data, m_rgbImage->m_matRgbImage->rows * m_rgbImage->m_matRgbImage->step[0]);
|
||||
// memcpy(frame->data[0], m_rgbImage->m_Qphoto.bits(), m_rgbImage->m_Qphoto.byteCount());
|
||||
|
||||
// 使用sws_scale进行颜色空间转换
|
||||
sws_scale(swsContext, frame->data, frame->linesize, 0, height,
|
||||
dstFrame->data, dstFrame->linesize);
|
||||
dstFrame->pts = encodeCounter;
|
||||
|
||||
// 将AVFrame编码为视频帧
|
||||
AVPacket pkt;
|
||||
av_init_packet(&pkt);
|
||||
pkt.data = nullptr;
|
||||
pkt.size = 0;
|
||||
|
||||
if (avcodec_send_frame(codecContext, dstFrame) == 0 &&
|
||||
avcodec_receive_packet(codecContext, &pkt) == 0)
|
||||
{
|
||||
// fwrite(pkt.data, 1, pkt.size, fp);
|
||||
//
|
||||
m_udpSocket->writeDatagram((const char *)pkt.data,pkt.size,m_clientIpAddress, PUSH_FLOW_PORT);
|
||||
//
|
||||
// std::cout<< "第 " << m_iFrameCounter<< " 帧," << "编码第 " << udpSendCounter << " 帧数据大小: " << pkt.size << std::endl;
|
||||
// std::cout<< "pkt.pts: " << pkt.pts << std::endl;
|
||||
// std::cout<< "pkt.dts: " << pkt.dts << std::endl << std::endl;
|
||||
udpSendCounter++;
|
||||
|
||||
// 将编码后的帧写入文件
|
||||
// pkt.stream_index = videoStream->index;
|
||||
// av_interleaved_write_frame(formatContext, &pkt);
|
||||
// av_write_frame(formatContext, &pkt);
|
||||
|
||||
av_packet_unref(&pkt);
|
||||
}
|
||||
encodeCounter++;
|
||||
}
|
||||
|
||||
|
||||
indexofbuff = m_iFrameCounter % number_WriteDisk;
|
||||
|
||||
@ -641,6 +801,24 @@ void XimeaImager::startRecord(double TimeDifferenceBetweensOSAndSbg,QString base
|
||||
writeData2Disk->exitWriteData2Disk();
|
||||
writeHdr();
|
||||
|
||||
fclose(fp);
|
||||
// 写入文件尾
|
||||
av_write_trailer(formatContext);
|
||||
|
||||
// 释放AVFrame和相关资源
|
||||
av_freep(&frame->data[0]);
|
||||
av_frame_free(&frame);
|
||||
|
||||
// 释放资源
|
||||
sws_freeContext(swsContext);
|
||||
av_freep(&dstFrame->data[0]);
|
||||
av_frame_free(&dstFrame);
|
||||
// av_packet_free(&pkt);
|
||||
avcodec_close(codecContext);
|
||||
avcodec_free_context(&codecContext);
|
||||
avio_closep(&formatContext->pb);
|
||||
avformat_free_context(formatContext);
|
||||
|
||||
delete[] sbgTimeBuffer;
|
||||
|
||||
double frameInTheory=runTime * getFramerate();
|
||||
@ -1025,7 +1203,7 @@ void WriteData2Disk::write2Disk()
|
||||
r_qtx.unlock();
|
||||
|
||||
//构造rgb图像,用于推流到m300遥控器
|
||||
// m_rgbImage->FillRgbImage(dataBuffer);
|
||||
m_rgbImage->FillRgbImage(dataBuffer);
|
||||
|
||||
|
||||
// std::cout<<"WriteData2Disk::write2Disk-----------------------正在写磁盘!" << m_pool->max_size() <<std::endl;//
|
||||
|
Reference in New Issue
Block a user