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 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
| #include <stdio.h>
#include <stdint.h>
#include <math.h>
#include <stdlib.h>
#include <SDL/SDL.h>
#include <SDL/SDL_mutex.h>
#include <GL/glu.h>
#include <vlc/vlc.h>
#define WIDTH 1024
#define HEIGHT 768
#define VIDEO_FILE "/home/user/path/movie.avi"
struct ctx {
SDL_Surface *surf;
SDL_mutex *mutex;
};
static void catchEx (libvlc_exception_t *ex) {
if(libvlc_exception_raised(ex)) {
fprintf(stderr, "Exception libvlc : %s\n", libvlc_exception_get_message(ex));
exit(EXIT_FAILURE);
}
libvlc_exception_clear(ex);
}
static void lock(struct ctx *ctx, void **pp_ret) {
SDL_LockMutex(ctx->mutex);
SDL_LockSurface(ctx->surf);
*pp_ret = ctx->surf->pixels;
}
static void unlock(struct ctx *ctx) {
SDL_UnlockSurface(ctx->surf);
SDL_UnlockMutex(ctx->mutex);
}
int loadTexture(SDL_Surface *sdlimage) {
GLuint retval;
void *raw;
int w, h, i, j, bpp;
Uint8 *srcPixel, *dstPixel;
Uint32 truePixel;
GLenum errorCode;
if (sdlimage->format->BytesPerPixel < 2) {
printf("Bad image -- not true color!\n");
SDL_FreeSurface(sdlimage);
return 0;
}
w = sdlimage->w;
h = sdlimage->h;
raw = (void *) malloc(w * h * 4);
dstPixel = (Uint8 *) raw;
bpp = sdlimage->format->BytesPerPixel;
for (i = h - 1; i >= 0; i--) {
for (j = 0; j < w; j++) {
srcPixel = (Uint8 *) sdlimage->pixels + i * sdlimage->pitch + j * bpp;
switch (bpp) {
case 1:
truePixel = *srcPixel;
break;
case 2:
truePixel = *(Uint16 *) srcPixel;
break;
case 3:
if (SDL_BYTEORDER == SDL_BIG_ENDIAN) {
truePixel = srcPixel[0] << 16 | srcPixel[1] << 8 | srcPixel[2];
} else {
truePixel = srcPixel[0] | srcPixel[1] << 8 | srcPixel[2] << 16;
}
break;
case 4:
truePixel = *(Uint32 *) srcPixel;
break;
default:
printf("Image bpp of %d unusable\n", bpp);
SDL_UnlockSurface(sdlimage);
SDL_FreeSurface(sdlimage);
free(raw);
return 0;
}
SDL_GetRGBA(truePixel, sdlimage->format, &(dstPixel[0]), &(dstPixel[1]), &(dstPixel[2]), &(dstPixel[3]));
dstPixel++;
dstPixel++;
dstPixel++;
dstPixel++;
}
}
while (glGetError()) {
;
}
glBindTexture(GL_TEXTURE_2D, retval);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
errorCode = glGetError();
if (errorCode != 0) {
if (errorCode == GL_OUT_OF_MEMORY) {
printf("Out of texture memory!\n");
}
glDeleteTextures(1, &retval);
free(raw);
return 0;
}
gluBuild2DMipmaps(GL_TEXTURE_2D, 4, w, h, GL_RGBA, GL_UNSIGNED_BYTE, (Uint8 *) raw);
errorCode = glGetError();
if (errorCode != 0) {
if (errorCode == GL_OUT_OF_MEMORY) {
printf("Out of texture memory!\n");
}
glDeleteTextures(1, &retval);
free(raw);
return 0;
}
return retval;
}
int main(int argc, char *argv[])
{
char clock[64], cunlock[64], cdata[64];
char width[32], height[32], pitch[32];
libvlc_exception_t ex;
libvlc_instance_t *libvlc;
libvlc_media_t *m;
libvlc_media_player_t *mp;
char const *vlc_argv[] = {
"--quiet",
"--ignore-config",
"--vout", "vmem",
"--vmem-width", width,
"--vmem-height", height,
"--vmem-pitch", pitch,
"--vmem-chroma", "RV32",
"--vmem-lock", clock,
"--vmem-unlock", cunlock,
"--vmem-data", cdata
};
int vlc_argc = sizeof(vlc_argv) / sizeof(*vlc_argv);
SDL_Event event;
int done = 0, action = 0;
struct ctx ctx;
/*
* Init libSDL
*/
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTTHREAD) == -1) {
printf("cannot initialize SDL\n");
exit(EXIT_FAILURE);
}
ctx.surf = SDL_CreateRGBSurface(SDL_SWSURFACE, WIDTH, HEIGHT, 32, 0xf800, 0x07e0, 0x001f, 0);
ctx.mutex = SDL_CreateMutex();
if( ! SDL_SetVideoMode(1280, 800, 0, SDL_ANYFORMAT | SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_OPENGL) ) {
printf("cannot set video mode\n");
exit(EXIT_FAILURE);
}
glEnable(GL_TEXTURE_2D);
GLuint image;
glGenTextures(1, &image);
/*
* Init libVLC
*/
sprintf(clock, "%lld", (long long int)(intptr_t)lock);
sprintf(cunlock, "%lld", (long long int)(intptr_t)unlock);
sprintf(cdata, "%lld", (long long int)(intptr_t)&ctx);
sprintf(width, "%i", WIDTH);
sprintf(height, "%i", HEIGHT);
sprintf(pitch, "%i", WIDTH * 4);
libvlc_exception_init(&ex);
libvlc = libvlc_new(vlc_argc, vlc_argv, &ex);
catchEx(&ex);
m = libvlc_media_new(libvlc, VIDEO_FILE, &ex);
catchEx(&ex);
mp = libvlc_media_player_new_from_media(m, &ex);
catchEx(&ex);
libvlc_media_release(m);
libvlc_media_player_play(mp, &ex);
catchEx(&ex);
while(!done) {
action = 0;
/* Keys: enter (fullscreen), space (pause), escape (quit) */
while( SDL_PollEvent( &event ) ) {
switch(event.type)
{
case SDL_QUIT:
done = 1;
break;
case SDL_KEYDOWN:
action = event.key.keysym.sym;
break;
}
}
switch(action) {
case SDLK_ESCAPE:
done = 1;
break;
}
glClear(GL_COLOR_BUFFER_BIT);
/* Blitting the surface does not prevent it from being locked and
* written to by another thread, so we use this additional mutex. */
SDL_LockMutex(ctx.mutex);
image = loadTexture(ctx.surf);
SDL_UnlockMutex(ctx.mutex);
glBegin(GL_QUADS);
glTexCoord2d(0, 1);
glVertex2f(0, 0.9);
glTexCoord2d(1, 1);
glVertex2f(0.9, 0.9);
glTexCoord2d(1, 0);
glVertex2f(0.9, -0.9);
glTexCoord2d(0, 0);
glVertex2f(0, -0.9);
glEnd();
glFlush();
SDL_GL_SwapBuffers();
SDL_Delay(10);
}
glDeleteTextures(1, &image);
/*
* Stop stream and clean up libVLC
*/
libvlc_media_player_stop(mp, &ex);
catchEx(&ex);
libvlc_media_player_release(mp);
libvlc_release(libvlc);
/*
* Close window and clean up libSDL
*/
SDL_DestroyMutex(ctx.mutex);
SDL_FreeSurface(ctx.surf);
SDL_Quit();
exit(EXIT_SUCCESS);
} |
Partager