Skip to content

Commit

Permalink
1. 增加qt支持,图形显示视频
Browse files Browse the repository at this point in the history
  • Loading branch information
SnailCoderGu committed Apr 27, 2024
1 parent 07bfcfd commit 2c95afd
Show file tree
Hide file tree
Showing 44 changed files with 18,026 additions and 13 deletions.
68 changes: 68 additions & 0 deletions AudioPlayer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#pragma once

#include <QApplication>
#include <QAudioOutput>
#include <QIODevice>
#include <QDebug>

class AudioPlayer {
public:
AudioPlayer() {

}

void SetFormat(int dst_nb_samples, int rate, int sample_size, int nch) {
QAudioFormat format;
format.setSampleRate(rate); // 采样率
format.setChannelCount(nch); // 声道数
format.setSampleSize(sample_size); // 采样大小
format.setCodec("audio/pcm"); // 音频编码格式
format.setByteOrder(QAudioFormat::LittleEndian); // 字节顺序
format.setSampleType(QAudioFormat::SignedInt); // 采样类型

int len = dst_nb_samples * format.channelCount() * format.sampleSize()/8;


// 创建 QAudioOutput 对象
audioOutput = new QAudioOutput(format);

audioOutput->setVolume(1.0); // 设置音量(0.0 - 1.0)

// 打开音频输出
outputDevice = audioOutput->start();
}

void writeData(const char* data, qint64 len) {
//audioData.insert(0, data, len);

outputDevice->write(data, len);
}

void Close()
{

if (outputDevice != nullptr)
{
outputDevice->close();
delete outputDevice;
outputDevice = NULL;
}
if (audioOutput)
{
//audioOutput->stop();
audioOutput = NULL;
}



}

private:
QIODevice* outputDevice;

QAudioOutput* audioOutput;
};




26 changes: 24 additions & 2 deletions FFmpegExamples.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,28 @@
#include <libavutil/avutil.h>
}
#include "FileDecode.h"
#include <qapplication.h>
#include "MyQtMainWindow.h"
#include <thread>

int main() {
void runFFmepg(MyQtMainWindow* mywindow);

int main(int argc, char* argv[]) {


QApplication app(argc, argv);

MyQtMainWindow* qwindow = new MyQtMainWindow(NULL);
qwindow->resize(960, 400);
qwindow->show();
std::thread t1(runFFmepg, qwindow);

app.exec();


}

void runFFmepg(MyQtMainWindow* mywindow){
// 注册所有的编解码器、格式和协议
av_register_all();

Expand All @@ -16,14 +36,16 @@ int main() {
{
std::cout << "AVOpenFile Faild";
}
fileDecode.SetMyWindow(mywindow);
ret = fileDecode.OpenVideoDecode();
ret = fileDecode.OpenAudioDecode();


ret = fileDecode.Decode();
if (ret == 0)
{
std::cout << "解码音频完成" << std::endl;
}
fileDecode.Close();
return 0;
mywindow = NULL;
}
48 changes: 43 additions & 5 deletions FFmpegExamples.vcxproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
Expand All @@ -24,6 +24,8 @@
<ProjectGuid>{372fd851-32ab-42c5-8d2d-c35dcc1cecc2}</ProjectGuid>
<RootNamespace>FFmpegExamples</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
<Keyword>QtVS_v304</Keyword>
<QtMsBuild Condition="'$(QtMsBuild)'=='' OR !Exists('$(QtMsBuild)\qt.targets')">$(MSBuildProjectDirectory)\QtMsBuild</QtMsBuild>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
Expand Down Expand Up @@ -53,21 +55,42 @@
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<Import Project="$(QtMsBuild)\qt_defaults.props" Condition="Exists('$(QtMsBuild)\qt_defaults.props')" />
<PropertyGroup Label="QtSettings" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<QtModules>core</QtModules>
</PropertyGroup>
<PropertyGroup Label="QtSettings" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<QtModules>core</QtModules>
</PropertyGroup>
<PropertyGroup Label="QtSettings" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<QtInstall>5.12.0_msvc2017_64</QtInstall>
<QtModules>core;opengl;gui;multimedia;widgets;openglextensions</QtModules>
</PropertyGroup>
<PropertyGroup Label="QtSettings" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<QtModules>core</QtModules>
</PropertyGroup>
<Target Name="QtMsBuildNotFound" BeforeTargets="CustomBuild;ClCompile" Condition="!Exists('$(QtMsBuild)\qt.targets') OR !Exists('$(QtMsBuild)\Qt.props')">
<Message Importance="High" Text="QtMsBuild: could not locate qt.targets, qt.props; project may not build correctly." />
</Target>
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="$(QtMsBuild)\Qt.props" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="$(QtMsBuild)\Qt.props" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="$(QtMsBuild)\Qt.props" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="$(QtMsBuild)\Qt.props" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
Expand All @@ -76,6 +99,7 @@
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand All @@ -90,6 +114,7 @@
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand All @@ -104,14 +129,15 @@
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(SolutionDir)ffmpeg3.4-x64-debug\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(SolutionDir)libyuv\include;GeneratedFiles\$(ConfigurationName);GeneratedFiles;$(SolutionDir)ffmpeg3.4-x64-debug\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<DisableSpecificWarnings>4996;%(DisableSpecificWarnings)</DisableSpecificWarnings>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>$(SolutionDir)ffmpeg3.4-x64-debug\bin;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>avcodec.lib;avfilter.lib;avformat.lib;avutil.lib;swresample.lib;swscale.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories>$(SolutionDir)libyuv;$(SolutionDir)ffmpeg3.4-x64-debug\bin;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>avcodec.lib;avfilter.lib;avformat.lib;avutil.lib;swresample.lib;swscale.lib;opengl32.lib;yuv.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
Expand All @@ -122,6 +148,7 @@
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand All @@ -133,13 +160,24 @@
<ItemGroup>
<ClCompile Include="FFmpegExamples.cpp" />
<ClCompile Include="FileDecode.cpp" />
<ClCompile Include="MyQtMainWindow.cpp" />
<ClCompile Include="SwrResample.cpp" />
<ClCompile Include="YuvRender.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="AudioPlayer.h" />
<ClInclude Include="FileDecode.h" />
<QtMoc Include="YuvRender.h" />
<QtMoc Include="MyQtMainWindow.h" />
<ClInclude Include="SwrResample.h" />
</ItemGroup>
<ItemGroup>
<QtUic Include="MainWindow.ui">
<QtUicDir Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(SolutionDir)</QtUicDir>
</QtUic>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<Import Project="$(QtMsBuild)\qt.targets" Condition="Exists('$(QtMsBuild)\qt.targets')" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
12 changes: 12 additions & 0 deletions FFmpegExamples.vcxproj.user
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup />
<PropertyGroup Label="QtSettings" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<QtLastBackgroundBuild>2024-04-27T07:48:48.9239996Z</QtLastBackgroundBuild>
</PropertyGroup>
<PropertyGroup Label="QtSettings" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<QtLastBackgroundBuild>2024-04-27T07:48:48.9958274Z</QtLastBackgroundBuild>
</PropertyGroup>
<PropertyGroup Label="QtSettings" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<QtLastBackgroundBuild>2024-04-27T07:48:49.1263235Z</QtLastBackgroundBuild>
</PropertyGroup>
<PropertyGroup Label="QtSettings" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<QtLastBackgroundBuild>2024-04-27T07:48:49.2096193Z</QtLastBackgroundBuild>
</PropertyGroup>
</Project>
21 changes: 16 additions & 5 deletions FileDecode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ int FileDecode::OpenAudioDecode()
return -1;
}


return 0;
}

Expand All @@ -92,6 +93,8 @@ int FileDecode::OpenVideoDecode() {
return -1;
}

qtWin->initData(videoCodecCtx->width, videoCodecCtx->height);

return 0;
}

Expand All @@ -111,12 +114,14 @@ int FileDecode::Decode()
if (avpkt.stream_index == audioStream)
{
//std::cout << "read one audio frame" << std::endl;
DecodeAudio(&avpkt);
//DecodeAudio(&avpkt);
av_packet_unref(&avpkt);
//Sleep(3);
}
else if(avpkt.stream_index == videoStream) {
DecodeVideo(&avpkt);
av_packet_unref(&avpkt);
Sleep(30);
continue;
}
} while (avpkt.data == NULL);
Expand All @@ -136,9 +141,6 @@ void FileDecode::Close()
fclose(outdecodedYUVfile);
#endif // WRITE_DECODED_PCM_FILE




if (audioCodecCtx) {
avcodec_close(audioCodecCtx); // 注意这里要用关闭,不要用下面free,会不够彻底,导致avformat_close_input崩溃
//avcodec_free_context(&audioCodecCtx);
Expand All @@ -151,9 +153,16 @@ void FileDecode::Close()
if (formatCtx) {
avformat_close_input(&formatCtx);
}

qtWin->Close();

}

void FileDecode::SetMyWindow(MyQtMainWindow* mywindow)
{
this->qtWin = mywindow;
}

int FileDecode::DecodeAudio(AVPacket* originalPacket)
{
int ret = avcodec_send_packet(audioCodecCtx, originalPacket);
Expand Down Expand Up @@ -195,7 +204,7 @@ int FileDecode::DecodeAudio(AVPacket* originalPacket)

int dst_ch_layout = AV_CH_LAYOUT_STEREO;
int dst_rate = 44100;
enum AVSampleFormat dst_sample_fmt = audioCodecCtx->sample_fmt;
enum AVSampleFormat dst_sample_fmt = AV_SAMPLE_FMT_S16;

//aac编码一般是这个,实际这个值只能从解码后的数据里面获取,所有这个初始化过程可以放在解码出第一帧的时候
int src_nb_samples = frame->nb_samples;
Expand Down Expand Up @@ -251,6 +260,8 @@ int FileDecode::DecodeVideo(AVPacket* originalPacket)

#endif // WRITE_DECODED_PCM_FILE

qtWin->updateYuv(frame->data[0], frame->data[1], frame->data[2]);

av_frame_free(&frame);

}
Expand Down
9 changes: 9 additions & 0 deletions FileDecode.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include <string>
#include <iostream>
#include "SwrResample.h"
#include "MyQtMainWindow.h"


#define WRITE_DECODED_PCM_FILE
#define WRITE_DECODED_YUV_FILE
Expand All @@ -22,6 +24,9 @@ class FileDecode
int OpenVideoDecode();
int Decode();
void Close();

void SetMyWindow(MyQtMainWindow* mywindow);

private:
int DecodeAudio(AVPacket* originalPacket);
int DecodeVideo(AVPacket* originalPacket);
Expand All @@ -46,5 +51,9 @@ class FileDecode

private:
SwrResample* swrResample = NULL;

MyQtMainWindow* qtWin = NULL;


};

Loading

0 comments on commit 2c95afd

Please sign in to comment.