#include <stdio.h>
#include <string.h>
#include "SDL2/SDL.h"
//自定义消息类型
#define REFRESH_EVENT (SDL_USEREVENT + 1) // 请求画面刷新事件
#define QUIT_EVENT (SDL_USEREVENT + 2) // 退出事件
//定义分辨率
// YUV像素分辨率
#define YUV_WIDTH 320
#define YUV_HEIGHT 240
//定义YUV格式
#define YUV_FORMAT SDL_PIXELFORMAT_IYUV
int s_thread_exit = 0; // 退出标志 = 1则退出
int refresh_video_timer(void *data)
{
while (!s_thread_exit)
{
SDL_Event event;
event.type = REFRESH_EVENT;
SDL_PushEvent(&event);
SDL_Delay(40);
}
s_thread_exit = 0;
//push quit event
SDL_Event event;
event.type = QUIT_EVENT;
SDL_PushEvent(&event);
return 0;
}
#undef main
int main(int argc, char* argv[])
{
//初始化 SDL
if(SDL_Init(SDL_INIT_VIDEO))
{
fprintf( stderr, "Could not initialize SDL - %s\n", SDL_GetError());
return -1;
}
// SDL
SDL_Event event; // 事件
SDL_Rect rect; // 矩形
SDL_Window *window = NULL; // 窗口
SDL_Renderer *renderer = NULL; // 渲染
SDL_Texture *texture = NULL; // 纹理
SDL_Thread *timer_thread = NULL; // 请求刷新线程
uint32_t pixformat = YUV_FORMAT; // YUV420P,即是SDL_PIXELFORMAT_IYUV
// 分辨率
// 1. YUV的分辨率
int video_width = YUV_WIDTH;
int video_height = YUV_HEIGHT;
// 2.显示窗口的分辨率
int win_width = YUV_WIDTH;
int win_height = YUV_WIDTH;
// YUV文件句柄
FILE *video_fd = NULL;
const char *yuv_path = "yuv420p_320x240.yuv";
size_t video_buff_len = 0;
uint8_t *video_buf = NULL; //读取数据后先把放到buffer里面
// 我们测试的文件是YUV420P格式
uint32_t y_frame_len = video_width * video_height;
uint32_t u_frame_len = video_width * video_height / 4;
uint32_t v_frame_len = video_width * video_height / 4;
uint32_t yuv_frame_len = y_frame_len + u_frame_len + v_frame_len;
//创建窗口
window = SDL_CreateWindow("Simplest YUV Player",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
video_width, video_height,
SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE);
if(!window)
{
fprintf(stderr, "SDL: could not create window, err:%s\n",SDL_GetError());
goto _FAIL;
}
// 基于窗口创建渲染器
renderer = SDL_CreateRenderer(window, -1, 0);
// 基于渲染器创建纹理
texture = SDL_CreateTexture(renderer,
pixformat,
SDL_TEXTUREACCESS_STREAMING,
video_width,
video_height);
// 分配空间
video_buf = (uint8_t*)malloc(yuv_frame_len);
if(!video_buf)
{
fprintf(stderr, "Failed to alloce yuv frame space!\n");
goto _FAIL;
}
// 打开YUV文件
video_fd = fopen(yuv_path, "rb");
if( !video_fd )
{
fprintf(stderr, "Failed to open yuv file\n");
goto _FAIL;
}
// 创建请求刷新线程
timer_thread = SDL_CreateThread(refresh_video_timer,
NULL,
NULL);
while (1)
{
// 收取SDL系统里面的事件
SDL_WaitEvent(&event);
if(event.type == REFRESH_EVENT) // 画面刷新事件
{
video_buff_len = fread(video_buf, 1, yuv_frame_len, video_fd);
if(video_buff_len <= 0)
{
fprintf(stderr, "Failed to read data from yuv file!\n");
goto _FAIL;
}
// 设置纹理的数据 video_width = 320, plane
SDL_UpdateTexture(texture, NULL, video_buf, video_width);
// 显示区域,可以通过修改w和h进行缩放
rect.x = 0;
rect.y = 0;
float w_ratio = win_width * 1.0 /video_width;
float h_ratio = win_height * 1.0 /video_height;
// 320x240 怎么保持原视频的宽高比例
rect.w = video_width * w_ratio;
rect.h = video_height * h_ratio;
// rect.w = video_width * 0.5;
// rect.h = video_height * 0.5;
// 清除当前显示
SDL_RenderClear(renderer);
// 将纹理的数据拷贝给渲染器
SDL_RenderCopy(renderer, texture, NULL, &rect);
// 显示
SDL_RenderPresent(renderer);
}
else if(event.type == SDL_WINDOWEVENT)
{
//If Resize
SDL_GetWindowSize(window, &win_width, &win_height);
printf("SDL_WINDOWEVENT win_width:%d, win_height:%d\n",win_width,
win_height );
}
else if(event.type == SDL_QUIT) //退出事件
{
s_thread_exit = 1;
}
else if(event.type == QUIT_EVENT)
{
break;
}
}
_FAIL:
s_thread_exit = 1; // 保证线程能够退出
// 释放资源
if(timer_thread)
SDL_WaitThread(timer_thread, NULL); // 等待线程退出
if(video_buf)
free(video_buf);
if(video_fd)
fclose(video_fd);
if(texture)
SDL_DestroyTexture(texture);
if(renderer)
SDL_DestroyRenderer(renderer);
if(window)
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
没有合适的资源?快使用搜索试试~ 我知道了~
09-SDL音视频渲染实战SDL安装和接口说明
共37个文件
pro:5个
stash:5个
user:5个
需积分: 5 0 下载量 64 浏览量
2022-10-24
09:07:06
上传
评论
收藏 41.48MB RAR 举报
温馨提示
05linux-sdl-window 06linux-sdl-event 07linux-sdl-thread 08linux-sdl-playyuv 09linux-sdl-pcm
资源详情
资源评论
资源推荐
收起资源包目录
09-SDL音视频渲染实战】SDL安装和接口说明实例.rar (37个子文件)
09-SDL音视频渲染实战】SDL安装和接口说明实例
05linux-sdl-window
05linux-sdl-window
main.c 2KB
05linux-sdl-window.pro 246B
05linux-sdl-window.pro.user 25KB
build-05linux-sdl-window-Desktop_Qt_5_12_10_GCC_64bit-Debug
05linux-sdl-window 17KB
.qmake.stash 739B
Makefile 51KB
main.o 10KB
06linux-sdl-event
06linux-sdl-event
main.c 3KB
06linux-sdl-event.pro.user 25KB
06linux-sdl-event.pro 246B
build-06linux-sdl-event-Desktop_Qt_5_12_10_GCC_64bit-Debug
06linux-sdl-event 33KB
.qmake.stash 739B
Makefile 51KB
main.o 45KB
08linux-sdl-playyuv
08linux-sdl-playyuv
main.c 5KB
08linux-sdl-playyuv.pro 246B
08linux-sdl-playyuv.pro.user 25KB
build-08linux-sdl-playyuv-Desktop_Qt_5_12_10_GCC_64bit-Debug
08linux-sdl-playyuv 34KB
.qmake.stash 739B
yuv420p_320x240.yuv 82.4MB
Makefile 51KB
main.o 46KB
07linux-sdl-thread
07linux-sdl-thread
main.c 2KB
07linux-sdl-thread.pro 299B
07linux-sdl-thread.pro.user 25KB
build-07linux-sdl-thread-Desktop_Qt_5_12_10_GCC_64bit-Debug
07linux-sdl-thread 28KB
.qmake.stash 739B
Makefile 51KB
main.o 34KB
09linux-sdl-pcm
09linux-sdl-pcm
09linux-sdl-pcm.pro.user 25KB
main.c 4KB
09linux-sdl-pcm.pro 246B
build-09linux-sdl-pcm-Desktop_Qt_5_12_10_GCC_64bit-Debug
.qmake.stash 739B
44100_16bit_2ch.pcm 3.36MB
09linux-sdl-pcm 30KB
Makefile 51KB
main.o 37KB
共 37 条
- 1
权艺
- 粉丝: 133
- 资源: 14
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0