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;
int videoWidth = YUV_WIDTH; int videoHeight = YUV_HEIGHT; int winWidth = YUV_WIDTH; int winHeight = YUV_HEIGHT;
FILE* video = NULL; const char* videoPath = "yuv420p_320x240.yuv";
size_t videoBufferLen = 0; uint8_t* videoBuffer = NULL;
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; }
|