Commit 4adc12a2 by lenz su

编解码优化

parent cdc38f18
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject> <!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.10.2, 2020-01-07T23:45:37. --> <!-- Written by QtCreator 4.10.2, 2020-01-08T10:10:24. -->
<qtcreator> <qtcreator>
<data> <data>
<variable>EnvironmentId</variable> <variable>EnvironmentId</variable>
......
No preview for this file type
...@@ -36,7 +36,7 @@ void MainWindow::initVideoRecoder(){ ...@@ -36,7 +36,7 @@ void MainWindow::initVideoRecoder(){
recordVideo = new GetVideoThread(); recordVideo = new GetVideoThread();
recordVideo->setHidDevice(usb->getHidDevice()); recordVideo->setHidDevice(usb->getHidDevice());
isVedioRecordInitSuccessful=recordVideo->init(); isVedioRecordInitSuccessful=recordVideo->init();
recordVideo->setQuantity(400); recordVideo->setQuantity(50);
} }
void MainWindow::initAudioRecoder(){ void MainWindow::initAudioRecoder(){
...@@ -61,11 +61,10 @@ void MainWindow::onRole(int s){ ...@@ -61,11 +61,10 @@ void MainWindow::onRole(int s){
sUI->label->setText(roleStr); sUI->label->setText(roleStr);
break; break;
case SCREEN_SHOT_START:{ case SCREEN_SHOT_START:{
QDateTime current_date_time =QDateTime::currentDateTime();
cout<<"start time"<<current_date_time.toString("yyyyMMddhhmmsszzz").toStdString()<<endl;
if(isFirstStartComplete==false){ if(isFirstStartComplete==false){
if (isVedioRecordInitSuccessful== SUCCEED) if (isVedioRecordInitSuccessful== SUCCEED)
{ {
// cout<<"delay test send frame to usb"<<QDateTime::currentDateTime().toString("hhmmsszzz").toStdString()<<endl;
recordVideo->start(); recordVideo->start();
} }
// recordAudio->start(); // recordAudio->start();
...@@ -77,8 +76,8 @@ void MainWindow::onRole(int s){ ...@@ -77,8 +76,8 @@ void MainWindow::onRole(int s){
break; break;
} }
case SCREEN_SHOT_STOP:{ case SCREEN_SHOT_STOP:{
/* recordVideo->pauseRecord(); recordVideo->pauseRecord();
cout<<"stop bbbbb"<<endl;*/ cout<<"stop bbbbb"<<endl;
break; break;
} }
} }
......
...@@ -101,7 +101,7 @@ ErroCode GetVideoThread::init() ...@@ -101,7 +101,7 @@ ErroCode GetVideoThread::init()
#if defined(WIN32) #if defined(WIN32)
AVInputFormat *ifmt = av_find_input_format("dshow"); //使用dshow AVInputFormat *ifmt = av_find_input_format("dshow"); //使用dshow
if(avformat_open_input(&pFormatCtx, "video=screen-capture-recorder", ifmt, nullptr)!=0) //avformat_close_input if(avformat_open_input(&pFormatCtx, "video=screen-capture-recorder", ifmt, nullptr)!=0) //avformat_close_input
{ {
...@@ -269,10 +269,14 @@ void GetVideoThread::run() ...@@ -269,10 +269,14 @@ void GetVideoThread::run()
int64_t timeIndex = 0; int64_t timeIndex = 0;
bool m_saveVideoFileThread = true; bool m_saveVideoFileThread = true;
int i=0;
while(m_isRun ) while(m_isRun )
{ {
if(i>100){
msleep(1000);
continue;
}
i++;
if (av_read_frame(pFormatCtx, packet)<0) if (av_read_frame(pFormatCtx, packet)<0)
{ {
fprintf(stderr, "read failed! \n"); fprintf(stderr, "read failed! \n");
...@@ -287,9 +291,6 @@ void GetVideoThread::run() ...@@ -287,9 +291,6 @@ void GetVideoThread::run()
continue; continue;
} }
QDateTime current_date_time =QDateTime::currentDateTime();
cout<<"frame_read"<<current_date_time.toString("yyyyMMddhhmmsszzz").toStdString()<<endl;
if(packet->stream_index==videoindex) if(packet->stream_index==videoindex)
{ {
int64_t time = 0; int64_t time = 0;
...@@ -327,6 +328,7 @@ void GetVideoThread::run() ...@@ -327,6 +328,7 @@ void GetVideoThread::run()
uint8_t * picture_buf = (uint8_t *)av_malloc(yuvSize); uint8_t * picture_buf = (uint8_t *)av_malloc(yuvSize);
memcpy(picture_buf, out_buffer, yuvSize); memcpy(picture_buf, out_buffer, yuvSize);
mVideoEncoder->inputYuvBuffer(picture_buf, yuvSize, time); //将yuv数据添加到h.264编码的线程 mVideoEncoder->inputYuvBuffer(picture_buf, yuvSize, time); //将yuv数据添加到h.264编码的线程
// cout<<"delay test read frame"<<QDateTime::currentDateTime().toString("hhmmsszzz").toStdString()<<endl;
} }
} }
......
...@@ -16,8 +16,7 @@ USBOperator::USBOperator(QObject *parent) : QThread(parent){ ...@@ -16,8 +16,7 @@ USBOperator::USBOperator(QObject *parent) : QThread(parent){
qDebug("hid_set_nonblocking error"); qDebug("hid_set_nonblocking error");
} }
QDateTime current_date_time =QDateTime::currentDateTime();
ssid ="pair-"+current_date_time.toString("yyyyMMddhhmmsszzz");
} }
hid_device* USBOperator::getHidDevice(){ hid_device* USBOperator::getHidDevice(){
return hid_fd; return hid_fd;
......
...@@ -15,7 +15,6 @@ ...@@ -15,7 +15,6 @@
#include <QDateTime> #include <QDateTime>
using namespace std; using namespace std;
//#define ENCODE_H265
#define MAX_PAYLOAD 1012 #define MAX_PAYLOAD 1012
void VideoEncoder::setHidDevice(hid_device *hid_fd){ void VideoEncoder::setHidDevice(hid_device *hid_fd){
...@@ -89,11 +88,6 @@ void VideoEncoder::run() ...@@ -89,11 +88,6 @@ void VideoEncoder::run()
AVPacket pkt; AVPacket pkt;
av_new_packet(&pkt, y_size*3);//zcq.... av_new_packet(&pkt, y_size*3);//zcq....
#ifdef ENCODE_H265
#else
char buff[2048];
if (hid_fd == nullptr) if (hid_fd == nullptr)
qDebug("usb hid open failed!"); qDebug("usb hid open failed!");
if (hid_set_nonblocking(hid_fd, 1) < 0) { if (hid_set_nonblocking(hid_fd, 1) < 0) {
...@@ -101,10 +95,12 @@ void VideoEncoder::run() ...@@ -101,10 +95,12 @@ void VideoEncoder::run()
return; return;
} }
static char usb_buf[1025] = {0}; static char usb_buf[2049] = {0};
usb_fram_t *usb_fram = (usb_fram_t *)&usb_buf[1]; usb_fram_t *usb_fram = (usb_fram_t *)&usb_buf[1];
usb_fram_t *usb_fram2 = (usb_fram_t *)&usb_buf[1025];
int frameIndex=1;
usb_fram->head = 0x44332211; usb_fram->head = 0x44332211;
#endif usb_fram2->head = 0x44332211;
while(1) while(1)
{ {
...@@ -118,6 +114,7 @@ void VideoEncoder::run() ...@@ -118,6 +114,7 @@ void VideoEncoder::run()
// cout<<"frame_read aaaa"<<current_date_time.toString("yyyyMMddhhmmsszzz").toStdString()<<endl; // cout<<"frame_read aaaa"<<current_date_time.toString("yyyyMMddhhmmsszzz").toStdString()<<endl;
FrameDataNode node = mYuvBufferList.front(); //取出一帧yuv数据 FrameDataNode node = mYuvBufferList.front(); //取出一帧yuv数据
//cout<<"frame_read aaaab"<<node.time<<endl; //cout<<"frame_read aaaab"<<node.time<<endl;
mYuvBufferList.pop_front(); mYuvBufferList.pop_front();
...@@ -128,6 +125,7 @@ void VideoEncoder::run() ...@@ -128,6 +125,7 @@ void VideoEncoder::run()
} }
memcpy(picture_buf, node.buffer, node.size); memcpy(picture_buf, node.buffer, node.size);
cout<<"delay test send codec"<<QDateTime::currentDateTime().toString("hhmmsszzz").toStdString()<<endl;
int ret = avcodec_send_frame(pCodecCtx, picture); int ret = avcodec_send_frame(pCodecCtx, picture);
...@@ -143,9 +141,6 @@ void VideoEncoder::run() ...@@ -143,9 +141,6 @@ void VideoEncoder::run()
while (0 == avcodec_receive_packet(pCodecCtx, &pkt)){ while (0 == avcodec_receive_packet(pCodecCtx, &pkt)){
int pkt_len = pkt.size; int pkt_len = pkt.size;
uint8_t *p = pkt.data; uint8_t *p = pkt.data;
if (pkt_len > 0) {
memset(buff, 0x00, sizeof (buff));
}
while(pkt_len>0) while(pkt_len>0)
{ {
if(p == nullptr) break; if(p == nullptr) break;
...@@ -153,26 +148,41 @@ void VideoEncoder::run() ...@@ -153,26 +148,41 @@ void VideoEncoder::run()
{ {
//offset = (char *)&usb_fram - (char*)&usb_fram->length; //offset = (char *)&usb_fram - (char*)&usb_fram->length;
//memset((char *)&usb_fram->length, 0x00, offset); //memset((char *)&usb_fram->length, 0x00, offset);
memcpy(usb_fram->buff, p, MAX_PAYLOAD); if(frameIndex==1){
usb_fram->length = MAX_PAYLOAD; memcpy(usb_fram->buff, p, MAX_PAYLOAD);
usb_fram->id = ++fram_id; usb_fram->length = MAX_PAYLOAD;
usb_fram->id = ++fram_id;
res = hid_write(hid_fd, (const unsigned char *)usb_buf, 1025); frameIndex=2;
}else{
memcpy(usb_fram2->buff, p, MAX_PAYLOAD);
usb_fram2->length = MAX_PAYLOAD;
usb_fram2->id = ++fram_id;
frameIndex=1;
res = hid_write(hid_fd, (const unsigned char *)usb_buf, 2049);
}
// cout<<"delay test send frame to usb"<<QDateTime::currentDateTime().toString("hhmmsszzz").toStdString()<<endl;
p+=MAX_PAYLOAD; p+=MAX_PAYLOAD;
pkt_len-=MAX_PAYLOAD; pkt_len-=MAX_PAYLOAD;
} }
else{ else{
if(frameIndex==1){
memcpy(usb_fram->buff, p, pkt_len);
usb_fram->length = pkt_len;
usb_fram->id = ++fram_id;
cout<<"delay test send frame to usb"<<QDateTime::currentDateTime().toString("hhmmsszzz").toStdString()<<endl;
res = hid_write(hid_fd, (const unsigned char *)usb_buf, 2049);
}else{
memcpy(usb_fram2->buff, p, pkt_len);
usb_fram2->length = pkt_len;
usb_fram2->id = ++fram_id;
cout<<"delay test send frame to usb"<<QDateTime::currentDateTime().toString("hhmmsszzz").toStdString()<<endl;
res = hid_write(hid_fd, (const unsigned char *)usb_buf, 2049);
}
//memset((char *)&usb_fram->length, 0x00, sizeof (usb_fram)-8); //memset((char *)&usb_fram->length, 0x00, sizeof (usb_fram)-8);
memcpy(usb_fram->buff, p, pkt_len); frameIndex=1;
usb_fram->length = pkt_len;
usb_fram->id = ++fram_id;
res = hid_write(hid_fd, (const unsigned char *)usb_buf, 1025);
pkt_len = 0; pkt_len = 0;
} }
memset(buff, 0x00, sizeof (buff));
sprintf(buff, "fram id:%d %d\n", fram_id, usb_fram->length);
} }
} }
...@@ -212,7 +222,6 @@ AVDictionary *VideoEncoder::setEncoderParam(const AVCodecID &codec_id) ...@@ -212,7 +222,6 @@ AVDictionary *VideoEncoder::setEncoderParam(const AVCodecID &codec_id)
{ {
int in_w = mWidth; int in_w = mWidth;
int in_h = mHeight;//宽高 int in_h = mHeight;//宽高
qDebug() << "" << __FUNCTION__;
pCodecCtx->codec_id = codec_id; pCodecCtx->codec_id = codec_id;
pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO; pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;
...@@ -222,7 +231,8 @@ AVDictionary *VideoEncoder::setEncoderParam(const AVCodecID &codec_id) ...@@ -222,7 +231,8 @@ AVDictionary *VideoEncoder::setEncoderParam(const AVCodecID &codec_id)
pCodecCtx->time_base.num = 1; pCodecCtx->time_base.num = 1;
pCodecCtx->time_base.den = 30;//帧率(既一秒钟多少张图片) pCodecCtx->time_base.den = 30;//帧率(既一秒钟多少张图片)
pCodecCtx->bit_rate = mBitRate; //比特率(调节这个大小可以改变编码后视频的质量) pCodecCtx->bit_rate = mBitRate; //比特率(调节这个大小可以改变编码后视频的质量)
pCodecCtx->gop_size=30*2; pCodecCtx->gop_size=30*1;
pCodecCtx->max_b_frames = 0;
//H264 还可以设置很多参数 自行研究吧 //H264 还可以设置很多参数 自行研究吧
//// pCodecCtx->me_range = 16; //// pCodecCtx->me_range = 16;
//// pCodecCtx->max_qdiff = 4; //// pCodecCtx->max_qdiff = 4;
...@@ -230,13 +240,11 @@ AVDictionary *VideoEncoder::setEncoderParam(const AVCodecID &codec_id) ...@@ -230,13 +240,11 @@ AVDictionary *VideoEncoder::setEncoderParam(const AVCodecID &codec_id)
//// pCodecCtx->qmin = 10; //// pCodecCtx->qmin = 10;
//// pCodecCtx->qmax = 51; //// pCodecCtx->qmax = 51;
// //Optional Param // ofstream oFile;
// pCodecCtx->max_b_frames=3; // oFile.open("test.csv",ios::out|ios::trunc);
ofstream oFile; // oFile<<"width"<<","<<"height"<<","<<"pix_fmt"<<","<<"fps"<<endl;
oFile.open("test.csv",ios::out|ios::trunc); // oFile<< pCodecCtx->width<<","<<pCodecCtx->height<<","<<"AV_PIX_FMT_YUV420P"<<","<<pCodecCtx->time_base.den<<endl;
oFile<<"width"<<","<<"height"<<","<<"pix_fmt"<<","<<"fps"<<endl; // oFile.close();
oFile<< pCodecCtx->width<<","<<pCodecCtx->height<<","<<"AV_PIX_FMT_YUV420P"<<","<<pCodecCtx->time_base.den<<endl;
oFile.close();
// some formats want stream headers to be separate // some formats want stream headers to be separate
...@@ -247,14 +255,17 @@ AVDictionary *VideoEncoder::setEncoderParam(const AVCodecID &codec_id) ...@@ -247,14 +255,17 @@ AVDictionary *VideoEncoder::setEncoderParam(const AVCodecID &codec_id)
AVDictionary *param = 0; AVDictionary *param = 0;
if(pCodecCtx->codec_id == AV_CODEC_ID_H264) if(pCodecCtx->codec_id == AV_CODEC_ID_H264)
{ {
av_dict_set(&param, "preset", "veryfast", 0);
av_dict_set(&param,"start_time_realtime",0,0);
av_opt_set(pCodecCtx->priv_data, "preset", "superfast", 0);
av_opt_set(pCodecCtx->priv_data, "tune", "zerolatency", 0);
//av_dict_set(&param, "video_size","1920x1080", 0); //av_dict_set(&param, "video_size","1920x1080", 0);
//av_dict_set(&param, "video_size","1280x720", 0); //av_dict_set(&param, "video_size","1280x720", 0);
av_dict_set(&param, "bufsize","1024000", 0); // av_dict_set(&param, "bufsize","1024000", 0);
av_dict_set(&param, "fps","30", 0); // av_dict_set(&param, "fps","30", 0);
// av_dict_set(&param, "preset", "superfast", 0); // av_dict_set(&param, "preset", "superfast", 0);
av_dict_set(&param, "tune", "zerolatency", 0); //实现实时编码 // av_dict_set(&param, "tune", "zerolatency", 0); //实现实时编码
av_dict_set(&param, "profile", "main", 0); // av_dict_set(&param, "profile", "main", 0);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment