#include #include #include #include }#include "error_code.h"#include "qtffmpegutils.h"namespace Ui {class MainWindow;}class MainWindow : public QMainWindow{Q_OBJECTpublic:explicit MainWindow(QWidget *parent = nullptr);~MainWindow();private:Ui::MainWindow *ui;AVPixelFormat mInputFormat;AVPixelFormat mOutputFormat;QLabel *mLabel;// Format debug functionQString GetAVPixelFormatString(AVPixelFormat format);void ShowImageInRGB888(RawVideoFile in);private slots:void on_OpenInputFileBtn_Clicked();void on_SelectOutputFileBtn_Clicked();void on_StartConvertBtn_Clicked();void on_ShowInputImageBtn_Clicked();void on_ShowOutputImageBtn_Clicked();void on_InputFormatComboBox_Activated(int item_idx);void on_OutputFormatComboBox_Activated(int item_idx);void on_AboutActicon_Clicked(bool trigger);};#endif // MAINWINDOW_H
.cpp 逻辑如下,主要处理一些按键之类的逻辑和显示 :
#include "mainwindow.h"#include "ui_mainwindow.h"#include "qtffmpegutils.h"#include #include #include #include #include #include #define WIDTH_IN_4K3840#define HEIGHT_IN_4K2160#define MAX_SUPPORT_WIDTH(WIDTH_IN_4K)#define MAX_SUPPORT_HEIGHT(HEIGHT_IN_4K)#define RGB888_IN_BYTE(8)typedef struct _DebugFormatInfo {AVPixelFormat format;QString string;} _DebugFormatInfo_t;static AVPixelFormat ComboxSupportFormatList[] = {AV_PIX_FMT_YUV420P,AV_PIX_FMT_NV12,AV_PIX_FMT_NV21,AV_PIX_FMT_YUV422P,AV_PIX_FMT_NV16,AV_PIX_FMT_YUYV422,AV_PIX_FMT_UYVY422,AV_PIX_FMT_YVYU422,AV_PIX_FMT_RGB24,AV_PIX_FMT_BGR24,AV_PIX_FMT_ARGB,AV_PIX_FMT_RGBA,AV_PIX_FMT_RGB565BE,AV_PIX_FMT_RGB565LE,AV_PIX_FMT_RGB444BE,AV_PIX_FMT_RGB444LE};static _DebugFormatInfo_t DebugFormatInfo[] = {{AV_PIX_FMT_YUV420P,"AV_PIX_FMT_YUV420P "},{AV_PIX_FMT_NV12,"AV_PIX_FMT_NV12"},{AV_PIX_FMT_NV21,"AV_PIX_FMT_NV21"},{AV_PIX_FMT_YUV422P,"AV_PIX_FMT_YUV422P "},{AV_PIX_FMT_NV16,"AV_PIX_FMT_NV16"},{AV_PIX_FMT_YUYV422,"AV_PIX_FMT_YUYV422 "},{AV_PIX_FMT_UYVY422,"AV_PIX_FMT_UYVY422 "},{AV_PIX_FMT_YVYU422,"AV_PIX_FMT_YVYU422 "},{AV_PIX_FMT_RGB24,"AV_PIX_FMT_RGB24"},{AV_PIX_FMT_BGR24,"AV_PIX_FMT_BGR24"},{AV_PIX_FMT_ARGB,"AV_PIX_FMT_ARGB"},{AV_PIX_FMT_RGBA,"AV_PIX_FMT_RGBA"},{AV_PIX_FMT_RGB565BE,"AV_PIX_FMT_RGB565BE"},{AV_PIX_FMT_RGB565LE,"AV_PIX_FMT_RGB565LE"},{AV_PIX_FMT_RGB444BE,"AV_PIX_FMT_RGB444BE"},{AV_PIX_FMT_RGB444LE,"AV_PIX_FMT_RGB444LE"}};MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow){ui->setupUi(this);qDebug("AVCodeC Version = %s", av_version_info());mInputFormat= ComboxSupportFormatList[0];mOutputFormat = ComboxSupportFormatList[0];mLabel = new QLabel();ui->lineEdit_Input_W->setValidator(new QIntValidator(0, MAX_SUPPORT_WIDTH, this));ui->lineEdit_Input_H->setValidator(new QIntValidator(0, MAX_SUPPORT_HEIGHT, this));ui->lineEdit_Output_W->setValidator(new QIntValidator(0, MAX_SUPPORT_WIDTH, this));ui->lineEdit_Output_H->setValidator(new QIntValidator(0, MAX_SUPPORT_HEIGHT, this));connect(ui->Btn_OpenInputFile, SIGNAL(clicked()), this, SLOT(on_OpenInputFileBtn_Clicked()));connect(ui->Btn_SelectOutputFile, SIGNAL(clicked()), this, SLOT(on_SelectOutputFileBtn_Clicked()));connect(ui->Btn_ShowInputImage, SIGNAL(clicked()), this, SLOT(on_ShowInputImageBtn_Clicked()));connect(ui->Btn_ShowOutputImage, SIGNAL(clicked()), this, SLOT(on_ShowOutputImageBtn_Clicked()));connect(ui->Btn_StartConvert, SIGNAL(clicked()), this, SLOT(on_StartConvertBtn_Clicked()));connect(ui->comboBoxIn, SIGNAL(activated(int)), this, SLOT(on_InputFormatComboBox_Activated(int)));connect(ui->comboBoxOut,SIGNAL(activated(int)), this, SLOT(on_OutputFormatComboBox_Activated(int)));connect(ui->actionVersion, SIGNAL(triggered(bool)), this, SLOT(on_AboutActicon_Clicked(bool)));}MainWindow::~MainWindow(){delete ui;}void MainWindow::on_OpenInputFileBtn_Clicked(){QFileDialog *fileDialog = new QFileDialog(this);fileDialog->setWindowTitle(tr("Open File"));fileDialog->setDirectory(".");fileDialog->setViewMode(QFileDialog::Detail);if(fileDialog->exec() == QDialog::Accepted) {QString path = fileDialog->selectedFiles()[0];QFileInfo fileInfo(path);if (fileInfo.isFile()) {ui->Label_InputFilePath->setText(path);} else {QMessageBox::information(nullptr, tr("Warning"), tr("Not exist :") + path);}}}void MainWindow::on_SelectOutputFileBtn_Clicked(){QFileDialog *fileDialog = new QFileDialog(this);fileDialog->setWindowTitle(tr("Select Location"));fileDialog->setDirectory(".");fileDialog->setViewMode(QFileDialog::Detail);if(fileDialog->exec() == QDialog::Accepted) {QString path = fileDialog->selectedFiles()[0];QFileInfo fileInfo(path);if (fileInfo.isFile()) {QMessageBox::StandardButton rb = QMessageBox::question(nullptr,tr("Question"),tr("Output File exist, Do you want cover it?"),QMessageBox::Yes | QMessageBox::No,QMessageBox::Yes);if(rb == QMessageBox::Yes) {ui->Label_OutputFilePath->setText(path);}} else {ui->Label_OutputFilePath->setText(path);}}}void MainWindow::on_ShowInputImageBtn_Clicked(){bool ok = false;int input_w, input_h;QString input_file_path;RawVideoFile video_info;input_w = ui->lineEdit_Input_W->text().toInt(&ok, 10);input_h = ui->lineEdit_Input_H->text().toInt(&ok, 10);// Check if the input file not be setinput_file_path = ui->Label_InputFilePath->text();if (input_file_path.isEmpty()){QMessageBox::information(nullptr, tr("Warning"), tr("Select a input file first"));return;}std::string in_str = input_file_path.toStdString();video_info.width = input_w;video_info.height = input_h;video_info.format = mInputFormat;video_info.filename = in_str.c_str();ShowImageInRGB888(video_info);}void MainWindow::on_ShowOutputImageBtn_Clicked(){bool ok = false;int output_w, output_h;QString output_file_path;RawVideoFile video_info;output_w = ui->lineEdit_Output_W->text().toInt(&ok, 10);output_h = ui->lineEdit_Output_H->text().toInt(&ok, 10);// Check if the output file not be setoutput_file_path = ui->Label_OutputFilePath->text();if (output_file_path.isEmpty()){QMessageBox::information(nullptr, tr("Warning"), tr("Select a input file first"));return;}std::string in_str = output_file_path.toStdString();video_info.width = output_w;video_info.height = output_h;video_info.format = mOutputFormat;video_info.filename = in_str.c_str();ShowImageInRGB888(video_info);}void MainWindow::on_InputFormatComboBox_Activated(int item_idx){mInputFormat = ComboxSupportFormatList[item_idx];qDebug("InputFormatComboBox Item=%d", item_idx);}void MainWindow::on_OutputFormatComboBox_Activated(int item_idx){mOutputFormat = ComboxSupportFormatList[item_idx];qDebug("OutputFormatComboBox Item=%d", item_idx);}void MainWindow::on_StartConvertBtn_Clicked(){bool ok = false;int input_w, input_h, output_w, output_h;QString input_file_path, output_file_path;RawVideoFile in;RawVideoFile out;int ret;// Check if the input file is nullif (ui->lineEdit_Input_W->text().isEmpty()||ui->lineEdit_Input_H->text().isEmpty()||ui->lineEdit_Output_H->text().isEmpty() ||ui->lineEdit_Output_W->text().isEmpty()){QMessageBox::information(nullptr, tr("Warning"), tr("Please add input/output width/height info!"));return;}input_w = ui->lineEdit_Input_W->text().toInt(&ok, 10);if (ok != true){QMessageBox::information(nullptr, tr("Warning"), tr("input_w String to Int failed"));return;}input_h = ui->lineEdit_Input_H->text().toInt(&ok, 10);if (ok != true){QMessageBox::information(nullptr, tr("Warning"), tr("input_h String to Int failed"));return;}output_w = ui->lineEdit_Output_W->text().toInt(&ok, 10);if (ok != true){QMessageBox::information(nullptr, tr("Warning"), tr("output_w String to Int failed"));return;}output_h = ui->lineEdit_Output_H->text().toInt(&ok, 10);if (ok != true){QMessageBox::information(nullptr, tr("Warning"), tr("output_h String to Int failed"));return;}// Check if the input file not be setinput_file_path = ui->Label_InputFilePath->text();if (input_file_path.isEmpty()){QMessageBox::information(nullptr, tr("Warning"), tr("Select a input file first"));return;}// Check if the output file not be setoutput_file_path = ui->Label_OutputFilePath->text();if (output_file_path.isEmpty()){QMessageBox::information(nullptr, tr("Warning"), tr("Select a output file"));return;}std::string in_str = input_file_path.toStdString();std::string out_str = output_file_path.toStdString();in.width = input_w;in.height = input_h;in.format = mInputFormat;in.filename = in_str.c_str();out.width = output_w;out.height = output_h;out.format = mOutputFormat;out.filename = out_str.c_str();qDebug("StartConvert input w=%d, h=%d", input_w, input_h);qDebug("input format=%s", GetAVPixelFormatString(mInputFormat).toStdString().data());qDebug("output w=%d, h=%d", output_w, output_h);qDebug("output format=%s", GetAVPixelFormatString(mOutputFormat).toStdString().data());ret = QtFFmpegUtils::convertRawVideo(in, out);if (ret == APP_SUCCESS) {QMessageBox::information(nullptr, tr("Congratulation!"), tr("Convert Successfully"),QMessageBox::Yes, QMessageBox::Yes);} else {QMessageBox::information(nullptr, tr("Oops"), tr("Convert Failed : ") + QString::number(ret, 10),QMessageBox::Yes, QMessageBox::Yes);}}void MainWindow::ShowImageInRGB888(RawVideoFile in){uint8_t *rgb888_data = http://www.kingceram.com/post/nullptr;int ret = APP_SUCCESS;int buffer_size = 0;buffer_size = QtFFmpegUtils::getBufferSize(AV_PIX_FMT_RGB24, in.width, in.height);//rgb888_data = (uint8_t *)malloc(in.width * in.height * RGB888_IN_BYTE);rgb888_data = (uint8_t *)malloc(buffer_size);if (rgb888_data == nullptr) {QMessageBox::information(nullptr, tr("Warning"), tr("Malloc failed"));return;}ret = QtFFmpegUtils::convertToRGB888(in, rgb888_data);if (ret == APP_SUCCESS) {qDebug("Convert to RGB888 Successful");} else {QMessageBox::information(nullptr, tr("Oops"), tr("Show Image Failed : ") + QString::number(ret, 10),QMessageBox::Yes, QMessageBox::Yes);}QImage *qimage = new QImage(rgb888_data, in.width, in.height, QImage::Format_RGB888);QPixmap pixmap = QPixmap::fromImage(*qimage);//mLabel->setGeometry(100, 100, in.width, in.height);mLabel->setMaximumSize(in.width, in.height);mLabel->resize(in.width, in.height);mLabel->setPixmap(pixmap);mLabel->show();free(rgb888_data);}void MainWindow::on_AboutActicon_Clicked(bool trigger){QMessageBox MBox;MBox.setWindowTitle("CSC Tools");MBox.setText("Powered by StephenZhou\n\nSoftware Version : V1.0 Beta \n\nGenerate at 2022.10.29");MBox.setIconPixmap(QPixmap(":/logo.ico"));MBox.exec();}QString MainWindow::GetAVPixelFormatString(AVPixelFormat format){QString str = "N/A";for (unsigned int i = 0; i