修复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/*
.cache/*
.vscode/*
/.vs
/CMakeSettings.json

View File

@ -6,14 +6,16 @@ set(CMAKE_CXX_STANDARD 20)
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)
set(GLAD_DIR /home/jie/documents/third/glad)
set(GLM_DIR /home/jie/documents/third/glm)
include_directories(${GLAD_DIR}/include)
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}
main.cc
${GLAD_DIR}/src/glad.c
@ -28,3 +30,47 @@ target_link_libraries(${PROJECT_N}
vorbis
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
#define DECODER_H
extern "C"{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libavutil/imgutils.h"
extern "C" {
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libavutil/imgutils.h"
}
struct DecoderParam{
struct DecoderParam {
AVFormatContext* fmtCtx;
AVCodecContext* codecCtx;
int width;
int height;
int videoStreamIndex;
};
extern "C"{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libavutil/imgutils.h"
}
void InitDecoder(const char* filepath, DecoderParam& param);
AVFrame* RequestFrame(DecoderParam& param);
#endif

View File

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

29
main.cc
View File

@ -1,7 +1,6 @@
#include <iostream>
#include <thread>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <filesystem>
#include "util.h"
@ -17,11 +16,11 @@ struct OpenglVideoParam
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);
int client_width = decoderParam.width / 2;
int client_height = decoderParam.height / 2;
const int client_width = decoderParam.width / 2;
const int client_height = decoderParam.height / 2;
window = SDL_CreateWindow(
"MP",
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, 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[]{
0,
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);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), 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);
glGenTextures(3, openglVideoParam.texs);
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);
}
shaderService = new ShaderService{vSource.data(), fSource.data()};
shaderService = new ShaderService{ vSource.data(), fSource.data() };
shaderService->Use();
shaderService->SetUniform<int>("textureY", 0);
shaderService->SetUniform<int>("textureU", 1);
shaderService->SetUniform<int>("textureV", 2);
return 0;
}
void OpenglRender(DecoderParam &decoderParam, OpenglVideoParam &openglVideoParam, ShaderService *shaderService)
void OpenglRender(DecoderParam& decoderParam, const OpenglVideoParam& openglVideoParam, ShaderService* shaderService)
{
auto frame = RequestFrame(decoderParam);
if(frame == nullptr)
if (frame == nullptr)
return;
int64_t pts = frame->pts;
static bool first_frame = true;
// TODO: TIMER
glBindTexture(GL_TEXTURE_2D, openglVideoParam.texs[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]);
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);
shaderService->Use();
glBindVertexArray(openglVideoParam.VAO);
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
const char *targetFilepath = argv[1];
const char* targetFilepath = argv[1];
if (targetFilepath == nullptr || !std::filesystem::exists(targetFilepath))
{
cout << "File Not Exist\n";
@ -143,10 +140,10 @@ int main(int argc, char **const argv)
// INIT
int client_width, client_height;
SDL_Window *window = nullptr;
SDL_Window* window = nullptr;
DecoderParam decoderParam{};
OpenglVideoParam openglVideoParam{};
ShaderService *shaderService = nullptr;
ShaderService* shaderService = nullptr;
double framerate = .0f;
if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
{

View File

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