SDL2显示YUV图像

来源:互联网 发布:建筑业 知乎 编辑:程序博客网 时间:2024/05/22 01:48
#include <stdio.h>#include <SDL/SDL.h>#include <Windows.h>#pragma comment(lib, "SDL2.lib")#pragma comment(lib, "SDL2main.lib")/* Prepare a dummy image. */static void FillYuvImage(BYTE* pYuv, int nWidth, int nHeight, int nIndex){int x, y, i;i = nIndex;BYTE* pY = pYuv;BYTE* pU = pYuv + nWidth * nHeight;BYTE* pV = pYuv + nWidth * nHeight * 5 / 4;/* Y */for (y = 0; y < nHeight; y++){for (x = 0; x < nWidth; x++){pY[y * nWidth + x] = x + y + i * 3;}}/* Cb and Cr */for (y = 0; y < nHeight / 2; y++){for (x = 0; x < nWidth / 2; x++){pU[y * (nWidth/2) + x] = 128 + y + i * 2;pV[y * (nWidth/2) + x] = 64 + x + i * 5;}}}int main(int argc, char* argv[]){if (SDL_Init(SDL_INIT_VIDEO)){printf("Could not initialize SDL - %s\n", SDL_GetError());return -1;}// 提升图像质量,否则默认缩放质量会有毛剌SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1");SDL_Window*   window  = SDL_CreateWindowFrom(::GetConsoleWindow());SDL_Renderer* render  = SDL_CreateRenderer(window, -1, 0);const int W = 1920;const int H = 1080;SDL_Texture*  texture = SDL_CreateTexture(render, SDL_PIXELFORMAT_YV12, SDL_TEXTUREACCESS_STREAMING, W, H);static BYTE Yuv[W*H * 2];BYTE* pY = Yuv;BYTE* pU = Yuv + W*H;BYTE* pV = Yuv + W*H * 5 / 4;int index = 0;while (true){FillYuvImage(Yuv, W, H, index++);int e = SDL_UpdateYUVTexture(texture, NULL,pY, W,pU, W / 2,pV, W / 2);//SDL_RenderClear(render);SDL_RenderCopy(render, texture, NULL, NULL);SDL_RenderPresent(render);Sleep(40);}return 0;}

原创粉丝点击