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
| #include "libMyPlayer.h"
#define LOAD_BGRA 1 #define LOAD_RGB24 0 #define LOAD_BGR24 0 #define LOAD_YUV420P 0
#if LOAD_BGRA const int bpp = 32; #elif LOAD_RGB24|LOAD_BGR24 const int bpp = 24; #elif LOAD_YUV420P const int bpp = 12; #endif
int screen_w = 500, screen_h = 500; int pixel_w = 320, pixel_h = 180;
unsigned char * buffer = new unsigned char[pixel_w*pixel_h*bpp / 8];
unsigned char * buffer_convert = new unsigned char[pixel_w*pixel_h * 4];
void CONVERT_24to32(unsigned char *image_in, unsigned char *image_out, int w, int h) { for (int i = 0;i < h;i++) for (int j = 0;j < w;j++) { if (SDL_BYTEORDER == SDL_LIL_ENDIAN) { image_out[(i*w + j) * 4 + 0] = image_in[(i*w + j) * 3 + 2]; image_out[(i*w + j) * 4 + 1] = image_in[(i*w + j) * 3 + 1]; image_out[(i*w + j) * 4 + 2] = image_in[(i*w + j) * 3]; image_out[(i*w + j) * 4 + 3] = '0'; } else { image_out[(i*w + j) * 4] = '0'; memcpy(image_out + (i*w + j) * 4 + 1, image_in + (i*w + j) * 3, 3); } } }
#define REFRESH_EVENT (SDL_USEREVENT + 1)
#define BREAK_EVENT (SDL_USEREVENT + 2)
int thread_exit = 0;
int refresh_video(void *opaque) { thread_exit = 0; while (!thread_exit) { SDL_Event event; event.type = REFRESH_EVENT; SDL_PushEvent(&event); SDL_Delay(40); } thread_exit = 0; SDL_Event event; event.type = BREAK_EVENT; SDL_PushEvent(&event); return 0; }
SDL_Window* screen; SDL_Renderer* sdlRenderer; SDL_Texture* sdlTexture;
SDL_Rect sdlRect;
SDL_Event event; HWND winhandler;
int SdlSetWin(HWND handler, const int win_w, const int win_h) { winhandler = handler; screen_w = win_w; screen_h = win_h; return 0; }
int SdlInit(const int pix_w, const int pix_h) { pixel_w = pix_w; pixel_h = pix_h;
buffer = new unsigned char[pixel_w*pixel_h*bpp / 8]; buffer_convert = new unsigned char[pixel_w*pixel_h * 4];
if (SDL_Init(SDL_INIT_VIDEO)) { printf("Could not initialize SDL - %s\n", SDL_GetError());
return -1; }
screen = SDL_CreateWindowFrom(winhandler);
if (!screen) { printf("SDL: could not create window - exiting:%s\n", SDL_GetError());
return -1; } sdlRenderer = SDL_CreateRenderer(screen, -1, 0);
Uint32 pixformat = 0; #if LOAD_BGRA pixformat = SDL_PIXELFORMAT_ARGB8888; #elif LOAD_RGB24 pixformat = SDL_PIXELFORMAT_RGB888; #elif LOAD_BGR24 pixformat = SDL_PIXELFORMAT_BGR888; #elif LOAD_YUV420P pixformat = SDL_PIXELFORMAT_IYUV; #endif
sdlTexture = SDL_CreateTexture(sdlRenderer, pixformat, SDL_TEXTUREACCESS_STREAMING, pixel_w, pixel_h); return 0; }
int SdlRender(unsigned char* buffer) {
#if LOAD_BGRA SDL_UpdateTexture(sdlTexture, NULL, buffer, pixel_w * 4); #elif LOAD_RGB24|LOAD_BGR24 CONVERT_24to32(buffer, buffer_convert, pixel_w, pixel_h); SDL_UpdateTexture(sdlTexture, NULL, buffer_convert, pixel_w * 4); #elif LOAD_YUV420P SDL_UpdateTexture(sdlTexture, NULL, buffer, pixel_w); #endif sdlRect.x = 0; sdlRect.y = 0; sdlRect.w = screen_w; sdlRect.h = screen_h;
SDL_RenderClear(sdlRenderer); SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, &sdlRect); SDL_RenderPresent(sdlRenderer);
return 0; }
|