Ai
1 Star 4 Fork 1

a53818742/SDL播放器

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
FFmpegVideo.cpp 4.16 KB
一键复制 编辑 原始数据 按行查看 历史
a53818742 提交于 2020-10-29 11:04 +08:00 . no commit message
#include "FFmpegVideo.h"
FFmpegVideo::FFmpegVideo(char* src, SDL_Renderer* r,int x0, int y0, int w0, int h0) {
memcpy_s(this->src, 300, src, strlen(src));
this->Randerer = r;
this->rect.x = x0;
this->rect.y = y0;
this->rect.w = w0;
this->rect.h = h0;
};
FFmpegVideo::~FFmpegVideo() {
};
bool FFmpegVideo::Close() {
av_frame_free(&this->pFrameYUV);
av_frame_free(&this->pFrame);
avcodec_close(this->pCodecCtx);
avformat_close_input(&this->pFormatCtx);
return 1;
}
bool FFmpegVideo::Init() {
this->isSuccess = false;
av_register_all();
avformat_network_init();
this->pFormatCtx = avformat_alloc_context();
av_dict_set(&opts, "rtsp_transport", "tcp", 0);
av_dict_set(&opts, "stimeout", "1000000", 0);
av_dict_set(&opts, "start_time_realtime", 0, 0);
av_dict_set(&this->opts, "buffer_size", "8192000", 0);
av_dict_set(&this->opts, "max_interleave_delta", "40000", 0);
if (avformat_open_input(&this->pFormatCtx, this->src, NULL, &this->opts) != 0) {
printf("Couldn't open input stream.\n");
return 0;
}
if (avformat_find_stream_info(this->pFormatCtx, NULL) < 0) {
printf("Couldn't find stream information.\n");
return 0;
}
this->videoindex = -1;
for (i = 0; i < this->pFormatCtx->nb_streams; i++)
if (this->pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
this->videoindex = i;
break;
}
if (this->videoindex == -1) {
printf("Didn't find a video stream.\n");
return 0;
}
this->pCodecCtx = this->pFormatCtx->streams[this->videoindex]->codec;
this->pCodec = avcodec_find_decoder(this->pCodecCtx->codec_id);
if (this->pCodec == NULL) {
printf("Codec not found.\n");
return 0;
}
if (avcodec_open2(this->pCodecCtx, this->pCodec, NULL) < 0) {
printf("Could not open codec.\n");
return 0;
}
this->pFrame = av_frame_alloc();
this->pFrameYUV = av_frame_alloc();
this->out_buffer = (unsigned char*)av_malloc(av_image_get_buffer_size(AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height, 1));
av_image_fill_arrays(pFrameYUV->data, pFrameYUV->linesize, out_buffer,
AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height, 1);
av_dump_format(pFormatCtx, 0, this->src, 0);
img_convert_ctx = sws_getContext(this->pCodecCtx->width, this->pCodecCtx->height, this->pCodecCtx->pix_fmt,
pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);
if (init_rander == false) {
init_rander = true;
this->text = SDL_CreateTexture(this->Randerer, SDL_PIXELFORMAT_IYUV, SDL_TEXTUREACCESS_STREAMING, pCodecCtx->width, pCodecCtx->height);
}
this->isSuccess = true;
return true;
};
void FFmpegVideo::Run() {
while (this->ThreadRunFlag)
{
if (isSuccess ==false) {
this->Init();
}
if (isSuccess == true) {
this->ReadFrame();
}
Sleep(20);
}
}
bool FFmpegVideo::ReadFrame() {
int flag = av_read_frame(pFormatCtx, packet);
if ( flag< 0) {
av_free_packet(packet);
if (flag < -10000) {
//ƵԴϿ
this->isSuccess = false;
this->Close();
}
return 0;
}
ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);
if (ret < 0) {
av_free_packet(packet);
return 0;
}
if (got_picture) {
this->lock();
sws_scale(img_convert_ctx, (const unsigned char* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameYUV->data, pFrameYUV->linesize);
this->unlock();
av_free_packet(packet);
return 1;
}
av_free_packet(packet);
return 1;
}
void FFmpegVideo::lock() {
int fal = mut.try_lock();
while (fal == false)
{
//printf("ʧ\n");
fal = mut.try_lock();
//Sleep(0);
};
}
void FFmpegVideo::unlock() {
this->mut.unlock();
}
void FFmpegVideo::RenderCopy() {
this->lock();
SDL_RenderCopy(this->Randerer, this->text, NULL, &this->rect);// &this->rect
this->unlock();
};
bool FFmpegVideo::Play() {
this->lock();
SDL_UpdateTexture(this->text, NULL, pFrameYUV->data[0], pFrameYUV->linesize[0]);
this->unlock();
return 0;
}
void FFmpegVideo::SetPosition(int x, int y, int w, int h) {
//this->lock();
this->rect.x = x;
this->rect.y = y;
this->rect.w = w;
this->rect.h = h;
//this->unlock();
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/mdata_986/sdl-player.git
git@gitee.com:mdata_986/sdl-player.git
mdata_986
sdl-player
SDL播放器
master

搜索帮助