SDL YUV 视频播放

SDL YUV 视频播放

SDL 视频显示流程

SDL 视频显示流程

上代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <SDL.h>

// 自定义消息类型
#define REFRESH_EVENT (SDL_USEREVENT + 0)
#define QUIT_EVENT (SDL_USEREVENT + 1)

// 定义分辨率
#define YUV_WIDTH 320
#define YUV_HEIGHT 240

int threadExitFlag = 0;

int
refreshVideoTimer(void* data)
{

while (!threadExitFlag) {
SDL_Event event;
event.type = REFRESH_EVENT;
SDL_PushEvent(&event);
SDL_Delay(40);
}

threadExitFlag = 0;

SDL_Event event;
event.type = QUIT_EVENT;
SDL_PushEvent(&event);

return 0;
}

int
main(int argc, char* argv[])
{
if (SDL_Init(SDL_INIT_VIDEO)) {
printf("Could not initialize SDL - %s\n", SDL_GetError());
return -1;
}

SDL_Event event; // 事件
SDL_Rect rect; // 矩形
SDL_Window* window = NULL; // 窗口
SDL_Renderer* renderer = NULL; // 渲染
SDL_Texture* texture = NULL; // 纹理
SDL_Thread* timerThread = NULL; // 刷新线程
uint32_t pixFormat = SDL_PIXELFORMAT_IYUV; // YUV420P

// YUV分辨率
int videoWidth = YUV_WIDTH;
int videoHeight = YUV_HEIGHT;
// 窗口分辨率
int winWidth = YUV_WIDTH;
int winHeight = YUV_HEIGHT;

// YUV 文件句柄
FILE* video = NULL;
const char* videoPath = "yuv420p_320x240.yuv";

size_t videoBufferLen = 0;
uint8_t* videoBuffer = NULL;

// 帧长度 yuv420p 四个亮度搭一个色度
uint32_t yFrameLen = videoWidth * videoHeight;
uint32_t uFrameLen = yFrameLen / 4;
uint32_t vFrameLen = yFrameLen / 4;
uint32_t frameLen = yFrameLen + uFrameLen + vFrameLen;

// 创建窗口
window = SDL_CreateWindow("SDL YUV Player",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
winWidth,
winHeight,
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
if (!window) {
printf("SDL: Could not create window - %s\n", SDL_GetError());
goto END;
}

renderer = SDL_CreateRenderer(window, -1, 0);
texture = SDL_CreateTexture(
renderer, pixFormat, SDL_TEXTUREACCESS_TARGET, videoWidth, videoHeight);

videoBuffer = (uint8_t*)malloc(frameLen);
if (!videoBuffer) {
printf("Failed to alloc yuv frame space.\n");
goto END;
}

fopen_s(&video, videoPath, "rb");
if (!video) {
printf("Failed to open yuv file.\n");
goto END;
}

timerThread = SDL_CreateThread(refreshVideoTimer, "RefreshVideoTimer", NULL);
if (!timerThread) {
printf("SDL: Could not create thread - %s\n", SDL_GetError());
goto END;
}

while (1) {
SDL_WaitEvent(&event);

if (event.type == REFRESH_EVENT) {
videoBufferLen = fread(videoBuffer, 1, frameLen, video);
if (videoBufferLen <= 0) {
printf("Failed to read data from yuv file.\n");
goto END;
}

// 设置纹理
SDL_UpdateTexture(texture, NULL, videoBuffer, videoWidth);

// 显示区域,通过修改宽高来进行缩放
rect.x = 0;
rect.y = 0;
float wRatio = winWidth * 1.0f / videoWidth;
float hRatio = winHeight * 1.0f / videoHeight;
rect.w = (int)(videoWidth * wRatio);
rect.h = (int)(videoHeight * hRatio);

// 清空渲染器
SDL_RenderClear(renderer);
// 渲染纹理
SDL_RenderCopy(renderer, texture, NULL, &rect);
// 更新渲染器
SDL_RenderPresent(renderer);
} else if (event.type == SDL_WINDOWEVENT) {
// 获取窗口大小,更新窗口拖动
SDL_GetWindowSize(window, &winWidth, &winHeight);
printf("[SDL_WINDOWEVENT] width: %d, height: %d\n", winWidth, winHeight);
} else if (event.type == SDL_QUIT) {
threadExitFlag = 1;
} else if (event.type == QUIT_EVENT) {
break;
}
}

END:
if (videoBuffer) {
free(videoBuffer);
}
if (video) {
fclose(video);
}
if (timerThread) {
SDL_WaitThread(timerThread, NULL);
}
if (texture) {
SDL_DestroyTexture(texture);
}
if (renderer) {
SDL_DestroyRenderer(renderer);
}
if (window) {
SDL_DestroyWindow(window);
}
SDL_Quit();

return 0;
}

SDL YUV 视频播放
https://irisislove.github.io/2025/06/05/sdl-yuv/
作者
Iris
发布于
2025年6月5日
许可协议