修复msvc下无法正确获取拓展名的问题, 修复视频文件结束后任然解码的问题, 添加CMake Window下编译

This commit is contained in:
JIe Jie 2024-02-20 18:37:05 +08:00
parent 3697de0b4a
commit 5cc0de59d5
6 changed files with 349 additions and 305 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
/build/* /build/*
.cache/* .cache/*
.vscode/* .vscode/*
/.vs
/CMakeSettings.json

View File

@ -6,14 +6,16 @@ set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
include_directories(${PROJECT_SOURCE_DIR}/include)
file(GLOB_RECURSE srcs ${PROJECT_SOURCE_DIR}/src/*.cc)
find_package(SDL2 REQUIRED)
IF(UNIX)
include_directories(/usr/include/x86_64-linux-gnu) include_directories(/usr/include/x86_64-linux-gnu)
set(GLAD_DIR /home/jie/documents/third/glad) set(GLAD_DIR /home/jie/documents/third/glad)
set(GLM_DIR /home/jie/documents/third/glm) set(GLM_DIR /home/jie/documents/third/glm)
include_directories(${GLAD_DIR}/include) include_directories(${GLAD_DIR}/include)
include_directories(${GLM_DIR}) include_directories(${GLM_DIR})
include_directories(${PROJECT_SOURCE_DIR}/include)
find_package(SDL2 REQUIRED)
file(GLOB_RECURSE srcs ${PROJECT_SOURCE_DIR}/src/*.cc)
add_executable(${PROJECT_N} add_executable(${PROJECT_N}
main.cc main.cc
${GLAD_DIR}/src/glad.c ${GLAD_DIR}/src/glad.c
@ -28,3 +30,47 @@ target_link_libraries(${PROJECT_N}
vorbis vorbis
avutil avutil
) )
ELSE(WIN32)
set(CMAKE_PREFIX_PATH C:/document/lib/)
set(THIRD_LIB_DIR C:/document/lib/)
#vcpkg
set(VCPKG_PACKAGE C:/document/lib/vcpkg/packages)
set(VCPKG_BUILDTREES_DIR C:/document/lib/vcpkg/buildtrees)
set(VCPKG_INSTALLED C:/document/lib/vcpkg/installed/x64-windows)
#opengl
find_package(OpenGL)
#SDL2
find_package(SDL2_image)
#rangev3
include_directories(${VCPKG_PACKAGE}/range-v3_x64-windows/include)
#ffmpeg
include_directories(${VCPKG_PACKAGE}/ffmpeg_x64-windows/include)
file(GLOB_RECURSE FFMPEG_LIBS ${VCPKG_PACKAGE}/ffmpeg_x64-windows/debug/lib/*.lib)
#glad
include_directories(${THIRD_LIB_DIR}/glad/include)
set(GLAD_SRCS ${THIRD_LIB_DIR}/glad/src/glad.c)
#glm
include_directories(${THIRD_LIB_DIR}/glm)
#opencv
include_directories(${THIRD_LIB_DIR}/opencv/opencv/build/include)
link_directories(E:/dependency/lib)
set(OpenCV_LIBS
opencv_world460d.lib
)
add_executable(${PROJECT_N}
${srcs}
main.cc
${GLAD_SRCS}
)
target_include_directories(${PROJECT_N} PUBLIC include)
target_link_libraries(${PROJECT_N}
${SDL2_LIBRARIES}
${FFMPEG_LIBS}
${OpenCV_LIBS}
OpenGL32
)
ENDIF()

View File

@ -1,25 +1,18 @@
#ifndef DECODER_H #ifndef DECODER_H
#define DECODER_H #define DECODER_H
extern "C"{ extern "C" {
#include "libavcodec/avcodec.h" #include "libavcodec/avcodec.h"
#include "libavformat/avformat.h" #include "libavformat/avformat.h"
#include "libavutil/imgutils.h" #include "libavutil/imgutils.h"
} }
struct DecoderParam{ struct DecoderParam {
AVFormatContext* fmtCtx; AVFormatContext* fmtCtx;
AVCodecContext* codecCtx; AVCodecContext* codecCtx;
int width; int width;
int height; int height;
int videoStreamIndex; int videoStreamIndex;
}; };
extern "C"{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libavutil/imgutils.h"
}
void InitDecoder(const char* filepath, DecoderParam& param); void InitDecoder(const char* filepath, DecoderParam& param);
AVFrame* RequestFrame(DecoderParam& param); AVFrame* RequestFrame(DecoderParam& param);
#endif #endif

View File

@ -6,68 +6,67 @@
#include <string> #include <string>
#include <array> #include <array>
#include <cstring> #include <cstring>
#include <string_view>
enum class FileType{ enum class FileType {
MUSIC, MUSIC,
VIDEO, VIDEO,
IMG, IMG,
ERRORTYPE ERRORTYPE
}; };
const std::array MusicFileExtensive = { const std::array<std::string_view, 1> MusicFileExtensive = {
"mp3" "mp3"
}; };
const std::array VideoFileExtensive = { const std::array<std::string_view, 1> VideoFileExtensive = {
"mp4" "mp4"
}; };
const std::array ImgFileExtensive = { const std::array<std::string_view, 3> ImgFileExtensive = {
"jpeg", "jpeg",
"jpg", "jpg",
"png", "png",
}; };
using namespace std::filesystem; using namespace std::filesystem;
class Util{ class Util {
private: private:
static const char* GetFileExtensive(path filepath){ static std::string_view GetFileExtensive(const path& filepath) {
std::string ext = filepath.extension().string(); static std::string ext = filepath.extension().string();
ext.erase(std::remove_if(ext.begin(), ext.end(), [](char c){return c=='.';}), ext.end()); ext.erase(std::ranges::remove_if(ext, [](char c) {return c == '.'; }).begin(), ext.end());
return ext.c_str(); return std::string_view{ ext.c_str() };
} }
static bool IsMusic(path filepath){ static bool IsMusic(const path& filepath) {
const auto ext = GetFileExtensive(filepath); const auto ext = GetFileExtensive(filepath);
for(const auto& it : MusicFileExtensive){ for (const auto& it : MusicFileExtensive) {
if(std::strcmp(it, ext) == 0) if (it == ext)
return true; return true;
} }
return false; return false;
} }
static bool IsVideo(path filepath){ static bool IsVideo(const path& filepath) {
const auto ext = GetFileExtensive(filepath); const auto ext = GetFileExtensive(filepath);
for(const auto& it : VideoFileExtensive){ for (const auto& it : VideoFileExtensive) {
if(std::strcmp(it, ext) == 0) if (it == ext)
return true; return true;
} }
return false; return false;
} }
static bool IsImg(path filepath){ static bool IsImg(const path& filepath) {
const auto ext = GetFileExtensive(filepath); const auto ext = GetFileExtensive(filepath);
for(const auto& it : ImgFileExtensive){ for (const auto& it : ImgFileExtensive) {
if(std::strcmp(it, ext) == 0) if (it == ext)
return true; return true;
} }
return false; return false;
} }
public: public:
static FileType GetFileType(path filepath){ static FileType GetFileType(const path& filepath) {
if(IsMusic(filepath)) return FileType::MUSIC; if (IsMusic(filepath)) return FileType::MUSIC;
if(IsVideo(filepath)) return FileType::VIDEO; if (IsVideo(filepath)) return FileType::VIDEO;
if(IsImg(filepath)) return FileType::IMG; if (IsImg(filepath)) return FileType::IMG;
return FileType::ERRORTYPE; return FileType::ERRORTYPE;
} }
}; };

29
main.cc
View File

@ -1,7 +1,6 @@
#include <iostream> #include <iostream>
#include <thread> #include <thread>
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <filesystem> #include <filesystem>
#include "util.h" #include "util.h"
@ -17,11 +16,11 @@ struct OpenglVideoParam
unsigned int texs[3]; unsigned int texs[3];
}; };
int InitVideo(SDL_Window *&window, const char *targetFilepath, DecoderParam &decoderParam, OpenglVideoParam &openglVideoParam, ShaderService *&shaderService) int InitVideo(SDL_Window*& window, const char* targetFilepath, DecoderParam& decoderParam, OpenglVideoParam& openglVideoParam, ShaderService*& shaderService)
{ {
InitDecoder(targetFilepath, decoderParam); InitDecoder(targetFilepath, decoderParam);
int client_width = decoderParam.width / 2; const int client_width = decoderParam.width / 2;
int client_height = decoderParam.height / 2; const int client_height = decoderParam.height / 2;
window = SDL_CreateWindow( window = SDL_CreateWindow(
"MP", "MP",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
@ -55,7 +54,7 @@ int InitVideo(SDL_Window *&window, const char *targetFilepath, DecoderParam &dec
1.f, 1.f, 0.0, 1.0, 0.0, 1.f, 1.f, 0.0, 1.0, 0.0,
1.f, -1.f, 0.0, 1.0, 1.0, 1.f, -1.f, 0.0, 1.0, 1.0,
-1.f, -1.f, 0.0, 0.0, 1.0, -1.f, -1.f, 0.0, 0.0, 1.0,
-1.f, 1.f, 0.0, 0.0, 0.0}; -1.f, 1.f, 0.0, 0.0, 0.0 };
unsigned int indices[]{ unsigned int indices[]{
0, 0,
1, 1,
@ -75,7 +74,7 @@ int InitVideo(SDL_Window *&window, const char *targetFilepath, DecoderParam &dec
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), 0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), 0);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *)(3 * sizeof(float))); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1); glEnableVertexAttribArray(1);
glGenTextures(3, openglVideoParam.texs); glGenTextures(3, openglVideoParam.texs);
for (int i = 0; i < 3; i++) for (int i = 0; i < 3; i++)
@ -87,20 +86,18 @@ int InitVideo(SDL_Window *&window, const char *targetFilepath, DecoderParam &dec
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
} }
shaderService = new ShaderService{vSource.data(), fSource.data()}; shaderService = new ShaderService{ vSource.data(), fSource.data() };
shaderService->Use(); shaderService->Use();
shaderService->SetUniform<int>("textureY", 0); shaderService->SetUniform<int>("textureY", 0);
shaderService->SetUniform<int>("textureU", 1); shaderService->SetUniform<int>("textureU", 1);
shaderService->SetUniform<int>("textureV", 2); shaderService->SetUniform<int>("textureV", 2);
return 0; return 0;
} }
void OpenglRender(DecoderParam &decoderParam, OpenglVideoParam &openglVideoParam, ShaderService *shaderService) void OpenglRender(DecoderParam& decoderParam, const OpenglVideoParam& openglVideoParam, ShaderService* shaderService)
{ {
auto frame = RequestFrame(decoderParam); auto frame = RequestFrame(decoderParam);
if(frame == nullptr) if (frame == nullptr)
return; return;
int64_t pts = frame->pts;
static bool first_frame = true;
// TODO: TIMER // TODO: TIMER
glBindTexture(GL_TEXTURE_2D, openglVideoParam.texs[0]); glBindTexture(GL_TEXTURE_2D, openglVideoParam.texs[0]);
glPixelStoref(GL_UNPACK_ROW_LENGTH, frame->linesize[0]); glPixelStoref(GL_UNPACK_ROW_LENGTH, frame->linesize[0]);
@ -116,18 +113,18 @@ void OpenglRender(DecoderParam &decoderParam, OpenglVideoParam &openglVideoParam
glPixelStoref(GL_UNPACK_ROW_LENGTH, frame->linesize[2]); glPixelStoref(GL_UNPACK_ROW_LENGTH, frame->linesize[2]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, frame->width / 2, frame->height / 2, 0, GL_RED, GL_UNSIGNED_BYTE, frame->data[2]); glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, frame->width / 2, frame->height / 2, 0, GL_RED, GL_UNSIGNED_BYTE, frame->data[2]);
av_frame_free(&frame);
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
shaderService->Use(); shaderService->Use();
glBindVertexArray(openglVideoParam.VAO); glBindVertexArray(openglVideoParam.VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
av_frame_free(&frame);
} }
int main(int argc, char **const argv) int main(int argc, char** const argv)
{ {
// Check File // Check File
const char *targetFilepath = argv[1]; const char* targetFilepath = argv[1];
if (targetFilepath == nullptr || !std::filesystem::exists(targetFilepath)) if (targetFilepath == nullptr || !std::filesystem::exists(targetFilepath))
{ {
cout << "File Not Exist\n"; cout << "File Not Exist\n";
@ -143,10 +140,10 @@ int main(int argc, char **const argv)
// INIT // INIT
int client_width, client_height; int client_width, client_height;
SDL_Window *window = nullptr; SDL_Window* window = nullptr;
DecoderParam decoderParam{}; DecoderParam decoderParam{};
OpenglVideoParam openglVideoParam{}; OpenglVideoParam openglVideoParam{};
ShaderService *shaderService = nullptr; ShaderService* shaderService = nullptr;
double framerate = .0f; double framerate = .0f;
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
{ {

View File

@ -1,15 +1,15 @@
#include "decoder.h" #include "decoder.h"
void InitDecoder(const char* filepath, DecoderParam& param){ void InitDecoder(const char* filepath, DecoderParam& param) {
AVFormatContext* fmtCtx = nullptr; AVFormatContext* fmtCtx = nullptr;
AVCodecContext* codecFmt = nullptr; AVCodecContext* codecFmt = nullptr;
auto ret = avformat_open_input(&fmtCtx, filepath, NULL, NULL); auto ret = avformat_open_input(&fmtCtx, filepath, NULL, NULL);
avformat_find_stream_info(fmtCtx, nullptr); avformat_find_stream_info(fmtCtx, nullptr);
for(int i=0; i<fmtCtx->nb_streams; i++){ for (int i = 0; i < fmtCtx->nb_streams; i++) {
auto stream = fmtCtx->streams[i]; const auto stream = fmtCtx->streams[i];
auto codec = avcodec_find_decoder(stream->codecpar->codec_id); const auto codec = avcodec_find_decoder(stream->codecpar->codec_id);
if(codec->type == AVMEDIA_TYPE_VIDEO){ if (codec->type == AVMEDIA_TYPE_VIDEO) {
param.videoStreamIndex = i; param.videoStreamIndex = i;
codecFmt = avcodec_alloc_context3(codec); codecFmt = avcodec_alloc_context3(codec);
avcodec_parameters_to_context(codecFmt, stream->codecpar); avcodec_parameters_to_context(codecFmt, stream->codecpar);
@ -23,30 +23,37 @@ void InitDecoder(const char* filepath, DecoderParam& param){
param.height = codecFmt->height; param.height = codecFmt->height;
} }
AVFrame* RequestFrame(DecoderParam& param){ AVFrame* RequestFrame(DecoderParam& param) {
auto& fmtCtx = param.fmtCtx; const auto& fmtCtx = param.fmtCtx;
auto& codecCtx = param.codecCtx; const auto& codecCtx = param.codecCtx;
auto& videoStreamIndex = param.videoStreamIndex; const auto& videoStreamIndex = param.videoStreamIndex;
while(true){
AVPacket* packet = av_packet_alloc(); AVPacket* packet = av_packet_alloc();
int ret = av_read_frame(fmtCtx, packet);
if(ret == 0 && packet->stream_index == videoStreamIndex){
ret = avcodec_send_packet(codecCtx, packet);
if(ret == 0){
AVFrame* frame = av_frame_alloc(); AVFrame* frame = av_frame_alloc();
while (true) {
int ret = av_read_frame(fmtCtx, packet);
if (ret == 0 && packet->stream_index == videoStreamIndex) {
ret = avcodec_send_packet(codecCtx, packet);
if (ret == 0) {
ret = avcodec_receive_frame(codecCtx, frame); ret = avcodec_receive_frame(codecCtx, frame);
if(ret == 0){ if (ret == 0) {
av_packet_unref(packet); av_packet_unref(packet);
return frame; return frame;
}else if(ret == AVERROR(EAGAIN)){ }
av_frame_unref(frame); if (ret == AVERROR(EAGAIN)) {
}else{ continue;
}
av_frame_free(&frame);
av_packet_free(&packet);
return nullptr; return nullptr;
} }
} }
else if (ret == AVERROR_EOF)
{
av_packet_unref(packet);
return nullptr;
} }
av_packet_unref(packet); av_packet_unref(packet);
} }
return nullptr;
} }