SDL —— FFmpeg解码本地视频在SDL渲染显示(附源码)
int FFmpeg_Video(const char *path)
{
if (!path) { return -1; }
/**********************************************************************/
AVFormatContext *VFormatContext = nullptr;
if (avformat_open_input(&VFormatContext, path, nullptr, nullptr) != 0) { return -2; }
if (avformat_find_stream_info(VFormatContext, nullptr) < 0) { return -3; }
int VideoIndex = av_find_best_stream(VFormatContext, AVMEDIA_TYPE_VIDEO, -1, -1, nullptr, 0);
if (VideoIndex == AVERROR_STREAM_NOT_FOUND) { return -4; }
AVCodecContext *VCodecContext = avcodec_alloc_context3(nullptr);
if (avcodec_parameters_to_context(VCodecContext, VFormatContext->streams[VideoIndex]->codecpar) < 0) { return -5; }
AVCodec *vCodec = avcodec_find_decoder(VCodecContext->codec_id);
if (!vCodec) { return -6; }
if (avcodec_open2(VCodecContext, vCodec, nullptr) != 0) { return -7; }
/**********************************************************************/
/**********************************************************************/
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) { return -11; }
SDL_Window *sdlWindow = SDL_CreateWindow
(
"Media Player",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
VCodecContext->width / 2, VCodecContext->height / 2,
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE
);
if (!sdlWindow) { return -12; }
SDL_Renderer *sdlRender = SDL_CreateRenderer(sdlWindow, -1, SDL_RENDERER_ACCELERATED);
if (!sdlRender) { return -13; }
SDL_Texture *sdlTexture = SDL_CreateTexture
(
sdlRender,
SDL_PIXELFORMAT_IYUV,
SDL_TEXTUREACCESS_STREAMING,
VCodecContext->width,
VCodecContext->height
);
if (!sdlTexture) { return -14; }
SDL_Rect rect;
rect.x = 0;
rect.y = 0;
rect.w = VCodecContext->width / 2;
rect.h = VCodecContext->height / 2;
/**********************************************************************/
}