8 #include "subopt-helper.h"
10 #include "video_out_internal.h"
11 #include "font_load.h"
14 #include "gl_common.h"
17 #include "gui/interface.h"
19 #include "fastmemcpy.h"
20 #include "libass/ass.h"
21 #include "libass/ass_mp.h"
23 static const vo_info_t info
=
27 "Arpad Gereoffy <arpi@esp-team.scene.hu>",
31 const LIBVO_EXTERN(gl
)
34 static int gl_vinfo
= 0;
35 static HGLRC gl_context
= 0;
36 #define update_xinerama_info w32_update_xinerama_info
37 #define vo_init vo_w32_init
38 #define vo_window vo_w32_window
40 static XVisualInfo
*gl_vinfo
= NULL
;
41 static GLXContext gl_context
= 0;
42 static int wsGLXAttrib
[] = { GLX_RGBA
,
51 static int scaled_osd
;
52 //! How many parts the OSD may consist of at most
53 #define MAX_OSD_PARTS 20
55 static GLuint osdtex
[MAX_OSD_PARTS
];
57 //! Alpha textures for OSD
58 static GLuint osdatex
[MAX_OSD_PARTS
];
60 static GLuint
*eosdtex
;
61 static GLuint largeeosdtex
[2];
62 //! Display lists that draw the OSD parts
63 static GLuint osdDispList
[MAX_OSD_PARTS
];
65 static GLuint osdaDispList
[MAX_OSD_PARTS
];
67 static GLuint eosdDispList
;
68 //! How many parts the OSD currently consists of
70 static int eosdtexCnt
;
73 static int use_aspect
;
78 static float filter_strength
;
79 static int yuvconvtype
;
80 static int use_rectangle
;
82 static uint32_t image_width
;
83 static uint32_t image_height
;
84 static uint32_t image_format
;
88 static int mesa_buffer
;
89 static int use_glFinish
;
90 static int swap_interval
;
91 static GLenum gl_target
;
92 static GLint gl_texfmt
;
93 static GLenum gl_format
;
94 static GLenum gl_type
;
95 static GLuint gl_buffer
;
96 static GLuint gl_buffer_uv
[2];
97 static int gl_buffersize
;
98 static int gl_buffersize_uv
;
99 static void *gl_bufferptr
;
100 static void *gl_bufferptr_uv
[2];
101 static int mesa_buffersize
;
102 static void *mesa_bufferptr
;
103 static GLuint fragprog
;
104 static GLuint default_texs
[22];
105 static char *custom_prog
;
106 static char *custom_tex
;
107 static int custom_tlin
;
108 static int custom_trect
;
110 static int int_pause
;
111 static int eq_bri
= 0;
112 static int eq_cont
= 0;
113 static int eq_sat
= 0;
114 static int eq_hue
= 0;
115 static int eq_rgamma
= 0;
116 static int eq_ggamma
= 0;
117 static int eq_bgamma
= 0;
119 static int texture_width
;
120 static int texture_height
;
121 static int mpi_flipped
;
122 static int vo_flipped
;
123 static int ass_border_x
, ass_border_y
;
125 static unsigned int slice_height
= 1;
127 static void redraw(void);
129 static void resize(int x
,int y
){
130 mp_msg(MSGT_VO
, MSGL_V
, "[gl] Resize: %dx%d\n",x
,y
);
132 int top
= 0, left
= 0, w
= x
, h
= y
;
133 geometry(&top
, &left
, &w
, &h
, vo_screenwidth
, vo_screenheight
);
134 glViewport(top
, left
, w
, h
);
136 glViewport( 0, 0, x
, y
);
138 glMatrixMode(GL_PROJECTION
);
140 if (vo_fs
&& use_aspect
) {
142 GLdouble scale_x
, scale_y
;
143 aspect(&new_w
, &new_h
, A_ZOOM
);
145 new_w
+= vo_panscan_x
;
146 new_h
+= vo_panscan_y
;
147 scale_x
= (GLdouble
)new_w
/ (GLdouble
)x
;
148 scale_y
= (GLdouble
)new_h
/ (GLdouble
)y
;
149 glScaled(scale_x
, scale_y
, 1);
150 ass_border_x
= (vo_screenwidth
- new_w
) / 2;
151 ass_border_y
= (vo_screenheight
- new_h
) / 2;
153 glOrtho(0, image_width
, image_height
, 0, -1,1);
155 glMatrixMode(GL_MODELVIEW
);
159 #ifdef CONFIG_FREETYPE
160 // adjust font size to display size
163 vo_osd_changed(OSDTYPE_OSD
);
165 glClear(GL_COLOR_BUFFER_BIT
);
169 static void texSize(int w
, int h
, int *texw
, int *texh
) {
171 *texw
= w
; *texh
= h
;
180 if (mesa_buffer
) *texw
= (*texw
+ 63) & ~63;
181 else if (ati_hack
) *texw
= (*texw
+ 511) & ~511;
184 //! maximum size of custom fragment program
185 #define MAX_CUSTOM_PROG_SIZE (1024 * 1024)
186 static void update_yuvconv(void) {
187 float bri
= eq_bri
/ 100.0;
188 float cont
= (eq_cont
+ 100) / 100.0;
189 float hue
= eq_hue
/ 100.0 * 3.1415927;
190 float sat
= (eq_sat
+ 100) / 100.0;
191 float rgamma
= exp(log(8.0) * eq_rgamma
/ 100.0);
192 float ggamma
= exp(log(8.0) * eq_ggamma
/ 100.0);
193 float bgamma
= exp(log(8.0) * eq_bgamma
/ 100.0);
194 gl_conversion_params_t params
= {gl_target
, yuvconvtype
,
195 bri
, cont
, hue
, sat
, rgamma
, ggamma
, bgamma
,
196 texture_width
, texture_height
, filter_strength
};
197 glSetupYUVConversion(¶ms
);
199 FILE *f
= fopen(custom_prog
, "r");
201 mp_msg(MSGT_VO
, MSGL_WARN
,
202 "[gl] Could not read customprog %s\n", custom_prog
);
204 char *prog
= calloc(1, MAX_CUSTOM_PROG_SIZE
+ 1);
205 fread(prog
, 1, MAX_CUSTOM_PROG_SIZE
, f
);
207 loadGPUProgram(GL_FRAGMENT_PROGRAM
, prog
);
210 ProgramEnvParameter4f(GL_FRAGMENT_PROGRAM
, 0,
211 1.0 / texture_width
, 1.0 / texture_height
,
212 texture_width
, texture_height
);
215 FILE *f
= fopen(custom_tex
, "r");
217 mp_msg(MSGT_VO
, MSGL_WARN
,
218 "[gl] Could not read customtex %s\n", custom_tex
);
220 int width
, height
, maxval
;
221 ActiveTexture(GL_TEXTURE3
);
222 if (glCreatePPMTex(custom_trect
?GL_TEXTURE_RECTANGLE
:GL_TEXTURE_2D
, 0,
223 custom_tlin
?GL_LINEAR
:GL_NEAREST
,
224 f
, &width
, &height
, &maxval
))
225 ProgramEnvParameter4f(GL_FRAGMENT_PROGRAM
, 1,
226 1.0 / width
, 1.0 / height
, width
, height
);
228 mp_msg(MSGT_VO
, MSGL_WARN
,
229 "[gl] Error parsing customtex %s\n", custom_tex
);
231 ActiveTexture(GL_TEXTURE0
);
237 * \brief remove all OSD textures and display-lists, thus clearing it.
239 static void clearOSD(void) {
243 glDeleteTextures(osdtexCnt
, osdtex
);
245 glDeleteTextures(osdtexCnt
, osdatex
);
246 for (i
= 0; i
< osdtexCnt
; i
++)
247 glDeleteLists(osdaDispList
[i
], 1);
249 for (i
= 0; i
< osdtexCnt
; i
++)
250 glDeleteLists(osdDispList
[i
], 1);
255 * \brief remove textures, display list and free memory used by EOSD
257 static void clearEOSD(void) {
259 glDeleteLists(eosdDispList
, 1);
262 glDeleteTextures(eosdtexCnt
, eosdtex
);
269 * \brief construct display list from ass image list
270 * \param img image list to create OSD from.
271 * A value of NULL has the same effect as clearEOSD()
273 static void genEOSD(mp_eosd_images_t
*imgs
) {
278 GLint scale_type
= scaled_osd
? GL_LINEAR
: GL_NEAREST
;
279 ass_image_t
*img
= imgs
->imgs
;
282 if (imgs
->changed
== 0) // there are elements, but they are unchanged
284 if (img
&& imgs
->changed
== 1) // there are elements, but they just moved
290 if (!largeeosdtex
[0]) {
291 glGenTextures(2, largeeosdtex
);
292 BindTexture(gl_target
, largeeosdtex
[0]);
293 glCreateClearTex(gl_target
, GL_ALPHA
, GL_ALPHA
, GL_UNSIGNED_BYTE
, scale_type
, 512, 512, 0);
294 BindTexture(gl_target
, largeeosdtex
[1]);
295 glCreateClearTex(gl_target
, GL_ALPHA
, GL_ALPHA
, GL_UNSIGNED_BYTE
, scale_type
, 512, 512, 0);
297 for (i
= img
; i
; i
= i
->next
)
299 if (i
->w
<= 0 || i
->h
<= 0 || i
->stride
< i
->w
)
301 if (i
->w
< 16 && i
->h
< 16 && tinytexcur
< 1024)
303 else if (i
->w
< 32 && i
->h
< 32 && smalltexcur
< 256)
308 mp_msg(MSGT_VO
, MSGL_DBG2
, "EOSD counts (tiny, small, all): %i, %i, %i\n",
309 tinytexcur
, smalltexcur
, eosdtexCnt
);
311 eosdtex
= calloc(eosdtexCnt
, sizeof(GLuint
));
312 glGenTextures(eosdtexCnt
, eosdtex
);
314 tinytexcur
= smalltexcur
= 0;
315 for (i
= img
, curtex
= eosdtex
; i
; i
= i
->next
) {
317 if (i
->w
<= 0 || i
->h
<= 0 || i
->stride
< i
->w
) {
318 mp_msg(MSGT_VO
, MSGL_V
, "Invalid dimensions OSD for part!\n");
321 if (i
->w
< 16 && i
->h
< 16 && tinytexcur
< 1024) {
322 x
= (tinytexcur
& 31) << 4;
323 y
= (tinytexcur
>> 5) << 4;
324 BindTexture(gl_target
, largeeosdtex
[0]);
326 } else if (i
->w
< 32 && i
->h
< 32 && smalltexcur
< 256) {
327 x
= (smalltexcur
& 15) << 5;
328 y
= (smalltexcur
>> 4) << 5;
329 BindTexture(gl_target
, largeeosdtex
[1]);
332 texSize(i
->w
, i
->h
, &sx
, &sy
);
333 BindTexture(gl_target
, *curtex
++);
334 glCreateClearTex(gl_target
, GL_ALPHA
, GL_ALPHA
, GL_UNSIGNED_BYTE
, scale_type
, sx
, sy
, 0);
336 glUploadTex(gl_target
, GL_ALPHA
, GL_UNSIGNED_BYTE
, i
->bitmap
, i
->stride
,
337 x
, y
, i
->w
, i
->h
, 0);
339 eosdDispList
= glGenLists(1);
341 glNewList(eosdDispList
, GL_COMPILE
);
342 tinytexcur
= smalltexcur
= 0;
343 for (i
= img
, curtex
= eosdtex
; i
; i
= i
->next
) {
345 if (i
->w
<= 0 || i
->h
<= 0 || i
->stride
< i
->w
)
347 glColor4ub(i
->color
>> 24, (i
->color
>> 16) & 0xff, (i
->color
>> 8) & 0xff, 255 - (i
->color
& 0xff));
348 if (i
->w
< 16 && i
->h
< 16 && tinytexcur
< 1024) {
349 x
= (tinytexcur
& 31) << 4;
350 y
= (tinytexcur
>> 5) << 4;
352 BindTexture(gl_target
, largeeosdtex
[0]);
354 } else if (i
->w
< 32 && i
->h
< 32 && smalltexcur
< 256) {
355 x
= (smalltexcur
& 15) << 5;
356 y
= (smalltexcur
>> 4) << 5;
358 BindTexture(gl_target
, largeeosdtex
[1]);
361 texSize(i
->w
, i
->h
, &sx
, &sy
);
362 BindTexture(gl_target
, *curtex
++);
364 glDrawTex(i
->dst_x
, i
->dst_y
, i
->w
, i
->h
, x
, y
, i
->w
, i
->h
, sx
, sy
, use_rectangle
== 1, 0, 0);
367 BindTexture(gl_target
, 0);
371 * \brief uninitialize OpenGL context, freeing textures, buffers etc.
373 static void uninitGl(void) {
375 if (DeletePrograms
&& fragprog
)
376 DeletePrograms(1, &fragprog
);
378 while (default_texs
[i
] != 0)
381 glDeleteTextures(i
, default_texs
);
386 glDeleteTextures(2, largeeosdtex
);
388 if (DeleteBuffers
&& gl_buffer
)
389 DeleteBuffers(1, &gl_buffer
);
390 gl_buffer
= 0; gl_buffersize
= 0;
392 if (DeleteBuffers
&& gl_buffer_uv
[0])
393 DeleteBuffers(2, gl_buffer_uv
);
394 gl_buffer_uv
[0] = gl_buffer_uv
[1] = 0; gl_buffersize_uv
= 0;
395 gl_bufferptr_uv
[0] = gl_bufferptr_uv
[1] = 0;
398 FreeMemoryMESA(mDisplay
, mScreen
, mesa_bufferptr
);
400 mesa_bufferptr
= NULL
;
404 static void autodetectGlExtensions(void) {
405 const char *extensions
= glGetString(GL_EXTENSIONS
);
406 const char *vendor
= glGetString(GL_VENDOR
);
407 int is_ati
= strstr(vendor
, "ATI") != NULL
;
408 if (ati_hack
== -1) ati_hack
= is_ati
;
409 if (force_pbo
== -1) force_pbo
= strstr(extensions
, "_pixel_buffer_object") ? is_ati
: 0;
410 if (use_rectangle
== -1) use_rectangle
= strstr(extensions
, "_texture_non_power_of_two") ? 0 : 0;
411 if (is_ati
&& (lscale
== 1 || lscale
== 2 || cscale
== 1 || cscale
== 2))
412 mp_msg(MSGT_VO
, MSGL_WARN
, "Selected scaling mode may be broken on ATI cards.\n"
413 "Tell _them_ to fix GL_REPEAT if you have issues.\n");
417 * \brief Initialize a (new or reused) OpenGL context.
418 * set global gl-related variables to their default values
420 static int initGl(uint32_t d_width
, uint32_t d_height
) {
421 autodetectGlExtensions();
422 texSize(image_width
, image_height
, &texture_width
, &texture_height
);
425 glDisable(GL_DEPTH_TEST
);
426 glDepthMask(GL_FALSE
);
427 glDisable(GL_CULL_FACE
);
429 glDrawBuffer(vo_doublebuffering
?GL_BACK
:GL_FRONT
);
430 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_MODULATE
);
432 mp_msg(MSGT_VO
, MSGL_V
, "[gl] Creating %dx%d texture...\n",
433 texture_width
, texture_height
);
435 if (image_format
== IMGFMT_YV12
) {
437 glGenTextures(21, default_texs
);
438 default_texs
[21] = 0;
439 for (i
= 0; i
< 7; i
++) {
440 ActiveTexture(GL_TEXTURE1
+ i
);
441 BindTexture(GL_TEXTURE_2D
, default_texs
[i
]);
442 BindTexture(GL_TEXTURE_RECTANGLE
, default_texs
[i
+ 7]);
443 BindTexture(GL_TEXTURE_3D
, default_texs
[i
+ 14]);
445 ActiveTexture(GL_TEXTURE1
);
446 glCreateClearTex(gl_target
, gl_texfmt
, gl_format
, gl_type
, GL_LINEAR
,
447 texture_width
/ 2, texture_height
/ 2, 128);
448 ActiveTexture(GL_TEXTURE2
);
449 glCreateClearTex(gl_target
, gl_texfmt
, gl_format
, gl_type
, GL_LINEAR
,
450 texture_width
/ 2, texture_height
/ 2, 128);
452 case YUV_CONVERSION_FRAGMENT_LOOKUP
:
453 case YUV_CONVERSION_FRAGMENT_POW
:
454 case YUV_CONVERSION_FRAGMENT
:
455 if (!GenPrograms
|| !BindProgram
) {
456 mp_msg(MSGT_VO
, MSGL_ERR
, "[gl] fragment program functions missing!\n");
459 GenPrograms(1, &fragprog
);
460 BindProgram(GL_FRAGMENT_PROGRAM
, fragprog
);
463 ActiveTexture(GL_TEXTURE0
);
464 BindTexture(gl_target
, 0);
467 glCreateClearTex(gl_target
, gl_texfmt
, gl_format
, gl_type
, GL_LINEAR
,
468 texture_width
, texture_height
, 0);
470 resize(d_width
, d_height
);
472 glClearColor( 0.0f
,0.0f
,0.0f
,0.0f
);
473 glClear( GL_COLOR_BUFFER_BIT
);
474 if (SwapInterval
&& swap_interval
>= 0)
475 SwapInterval(swap_interval
);
479 /* connect to server, create and map window,
480 * allocate colors and (shared) memory
483 config(uint32_t width
, uint32_t height
, uint32_t d_width
, uint32_t d_height
, uint32_t flags
, char *title
, uint32_t format
)
485 image_height
= height
;
487 image_format
= format
;
488 glFindFormat(format
, NULL
, &gl_texfmt
, &gl_format
, &gl_type
);
491 vo_flipped
= !!(flags
& VOFLAG_FLIPPING
);
495 // GUI creates and manages window for us
496 guiGetEvent(guiSetShVideo
, 0);
503 if (!vo_w32_config(d_width
, d_height
, flags
))
507 XVisualInfo
*vinfo
=glXChooseVisual( mDisplay
,mScreen
,wsGLXAttrib
);
510 mp_msg(MSGT_VO
, MSGL_ERR
, "[gl] no GLX support present\n");
514 vo_x11_create_vo_window(vinfo
, vo_dx
, vo_dy
, d_width
, d_height
, flags
,
515 XCreateColormap(mDisplay
, mRootWin
, vinfo
->visual
, AllocNone
),
523 setGlWindow(&gl_vinfo
, &gl_context
, vo_window
);
524 initGl(vo_dwidth
, vo_dheight
);
529 static void check_events(void)
531 int e
=vo_check_events();
532 if(e
&VO_EVENT_RESIZE
) resize(vo_dwidth
,vo_dheight
);
533 if(e
&VO_EVENT_EXPOSE
&& int_pause
) redraw();
537 * Creates the textures and the display list needed for displaying
539 * Callback function for vo_draw_text().
541 static void create_osd_texture(int x0
, int y0
, int w
, int h
,
542 unsigned char *src
, unsigned char *srca
,
545 // initialize to 8 to avoid special-casing on alignment
547 GLint scale_type
= scaled_osd
? GL_LINEAR
: GL_NEAREST
;
549 if (w
<= 0 || h
<= 0 || stride
< w
) {
550 mp_msg(MSGT_VO
, MSGL_V
, "Invalid dimensions OSD for part!\n");
553 texSize(w
, h
, &sx
, &sy
);
555 if (osdtexCnt
>= MAX_OSD_PARTS
) {
556 mp_msg(MSGT_VO
, MSGL_ERR
, "Too many OSD parts, contact the developers!\n");
560 // create Textures for OSD part
561 glGenTextures(1, &osdtex
[osdtexCnt
]);
562 BindTexture(gl_target
, osdtex
[osdtexCnt
]);
563 glCreateClearTex(gl_target
, GL_LUMINANCE
, GL_LUMINANCE
, GL_UNSIGNED_BYTE
, scale_type
, sx
, sy
, 0);
564 glUploadTex(gl_target
, GL_LUMINANCE
, GL_UNSIGNED_BYTE
, src
, stride
,
568 glGenTextures(1, &osdatex
[osdtexCnt
]);
569 BindTexture(gl_target
, osdatex
[osdtexCnt
]);
570 glCreateClearTex(gl_target
, GL_ALPHA
, GL_ALPHA
, GL_UNSIGNED_BYTE
, scale_type
, sx
, sy
, 0);
573 char *tmp
= malloc(stride
* h
);
574 // convert alpha from weird MPlayer scale.
575 // in-place is not possible since it is reused for future OSDs
576 for (i
= h
* stride
- 1; i
>= 0; i
--)
578 glUploadTex(gl_target
, GL_ALPHA
, GL_UNSIGNED_BYTE
, tmp
, stride
,
584 BindTexture(gl_target
, 0);
586 // Create a list for rendering this OSD part
588 osdaDispList
[osdtexCnt
] = glGenLists(1);
589 glNewList(osdaDispList
[osdtexCnt
], GL_COMPILE
);
591 BindTexture(gl_target
, osdatex
[osdtexCnt
]);
592 glDrawTex(x0
, y0
, w
, h
, 0, 0, w
, h
, sx
, sy
, use_rectangle
== 1, 0, 0);
595 osdDispList
[osdtexCnt
] = glGenLists(1);
596 glNewList(osdDispList
[osdtexCnt
], GL_COMPILE
);
598 BindTexture(gl_target
, osdtex
[osdtexCnt
]);
599 glDrawTex(x0
, y0
, w
, h
, 0, 0, w
, h
, sx
, sy
, use_rectangle
== 1, 0, 0);
605 static void do_render_osd(void);
607 static void draw_osd(void)
609 if (!use_osd
) return;
610 if (vo_osd_changed(0)) {
613 osd_w
= scaled_osd
? image_width
: vo_dwidth
;
614 osd_h
= scaled_osd
? image_height
: vo_dheight
;
615 vo_draw_text(osd_w
, osd_h
, create_osd_texture
);
617 if (vo_doublebuffering
) do_render_osd();
620 static void do_render(void) {
621 // glEnable(GL_TEXTURE_2D);
622 // glBindTexture(GL_TEXTURE_2D, texture_id);
625 if (image_format
== IMGFMT_YV12
)
626 glEnableYUVConversion(gl_target
, yuvconvtype
);
627 glDrawTex(0, 0, image_width
, image_height
,
628 0, 0, image_width
, image_height
,
629 texture_width
, texture_height
,
630 use_rectangle
== 1, image_format
== IMGFMT_YV12
,
631 mpi_flipped
^ vo_flipped
);
632 if (image_format
== IMGFMT_YV12
)
633 glDisableYUVConversion(gl_target
, yuvconvtype
);
636 static void do_render_osd(void) {
637 if (osdtexCnt
> 0 || eosdDispList
) {
638 // set special rendering parameters
640 glMatrixMode(GL_PROJECTION
);
643 glOrtho(0, vo_dwidth
, vo_dheight
, 0, -1, 1);
647 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
648 glCallList(eosdDispList
);
651 glColor4ub((osd_color
>> 16) & 0xff, (osd_color
>> 8) & 0xff, osd_color
& 0xff, 0xff - (osd_color
>> 24));
654 glBlendFunc(GL_ZERO
, GL_ONE_MINUS_SRC_ALPHA
);
655 glCallLists(osdtexCnt
, GL_UNSIGNED_INT
, osdaDispList
);
657 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
658 glCallLists(osdtexCnt
, GL_UNSIGNED_INT
, osdDispList
);
660 // set rendering parameters back to defaults
664 BindTexture(gl_target
, 0);
668 static void flip_page(void) {
669 if (vo_doublebuffering
) {
670 if (use_glFinish
) glFinish();
672 if (vo_fs
&& use_aspect
)
673 glClear(GL_COLOR_BUFFER_BIT
);
677 if (use_glFinish
) glFinish();
682 static void redraw(void) {
683 if (vo_doublebuffering
) { do_render(); do_render_osd(); }
687 //static inline uint32_t draw_slice_x11(uint8_t *src[], uint32_t slice_num)
688 static int draw_slice(uint8_t *src
[], int stride
[], int w
,int h
,int x
,int y
)
690 mpi_flipped
= stride
[0] < 0;
691 glUploadTex(gl_target
, gl_format
, gl_type
, src
[0], stride
[0],
692 x
, y
, w
, h
, slice_height
);
693 if (image_format
== IMGFMT_YV12
) {
694 ActiveTexture(GL_TEXTURE1
);
695 glUploadTex(gl_target
, gl_format
, gl_type
, src
[1], stride
[1],
696 x
/ 2, y
/ 2, w
/ 2, h
/ 2, slice_height
);
697 ActiveTexture(GL_TEXTURE2
);
698 glUploadTex(gl_target
, gl_format
, gl_type
, src
[2], stride
[2],
699 x
/ 2, y
/ 2, w
/ 2, h
/ 2, slice_height
);
700 ActiveTexture(GL_TEXTURE0
);
705 static uint32_t get_image(mp_image_t
*mpi
) {
707 if (!GenBuffers
|| !BindBuffer
|| !BufferData
|| !MapBuffer
) {
709 mp_msg(MSGT_VO
, MSGL_ERR
, "[gl] extensions missing for dr\n"
710 "Expect a _major_ speed penalty\n");
714 if (mpi
->flags
& MP_IMGFLAG_READABLE
) return VO_FALSE
;
715 if (mesa_buffer
) mpi
->width
= texture_width
;
717 mpi
->width
= texture_width
;
718 mpi
->height
= texture_height
;
720 mpi
->stride
[0] = mpi
->width
* mpi
->bpp
/ 8;
721 needed_size
= mpi
->stride
[0] * mpi
->height
;
724 if (mesa_bufferptr
&& needed_size
> mesa_buffersize
) {
725 FreeMemoryMESA(mDisplay
, mScreen
, mesa_bufferptr
);
726 mesa_bufferptr
= NULL
;
729 mesa_bufferptr
= AllocateMemoryMESA(mDisplay
, mScreen
, needed_size
, 0, 0, 0);
730 mesa_buffersize
= needed_size
;
732 mpi
->planes
[0] = mesa_bufferptr
;
735 GenBuffers(1, &gl_buffer
);
736 BindBuffer(GL_PIXEL_UNPACK_BUFFER
, gl_buffer
);
737 if (needed_size
> gl_buffersize
) {
738 gl_buffersize
= needed_size
;
739 BufferData(GL_PIXEL_UNPACK_BUFFER
, gl_buffersize
,
740 NULL
, GL_DYNAMIC_DRAW
);
743 gl_bufferptr
= MapBuffer(GL_PIXEL_UNPACK_BUFFER
, GL_WRITE_ONLY
);
744 mpi
->planes
[0] = gl_bufferptr
;
745 BindBuffer(GL_PIXEL_UNPACK_BUFFER
, 0);
747 if (!mpi
->planes
[0]) {
749 mp_msg(MSGT_VO
, MSGL_ERR
, "[gl] could not acquire buffer for dr\n"
750 "Expect a _major_ speed penalty\n");
754 if (mpi
->imgfmt
== IMGFMT_YV12
) {
756 mpi
->flags
|= MP_IMGFLAG_COMMON_STRIDE
| MP_IMGFLAG_COMMON_PLANE
;
757 mpi
->stride
[0] = mpi
->width
;
758 mpi
->planes
[1] = mpi
->planes
[0] + mpi
->stride
[0] * mpi
->height
;
759 mpi
->stride
[1] = mpi
->width
>> 1;
760 mpi
->planes
[2] = mpi
->planes
[1] + mpi
->stride
[1] * (mpi
->height
>> 1);
761 mpi
->stride
[2] = mpi
->width
>> 1;
762 if (ati_hack
&& !mesa_buffer
) {
763 mpi
->flags
&= ~MP_IMGFLAG_COMMON_PLANE
;
764 if (!gl_buffer_uv
[0]) GenBuffers(2, gl_buffer_uv
);
765 if (mpi
->stride
[1] * mpi
->height
> gl_buffersize_uv
) {
766 BindBuffer(GL_PIXEL_UNPACK_BUFFER
, gl_buffer_uv
[0]);
767 BufferData(GL_PIXEL_UNPACK_BUFFER
, mpi
->stride
[1] * mpi
->height
,
768 NULL
, GL_DYNAMIC_DRAW
);
769 BindBuffer(GL_PIXEL_UNPACK_BUFFER
, gl_buffer_uv
[1]);
770 BufferData(GL_PIXEL_UNPACK_BUFFER
, mpi
->stride
[1] * mpi
->height
,
771 NULL
, GL_DYNAMIC_DRAW
);
772 gl_buffersize_uv
= mpi
->stride
[1] * mpi
->height
;
774 if (!gl_bufferptr_uv
[0]) {
775 BindBuffer(GL_PIXEL_UNPACK_BUFFER
, gl_buffer_uv
[0]);
776 gl_bufferptr_uv
[0] = MapBuffer(GL_PIXEL_UNPACK_BUFFER
, GL_WRITE_ONLY
);
777 BindBuffer(GL_PIXEL_UNPACK_BUFFER
, gl_buffer_uv
[1]);
778 gl_bufferptr_uv
[1] = MapBuffer(GL_PIXEL_UNPACK_BUFFER
, GL_WRITE_ONLY
);
780 mpi
->planes
[1] = gl_bufferptr_uv
[0];
781 mpi
->planes
[2] = gl_bufferptr_uv
[1];
784 mpi
->flags
|= MP_IMGFLAG_DIRECT
;
788 static void clear_border(uint8_t *dst
, int start
, int stride
, int height
, int full_height
, int value
) {
789 int right_border
= stride
- start
;
790 int bottom_border
= full_height
- height
;
792 memset(dst
+ start
, value
, right_border
);
796 if (bottom_border
> 0)
797 memset(dst
, value
, stride
* bottom_border
);
800 static uint32_t draw_image(mp_image_t
*mpi
) {
801 int slice
= slice_height
;
803 unsigned char *planes
[3];
804 mp_image_t mpi2
= *mpi
;
805 int w
= mpi
->w
, h
= mpi
->h
;
806 if (mpi
->flags
& MP_IMGFLAG_DRAW_CALLBACK
)
808 mpi2
.flags
= 0; mpi2
.type
= MP_IMGTYPE_TEMP
;
809 mpi2
.width
= mpi2
.w
; mpi2
.height
= mpi2
.h
;
810 if (force_pbo
&& !(mpi
->flags
& MP_IMGFLAG_DIRECT
) && !gl_bufferptr
&& get_image(&mpi2
) == VO_TRUE
) {
811 int bpp
= mpi
->imgfmt
== IMGFMT_YV12
? 8 : mpi
->bpp
;
812 memcpy_pic(mpi2
.planes
[0], mpi
->planes
[0], mpi
->w
* bpp
/ 8, mpi
->h
, mpi2
.stride
[0], mpi
->stride
[0]);
813 if (mpi
->imgfmt
== IMGFMT_YV12
) {
814 memcpy_pic(mpi2
.planes
[1], mpi
->planes
[1], mpi
->w
>> 1, mpi
->h
>> 1, mpi2
.stride
[1], mpi
->stride
[1]);
815 memcpy_pic(mpi2
.planes
[2], mpi
->planes
[2], mpi
->w
>> 1, mpi
->h
>> 1, mpi2
.stride
[2], mpi
->stride
[2]);
817 if (ati_hack
) { // since we have to do a full upload we need to clear the borders
818 clear_border(mpi2
.planes
[0], mpi
->w
* bpp
/ 8, mpi2
.stride
[0], mpi
->h
, mpi2
.height
, 0);
819 if (mpi
->imgfmt
== IMGFMT_YV12
) {
820 clear_border(mpi2
.planes
[1], mpi
->w
>> 1, mpi2
.stride
[1], mpi
->h
>> 1, mpi2
.height
>> 1, 128);
821 clear_border(mpi2
.planes
[2], mpi
->w
>> 1, mpi2
.stride
[2], mpi
->h
>> 1, mpi2
.height
>> 1, 128);
826 stride
[0] = mpi
->stride
[0]; stride
[1] = mpi
->stride
[1]; stride
[2] = mpi
->stride
[2];
827 planes
[0] = mpi
->planes
[0]; planes
[1] = mpi
->planes
[1]; planes
[2] = mpi
->planes
[2];
828 mpi_flipped
= stride
[0] < 0;
829 if (mpi
->flags
& MP_IMGFLAG_DIRECT
) {
831 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE
, 1);
834 intptr_t base
= (intptr_t)planes
[0];
835 if (ati_hack
) { w
= texture_width
; h
= texture_height
; }
837 base
+= (mpi
->h
- 1) * stride
[0];
841 BindBuffer(GL_PIXEL_UNPACK_BUFFER
, gl_buffer
);
842 UnmapBuffer(GL_PIXEL_UNPACK_BUFFER
);
844 if (!(mpi
->flags
& MP_IMGFLAG_COMMON_PLANE
))
845 planes
[0] = planes
[1] = planes
[2] = NULL
;
847 slice
= 0; // always "upload" full texture
849 glUploadTex(gl_target
, gl_format
, gl_type
, planes
[0], stride
[0],
850 mpi
->x
, mpi
->y
, w
, h
, slice
);
851 if (mpi
->imgfmt
== IMGFMT_YV12
) {
852 if ((mpi
->flags
& MP_IMGFLAG_DIRECT
) && !(mpi
->flags
& MP_IMGFLAG_COMMON_PLANE
)) {
853 BindBuffer(GL_PIXEL_UNPACK_BUFFER
, gl_buffer_uv
[0]);
854 UnmapBuffer(GL_PIXEL_UNPACK_BUFFER
);
855 gl_bufferptr_uv
[0] = NULL
;
857 ActiveTexture(GL_TEXTURE1
);
858 glUploadTex(gl_target
, gl_format
, gl_type
, planes
[1], stride
[1],
859 mpi
->x
/ 2, mpi
->y
/ 2, w
/ 2, h
/ 2, slice
);
860 if ((mpi
->flags
& MP_IMGFLAG_DIRECT
) && !(mpi
->flags
& MP_IMGFLAG_COMMON_PLANE
)) {
861 BindBuffer(GL_PIXEL_UNPACK_BUFFER
, gl_buffer_uv
[1]);
862 UnmapBuffer(GL_PIXEL_UNPACK_BUFFER
);
863 gl_bufferptr_uv
[1] = NULL
;
865 ActiveTexture(GL_TEXTURE2
);
866 glUploadTex(gl_target
, gl_format
, gl_type
, planes
[2], stride
[2],
867 mpi
->x
/ 2, mpi
->y
/ 2, w
/ 2, h
/ 2, slice
);
868 ActiveTexture(GL_TEXTURE0
);
870 if (mpi
->flags
& MP_IMGFLAG_DIRECT
) {
871 if (mesa_buffer
) glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE
, 0);
872 else BindBuffer(GL_PIXEL_UNPACK_BUFFER
, 0);
875 if (vo_doublebuffering
) do_render();
880 draw_frame(uint8_t *src
[])
886 query_format(uint32_t format
)
888 int caps
= VFCAP_CSP_SUPPORTED
| VFCAP_CSP_SUPPORTED_BY_HW
|
890 VFCAP_HWSCALE_UP
| VFCAP_HWSCALE_DOWN
| VFCAP_ACCEPT_STRIDE
;
892 caps
|= VFCAP_OSD
| VFCAP_EOSD
| (scaled_osd
? 0 : VFCAP_EOSD_UNSCALED
);
893 if (format
== IMGFMT_RGB24
|| format
== IMGFMT_RGBA
)
895 if (use_yuv
&& format
== IMGFMT_YV12
)
897 // HACK, otherwise we get only b&w with some filters (e.g. -vf eq)
898 // ideally MPlayer should be fixed instead not to use Y800 when it has the choice
899 if (!use_yuv
&& (format
== IMGFMT_Y8
|| format
== IMGFMT_Y800
))
901 if (!use_ycbcr
&& (format
== IMGFMT_UYVY
|| format
== IMGFMT_YUY2
))
904 glFindFormat(format
, NULL
, NULL
, NULL
, NULL
))
913 if (!vo_config_count
) return;
915 releaseGlContext(&gl_vinfo
, &gl_context
);
916 if (custom_prog
) free(custom_prog
);
918 if (custom_tex
) free(custom_tex
);
923 static opt_t subopts
[] = {
924 {"manyfmts", OPT_ARG_BOOL
, &many_fmts
, NULL
},
925 {"osd", OPT_ARG_BOOL
, &use_osd
, NULL
},
926 {"scaled-osd", OPT_ARG_BOOL
, &scaled_osd
, NULL
},
927 {"aspect", OPT_ARG_BOOL
, &use_aspect
, NULL
},
928 {"ycbcr", OPT_ARG_BOOL
, &use_ycbcr
, NULL
},
929 {"slice-height", OPT_ARG_INT
, &slice_height
, (opt_test_f
)int_non_neg
},
930 {"rectangle", OPT_ARG_INT
, &use_rectangle
,(opt_test_f
)int_non_neg
},
931 {"yuv", OPT_ARG_INT
, &use_yuv
, (opt_test_f
)int_non_neg
},
932 {"lscale", OPT_ARG_INT
, &lscale
, (opt_test_f
)int_non_neg
},
933 {"cscale", OPT_ARG_INT
, &cscale
, (opt_test_f
)int_non_neg
},
934 {"filter-strength", OPT_ARG_FLOAT
, &filter_strength
, NULL
},
935 {"ati-hack", OPT_ARG_BOOL
, &ati_hack
, NULL
},
936 {"force-pbo", OPT_ARG_BOOL
, &force_pbo
, NULL
},
937 {"mesa-buffer", OPT_ARG_BOOL
, &mesa_buffer
, NULL
},
938 {"glfinish", OPT_ARG_BOOL
, &use_glFinish
, NULL
},
939 {"swapinterval", OPT_ARG_INT
, &swap_interval
,NULL
},
940 {"customprog", OPT_ARG_MSTRZ
,&custom_prog
, NULL
},
941 {"customtex", OPT_ARG_MSTRZ
,&custom_tex
, NULL
},
942 {"customtlin", OPT_ARG_BOOL
, &custom_tlin
, NULL
},
943 {"customtrect", OPT_ARG_BOOL
, &custom_trect
, NULL
},
944 {"osdcolor", OPT_ARG_INT
, &osd_color
, NULL
},
948 static int preinit(const char *arg
)
959 filter_strength
= 0.5;
971 osd_color
= 0xffffff;
972 if (subopt_parse(arg
, subopts
) != 0) {
973 mp_msg(MSGT_VO
, MSGL_FATAL
,
974 "\n-vo gl command line help:\n"
975 "Example: mplayer -vo gl:slice-height=4\n"
978 " Disable extended color formats for OpenGL 1.2 and later\n"
979 " slice-height=<0-...>\n"
980 " Slice size for texture transfer, 0 for whole image\n"
982 " Do not use OpenGL OSD code\n"
984 " Render OSD at movie resolution and scale it\n"
986 " Do not do aspect scaling\n"
987 " rectangle=<0,1,2>\n"
988 " 0: use power-of-two textures\n"
989 " 1: use texture_rectangle\n"
990 " 2: use texture_non_power_of_two\n"
992 " Workaround ATI bug with PBOs\n"
994 " Force use of PBO even if this involves an extra memcpy\n"
996 " Call glFinish() before swapping buffers\n"
997 " swapinterval=<n>\n"
998 " Interval in displayed frames between to buffer swaps.\n"
999 " 1 is equivalent to enable VSYNC, 0 to disable VSYNC.\n"
1000 " Requires GLX_SGI_swap_control support to work.\n"
1002 " 0: use software YUV to RGB conversion.\n"
1003 " 1: use register combiners (nVidia only, for older cards).\n"
1004 " 2: use fragment program.\n"
1005 " 3: use fragment program with gamma correction.\n"
1006 " 4: use fragment program with gamma correction via lookup.\n"
1007 " 5: use ATI-specific method (for older cards).\n"
1008 " 6: use lookup via 3D texture.\n"
1010 " 0: use standard bilinear scaling for luma.\n"
1011 " 1: use improved bicubic scaling for luma.\n"
1012 " 2: use cubic in X, linear in Y direction scaling for luma.\n"
1013 " 3: as 1 but without using a lookup texture.\n"
1014 " 4: experimental unsharp masking (sharpening).\n"
1015 " 5: experimental unsharp masking (sharpening) with larger radius.\n"
1017 " as lscale but for chroma (2x slower with little visible effect).\n"
1018 " filter-strength=<value>\n"
1019 " set the effect strength for some lscale/cscale filters\n"
1020 " customprog=<filename>\n"
1021 " use a custom YUV conversion program\n"
1022 " customtex=<filename>\n"
1023 " use a custom YUV conversion lookup texture\n"
1025 " use GL_NEAREST scaling for customtex texture\n"
1027 " use texture_rectangle for customtex texture\n"
1028 " osdcolor=<0xAARRGGBB>\n"
1029 " use the given color for the OSD\n"
1031 " also try to use the GL_MESA_ycbcr_texture extension\n"
1035 if (use_rectangle
== 1)
1036 gl_target
= GL_TEXTURE_RECTANGLE
;
1038 gl_target
= GL_TEXTURE_2D
;
1039 yuvconvtype
= use_yuv
| lscale
<< YUV_LUM_SCALER_SHIFT
| cscale
<< YUV_CHROM_SCALER_SHIFT
;
1041 mp_msg(MSGT_VO
, MSGL_INFO
, "[gl] using extended formats. "
1042 "Use -vo gl:nomanyfmts if playback fails.\n");
1043 mp_msg(MSGT_VO
, MSGL_V
, "[gl] Using %d as slice height "
1044 "(0 means image height).\n", slice_height
);
1045 if (!vo_init()) return -1; // Can't open X11
1050 #define MASK_ALL_YUV (~(1 << YUV_CONVERSION_NONE))
1051 #define MASK_NOT_COMBINERS (~((1 << YUV_CONVERSION_NONE) | (1 << YUV_CONVERSION_COMBINERS) | (1 << YUV_CONVERSION_COMBINERS_ATI)))
1052 #define MASK_GAMMA_SUPPORT (MASK_NOT_COMBINERS & ~(1 << YUV_CONVERSION_FRAGMENT))
1054 static const struct {
1059 {"brightness", &eq_bri
, MASK_NOT_COMBINERS
},
1060 {"contrast", &eq_cont
, MASK_NOT_COMBINERS
},
1061 {"saturation", &eq_sat
, MASK_ALL_YUV
},
1062 {"hue", &eq_hue
, MASK_ALL_YUV
},
1063 {"gamma", &eq_rgamma
, MASK_GAMMA_SUPPORT
},
1064 {"red_gamma", &eq_rgamma
, MASK_GAMMA_SUPPORT
},
1065 {"green_gamma", &eq_ggamma
, MASK_GAMMA_SUPPORT
},
1066 {"blue_gamma", &eq_bgamma
, MASK_GAMMA_SUPPORT
},
1070 static int control(uint32_t request
, void *data
)
1075 int_pause
= (request
== VOCTRL_PAUSE
);
1077 case VOCTRL_QUERY_FORMAT
:
1078 return query_format(*(uint32_t*)data
);
1079 case VOCTRL_GET_IMAGE
:
1080 return get_image(data
);
1081 case VOCTRL_DRAW_IMAGE
:
1082 return draw_image(data
);
1083 case VOCTRL_DRAW_EOSD
:
1088 case VOCTRL_GET_EOSD_RES
:
1090 mp_eosd_res_t
*r
= data
;
1091 r
->mt
= r
->mb
= r
->ml
= r
->mr
= 0;
1092 if (scaled_osd
) {r
->w
= image_width
; r
->h
= image_height
;}
1094 r
->w
= vo_screenwidth
; r
->h
= vo_screenheight
;
1095 r
->ml
= r
->mr
= ass_border_x
;
1096 r
->mt
= r
->mb
= ass_border_y
;
1098 r
->w
= vo_dwidth
; r
->h
= vo_dheight
;
1102 case VOCTRL_GUISUPPORT
:
1107 case VOCTRL_FULLSCREEN
:
1109 resize(vo_dwidth
, vo_dheight
);
1112 vo_gl_border(global_vo
);
1113 resize(vo_dwidth
, vo_dheight
);
1115 case VOCTRL_GET_PANSCAN
:
1116 if (!use_aspect
) return VO_NOTIMPL
;
1118 case VOCTRL_SET_PANSCAN
:
1119 if (!use_aspect
) return VO_NOTIMPL
;
1120 resize(vo_dwidth
, vo_dheight
);
1122 case VOCTRL_GET_EQUALIZER
:
1123 if (image_format
== IMGFMT_YV12
) {
1124 struct voctrl_get_equalizer_args
*args
= data
;
1126 for (i
= 0; eq_map
[i
].name
; i
++)
1127 if (strcmp(args
->name
, eq_map
[i
].name
) == 0) break;
1128 if (!(eq_map
[i
].supportmask
& (1 << use_yuv
)))
1130 *args
->valueptr
= *eq_map
[i
].value
;
1134 case VOCTRL_SET_EQUALIZER
:
1135 if (image_format
== IMGFMT_YV12
) {
1136 struct voctrl_set_equalizer_args
*args
= data
;
1138 for (i
= 0; eq_map
[i
].name
; i
++)
1139 if (strcmp(args
->name
, eq_map
[i
].name
) == 0) break;
1140 if (!(eq_map
[i
].supportmask
& (1 << use_yuv
)))
1142 *eq_map
[i
].value
= args
->value
;
1147 case VOCTRL_UPDATE_SCREENINFO
:
1148 update_xinerama_info();