SDL介绍
SDL(Simple DirectMedia Layer)是一个跨平台的多媒体开发库,广泛用于游戏开发和多媒体应用的创建。它提供了对音频、键盘、鼠标、游戏控制器、图形和窗口的底层访问,从而使开发者能够编写高性能的多媒体应用程序。SDL被许多流行的游戏和软件项目所使用。
SDL的主要特点
跨平台支持: SDL支持多个操作系统,包括Windows、Mac OS、Linux、iOS和Android等,使得开发者能够编写一次代码,运行在多个平台上。
简化多媒体处理: SDL提供了对音频、视频、输入设备、计时器等多媒体功能的简化接口,使开发者不需要处理底层系统的细节。
硬件加速: SDL支持使用OpenGL、Direct3D等图形库进行硬件加速,从而提高图形渲染的性能。
模块化设计: SDL的功能被划分为多个模块,如视频、音频、事件处理、线程、文件I/O等,开发者可以按需使用。
XSDL.h
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 #pragma once #include "xvideo_view.h" struct SDL_Window ; struct SDL_Renderer ;struct SDL_Texture ;class XSDL :public XVideoView{ public : void Close () override ; bool Init (int w, int h, Format fmt = RGBA) override ; bool Draw (const unsigned char * data, int linesize = 0 ) override ; bool Draw (const unsigned char * y, int y_pitch, const unsigned char * u, int u_pitch, const unsigned char * v, int v_pitch ) override ; bool IsExit () override ; private : SDL_Window *win_ = nullptr ; SDL_Renderer *render_ = nullptr ; SDL_Texture *texture_ = nullptr ; };
参数介绍
1 2 3 4 private : SDL_Window *win_ = nullptr ; SDL_Renderer *render_ = nullptr ; SDL_Texture *texture_ = nullptr ;
XSDL.cpp
是XVideoView的派生类
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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 #include "xsdl.h" #include <sdl/SDL.h> #include <thread> #include <iostream> #pragma comment(lib, "SDL2.lib" ) static bool InitVideo () { static bool is_first = true ; static std::mutex mux; std::unique_lock<std::mutex> sdl_lock (mux) ; if (!is_first) return true ; is_first = false ; if (SDL_Init (SDL_INIT_VIDEO)) { std::cout << SDL_GetError () << std::endl; return false ; } SDL_SetHint (SDL_HINT_RENDER_SCALE_QUALITY, "1" ); return true ; } bool XSDL::IsExit () { SDL_Event ev; SDL_WaitEventTimeout (&ev, 1 ); if (ev.type == SDL_QUIT) return true ; return false ; } void XSDL::Close () { std::unique_lock<std::mutex> sdl_lock (mtx_) ; if (texture_) { SDL_DestroyTexture (texture_); texture_ = nullptr ; } if (render_) { SDL_DestroyRenderer (render_); render_ = nullptr ; } if (win_) { SDL_DestroyWindow (win_); win_ = nullptr ; } } int find_renderer_index (const char * name) { int count = SDL_GetNumRenderDrivers (); SDL_RendererInfo info; for (int i = 0 ; i < count; ++i) { if (SDL_GetRenderDriverInfo (i, &info) == 0 ) { if (strcmp (info.name, name) == 0 ) { return i; } } } return -1 ; } bool XSDL::Init (int w, int h, Format fmt) { if (w <= 0 || h <= 0 ) return false ; InitVideo (); std::unique_lock<std::mutex> sdl_lock (mtx_) ; width_ = w; height_ = h; fmt_ = fmt; if (texture_) SDL_DestroyTexture (texture_); if (render_) SDL_DestroyRenderer (render_); if (!win_) { if (win_id_) { win_ = SDL_CreateWindowFrom (win_id_); } else { win_ = SDL_CreateWindow ("Video" , SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, w, h, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE); } } if (!win_) { std::cerr << SDL_GetError () << std::endl; return false ; } render_ = SDL_CreateRenderer (win_, -1 , SDL_RENDERER_ACCELERATED); if (!render_) { std::cerr << SDL_GetError () << std::endl; return false ; } unsigned int sdl_fmt = SDL_PIXELFORMAT_RGBA8888; switch (fmt_) { case XVideoView::RGBA: sdl_fmt = SDL_PIXELFORMAT_RGBA32; break ; case XVideoView::BGRA: sdl_fmt = SDL_PIXELFORMAT_BGRA32; break ; case XVideoView::YUV420P: sdl_fmt = SDL_PIXELFORMAT_IYUV; break ; case XVideoView::ARGB: sdl_fmt = SDL_PIXELFORMAT_ARGB32; break ; case XVideoView::NV12: sdl_fmt = SDL_PIXELFORMAT_NV12; break ; default : break ; } texture_ = SDL_CreateTexture (render_, sdl_fmt, SDL_TEXTUREACCESS_STREAMING, w, h ); if (!texture_) { std::cerr << SDL_GetError () << std::endl; return false ; } return true ; } bool XSDL::Draw (const unsigned char * y, int y_pitch, const unsigned char * u, int u_pitch, const unsigned char * v, int v_pitch ) { if (!y || !u || !v) return false ; std::unique_lock<std::mutex> sdl_lock (mtx_) ; if (!texture_ || !render_ || !win_ || width_ <= 0 || height_ <= 0 ) return false ; auto re = SDL_UpdateYUVTexture (texture_, NULL , y, y_pitch, u, u_pitch, v, v_pitch ); if (re != 0 ) { std::cout << SDL_GetError () << std::endl; return false ; } SDL_RenderClear (render_); if (scale_w_ <= 0 ) scale_w_ = width_; if (scale_h_ <= 0 ) scale_h_ = height_; SDL_Rect rect; SDL_Rect *prect = nullptr ; if (scale_w_ > 0 ) { rect.x = 0 ; rect.y = 0 ; rect.h = scale_h_; rect.w = scale_w_; prect = ▭ } re = SDL_RenderCopy (render_, texture_, NULL , prect); if (re != 0 ) { std::cout << SDL_GetError () << std::endl; return false ; } SDL_RenderPresent (render_); return true ; } bool XSDL::Draw (const unsigned char * data, int linesize) { if (!data) return false ; std::unique_lock<std::mutex> sdl_lock (mtx_) ; if (!texture_ || !render_ || !win_ || width_ <= 0 || height_ <= 0 ) return false ; if (linesize <= 0 ) { switch (fmt_) { case XVideoView::RGBA: break ; case XVideoView::YUV420P: linesize = width_; break ; case XVideoView::ARGB: linesize = width_ * 4 ; break ; default : break ; } } if (linesize <= 0 ) return false ; auto re = SDL_UpdateTexture (texture_, NULL , data, linesize); if (re != 0 ) { std::cout << SDL_GetError () << std::endl; return false ; } SDL_RenderClear (render_); if (scale_w_ <= 0 ) scale_w_ = width_; if (scale_h_ <= 0 ) scale_h_ = height_; SDL_Rect rect; SDL_Rect *prect = nullptr ; if (scale_w_ > 0 ) { rect.x = 0 ; rect.y = 0 ; rect.h = scale_h_; rect.w = scale_w_; prect = ▭ } re = SDL_RenderCopy (render_, texture_, NULL , prect); if (re != 0 ) { std::cout << SDL_GetError () << std::endl; return false ; } SDL_RenderPresent (render_); return true ; }
InitVideo()
作用:用于第一次初始化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 static bool InitVideo () { static bool is_first = true ; static std::mutex mux; std::unique_lock<std::mutex> sdl_lock (mux) ; if (!is_first) return true ; is_first = false ; if (SDL_Init (SDL_INIT_VIDEO)) { std::cout << SDL_GetError () << std::endl; return false ; } SDL_SetHint (SDL_HINT_RENDER_SCALE_QUALITY, "1" ); return true ; }
IsExit()
作用:处理检测用户关闭程序,并返回是否关闭的信号
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 bool XSDL::IsExit () { SDL_Event ev; SDL_WaitEventTimeout (&ev, 1 ); if (ev.type == SDL_QUIT) return true ; return false ; }
Close()
作用:关闭程序后,需要清理指针,防止内存泄漏。
先清理材质、再清理渲染器、最后清理窗口句柄
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 void XSDL::Close () { std::unique_lock<std::mutex> sdl_lock (mtx_) ; if (texture_) { SDL_DestroyTexture (texture_); texture_ = nullptr ; } if (render_) { SDL_DestroyRenderer (render_); render_ = nullptr ; } if (win_) { SDL_DestroyWindow (win_); win_ = nullptr ; } }
find_renderer_index()
作用:用于在 SDL 中查找具有指定名称的渲染驱动程序的索引。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 int find_renderer_index (const char * name) { int count = SDL_GetNumRenderDrivers (); SDL_RendererInfo info; for (int i = 0 ; i < count; ++i) { if (SDL_GetRenderDriverInfo (i, &info) == 0 ) { if (strcmp (info.name, name) == 0 ) { return i; } } } return -1 ; }
Init()
作用:初始化
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 bool XSDL::Init (int w, int h, Format fmt) { if (w <= 0 || h <= 0 ) return false ; InitVideo (); std::unique_lock<std::mutex> sdl_lock (mtx_) ; width_ = w; height_ = h; fmt_ = fmt; if (texture_) SDL_DestroyTexture (texture_); if (render_) SDL_DestroyRenderer (render_); if (!win_) { if (win_id_) { win_ = SDL_CreateWindowFrom (win_id_); } else { win_ = SDL_CreateWindow ("Video" , SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, w, h, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE); } } if (!win_) { std::cerr << SDL_GetError () << std::endl; return false ; } render_ = SDL_CreateRenderer (win_, -1 , SDL_RENDERER_ACCELERATED); if (!render_) { std::cerr << SDL_GetError () << std::endl; return false ; } unsigned int sdl_fmt = SDL_PIXELFORMAT_RGBA8888; switch (fmt_) { case XVideoView::RGBA: sdl_fmt = SDL_PIXELFORMAT_RGBA32; break ; case XVideoView::BGRA: sdl_fmt = SDL_PIXELFORMAT_BGRA32; break ; case XVideoView::YUV420P: sdl_fmt = SDL_PIXELFORMAT_IYUV; break ; case XVideoView::ARGB: sdl_fmt = SDL_PIXELFORMAT_ARGB32; break ; case XVideoView::NV12: sdl_fmt = SDL_PIXELFORMAT_NV12; break ; default : break ; } texture_ = SDL_CreateTexture (render_, sdl_fmt, SDL_TEXTUREACCESS_STREAMING, w, h ); if (!texture_) { std::cerr << SDL_GetError () << std::endl; return false ; } return true ; }
Draw(),专门处理YUV格式
由XVideoView::DrawFrame
进行数据转发
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 bool XVideoView::DrawFrame (AVFrame* frame) { ... switch (frame->format) { case AV_PIX_FMT_YUV420P: return Draw (frame->data[0 ], frame->linesize[0 ], frame->data[1 ], frame->linesize[1 ], frame->data[2 ], frame->linesize[2 ] ); case AV_PIX_FMT_BGRA: return Draw (frame->data[0 ], frame->linesize[0 ]); } ... }
作用:接收 YUV 格式的图像数据,将其更新到纹理,并通过 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 bool XSDL::Draw (const unsigned char * y, int y_pitch, const unsigned char * u, int u_pitch, const unsigned char * v, int v_pitch ) { if (!y || !u || !v) return false ; std::unique_lock<std::mutex> sdl_lock (mtx_) ; if (!texture_ || !render_ || !win_ || width_ <= 0 || height_ <= 0 ) return false ; auto re = SDL_UpdateYUVTexture (texture_, NULL , y, y_pitch, u, u_pitch, v, v_pitch ); if (re != 0 ) { std::cout << SDL_GetError () << std::endl; return false ; } SDL_RenderClear (render_); if (scale_w_ <= 0 ) scale_w_ = width_; if (scale_h_ <= 0 ) scale_h_ = height_; SDL_Rect rect; SDL_Rect *prect = nullptr ; if (scale_w_ > 0 ) { rect.x = 0 ; rect.y = 0 ; rect.h = scale_h_; rect.w = scale_w_; prect = ▭ } re = SDL_RenderCopy (render_, texture_, NULL , prect); if (re != 0 ) { std::cout << SDL_GetError () << std::endl; return false ; } SDL_RenderPresent (render_); return true ; }
使用到了SDL_UpdateYUVTexture
,而下面的Draw函数则是使用SDL_UpdateTexture
Draw(),处理YUV、RGB格式
由XVideoView::DrawFrame
进行数据转发
作用:接收 RGB 格式的图像数据,将其更新到纹理,并通过 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 bool XSDL::Draw (const unsigned char * data, int linesize) { if (!data) return false ; std::unique_lock<std::mutex> sdl_lock (mtx_) ; if (!texture_ || !render_ || !win_ || width_ <= 0 || height_ <= 0 ) return false ; if (linesize <= 0 ) { switch (fmt_) { case XVideoView::RGBA: linesize = width_ * 4 ; break ; case XVideoView::YUV420P: linesize = width_; break ; case XVideoView::ARGB: linesize = width_ * 4 ; break ; default : break ; } } if (linesize <= 0 ) return false ; auto re = SDL_UpdateTexture (texture_, NULL , data, linesize); if (re != 0 ) { std::cout << SDL_GetError () << std::endl; return false ; } SDL_RenderClear (render_); if (scale_w_ <= 0 ) scale_w_ = width_; if (scale_h_ <= 0 ) scale_h_ = height_; SDL_Rect rect; SDL_Rect *prect = nullptr ; if (scale_w_ > 0 ) { rect.x = 0 ; rect.y = 0 ; rect.h = scale_h_; rect.w = scale_w_; prect = ▭ } re = SDL_RenderCopy (render_, texture_, NULL , prect); if (re != 0 ) { std::cout << SDL_GetError () << std::endl; return false ; } SDL_RenderPresent (render_); return true ; }
【为什么YUV420P不需要×4? 】
1 2 3 4 5 6 7 8 9 case XVideoView::RGBA: linesize = width_ * 4 ; break ; case XVideoView::YUV420P: linesize = width_; break ; case XVideoView::ARGB: linesize = width_ * 4 ; break ;
在 RGBA 格式中,每个像素由 4 个字节表示(R、G、B 和 A 各占 1 个字节),因此 linesize
是 width_ × 4
。
但是在 YUV420P 格式中:
YUV420P 是一种色度子采样格式,意味着 YUV 数据被分为三个独立的平面
Y 平面 :每个像素只占用 1 个字节(即 Y 分量),因此 linesize
为 width_
。
U 和 V 平面 :由于色度分量的分辨率被降低到四分之一(色度采样为 4:2:0),每个平面的 linesize
是 width_ / 2
。
因此,对于 YUV420P 格式,linesize
直接等于图像的宽度 width_
而无需乘以 4。这样可以确保你只处理每个像素的亮度(Y 分量),而不是每个像素的所有颜色信息(RGBA)。
总结一下 :YUV420P(YUV420P介绍 )一个像素共用Y、U或V,而RGBA每个像素的颜色信息是独立 的,四个通道(R、G、B、A)一起定义了像素的颜色和透明度。
使用场景
直接传入YUV数据
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 #include "sdlqtrgb.h" #include <fstream> #include <QMessageBox> #include "xvideo_view.h" static int sdl_width = 0 ;static int sdl_height = 0 ;static unsigned char * yuv = nullptr ;static int pix_size = 2 ;static std::ifstream yuv_file;static XVideoView *view = nullptr ;void SDLQtRGB::timerEvent (QTimerEvent* ev) { yuv_file.read ((char *)yuv, sdl_width * sdl_height * 1.5 ); if (view->IsExit ()) { view->Close (); exit (0 ); } view->Draw (yuv); } SDLQtRGB::SDLQtRGB (QWidget *parent) : QWidget (parent) { yuv_file.open ("400_300_25.yuv" , std::ios::binary); ui.setupUi (this ); sdl_width = 400 ; sdl_height = 300 ; this ->resize (sdl_width, sdl_height); ui.label->resize (sdl_width, sdl_height); view = XVideoView::Create (); set_win_id ((void *)ui.label->winId ()); view->Init (sdl_width, sdl_height, XVideoView::YUV420P); yuv = new unsigned char [sdl_width * sdl_height * pix_size]; startTimer (40 ); } void SDLQtRGB::resizeEvent (QResizeEvent *ev) { ui.label->resize (size ()); ui.label->move (0 , 0 ); view->Scale (width (), height ()); } SDLQtRGB::~SDLQtRGB () { SDL_Quit (); Close (); }
使用AVFrame传入YUV数据
AVFrame 介绍
(不单单可以传入YUV数据,也可以传入RGBA等数据 )
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 #include "sdlqtrgb.h" #include <fstream> #include <iostream> #include <QMessageBox> #include "xvideo_view.h" extern "C" { #include <libavcodec/avcodec.h> } #pragma comment(lib, "avutil.lib" ) static int sdl_width = 0 ;static int sdl_height = 0 ;static std::ifstream yuv_file;static XVideoView *view = nullptr ;static AVFrame* frame = nullptr ;void SDLQtRGB::timerEvent (QTimerEvent* ev) { yuv_file.read ((char *)frame->data[0 ], sdl_width * sdl_height); yuv_file.read ((char *)frame->data[1 ], sdl_width * sdl_height / 4 ); yuv_file.read ((char *)frame->data[2 ], sdl_width * sdl_height / 4 ); if (view->IsExit ()) { view->Close (); exit (0 ); } view->DrawFrame (frame); } SDLQtRGB::SDLQtRGB (QWidget *parent) : QWidget (parent) { yuv_file.open ("400_300_25.yuv" , std::ios::binary); if (!yuv_file) { QMessageBox::information (this , "" , "open yuv failed!" ); return ; } ui.setupUi (this ); sdl_width = 400 ; sdl_height = 300 ; this ->resize (sdl_width, sdl_height); ui.label->resize (sdl_width, sdl_height); view = XVideoView::Create (); view->Init (sdl_width, sdl_height, XVideoView::YUV420P); view->Close (); view->Init (sdl_width, sdl_height, XVideoView::YUV420P, (void *)ui.label->winId ()); frame = av_frame_alloc (); frame->width = sdl_width; frame->height = sdl_height; frame->format = AV_PIX_FMT_YUV420P; frame->linesize[0 ] = sdl_width; frame->linesize[1 ] = sdl_width / 2 ; frame->linesize[2 ] = sdl_width / 2 ; auto re = av_frame_get_buffer (frame, 0 ); if (re != 0 ) { char buf[1024 ] = { 0 }; av_strerror (re, buf, sizeof (buf)); std::cerr << buf << std::endl; } startTimer (40 ); } void SDLQtRGB::resizeEvent (QResizeEvent *ev) { ui.label->resize (size ()); ui.label->move (0 , 0 ); view->Scale (width (), height ()); } SDLQtRGB::~SDLQtRGB () {}