typo fixes
[mplayer/greg.git] / libvo / vo_gl.c
blobd026d9deb768390fa17aff2eab90df23c35a5bbe
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <math.h>
6 #include "config.h"
7 #include "mp_msg.h"
8 #include "subopt-helper.h"
9 #include "video_out.h"
10 #include "video_out_internal.h"
11 #include "font_load.h"
12 #include "sub.h"
14 #include "gl_common.h"
15 #include "aspect.h"
16 #ifdef HAVE_NEW_GUI
17 #include "Gui/interface.h"
18 #endif
20 static vo_info_t info =
22 "X11 (OpenGL)",
23 "gl",
24 "Arpad Gereoffy <arpi@esp-team.scene.hu>",
28 LIBVO_EXTERN(gl)
30 #ifdef GL_WIN32
31 static int gl_vinfo = 0;
32 static HGLRC gl_context = 0;
33 #else
34 static XVisualInfo *gl_vinfo = NULL;
35 static GLXContext gl_context = 0;
36 static int wsGLXAttrib[] = { GLX_RGBA,
37 GLX_RED_SIZE,1,
38 GLX_GREEN_SIZE,1,
39 GLX_BLUE_SIZE,1,
40 GLX_DOUBLEBUFFER,
41 None };
42 #endif
44 static int use_osd;
45 static int scaled_osd;
46 //! How many parts the OSD may consist of at most
47 #define MAX_OSD_PARTS 20
48 //! Textures for OSD
49 static GLuint osdtex[MAX_OSD_PARTS];
50 #ifndef FAST_OSD
51 //! Alpha textures for OSD
52 static GLuint osdatex[MAX_OSD_PARTS];
53 #endif
54 //! Display lists that draw the OSD parts
55 static GLuint osdDispList[MAX_OSD_PARTS];
56 //! How many parts the OSD currently consists of
57 static int osdtexCnt;
58 static int osd_color;
60 static int use_aspect;
61 static int use_yuv;
62 static int use_rectangle;
63 static int err_shown;
64 static uint32_t image_width;
65 static uint32_t image_height;
66 static uint32_t image_format;
67 static int many_fmts;
68 static int use_glFinish;
69 static int swap_interval;
70 static GLenum gl_target;
71 static GLint gl_texfmt;
72 static GLenum gl_format;
73 static GLenum gl_type;
74 static GLuint gl_buffer;
75 static int gl_buffersize;
76 static GLuint fragprog;
77 static GLuint uvtexs[2];
78 static GLuint lookupTex;
79 static char *custom_prog;
80 static char *custom_tex;
81 static int custom_tlin;
83 static int int_pause;
84 static int eq_bri = 0;
85 static int eq_cont = 0;
86 static int eq_sat = 0;
87 static int eq_hue = 0;
88 static int eq_rgamma = 0;
89 static int eq_ggamma = 0;
90 static int eq_bgamma = 0;
92 static int texture_width;
93 static int texture_height;
94 static int mpi_flipped;
96 static unsigned int slice_height = 1;
98 static void resize(int x,int y){
99 mp_msg(MSGT_VO, MSGL_V, "[gl] Resize: %dx%d\n",x,y);
100 if (WinID >= 0) {
101 int top = 0, left = 0, w = x, h = y;
102 geometry(&top, &left, &w, &h, vo_screenwidth, vo_screenheight);
103 glViewport(top, left, w, h);
104 } else
105 glViewport( 0, 0, x, y );
107 glMatrixMode(GL_PROJECTION);
108 glLoadIdentity();
109 if (vo_fs && use_aspect) {
110 int new_w, new_h;
111 GLdouble scale_x, scale_y;
112 aspect(&new_w, &new_h, A_ZOOM);
113 panscan_calc();
114 new_w += vo_panscan_x;
115 new_h += vo_panscan_y;
116 scale_x = (GLdouble) new_w / (GLdouble) x;
117 scale_y = (GLdouble) new_h / (GLdouble) y;
118 glScaled(scale_x, scale_y, 1);
120 glOrtho(0, image_width, image_height, 0, -1,1);
122 glMatrixMode(GL_MODELVIEW);
123 glLoadIdentity();
125 if (!scaled_osd) {
126 #ifdef HAVE_FREETYPE
127 // adjust font size to display size
128 force_load_font = 1;
129 #endif
130 vo_osd_changed(OSDTYPE_OSD);
132 if (vo_fs && use_aspect && !vo_doublebuffering)
133 glClear(GL_COLOR_BUFFER_BIT);
136 static void texSize(int w, int h, int *texw, int *texh) {
137 if (use_rectangle) {
138 *texw = w; *texh = h;
139 } else {
140 *texw = 32;
141 while (*texw < w)
142 *texw *= 2;
143 *texh = 32;
144 while (*texh < h)
145 *texh *= 2;
149 //! maximum size of custom fragment program
150 #define MAX_CUSTOM_PROG_SIZE (1024 * 1024)
151 static void update_yuvconv(void) {
152 float bri = eq_bri / 100.0;
153 float cont = (eq_cont + 100) / 100.0;
154 float hue = eq_hue / 100.0 * 3.1415927;
155 float sat = (eq_sat + 100) / 100.0;
156 float rgamma = exp(log(8.0) * eq_rgamma / 100.0);
157 float ggamma = exp(log(8.0) * eq_ggamma / 100.0);
158 float bgamma = exp(log(8.0) * eq_bgamma / 100.0);
159 glSetupYUVConversion(gl_target, use_yuv, bri, cont, hue, sat,
160 rgamma, ggamma, bgamma);
161 if (custom_prog) {
162 FILE *f = fopen(custom_prog, "r");
163 if (!f)
164 mp_msg(MSGT_VO, MSGL_WARN,
165 "[gl] Could not read customprog %s\n", custom_prog);
166 else {
167 int i;
168 char *prog = calloc(1, MAX_CUSTOM_PROG_SIZE + 1);
169 fread(prog, 1, MAX_CUSTOM_PROG_SIZE, f);
170 fclose(f);
171 ProgramString(GL_FRAGMENT_PROGRAM, GL_PROGRAM_FORMAT_ASCII,
172 strlen(prog), prog);
173 glGetIntegerv(GL_PROGRAM_ERROR_POSITION, &i);
174 if (i != -1)
175 mp_msg(MSGT_VO, MSGL_ERR,
176 "[gl] Error in custom program at pos %i (%.20s)\n", i, &prog[i]);
177 free(prog);
179 ProgramEnvParameter4f(GL_FRAGMENT_PROGRAM, 0,
180 1.0 / texture_width, 1.0 / texture_height, 0, 0);
182 if (custom_tex) {
183 FILE *f = fopen(custom_tex, "r");
184 if (!f)
185 mp_msg(MSGT_VO, MSGL_WARN,
186 "[gl] Could not read customtex %s\n", custom_tex);
187 else {
188 int width, height, maxval;
189 ActiveTexture(GL_TEXTURE3);
190 if (glCreatePPMTex(GL_TEXTURE_2D, 3,
191 custom_tlin?GL_LINEAR:GL_NEAREST,
192 f, &width, &height, &maxval))
193 ProgramEnvParameter4f(GL_FRAGMENT_PROGRAM, 1,
194 1.0 / width, 1.0 / height, 1.0 / maxval, 0);
195 else
196 mp_msg(MSGT_VO, MSGL_WARN,
197 "[gl] Error parsing customtex %s\n", custom_tex);
198 fclose(f);
199 ActiveTexture(GL_TEXTURE0);
205 * \brief remove all OSD textures and display-lists, thus clearing it.
207 static void clearOSD(void) {
208 int i;
209 if (!osdtexCnt)
210 return;
211 glDeleteTextures(osdtexCnt, osdtex);
212 #ifndef FAST_OSD
213 glDeleteTextures(osdtexCnt, osdatex);
214 #endif
215 for (i = 0; i < osdtexCnt; i++)
216 glDeleteLists(osdDispList[i], 1);
217 osdtexCnt = 0;
221 * \brief uninitialize OpenGL context, freeing textures, buffers etc.
223 static void uninitGl(void) {
224 if (DeletePrograms && fragprog)
225 DeletePrograms(1, &fragprog);
226 fragprog = 0;
227 if (uvtexs[0] || uvtexs[1])
228 glDeleteTextures(2, uvtexs);
229 uvtexs[0] = uvtexs[1] = 0;
230 if (lookupTex)
231 glDeleteTextures(1, &lookupTex);
232 lookupTex = 0;
233 clearOSD();
234 if (DeleteBuffers && gl_buffer)
235 DeleteBuffers(1, &gl_buffer);
236 gl_buffer = 0; gl_buffersize = 0;
237 err_shown = 0;
241 * \brief Initialize a (new or reused) OpenGL context.
242 * set global gl-related variables to their default values
244 static int initGl(uint32_t d_width, uint32_t d_height) {
245 osdtexCnt = 0; gl_buffer = 0; gl_buffersize = 0; err_shown = 0;
246 fragprog = 0; uvtexs[0] = 0; uvtexs[1] = 0; lookupTex = 0;
247 texSize(image_width, image_height, &texture_width, &texture_height);
249 glDisable(GL_BLEND);
250 glDisable(GL_DEPTH_TEST);
251 glDepthMask(GL_FALSE);
252 glDisable(GL_CULL_FACE);
253 glEnable(gl_target);
254 glDrawBuffer(vo_doublebuffering?GL_BACK:GL_FRONT);
255 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
257 mp_msg(MSGT_VO, MSGL_V, "[gl] Creating %dx%d texture...\n",
258 texture_width, texture_height);
260 if (image_format == IMGFMT_YV12) {
261 glGenTextures(2, uvtexs);
262 ActiveTexture(GL_TEXTURE1);
263 BindTexture(gl_target, uvtexs[0]);
264 glCreateClearTex(gl_target, gl_texfmt, GL_LINEAR,
265 texture_width / 2, texture_height / 2, 128);
266 ActiveTexture(GL_TEXTURE2);
267 BindTexture(gl_target, uvtexs[1]);
268 glCreateClearTex(gl_target, gl_texfmt, GL_LINEAR,
269 texture_width / 2, texture_height / 2, 128);
270 switch (use_yuv) {
271 case YUV_CONVERSION_FRAGMENT_LOOKUP:
272 glGenTextures(1, &lookupTex);
273 ActiveTexture(GL_TEXTURE3);
274 glBindTexture(GL_TEXTURE_2D, lookupTex);
275 case YUV_CONVERSION_FRAGMENT_POW:
276 case YUV_CONVERSION_FRAGMENT:
277 GenPrograms(1, &fragprog);
278 BindProgram(GL_FRAGMENT_PROGRAM, fragprog);
279 break;
281 ActiveTexture(GL_TEXTURE0);
282 BindTexture(gl_target, 0);
283 update_yuvconv();
285 glCreateClearTex(gl_target, gl_texfmt, GL_LINEAR,
286 texture_width, texture_height, 0);
288 resize(d_width, d_height);
290 glClearColor( 0.0f,0.0f,0.0f,0.0f );
291 glClear( GL_COLOR_BUFFER_BIT );
292 if (SwapInterval && swap_interval >= 0)
293 SwapInterval(swap_interval);
294 return 1;
297 /* connect to server, create and map window,
298 * allocate colors and (shared) memory
300 static int
301 config(uint32_t width, uint32_t height, uint32_t d_width, uint32_t d_height, uint32_t flags, char *title, uint32_t format)
303 image_height = height;
304 image_width = width;
305 image_format = format;
306 glFindFormat(format, NULL, &gl_texfmt, &gl_format, &gl_type);
308 int_pause = 0;
310 panscan_init();
311 aspect_save_orig(width,height);
312 aspect_save_prescale(d_width,d_height);
313 update_xinerama_info();
315 aspect(&d_width,&d_height,A_NOZOOM);
316 vo_dx = (int)(vo_screenwidth - d_width) / 2;
317 vo_dy = (int)(vo_screenheight - d_height) / 2;
318 geometry(&vo_dx, &vo_dy, &d_width, &d_height,
319 vo_screenwidth, vo_screenheight);
320 vo_dx += xinerama_x;
321 vo_dy += xinerama_y;
322 #ifdef HAVE_NEW_GUI
323 if (use_gui) {
324 // GUI creates and manages window for us
325 vo_dwidth = d_width;
326 vo_dheight= d_height;
327 guiGetEvent(guiSetShVideo, 0);
328 goto glconfig;
330 #endif
331 #ifdef GL_WIN32
332 if (!vo_w32_config(d_width, d_height, flags))
333 return -1;
334 #else
335 if (WinID >= 0) {
336 vo_window = WinID ? (Window)WinID : mRootWin;
337 vo_x11_selectinput_witherr(mDisplay, vo_window,
338 StructureNotifyMask | KeyPressMask | PointerMotionMask |
339 ButtonPressMask | ButtonReleaseMask | ExposureMask);
340 goto glconfig;
342 if ( vo_window == None ) {
343 unsigned int fg, bg;
344 XSizeHints hint;
345 XVisualInfo *vinfo;
346 XEvent xev;
348 vo_fs = VO_FALSE;
350 hint.x = vo_dx;
351 hint.y = vo_dy;
352 hint.width = d_width;
353 hint.height = d_height;
354 hint.flags = PPosition | PSize;
356 /* Get some colors */
358 bg = WhitePixel(mDisplay, mScreen);
359 fg = BlackPixel(mDisplay, mScreen);
361 /* Make the window */
363 vinfo=glXChooseVisual( mDisplay,mScreen,wsGLXAttrib );
364 if (vinfo == NULL)
366 mp_msg(MSGT_VO, MSGL_ERR, "[gl] no GLX support present\n");
367 return -1;
372 vo_window = vo_x11_create_smooth_window(mDisplay, mRootWin, vinfo->visual, hint.x, hint.y, hint.width, hint.height,
373 vinfo->depth, XCreateColormap(mDisplay, mRootWin, vinfo->visual, AllocNone));
375 vo_x11_classhint( mDisplay,vo_window,"gl" );
376 vo_hidecursor(mDisplay,vo_window);
378 XSelectInput(mDisplay, vo_window, StructureNotifyMask);
379 /* Tell other applications about this window */
380 XSetStandardProperties(mDisplay, vo_window, title, title, None, NULL, 0, &hint);
381 /* Map window. */
382 XMapWindow(mDisplay, vo_window);
384 /* Wait for map. */
387 XNextEvent(mDisplay, &xev);
389 while (xev.type != MapNotify || xev.xmap.event != vo_window);
391 XSelectInput(mDisplay, vo_window, NoEventMask);
393 XSync(mDisplay, False);
395 vo_x11_selectinput_witherr(mDisplay, vo_window, StructureNotifyMask | KeyPressMask | PointerMotionMask
396 | ButtonPressMask | ButtonReleaseMask | ExposureMask
399 if (vo_ontop) vo_x11_setlayer(mDisplay, vo_window, vo_ontop);
401 vo_x11_nofs_sizepos(vo_dx, vo_dy, d_width, d_height);
402 if (vo_fs ^ (flags & VOFLAG_FULLSCREEN))
403 vo_x11_fullscreen();
404 #endif
406 glconfig:
407 if (vo_config_count)
408 uninitGl();
409 setGlWindow(&gl_vinfo, &gl_context, vo_window);
410 initGl(vo_dwidth, vo_dheight);
412 return 0;
415 static void check_events(void)
417 int e=vo_check_events();
418 if(e&VO_EVENT_RESIZE) resize(vo_dwidth,vo_dheight);
419 if(e&VO_EVENT_EXPOSE && int_pause) flip_page();
423 * Creates the textures and the display list needed for displaying
424 * an OSD part.
425 * Callback function for vo_draw_text().
427 static void create_osd_texture(int x0, int y0, int w, int h,
428 unsigned char *src, unsigned char *srca,
429 int stride)
431 // initialize to 8 to avoid special-casing on alignment
432 int sx = 8, sy = 8;
433 GLint scale_type = (scaled_osd) ? GL_LINEAR : GL_NEAREST;
435 if (w <= 0 || h <= 0 || stride < w) {
436 mp_msg(MSGT_VO, MSGL_V, "Invalid dimensions OSD for part!\n");
437 return;
439 texSize(w, h, &sx, &sy);
441 if (osdtexCnt >= MAX_OSD_PARTS) {
442 mp_msg(MSGT_VO, MSGL_ERR, "Too many OSD parts, contact the developers!\n");
443 return;
446 // create Textures for OSD part
447 glGenTextures(1, &osdtex[osdtexCnt]);
448 BindTexture(gl_target, osdtex[osdtexCnt]);
449 glCreateClearTex(gl_target, GL_LUMINANCE, scale_type, sx, sy, 0);
450 glUploadTex(gl_target, GL_LUMINANCE, GL_UNSIGNED_BYTE, src, stride,
451 0, 0, w, h, 0);
453 #ifndef FAST_OSD
454 glGenTextures(1, &osdatex[osdtexCnt]);
455 BindTexture(gl_target, osdatex[osdtexCnt]);
456 glCreateClearTex(gl_target, GL_ALPHA, scale_type, sx, sy, 255);
458 int i;
459 char *tmp = (char *)malloc(stride * h);
460 // convert alpha from weird MPlayer scale.
461 // in-place is not possible since it is reused for future OSDs
462 for (i = h * stride - 1; i > 0; i--)
463 tmp[i] = srca[i] - 1;
464 glUploadTex(gl_target, GL_ALPHA, GL_UNSIGNED_BYTE, tmp, stride,
465 0, 0, w, h, 0);
466 free(tmp);
468 #endif
470 BindTexture(gl_target, 0);
472 // Create a list for rendering this OSD part
473 osdDispList[osdtexCnt] = glGenLists(1);
474 glNewList(osdDispList[osdtexCnt], GL_COMPILE);
475 #ifndef FAST_OSD
476 // render alpha
477 glBlendFunc(GL_ZERO, GL_SRC_ALPHA);
478 BindTexture(gl_target, osdatex[osdtexCnt]);
479 glDrawTex(x0, y0, w, h, 0, 0, w, h, sx, sy, use_rectangle == 1, 0, 0);
480 #endif
481 // render OSD
482 glBlendFunc (GL_ONE, GL_ONE);
483 BindTexture(gl_target, osdtex[osdtexCnt]);
484 glDrawTex(x0, y0, w, h, 0, 0, w, h, sx, sy, use_rectangle == 1, 0, 0);
485 glEndList();
487 osdtexCnt++;
490 static void draw_osd(void)
492 if (!use_osd) return;
493 if (vo_osd_changed(0)) {
494 int osd_h, osd_w;
495 clearOSD();
496 osd_w = (scaled_osd) ? image_width : vo_dwidth;
497 osd_h = (scaled_osd) ? image_height : vo_dheight;
498 vo_draw_text(osd_w, osd_h, create_osd_texture);
502 static void
503 flip_page(void)
505 // glEnable(GL_TEXTURE_2D);
506 // glBindTexture(GL_TEXTURE_2D, texture_id);
508 glColor3f(1,1,1);
509 if (image_format == IMGFMT_YV12)
510 glEnableYUVConversion(gl_target, use_yuv);
511 glDrawTex(0, 0, image_width, image_height,
512 0, 0, image_width, image_height,
513 texture_width, texture_height,
514 use_rectangle == 1, image_format == IMGFMT_YV12, mpi_flipped);
515 if (image_format == IMGFMT_YV12)
516 glDisableYUVConversion(gl_target, use_yuv);
518 if (osdtexCnt > 0) {
519 // set special rendering parameters
520 if (!scaled_osd) {
521 glMatrixMode(GL_PROJECTION);
522 glPushMatrix();
523 glLoadIdentity();
524 glOrtho(0, vo_dwidth, vo_dheight, 0, -1, 1);
526 glEnable(GL_BLEND);
527 glColor4ub((osd_color >> 16) & 0xff, (osd_color >> 8) & 0xff, osd_color & 0xff, 0xff);
528 // draw OSD
529 glCallLists(osdtexCnt, GL_UNSIGNED_INT, osdDispList);
530 // set rendering parameters back to defaults
531 glDisable (GL_BLEND);
532 if (!scaled_osd)
533 glPopMatrix();
534 BindTexture(gl_target, 0);
537 if (use_glFinish)
538 glFinish();
539 if (vo_doublebuffering)
540 swapGlBuffers();
541 else if (!use_glFinish)
542 glFlush();
544 if (vo_fs && use_aspect && vo_doublebuffering)
545 glClear(GL_COLOR_BUFFER_BIT);
548 //static inline uint32_t draw_slice_x11(uint8_t *src[], uint32_t slice_num)
549 static int draw_slice(uint8_t *src[], int stride[], int w,int h,int x,int y)
551 mpi_flipped = (stride[0] < 0);
552 glUploadTex(gl_target, gl_format, gl_type, src[0], stride[0],
553 x, y, w, h, slice_height);
554 if (image_format == IMGFMT_YV12) {
555 ActiveTexture(GL_TEXTURE1);
556 glUploadTex(gl_target, gl_format, gl_type, src[1], stride[1],
557 x / 2, y / 2, w / 2, h / 2, slice_height);
558 ActiveTexture(GL_TEXTURE2);
559 glUploadTex(gl_target, gl_format, gl_type, src[2], stride[2],
560 x / 2, y / 2, w / 2, h / 2, slice_height);
561 ActiveTexture(GL_TEXTURE0);
563 return 0;
566 static uint32_t get_image(mp_image_t *mpi) {
567 if (!GenBuffers || !BindBuffer || !BufferData || !MapBuffer) {
568 if (!err_shown)
569 mp_msg(MSGT_VO, MSGL_ERR, "[gl] extensions missing for dr\n"
570 "Expect a _major_ speed penalty\n");
571 err_shown = 1;
572 return VO_FALSE;
574 if (mpi->flags & MP_IMGFLAG_READABLE) return VO_FALSE;
575 if (mpi->type == MP_IMGTYPE_IP || mpi->type == MP_IMGTYPE_IPB)
576 return VO_FALSE; // we can not provide readable buffers
577 if (!gl_buffer)
578 GenBuffers(1, &gl_buffer);
579 BindBuffer(GL_PIXEL_UNPACK_BUFFER, gl_buffer);
580 mpi->stride[0] = mpi->width * mpi->bpp / 8;
581 if (mpi->stride[0] * mpi->h > gl_buffersize) {
582 BufferData(GL_PIXEL_UNPACK_BUFFER, mpi->stride[0] * mpi->h,
583 NULL, GL_DYNAMIC_DRAW);
584 gl_buffersize = mpi->stride[0] * mpi->h;
586 UnmapBuffer(GL_PIXEL_UNPACK_BUFFER); // HACK, needed for some MPEG4 files??
587 mpi->planes[0] = MapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY);
588 BindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
589 if (mpi->planes[0] == NULL) {
590 if (!err_shown)
591 mp_msg(MSGT_VO, MSGL_ERR, "[gl] could not aquire buffer for dr\n"
592 "Expect a _major_ speed penalty\n");
593 err_shown = 1;
594 return VO_FALSE;
596 if (mpi->imgfmt == IMGFMT_YV12) {
597 // YV12
598 mpi->flags |= MP_IMGFLAG_COMMON_STRIDE | MP_IMGFLAG_COMMON_PLANE;
599 mpi->stride[0] = mpi->width;
600 mpi->planes[1] = mpi->planes[0] + mpi->stride[0] * mpi->height;
601 mpi->stride[1] = mpi->width >> 1;
602 mpi->planes[2] = mpi->planes[1] + mpi->stride[1] * (mpi->height >> 1);
603 mpi->stride[2] = mpi->width >> 1;
605 mpi->flags |= MP_IMGFLAG_DIRECT;
606 return VO_TRUE;
609 static uint32_t draw_image(mp_image_t *mpi) {
610 unsigned char *data = mpi->planes[0];
611 int slice = slice_height;
612 if (mpi->flags & MP_IMGFLAG_DRAW_CALLBACK)
613 return VO_TRUE;
614 if (mpi->flags & MP_IMGFLAG_DIRECT) {
615 data = NULL;
616 BindBuffer(GL_PIXEL_UNPACK_BUFFER, gl_buffer);
617 UnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
618 slice = 0; // always "upload" full texture
620 mpi_flipped = (mpi->stride[0] < 0);
621 glUploadTex(gl_target, gl_format, gl_type, data, mpi->stride[0],
622 mpi->x, mpi->y, mpi->w, mpi->h, slice);
623 if (mpi->imgfmt == IMGFMT_YV12) {
624 data += mpi->planes[1] - mpi->planes[0];
625 ActiveTexture(GL_TEXTURE1);
626 glUploadTex(gl_target, gl_format, gl_type, data, mpi->stride[1],
627 mpi->x / 2, mpi->y / 2, mpi->w / 2, mpi->h / 2, slice);
628 data += mpi->planes[2] - mpi->planes[1];
629 ActiveTexture(GL_TEXTURE2);
630 glUploadTex(gl_target, gl_format, gl_type, data, mpi->stride[2],
631 mpi->x / 2, mpi->y / 2, mpi->w / 2, mpi->h / 2, slice);
632 ActiveTexture(GL_TEXTURE0);
634 if (mpi->flags & MP_IMGFLAG_DIRECT)
635 BindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
636 return VO_TRUE;
639 static int
640 draw_frame(uint8_t *src[])
642 return VO_ERROR;
645 static int
646 query_format(uint32_t format)
648 int caps = VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW |
649 VFCAP_HWSCALE_UP | VFCAP_HWSCALE_DOWN | VFCAP_ACCEPT_STRIDE;
650 if (use_osd)
651 caps |= VFCAP_OSD;
652 if ((format == IMGFMT_RGB24) || (format == IMGFMT_RGBA))
653 return caps;
654 if (use_yuv && format == IMGFMT_YV12)
655 return caps;
656 if (many_fmts &&
657 glFindFormat(format, NULL, NULL, NULL, NULL))
658 return caps;
659 return 0;
663 static void
664 uninit(void)
666 if ( !vo_config_count ) return;
667 uninitGl();
668 releaseGlContext(&gl_vinfo, &gl_context);
669 if (custom_prog) free(custom_prog);
670 custom_prog = NULL;
671 if (custom_tex) free(custom_tex);
672 custom_tex = NULL;
673 vo_uninit();
676 static opt_t subopts[] = {
677 {"manyfmts", OPT_ARG_BOOL, &many_fmts, NULL},
678 {"osd", OPT_ARG_BOOL, &use_osd, NULL},
679 {"scaled-osd", OPT_ARG_BOOL, &scaled_osd, NULL},
680 {"aspect", OPT_ARG_BOOL, &use_aspect, NULL},
681 {"slice-height", OPT_ARG_INT, &slice_height, (opt_test_f)int_non_neg},
682 {"rectangle", OPT_ARG_INT, &use_rectangle,(opt_test_f)int_non_neg},
683 {"yuv", OPT_ARG_INT, &use_yuv, (opt_test_f)int_non_neg},
684 {"glfinish", OPT_ARG_BOOL, &use_glFinish, NULL},
685 {"swapinterval", OPT_ARG_INT, &swap_interval,NULL},
686 {"customprog", OPT_ARG_MSTRZ,&custom_prog, NULL},
687 {"customtex", OPT_ARG_MSTRZ,&custom_tex, NULL},
688 {"customtlin", OPT_ARG_BOOL, &custom_tlin, NULL},
689 {"osdcolor", OPT_ARG_INT, &osd_color, NULL},
690 {NULL}
693 static int preinit(const char *arg)
695 // set defaults
696 many_fmts = 1;
697 use_osd = 1;
698 scaled_osd = 0;
699 use_aspect = 1;
700 use_yuv = 0;
701 use_rectangle = 0;
702 use_glFinish = 0;
703 swap_interval = 1;
704 slice_height = -1;
705 custom_prog = NULL;
706 custom_tex = NULL;
707 custom_tlin = 1;
708 osd_color = 0xffffff;
709 if (subopt_parse(arg, subopts) != 0) {
710 mp_msg(MSGT_VO, MSGL_FATAL,
711 "\n-vo gl command line help:\n"
712 "Example: mplayer -vo gl:slice-height=4\n"
713 "\nOptions:\n"
714 " manyfmts\n"
715 " Enable extended color formats for OpenGL 1.2 and later\n"
716 " slice-height=<0-...>\n"
717 " Slice size for texture transfer, 0 for whole image\n"
718 " noosd\n"
719 " Do not use OpenGL OSD code\n"
720 " noaspect\n"
721 " Do not do aspect scaling\n"
722 " rectangle=<0,1,2>\n"
723 " 0: use power-of-two textures\n"
724 " 1: use texture_rectangle\n"
725 " 2: use texture_non_power_of_two\n"
726 " glfinish\n"
727 " Call glFinish() before swapping buffers\n"
728 " swapinterval=<n>\n"
729 " Interval in displayed frames between to buffer swaps.\n"
730 " 1 is equivalent to enable VSYNC, 0 to disable VSYNC.\n"
731 " Requires GLX_SGI_swap_control support to work.\n"
732 " yuv=<n>\n"
733 " 0: use software YUV to RGB conversion.\n"
734 " 1: use register combiners (nVidia only, for older cards).\n"
735 " 2: use fragment program.\n"
736 " 3: use fragment program with gamma correction.\n"
737 " 4: use fragment program with gamma correction via lookup.\n"
738 " 5: use ATI-specific method (for older cards).\n"
739 " customprog=<filename>\n"
740 " use a custom YUV conversion program\n"
741 " customtex=<filename>\n"
742 " use a custom YUV conversion lookup texture\n"
743 " nocustomtlin\n"
744 " use GL_NEAREST scaling for customtex texture\n"
745 " osdcolor=<0xRRGGBB>\n"
746 " use the given color for the OSD\n"
747 "\n" );
748 return -1;
750 if (use_rectangle == 1)
751 gl_target = GL_TEXTURE_RECTANGLE;
752 else
753 gl_target = GL_TEXTURE_2D;
754 if (slice_height == -1)
755 slice_height = use_yuv ? 16 : 4;
756 if (many_fmts)
757 mp_msg (MSGT_VO, MSGL_INFO, "[gl] using extended formats. "
758 "Use -vo gl:nomanyfmts if playback fails.\n");
759 mp_msg (MSGT_VO, MSGL_V, "[gl] Using %d as slice height "
760 "(0 means image height).\n", slice_height);
761 if( !vo_init() ) return -1; // Can't open X11
763 return 0;
766 static int control(uint32_t request, void *data, ...)
768 switch (request) {
769 case VOCTRL_PAUSE: return (int_pause=1);
770 case VOCTRL_RESUME: return (int_pause=0);
771 case VOCTRL_QUERY_FORMAT:
772 return query_format(*((uint32_t*)data));
773 case VOCTRL_GET_IMAGE:
774 return get_image(data);
775 case VOCTRL_DRAW_IMAGE:
776 return draw_image(data);
777 case VOCTRL_GUISUPPORT:
778 return VO_TRUE;
779 case VOCTRL_ONTOP:
780 vo_ontop();
781 return VO_TRUE;
782 case VOCTRL_FULLSCREEN:
783 vo_fullscreen();
784 resize(vo_dwidth, vo_dheight);
785 return VO_TRUE;
786 #ifdef GL_WIN32
787 case VOCTRL_BORDER:
788 vo_w32_border();
789 return VO_TRUE;
790 #endif
791 case VOCTRL_GET_PANSCAN:
792 if (!use_aspect) return VO_NOTIMPL;
793 return VO_TRUE;
794 case VOCTRL_SET_PANSCAN:
795 if (!use_aspect) return VO_NOTIMPL;
796 resize (vo_dwidth, vo_dheight);
797 return VO_TRUE;
798 case VOCTRL_GET_EQUALIZER:
799 if (image_format == IMGFMT_YV12) {
800 va_list va;
801 int *value;
802 va_start(va, data);
803 value = va_arg(va, int *);
804 va_end(va);
805 if (strcasecmp(data, "brightness") == 0) {
806 *value = eq_bri;
807 if (use_yuv == YUV_CONVERSION_COMBINERS) break; // not supported
808 } else if (strcasecmp(data, "contrast") == 0) {
809 *value = eq_cont;
810 if (use_yuv == YUV_CONVERSION_COMBINERS) break; // not supported
811 } else if (strcasecmp(data, "saturation") == 0) {
812 *value = eq_sat;
813 } else if (strcasecmp(data, "hue") == 0) {
814 *value = eq_hue;
815 } else if (strcasecmp(data, "gamma") == 0) {
816 *value = eq_rgamma;
817 if (use_yuv == YUV_CONVERSION_COMBINERS ||
818 use_yuv == YUV_CONVERSION_FRAGMENT) break; // not supported
819 } else if (strcasecmp(data, "red_gamma") == 0) {
820 *value = eq_rgamma;
821 if (use_yuv == YUV_CONVERSION_COMBINERS ||
822 use_yuv == YUV_CONVERSION_FRAGMENT) break; // not supported
823 } else if (strcasecmp(data, "green_gamma") == 0) {
824 *value = eq_ggamma;
825 if (use_yuv == YUV_CONVERSION_COMBINERS ||
826 use_yuv == YUV_CONVERSION_FRAGMENT) break; // not supported
827 } else if (strcasecmp(data, "blue_gamma") == 0) {
828 *value = eq_bgamma;
829 if (use_yuv == YUV_CONVERSION_COMBINERS ||
830 use_yuv == YUV_CONVERSION_FRAGMENT) break; // not supported
832 return VO_TRUE;
834 break;
835 case VOCTRL_SET_EQUALIZER:
836 if (image_format == IMGFMT_YV12) {
837 va_list va;
838 int value;
839 va_start(va, data);
840 value = va_arg(va, int);
841 va_end(va);
842 if (strcasecmp(data, "brightness") == 0) {
843 eq_bri = value;
844 if (use_yuv == YUV_CONVERSION_COMBINERS) break; // not supported
845 } else if (strcasecmp(data, "contrast") == 0) {
846 eq_cont = value;
847 if (use_yuv == YUV_CONVERSION_COMBINERS) break; // not supported
848 } else if (strcasecmp(data, "saturation") == 0) {
849 eq_sat = value;
850 } else if (strcasecmp(data, "hue") == 0) {
851 eq_hue = value;
852 } else if (strcasecmp(data, "gamma") == 0) {
853 eq_rgamma = eq_ggamma = eq_bgamma = value;
854 if (use_yuv == YUV_CONVERSION_COMBINERS ||
855 use_yuv == YUV_CONVERSION_FRAGMENT) break; // not supported
856 } else if (strcasecmp(data, "red_gamma") == 0) {
857 eq_rgamma = value;
858 if (use_yuv == YUV_CONVERSION_COMBINERS ||
859 use_yuv == YUV_CONVERSION_FRAGMENT) break; // not supported
860 } else if (strcasecmp(data, "green_gamma") == 0) {
861 eq_ggamma = value;
862 if (use_yuv == YUV_CONVERSION_COMBINERS ||
863 use_yuv == YUV_CONVERSION_FRAGMENT) break; // not supported
864 } else if (strcasecmp(data, "blue_gamma") == 0) {
865 eq_bgamma = value;
866 if (use_yuv == YUV_CONVERSION_COMBINERS ||
867 use_yuv == YUV_CONVERSION_FRAGMENT) break; // not supported
869 update_yuvconv();
870 return VO_TRUE;
872 break;
874 return VO_NOTIMPL;