修复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,70 +6,69 @@
#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;
} }
}; };
#endif #endif

369
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"
@ -12,212 +11,210 @@ using std::cout, std::endl;
struct OpenglVideoParam struct OpenglVideoParam
{ {
SDL_GLContext glContext; SDL_GLContext glContext;
unsigned int VAO, VBO, EBO; unsigned int VAO, VBO, EBO;
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,
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
client_width, client_width,
client_height, client_height,
SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL); SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);
if (!window) if (!window)
{ {
cout << SDL_GetError() << "\n"; cout << SDL_GetError() << "\n";
return -1; return -1;
} }
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
openglVideoParam.glContext = SDL_GL_CreateContext(window); openglVideoParam.glContext = SDL_GL_CreateContext(window);
if (!openglVideoParam.glContext) if (!openglVideoParam.glContext)
{ {
cout << SDL_GetError() << "\n"; cout << SDL_GetError() << "\n";
return -1; return -1;
} }
if (!gladLoadGLLoader((GLADloadproc)SDL_GL_GetProcAddress)) if (!gladLoadGLLoader((GLADloadproc)SDL_GL_GetProcAddress))
{ {
auto error = glad_glGetError(); auto error = glad_glGetError();
return static_cast<int>(error); return static_cast<int>(error);
} }
float vertices[] = { float vertices[] = {
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,
3, 3,
1, 1,
2, 2,
3, 3,
}; };
glGenVertexArrays(1, &openglVideoParam.VAO); glGenVertexArrays(1, &openglVideoParam.VAO);
glGenBuffers(1, &openglVideoParam.VBO); glGenBuffers(1, &openglVideoParam.VBO);
glGenBuffers(1, &openglVideoParam.EBO); glGenBuffers(1, &openglVideoParam.EBO);
glBindVertexArray(openglVideoParam.VAO); glBindVertexArray(openglVideoParam.VAO);
glBindBuffer(GL_ARRAY_BUFFER, openglVideoParam.VBO); glBindBuffer(GL_ARRAY_BUFFER, openglVideoParam.VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, openglVideoParam.EBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, openglVideoParam.EBO);
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++)
{ {
glBindTexture(GL_TEXTURE_2D, openglVideoParam.texs[i]); glBindTexture(GL_TEXTURE_2D, openglVideoParam.texs[i]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
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; // TODO: TIMER
static bool first_frame = true; glBindTexture(GL_TEXTURE_2D, openglVideoParam.texs[0]);
// TODO: TIMER glPixelStoref(GL_UNPACK_ROW_LENGTH, frame->linesize[0]);
glBindTexture(GL_TEXTURE_2D, openglVideoParam.texs[0]); glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, frame->width, frame->height, 0, GL_RED, GL_UNSIGNED_BYTE, frame->data[0]);
glPixelStoref(GL_UNPACK_ROW_LENGTH, frame->linesize[0]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, frame->width, frame->height, 0, GL_RED, GL_UNSIGNED_BYTE, frame->data[0]);
glActiveTexture(GL_TEXTURE1); glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, openglVideoParam.texs[1]); glBindTexture(GL_TEXTURE_2D, openglVideoParam.texs[1]);
glPixelStoref(GL_UNPACK_ROW_LENGTH, frame->linesize[1]); glPixelStoref(GL_UNPACK_ROW_LENGTH, frame->linesize[1]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, frame->width / 2, frame->height / 2, 0, GL_RED, GL_UNSIGNED_BYTE, frame->data[1]); glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, frame->width / 2, frame->height / 2, 0, GL_RED, GL_UNSIGNED_BYTE, frame->data[1]);
glActiveTexture(GL_TEXTURE2); glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, openglVideoParam.texs[2]); glBindTexture(GL_TEXTURE_2D, openglVideoParam.texs[2]);
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]);
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); av_frame_free(&frame);
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";
return 0; return 0;
} }
const FileType fileType = Util::GetFileType(targetFilepath); const FileType fileType = Util::GetFileType(targetFilepath);
if (fileType == FileType::ERRORTYPE) if (fileType == FileType::ERRORTYPE)
{ {
cout << "unsupport file type\n"; cout << "unsupport file type\n";
return 0; return 0;
} }
// 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)
{ {
cout << SDL_GetError() << "\n"; cout << SDL_GetError() << "\n";
return -1; return -1;
} }
switch (fileType) switch (fileType)
{ {
case FileType::VIDEO: case FileType::VIDEO:
{ {
InitVideo(window, targetFilepath, decoderParam, openglVideoParam, shaderService); InitVideo(window, targetFilepath, decoderParam, openglVideoParam, shaderService);
auto& time_base = decoderParam.fmtCtx->streams[decoderParam.videoStreamIndex]->time_base; auto& time_base = decoderParam.fmtCtx->streams[decoderParam.videoStreamIndex]->time_base;
framerate = (double)time_base.num / time_base.den; framerate = (double)time_base.num / time_base.den;
break; break;
} }
case FileType::IMG: case FileType::IMG:
{ {
break; break;
} }
case FileType::MUSIC: case FileType::MUSIC:
{ {
break; break;
} }
default: default:
return -1; return -1;
} }
bool quit = false; bool quit = false;
auto current_time = std::chrono::system_clock::now(); auto current_time = std::chrono::system_clock::now();
SDL_Event event; SDL_Event event;
while (!quit) while (!quit)
{ {
while (SDL_PollEvent(&event)) while (SDL_PollEvent(&event))
{ {
switch (event.type) switch (event.type)
{ {
case SDL_KEYDOWN: case SDL_KEYDOWN:
switch (event.key.keysym.scancode) switch (event.key.keysym.scancode)
{ {
case SDL_SCANCODE_ESCAPE: case SDL_SCANCODE_ESCAPE:
return -1; return -1;
default: default:
break; break;
} }
break; break;
case SDL_QUIT: case SDL_QUIT:
return -1; return -1;
default: default:
break; break;
} }
} }
// Render // Render
switch (fileType) switch (fileType)
{ {
case FileType::VIDEO: case FileType::VIDEO:
OpenglRender(decoderParam, openglVideoParam, shaderService); OpenglRender(decoderParam, openglVideoParam, shaderService);
SDL_GL_SwapWindow(window); SDL_GL_SwapWindow(window);
std::this_thread::sleep_until(current_time + std::chrono::milliseconds((int)(framerate * 1000))); std::this_thread::sleep_until(current_time + std::chrono::milliseconds((int)(framerate * 1000)));
current_time = std::chrono::system_clock::now(); current_time = std::chrono::system_clock::now();
break; break;
default: default:
break; break;
} }
} }
avcodec_close(decoderParam.codecCtx); avcodec_close(decoderParam.codecCtx);
avformat_close_input(&(decoderParam.fmtCtx)); avformat_close_input(&(decoderParam.fmtCtx));
SDL_GL_DeleteContext(openglVideoParam.glContext); SDL_GL_DeleteContext(openglVideoParam.glContext);
SDL_DestroyWindow(window); SDL_DestroyWindow(window);
SDL_Quit(); SDL_Quit();
return 0; return 0;
} }

View File

@ -1,52 +1,59 @@
#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);
avcodec_open2(codecFmt, codec, nullptr); avcodec_open2(codecFmt, codec, nullptr);
} }
} }
param.codecCtx = codecFmt; param.codecCtx = codecFmt;
param.fmtCtx = fmtCtx; param.fmtCtx = fmtCtx;
param.width = codecFmt->width; param.width = codecFmt->width;
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(); AVFrame* frame = av_frame_alloc();
int ret = av_read_frame(fmtCtx, packet);
if(ret == 0 && packet->stream_index == videoStreamIndex){ while (true) {
ret = avcodec_send_packet(codecCtx, packet); int ret = av_read_frame(fmtCtx, packet);
if(ret == 0){ if (ret == 0 && packet->stream_index == videoStreamIndex) {
AVFrame* frame = av_frame_alloc(); ret = avcodec_send_packet(codecCtx, packet);
ret = avcodec_receive_frame(codecCtx, frame); if (ret == 0) {
if(ret == 0){ ret = avcodec_receive_frame(codecCtx, frame);
av_packet_unref(packet); if (ret == 0) {
return frame; av_packet_unref(packet);
}else if(ret == AVERROR(EAGAIN)){ return frame;
av_frame_unref(frame); }
}else{ if (ret == AVERROR(EAGAIN)) {
return nullptr; continue;
} }
} av_frame_free(&frame);
} av_packet_free(&packet);
av_packet_unref(packet); return nullptr;
} }
return nullptr; }
else if (ret == AVERROR_EOF)
{
av_packet_unref(packet);
return nullptr;
}
av_packet_unref(packet);
}
} }