SDL库的配置比较简单,略。?
SDL视频显示函数
- SDL_Init():初始化SDL系统
- SDL_CreateWindow():创建窗口 SDL_Window
- SDL_CreateRenderer():创建渲染器 SDL_Renderer
- SDL_CreateTexture():创建纹理 SDL_Texture
- SDL_UpdateTexture():设置纹理的数据
- SDL_RenderClear():清除渲染器的内容
- SDL_RenderCopy():将纹理的数据拷贝给渲染器
- SDL_RenderPresent():显示
- SDL_Delay():工具函数,用于延时。
- SDL_Quit():退出SDL系统
SDL数据结构
- SDL_Window:代表了一个“窗口”
- SDL_Renderer:代表了一个“渲染器”
- SDL_Texture:代表了一个“纹理”
- SDL_Rect:一个简单的矩形结构
播放视频代码
#include <QDebug>
#include <QFile>
extern "C"
{
#include "SDL.h"
#undef main //不能少
};
int screen_w = 640,screen_h = 360;
const int pixel_w = 640,pixel_h = 360;
uint32_t y_frame_len = pixel_w * pixel_h;
uint32_t u_frame_len = pixel_w * pixel_h / 4;
uint32_t v_frame_len = pixel_w * pixel_h / 4;
const int yuv_frame_len = y_frame_len + u_frame_len + v_frame_len;//一帧yuv数据的长度
int main(int argc, char* argv[])
{
if(SDL_Init(SDL_INIT_VIDEO))
{
qDebug("无法初始化 SDL - %s\n", SDL_GetError());
return -1;
}
SDL_Window *screen = SDL_CreateWindow("简单的SDL2播放窗口",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
screen_w, //窗口尺寸
screen_h,
SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE);
if(!screen)
{
printf("SDL无法创建窗口 :%s\n",SDL_GetError());
return -1;
}
SDL_Renderer* sdlRenderer = SDL_CreateRenderer(screen, -1, 0);
SDL_Texture* sdlTexture = SDL_CreateTexture(sdlRenderer,
SDL_PIXELFORMAT_IYUV,
SDL_TEXTUREACCESS_STREAMING,
pixel_w,
pixel_h);
SDL_Rect sdlRect;
sdlRect.x = 0;
sdlRect.y = 0;
sdlRect.w = screen_w;
sdlRect.h = screen_h;
QFile file("D:\\sintel_640_360.yuv");
file.open(QIODevice::ReadOnly);
while(1)
{
QByteArray array = file.read(yuv_frame_len);
if(array.isEmpty())
{
file.seek(0);//游标回到起始位置(0),然后从头读取,即重复播放
array = file.read(yuv_frame_len);
}
SDL_UpdateTexture(sdlTexture, nullptr, array, pixel_w);
SDL_RenderClear(sdlRenderer);//清除当前渲染器的内容
SDL_RenderCopy(sdlRenderer, sdlTexture, nullptr, &sdlRect);
SDL_RenderPresent(sdlRenderer);
SDL_Delay(40);
}
file.close();
SDL_Quit();
return 0;
}
|