修复msvc下无法正确获取拓展名的问题, 修复视频文件结束后任然解码的问题, 添加CMake Window下编译
This commit is contained in:
parent
3697de0b4a
commit
5cc0de59d5
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,3 +1,5 @@
|
|||||||
/build/*
|
/build/*
|
||||||
.cache/*
|
.cache/*
|
||||||
.vscode/*
|
.vscode/*
|
||||||
|
/.vs
|
||||||
|
/CMakeSettings.json
|
||||||
|
@ -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()
|
||||||
|
@ -13,13 +13,6 @@ struct DecoderParam{
|
|||||||
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
|
@ -6,7 +6,7 @@
|
|||||||
#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,
|
||||||
@ -14,57 +14,56 @@ enum class FileType{
|
|||||||
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;
|
||||||
|
11
main.cc
11
main.cc
@ -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"
|
||||||
@ -20,8 +19,8 @@ struct OpenglVideoParam
|
|||||||
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,
|
||||||
@ -94,13 +93,11 @@ int InitVideo(SDL_Window *&window, const char *targetFilepath, DecoderParam &dec
|
|||||||
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,12 +113,12 @@ 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)
|
||||||
|
@ -7,8 +7,8 @@ void InitDecoder(const char* filepath, DecoderParam& param){
|
|||||||
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);
|
||||||
@ -24,29 +24,36 @@ void InitDecoder(const char* filepath, DecoderParam& param){
|
|||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
|
|
||||||
|
AVPacket* packet = av_packet_alloc();
|
||||||
|
AVFrame* frame = av_frame_alloc();
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
AVPacket* packet = av_packet_alloc();
|
|
||||||
int ret = av_read_frame(fmtCtx, packet);
|
int ret = av_read_frame(fmtCtx, packet);
|
||||||
if (ret == 0 && packet->stream_index == videoStreamIndex) {
|
if (ret == 0 && packet->stream_index == videoStreamIndex) {
|
||||||
ret = avcodec_send_packet(codecCtx, packet);
|
ret = avcodec_send_packet(codecCtx, packet);
|
||||||
if (ret == 0) {
|
if (ret == 0) {
|
||||||
AVFrame* frame = av_frame_alloc();
|
|
||||||
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;
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user