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
;
77 static float filter_strength
;
78 static int yuvconvtype
;
79 static int use_rectangle
;
81 static uint32_t image_width
;
82 static uint32_t image_height
;
83 static uint32_t image_format
;
87 static int use_glFinish
;
88 static int swap_interval
;
89 static GLenum gl_target
;
90 static GLint gl_texfmt
;
91 static GLenum gl_format
;
92 static GLenum gl_type
;
93 static GLuint gl_buffer
;
94 static int gl_buffersize
;
95 static void *gl_bufferptr
;
96 static GLuint fragprog
;
97 static GLuint default_texs
[22];
98 static char *custom_prog
;
99 static char *custom_tex
;
100 static int custom_tlin
;
101 static int custom_trect
;
103 static int int_pause
;
104 static int eq_bri
= 0;
105 static int eq_cont
= 0;
106 static int eq_sat
= 0;
107 static int eq_hue
= 0;
108 static int eq_rgamma
= 0;
109 static int eq_ggamma
= 0;
110 static int eq_bgamma
= 0;
112 static int texture_width
;
113 static int texture_height
;
114 static int mpi_flipped
;
115 static int vo_flipped
;
116 static int ass_border_x
, ass_border_y
;
118 static unsigned int slice_height
= 1;
120 static void redraw(void);
122 static void resize(int x
,int y
){
123 mp_msg(MSGT_VO
, MSGL_V
, "[gl] Resize: %dx%d\n",x
,y
);
125 int top
= 0, left
= 0, w
= x
, h
= y
;
126 geometry(&top
, &left
, &w
, &h
, vo_screenwidth
, vo_screenheight
);
127 glViewport(top
, left
, w
, h
);
129 glViewport( 0, 0, x
, y
);
131 glMatrixMode(GL_PROJECTION
);
133 if (vo_fs
&& use_aspect
) {
135 GLdouble scale_x
, scale_y
;
136 aspect(&new_w
, &new_h
, A_ZOOM
);
138 new_w
+= vo_panscan_x
;
139 new_h
+= vo_panscan_y
;
140 scale_x
= (GLdouble
) new_w
/ (GLdouble
) x
;
141 scale_y
= (GLdouble
) new_h
/ (GLdouble
) y
;
142 glScaled(scale_x
, scale_y
, 1);
143 ass_border_x
= (vo_screenwidth
- new_w
) / 2;
144 ass_border_y
= (vo_screenheight
- new_h
) / 2;
146 glOrtho(0, image_width
, image_height
, 0, -1,1);
148 glMatrixMode(GL_MODELVIEW
);
153 // adjust font size to display size
156 vo_osd_changed(OSDTYPE_OSD
);
158 glClear(GL_COLOR_BUFFER_BIT
);
162 static void texSize(int w
, int h
, int *texw
, int *texh
) {
164 *texw
= w
; *texh
= h
;
175 //! maximum size of custom fragment program
176 #define MAX_CUSTOM_PROG_SIZE (1024 * 1024)
177 static void update_yuvconv(void) {
178 float bri
= eq_bri
/ 100.0;
179 float cont
= (eq_cont
+ 100) / 100.0;
180 float hue
= eq_hue
/ 100.0 * 3.1415927;
181 float sat
= (eq_sat
+ 100) / 100.0;
182 float rgamma
= exp(log(8.0) * eq_rgamma
/ 100.0);
183 float ggamma
= exp(log(8.0) * eq_ggamma
/ 100.0);
184 float bgamma
= exp(log(8.0) * eq_bgamma
/ 100.0);
185 gl_conversion_params_t params
= {gl_target
, yuvconvtype
,
186 bri
, cont
, hue
, sat
, rgamma
, ggamma
, bgamma
,
187 texture_width
, texture_height
, filter_strength
};
188 glSetupYUVConversion(¶ms
);
190 FILE *f
= fopen(custom_prog
, "r");
192 mp_msg(MSGT_VO
, MSGL_WARN
,
193 "[gl] Could not read customprog %s\n", custom_prog
);
195 char *prog
= calloc(1, MAX_CUSTOM_PROG_SIZE
+ 1);
196 fread(prog
, 1, MAX_CUSTOM_PROG_SIZE
, f
);
198 loadGPUProgram(GL_FRAGMENT_PROGRAM
, prog
);
201 ProgramEnvParameter4f(GL_FRAGMENT_PROGRAM
, 0,
202 1.0 / texture_width
, 1.0 / texture_height
,
203 texture_width
, texture_height
);
206 FILE *f
= fopen(custom_tex
, "r");
208 mp_msg(MSGT_VO
, MSGL_WARN
,
209 "[gl] Could not read customtex %s\n", custom_tex
);
211 int width
, height
, maxval
;
212 ActiveTexture(GL_TEXTURE3
);
213 if (glCreatePPMTex(custom_trect
?GL_TEXTURE_RECTANGLE
:GL_TEXTURE_2D
, 0,
214 custom_tlin
?GL_LINEAR
:GL_NEAREST
,
215 f
, &width
, &height
, &maxval
))
216 ProgramEnvParameter4f(GL_FRAGMENT_PROGRAM
, 1,
217 1.0 / width
, 1.0 / height
, width
, height
);
219 mp_msg(MSGT_VO
, MSGL_WARN
,
220 "[gl] Error parsing customtex %s\n", custom_tex
);
222 ActiveTexture(GL_TEXTURE0
);
228 * \brief remove all OSD textures and display-lists, thus clearing it.
230 static void clearOSD(void) {
234 glDeleteTextures(osdtexCnt
, osdtex
);
236 glDeleteTextures(osdtexCnt
, osdatex
);
237 for (i
= 0; i
< osdtexCnt
; i
++)
238 glDeleteLists(osdaDispList
[i
], 1);
240 for (i
= 0; i
< osdtexCnt
; i
++)
241 glDeleteLists(osdDispList
[i
], 1);
246 * \brief remove textures, display list and free memory used by EOSD
248 static void clearEOSD(void) {
250 glDeleteLists(eosdDispList
, 1);
253 glDeleteTextures(eosdtexCnt
, eosdtex
);
260 * \brief construct display list from ass image list
261 * \param img image list to create OSD from.
262 * A value of NULL has the same effect as clearEOSD()
264 static void genEOSD(mp_eosd_images_t
*imgs
) {
269 GLint scale_type
= (scaled_osd
) ? GL_LINEAR
: GL_NEAREST
;
270 ass_image_t
*img
= imgs
->imgs
;
273 if (imgs
->changed
== 0) // there are elements, but they are unchanged
275 if (img
&& imgs
->changed
== 1) // there are elements, but they just moved
281 if (!largeeosdtex
[0]) {
282 glGenTextures(2, largeeosdtex
);
283 BindTexture(gl_target
, largeeosdtex
[0]);
284 glCreateClearTex(gl_target
, GL_ALPHA
, scale_type
, 512, 512, 0);
285 BindTexture(gl_target
, largeeosdtex
[1]);
286 glCreateClearTex(gl_target
, GL_ALPHA
, scale_type
, 512, 512, 0);
288 for (i
= img
; i
; i
= i
->next
)
290 if (i
->w
<= 0 || i
->h
<= 0 || i
->stride
< i
->w
)
292 if (i
->w
< 16 && i
->h
< 16 && tinytexcur
< 1024)
294 else if (i
->w
< 32 && i
->h
< 32 && smalltexcur
< 256)
299 mp_msg(MSGT_VO
, MSGL_DBG2
, "EOSD counts (tiny, small, all): %i, %i, %i\n",
300 tinytexcur
, smalltexcur
, eosdtexCnt
);
302 eosdtex
= calloc(eosdtexCnt
, sizeof(GLuint
));
303 glGenTextures(eosdtexCnt
, eosdtex
);
305 tinytexcur
= smalltexcur
= 0;
306 for (i
= img
, curtex
= eosdtex
; i
; i
= i
->next
) {
308 if (i
->w
<= 0 || i
->h
<= 0 || i
->stride
< i
->w
) {
309 mp_msg(MSGT_VO
, MSGL_V
, "Invalid dimensions OSD for part!\n");
312 if (i
->w
< 16 && i
->h
< 16 && tinytexcur
< 1024) {
313 x
= (tinytexcur
& 31) << 4;
314 y
= (tinytexcur
>> 5) << 4;
315 BindTexture(gl_target
, largeeosdtex
[0]);
317 } else if (i
->w
< 32 && i
->h
< 32 && smalltexcur
< 256) {
318 x
= (smalltexcur
& 15) << 5;
319 y
= (smalltexcur
>> 4) << 5;
320 BindTexture(gl_target
, largeeosdtex
[1]);
323 texSize(i
->w
, i
->h
, &sx
, &sy
);
324 BindTexture(gl_target
, *curtex
++);
325 glCreateClearTex(gl_target
, GL_ALPHA
, scale_type
, sx
, sy
, 0);
327 glUploadTex(gl_target
, GL_ALPHA
, GL_UNSIGNED_BYTE
, i
->bitmap
, i
->stride
,
328 x
, y
, i
->w
, i
->h
, 0);
330 eosdDispList
= glGenLists(1);
332 glNewList(eosdDispList
, GL_COMPILE
);
333 tinytexcur
= smalltexcur
= 0;
334 for (i
= img
, curtex
= eosdtex
; i
; i
= i
->next
) {
336 if (i
->w
<= 0 || i
->h
<= 0 || i
->stride
< i
->w
)
338 glColor4ub(i
->color
>> 24, (i
->color
>> 16) & 0xff, (i
->color
>> 8) & 0xff, 255 - (i
->color
& 0xff));
339 if (i
->w
< 16 && i
->h
< 16 && tinytexcur
< 1024) {
340 x
= (tinytexcur
& 31) << 4;
341 y
= (tinytexcur
>> 5) << 4;
343 BindTexture(gl_target
, largeeosdtex
[0]);
345 } else if (i
->w
< 32 && i
->h
< 32 && smalltexcur
< 256) {
346 x
= (smalltexcur
& 15) << 5;
347 y
= (smalltexcur
>> 4) << 5;
349 BindTexture(gl_target
, largeeosdtex
[1]);
352 texSize(i
->w
, i
->h
, &sx
, &sy
);
353 BindTexture(gl_target
, *curtex
++);
355 glDrawTex(i
->dst_x
, i
->dst_y
, i
->w
, i
->h
, x
, y
, i
->w
, i
->h
, sx
, sy
, use_rectangle
== 1, 0, 0);
358 BindTexture(gl_target
, 0);
362 * \brief uninitialize OpenGL context, freeing textures, buffers etc.
364 static void uninitGl(void) {
366 if (DeletePrograms
&& fragprog
)
367 DeletePrograms(1, &fragprog
);
369 while (default_texs
[i
] != 0)
372 glDeleteTextures(i
, default_texs
);
377 glDeleteTextures(2, largeeosdtex
);
379 if (DeleteBuffers
&& gl_buffer
)
380 DeleteBuffers(1, &gl_buffer
);
381 gl_buffer
= 0; gl_buffersize
= 0;
387 * \brief Initialize a (new or reused) OpenGL context.
388 * set global gl-related variables to their default values
390 static int initGl(uint32_t d_width
, uint32_t d_height
) {
391 texSize(image_width
, image_height
, &texture_width
, &texture_height
);
394 glDisable(GL_DEPTH_TEST
);
395 glDepthMask(GL_FALSE
);
396 glDisable(GL_CULL_FACE
);
398 glDrawBuffer(vo_doublebuffering
?GL_BACK
:GL_FRONT
);
399 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_MODULATE
);
401 mp_msg(MSGT_VO
, MSGL_V
, "[gl] Creating %dx%d texture...\n",
402 texture_width
, texture_height
);
404 if (image_format
== IMGFMT_YV12
) {
406 glGenTextures(21, default_texs
);
407 default_texs
[21] = 0;
408 for (i
= 0; i
< 7; i
++) {
409 ActiveTexture(GL_TEXTURE1
+ i
);
410 BindTexture(GL_TEXTURE_2D
, default_texs
[i
]);
411 BindTexture(GL_TEXTURE_RECTANGLE
, default_texs
[i
+ 7]);
412 BindTexture(GL_TEXTURE_3D
, default_texs
[i
+ 14]);
414 ActiveTexture(GL_TEXTURE1
);
415 glCreateClearTex(gl_target
, gl_texfmt
, GL_LINEAR
,
416 texture_width
/ 2, texture_height
/ 2, 128);
417 ActiveTexture(GL_TEXTURE2
);
418 glCreateClearTex(gl_target
, gl_texfmt
, GL_LINEAR
,
419 texture_width
/ 2, texture_height
/ 2, 128);
421 case YUV_CONVERSION_FRAGMENT_LOOKUP
:
422 case YUV_CONVERSION_FRAGMENT_POW
:
423 case YUV_CONVERSION_FRAGMENT
:
424 if (!GenPrograms
|| !BindProgram
) {
425 mp_msg(MSGT_VO
, MSGL_ERR
, "[gl] fragment program functions missing!\n");
428 GenPrograms(1, &fragprog
);
429 BindProgram(GL_FRAGMENT_PROGRAM
, fragprog
);
432 ActiveTexture(GL_TEXTURE0
);
433 BindTexture(gl_target
, 0);
436 glCreateClearTex(gl_target
, gl_texfmt
, GL_LINEAR
,
437 texture_width
, texture_height
, 0);
439 resize(d_width
, d_height
);
441 glClearColor( 0.0f
,0.0f
,0.0f
,0.0f
);
442 glClear( GL_COLOR_BUFFER_BIT
);
443 if (SwapInterval
&& swap_interval
>= 0)
444 SwapInterval(swap_interval
);
448 /* connect to server, create and map window,
449 * allocate colors and (shared) memory
452 config(uint32_t width
, uint32_t height
, uint32_t d_width
, uint32_t d_height
, uint32_t flags
, char *title
, uint32_t format
)
454 image_height
= height
;
456 image_format
= format
;
457 glFindFormat(format
, NULL
, &gl_texfmt
, &gl_format
, &gl_type
);
460 vo_flipped
= !!(flags
& VOFLAG_FLIPPING
);
464 // GUI creates and manages window for us
465 guiGetEvent(guiSetShVideo
, 0);
472 if (!vo_w32_config(d_width
, d_height
, flags
))
475 vo_mouse_autohide
= 1;
477 vo_window
= WinID
? (Window
)WinID
: mRootWin
;
478 vo_x11_selectinput_witherr(mDisplay
, vo_window
,
479 StructureNotifyMask
| KeyPressMask
| PointerMotionMask
|
480 ButtonPressMask
| ButtonReleaseMask
| ExposureMask
);
484 XVisualInfo
*vinfo
=glXChooseVisual( mDisplay
,mScreen
,wsGLXAttrib
);
487 mp_msg(MSGT_VO
, MSGL_ERR
, "[gl] no GLX support present\n");
491 vo_x11_create_vo_window(vinfo
, vo_dx
, vo_dy
, d_width
, d_height
, flags
,
492 XCreateColormap(mDisplay
, mRootWin
, vinfo
->visual
, AllocNone
),
500 setGlWindow(&gl_vinfo
, &gl_context
, vo_window
);
501 initGl(vo_dwidth
, vo_dheight
);
506 static void check_events(void)
508 int e
=vo_check_events();
509 if(e
&VO_EVENT_RESIZE
) resize(vo_dwidth
,vo_dheight
);
510 if(e
&VO_EVENT_EXPOSE
&& int_pause
) redraw();
514 * Creates the textures and the display list needed for displaying
516 * Callback function for vo_draw_text().
518 static void create_osd_texture(int x0
, int y0
, int w
, int h
,
519 unsigned char *src
, unsigned char *srca
,
522 // initialize to 8 to avoid special-casing on alignment
524 GLint scale_type
= (scaled_osd
) ? GL_LINEAR
: GL_NEAREST
;
526 if (w
<= 0 || h
<= 0 || stride
< w
) {
527 mp_msg(MSGT_VO
, MSGL_V
, "Invalid dimensions OSD for part!\n");
530 texSize(w
, h
, &sx
, &sy
);
532 if (osdtexCnt
>= MAX_OSD_PARTS
) {
533 mp_msg(MSGT_VO
, MSGL_ERR
, "Too many OSD parts, contact the developers!\n");
537 // create Textures for OSD part
538 glGenTextures(1, &osdtex
[osdtexCnt
]);
539 BindTexture(gl_target
, osdtex
[osdtexCnt
]);
540 glCreateClearTex(gl_target
, GL_LUMINANCE
, scale_type
, sx
, sy
, 0);
541 glUploadTex(gl_target
, GL_LUMINANCE
, GL_UNSIGNED_BYTE
, src
, stride
,
545 glGenTextures(1, &osdatex
[osdtexCnt
]);
546 BindTexture(gl_target
, osdatex
[osdtexCnt
]);
547 glCreateClearTex(gl_target
, GL_ALPHA
, scale_type
, sx
, sy
, 255);
550 char *tmp
= malloc(stride
* h
);
551 // convert alpha from weird MPlayer scale.
552 // in-place is not possible since it is reused for future OSDs
553 for (i
= h
* stride
- 1; i
>= 0; i
--)
555 glUploadTex(gl_target
, GL_ALPHA
, GL_UNSIGNED_BYTE
, tmp
, stride
,
561 BindTexture(gl_target
, 0);
563 // Create a list for rendering this OSD part
565 osdaDispList
[osdtexCnt
] = glGenLists(1);
566 glNewList(osdaDispList
[osdtexCnt
], GL_COMPILE
);
568 BindTexture(gl_target
, osdatex
[osdtexCnt
]);
569 glDrawTex(x0
, y0
, w
, h
, 0, 0, w
, h
, sx
, sy
, use_rectangle
== 1, 0, 0);
572 osdDispList
[osdtexCnt
] = glGenLists(1);
573 glNewList(osdDispList
[osdtexCnt
], GL_COMPILE
);
575 BindTexture(gl_target
, osdtex
[osdtexCnt
]);
576 glDrawTex(x0
, y0
, w
, h
, 0, 0, w
, h
, sx
, sy
, use_rectangle
== 1, 0, 0);
582 static void do_render_osd(void);
584 static void draw_osd(void)
586 if (!use_osd
) return;
587 if (vo_osd_changed(0)) {
590 osd_w
= (scaled_osd
) ? image_width
: vo_dwidth
;
591 osd_h
= (scaled_osd
) ? image_height
: vo_dheight
;
592 vo_draw_text(osd_w
, osd_h
, create_osd_texture
);
594 if (vo_doublebuffering
) do_render_osd();
597 static void do_render(void) {
598 // glEnable(GL_TEXTURE_2D);
599 // glBindTexture(GL_TEXTURE_2D, texture_id);
602 if (image_format
== IMGFMT_YV12
)
603 glEnableYUVConversion(gl_target
, yuvconvtype
);
604 glDrawTex(0, 0, image_width
, image_height
,
605 0, 0, image_width
, image_height
,
606 texture_width
, texture_height
,
607 use_rectangle
== 1, image_format
== IMGFMT_YV12
,
608 mpi_flipped
^ vo_flipped
);
609 if (image_format
== IMGFMT_YV12
)
610 glDisableYUVConversion(gl_target
, yuvconvtype
);
613 static void do_render_osd(void) {
614 if (osdtexCnt
> 0 || eosdDispList
) {
615 // set special rendering parameters
617 glMatrixMode(GL_PROJECTION
);
620 glOrtho(0, vo_dwidth
, vo_dheight
, 0, -1, 1);
624 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
625 glCallList(eosdDispList
);
628 glColor4ub((osd_color
>> 16) & 0xff, (osd_color
>> 8) & 0xff, osd_color
& 0xff, 0xff - (osd_color
>> 24));
631 glBlendFunc(GL_ZERO
, GL_ONE_MINUS_SRC_ALPHA
);
632 glCallLists(osdtexCnt
, GL_UNSIGNED_INT
, osdaDispList
);
634 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
635 glCallLists(osdtexCnt
, GL_UNSIGNED_INT
, osdDispList
);
637 // set rendering parameters back to defaults
638 glDisable (GL_BLEND
);
641 BindTexture(gl_target
, 0);
645 static void flip_page(void) {
646 if (vo_doublebuffering
) {
647 if (use_glFinish
) glFinish();
649 if (vo_fs
&& use_aspect
)
650 glClear(GL_COLOR_BUFFER_BIT
);
654 if (use_glFinish
) glFinish();
659 static void redraw(void) {
660 if (vo_doublebuffering
) { do_render(); do_render_osd(); }
664 //static inline uint32_t draw_slice_x11(uint8_t *src[], uint32_t slice_num)
665 static int draw_slice(uint8_t *src
[], int stride
[], int w
,int h
,int x
,int y
)
667 mpi_flipped
= (stride
[0] < 0);
668 glUploadTex(gl_target
, gl_format
, gl_type
, src
[0], stride
[0],
669 x
, y
, w
, h
, slice_height
);
670 if (image_format
== IMGFMT_YV12
) {
671 ActiveTexture(GL_TEXTURE1
);
672 glUploadTex(gl_target
, gl_format
, gl_type
, src
[1], stride
[1],
673 x
/ 2, y
/ 2, w
/ 2, h
/ 2, slice_height
);
674 ActiveTexture(GL_TEXTURE2
);
675 glUploadTex(gl_target
, gl_format
, gl_type
, src
[2], stride
[2],
676 x
/ 2, y
/ 2, w
/ 2, h
/ 2, slice_height
);
677 ActiveTexture(GL_TEXTURE0
);
682 static uint32_t get_image(mp_image_t
*mpi
) {
683 if (!GenBuffers
|| !BindBuffer
|| !BufferData
|| !MapBuffer
) {
685 mp_msg(MSGT_VO
, MSGL_ERR
, "[gl] extensions missing for dr\n"
686 "Expect a _major_ speed penalty\n");
690 if (mpi
->flags
& MP_IMGFLAG_READABLE
) return VO_FALSE
;
693 // for unexplainable reasons, with width < 512 chroma tends to be messed up
694 // (after ca. 2/3 the previous line repeats all over)
695 if (mpi
->width
< 512) return VO_FALSE
;
696 while (s
< mpi
->width
) s
*= 2;
700 GenBuffers(1, &gl_buffer
);
701 BindBuffer(GL_PIXEL_UNPACK_BUFFER
, gl_buffer
);
702 mpi
->stride
[0] = mpi
->width
* mpi
->bpp
/ 8;
703 if (mpi
->stride
[0] * mpi
->height
> gl_buffersize
) {
704 BufferData(GL_PIXEL_UNPACK_BUFFER
, mpi
->stride
[0] * mpi
->height
,
705 NULL
, GL_DYNAMIC_DRAW
);
706 gl_buffersize
= mpi
->stride
[0] * mpi
->height
;
709 gl_bufferptr
= MapBuffer(GL_PIXEL_UNPACK_BUFFER
, GL_WRITE_ONLY
);
710 mpi
->planes
[0] = gl_bufferptr
;
711 BindBuffer(GL_PIXEL_UNPACK_BUFFER
, 0);
712 if (mpi
->planes
[0] == NULL
) {
714 mp_msg(MSGT_VO
, MSGL_ERR
, "[gl] could not acquire buffer for dr\n"
715 "Expect a _major_ speed penalty\n");
719 if (mpi
->imgfmt
== IMGFMT_YV12
) {
721 mpi
->flags
|= MP_IMGFLAG_COMMON_STRIDE
| MP_IMGFLAG_COMMON_PLANE
;
722 mpi
->stride
[0] = mpi
->width
;
723 mpi
->planes
[1] = mpi
->planes
[0] + mpi
->stride
[0] * mpi
->height
;
724 mpi
->stride
[1] = mpi
->width
>> 1;
725 mpi
->planes
[2] = mpi
->planes
[1] + mpi
->stride
[1] * (mpi
->height
>> 1);
726 mpi
->stride
[2] = mpi
->width
>> 1;
728 mpi
->flags
|= MP_IMGFLAG_DIRECT
;
732 static uint32_t draw_image(mp_image_t
*mpi
) {
733 int slice
= slice_height
;
735 unsigned char *planes
[3];
736 mp_image_t mpi2
= *mpi
;
737 if (mpi
->flags
& MP_IMGFLAG_DRAW_CALLBACK
)
739 mpi2
.flags
= 0; mpi2
.type
= MP_IMGTYPE_TEMP
;
740 mpi2
.width
= mpi2
.w
; mpi2
.height
= mpi2
.h
;
741 if (force_pbo
&& !(mpi
->flags
& MP_IMGFLAG_DIRECT
) && !gl_bufferptr
&& get_image(&mpi2
) == VO_TRUE
) {
742 int bpp
= mpi
->imgfmt
== IMGFMT_YV12
? 8 : mpi
->bpp
;
743 memcpy_pic(mpi2
.planes
[0], mpi
->planes
[0], mpi
->w
* bpp
/ 8, mpi
->h
, mpi2
.stride
[0], mpi
->stride
[0]);
744 if (mpi
->imgfmt
== IMGFMT_YV12
) {
745 memcpy_pic(mpi2
.planes
[1], mpi
->planes
[1], mpi
->w
>> 1, mpi
->h
>> 1, mpi2
.stride
[1], mpi
->stride
[1]);
746 memcpy_pic(mpi2
.planes
[2], mpi
->planes
[2], mpi
->w
>> 1, mpi
->h
>> 1, mpi2
.stride
[2], mpi
->stride
[2]);
750 stride
[0] = mpi
->stride
[0]; stride
[1] = mpi
->stride
[1]; stride
[2] = mpi
->stride
[2];
751 planes
[0] = mpi
->planes
[0]; planes
[1] = mpi
->planes
[1]; planes
[2] = mpi
->planes
[2];
752 mpi_flipped
= (stride
[0] < 0);
753 if (mpi
->flags
& MP_IMGFLAG_DIRECT
) {
754 intptr_t base
= (intptr_t)planes
[0];
756 base
+= (mpi
->h
- 1) * stride
[0];
760 BindBuffer(GL_PIXEL_UNPACK_BUFFER
, gl_buffer
);
761 UnmapBuffer(GL_PIXEL_UNPACK_BUFFER
);
763 slice
= 0; // always "upload" full texture
765 glUploadTex(gl_target
, gl_format
, gl_type
, planes
[0], stride
[0],
766 mpi
->x
, mpi
->y
, mpi
->w
, mpi
->h
, slice
);
767 if (mpi
->imgfmt
== IMGFMT_YV12
) {
768 ActiveTexture(GL_TEXTURE1
);
769 glUploadTex(gl_target
, gl_format
, gl_type
, planes
[1], stride
[1],
770 mpi
->x
/ 2, mpi
->y
/ 2, mpi
->w
/ 2, mpi
->h
/ 2, slice
);
771 ActiveTexture(GL_TEXTURE2
);
772 glUploadTex(gl_target
, gl_format
, gl_type
, planes
[2], stride
[2],
773 mpi
->x
/ 2, mpi
->y
/ 2, mpi
->w
/ 2, mpi
->h
/ 2, slice
);
774 ActiveTexture(GL_TEXTURE0
);
776 if (mpi
->flags
& MP_IMGFLAG_DIRECT
)
777 BindBuffer(GL_PIXEL_UNPACK_BUFFER
, 0);
779 if (vo_doublebuffering
) do_render();
784 draw_frame(uint8_t *src
[])
790 query_format(uint32_t format
)
792 int caps
= VFCAP_CSP_SUPPORTED
| VFCAP_CSP_SUPPORTED_BY_HW
|
794 VFCAP_HWSCALE_UP
| VFCAP_HWSCALE_DOWN
| VFCAP_ACCEPT_STRIDE
;
796 caps
|= VFCAP_OSD
| VFCAP_EOSD
| (scaled_osd
? 0 : VFCAP_EOSD_UNSCALED
);
797 if ((format
== IMGFMT_RGB24
) || (format
== IMGFMT_RGBA
))
799 if (use_yuv
&& format
== IMGFMT_YV12
)
801 // HACK, otherwise we get only b&w with some filters (e.g. -vf eq)
802 // ideally MPlayer should be fixed instead not to use Y800 when it has the choice
803 if (!use_yuv
&& (format
== IMGFMT_Y8
|| format
== IMGFMT_Y800
))
806 glFindFormat(format
, NULL
, NULL
, NULL
, NULL
))
815 if ( !vo_config_count
) return;
817 releaseGlContext(&gl_vinfo
, &gl_context
);
818 if (custom_prog
) free(custom_prog
);
820 if (custom_tex
) free(custom_tex
);
825 static opt_t subopts
[] = {
826 {"manyfmts", OPT_ARG_BOOL
, &many_fmts
, NULL
},
827 {"osd", OPT_ARG_BOOL
, &use_osd
, NULL
},
828 {"scaled-osd", OPT_ARG_BOOL
, &scaled_osd
, NULL
},
829 {"aspect", OPT_ARG_BOOL
, &use_aspect
, NULL
},
830 {"slice-height", OPT_ARG_INT
, &slice_height
, (opt_test_f
)int_non_neg
},
831 {"rectangle", OPT_ARG_INT
, &use_rectangle
,(opt_test_f
)int_non_neg
},
832 {"yuv", OPT_ARG_INT
, &use_yuv
, (opt_test_f
)int_non_neg
},
833 {"lscale", OPT_ARG_INT
, &lscale
, (opt_test_f
)int_non_neg
},
834 {"cscale", OPT_ARG_INT
, &cscale
, (opt_test_f
)int_non_neg
},
835 {"filter-strength", OPT_ARG_FLOAT
, &filter_strength
, NULL
},
836 {"ati-hack", OPT_ARG_BOOL
, &ati_hack
, NULL
},
837 {"force-pbo", OPT_ARG_BOOL
, &force_pbo
, NULL
},
838 {"glfinish", OPT_ARG_BOOL
, &use_glFinish
, NULL
},
839 {"swapinterval", OPT_ARG_INT
, &swap_interval
,NULL
},
840 {"customprog", OPT_ARG_MSTRZ
,&custom_prog
, NULL
},
841 {"customtex", OPT_ARG_MSTRZ
,&custom_tex
, NULL
},
842 {"customtlin", OPT_ARG_BOOL
, &custom_tlin
, NULL
},
843 {"customtrect", OPT_ARG_BOOL
, &custom_trect
, NULL
},
844 {"osdcolor", OPT_ARG_INT
, &osd_color
, NULL
},
848 static int preinit(const char *arg
)
858 filter_strength
= 0.5;
869 osd_color
= 0xffffff;
870 if (subopt_parse(arg
, subopts
) != 0) {
871 mp_msg(MSGT_VO
, MSGL_FATAL
,
872 "\n-vo gl command line help:\n"
873 "Example: mplayer -vo gl:slice-height=4\n"
876 " Disable extended color formats for OpenGL 1.2 and later\n"
877 " slice-height=<0-...>\n"
878 " Slice size for texture transfer, 0 for whole image\n"
880 " Do not use OpenGL OSD code\n"
882 " Do not do aspect scaling\n"
883 " rectangle=<0,1,2>\n"
884 " 0: use power-of-two textures\n"
885 " 1: use texture_rectangle\n"
886 " 2: use texture_non_power_of_two\n"
888 " Workaround ATI bug with PBOs\n"
890 " Force use of PBO even if this involves an extra memcpy\n"
892 " Call glFinish() before swapping buffers\n"
893 " swapinterval=<n>\n"
894 " Interval in displayed frames between to buffer swaps.\n"
895 " 1 is equivalent to enable VSYNC, 0 to disable VSYNC.\n"
896 " Requires GLX_SGI_swap_control support to work.\n"
898 " 0: use software YUV to RGB conversion.\n"
899 " 1: use register combiners (nVidia only, for older cards).\n"
900 " 2: use fragment program.\n"
901 " 3: use fragment program with gamma correction.\n"
902 " 4: use fragment program with gamma correction via lookup.\n"
903 " 5: use ATI-specific method (for older cards).\n"
904 " 6: use lookup via 3D texture.\n"
906 " 0: use standard bilinear scaling for luma.\n"
907 " 1: use improved bicubic scaling for luma.\n"
908 " 2: use cubic in X, linear in Y direction scaling for luma.\n"
909 " 3: as 1 but without using a lookup texture.\n"
910 " 4: experimental unsharp masking.\n"
912 " as lscale but for chroma (2x slower with little visible effect).\n"
913 " customprog=<filename>\n"
914 " use a custom YUV conversion program\n"
915 " customtex=<filename>\n"
916 " use a custom YUV conversion lookup texture\n"
918 " use GL_NEAREST scaling for customtex texture\n"
920 " use texture_rectangle for customtex texture\n"
921 " osdcolor=<0xAARRGGBB>\n"
922 " use the given color for the OSD\n"
926 if (use_rectangle
== 1)
927 gl_target
= GL_TEXTURE_RECTANGLE
;
929 gl_target
= GL_TEXTURE_2D
;
930 yuvconvtype
= use_yuv
| lscale
<< YUV_LUM_SCALER_SHIFT
| cscale
<< YUV_CHROM_SCALER_SHIFT
;
932 mp_msg (MSGT_VO
, MSGL_INFO
, "[gl] using extended formats. "
933 "Use -vo gl:nomanyfmts if playback fails.\n");
934 mp_msg (MSGT_VO
, MSGL_V
, "[gl] Using %d as slice height "
935 "(0 means image height).\n", slice_height
);
936 if( !vo_init() ) return -1; // Can't open X11
941 #define MASK_ALL_YUV (~(1 << YUV_CONVERSION_NONE))
942 #define MASK_NOT_COMBINERS (~((1 << YUV_CONVERSION_NONE) | (1 << YUV_CONVERSION_COMBINERS) | (1 << YUV_CONVERSION_COMBINERS_ATI)))
943 #define MASK_GAMMA_SUPPORT (MASK_NOT_COMBINERS & ~(1 << YUV_CONVERSION_FRAGMENT))
945 static const struct {
950 {"brightness", &eq_bri
, MASK_NOT_COMBINERS
},
951 {"contrast", &eq_cont
, MASK_NOT_COMBINERS
},
952 {"saturation", &eq_sat
, MASK_ALL_YUV
},
953 {"hue", &eq_hue
, MASK_ALL_YUV
},
954 {"gamma", &eq_rgamma
, MASK_GAMMA_SUPPORT
},
955 {"red_gamma", &eq_rgamma
, MASK_GAMMA_SUPPORT
},
956 {"green_gamma", &eq_ggamma
, MASK_GAMMA_SUPPORT
},
957 {"blue_gamma", &eq_bgamma
, MASK_GAMMA_SUPPORT
},
961 static int control(uint32_t request
, void *data
)
966 int_pause
= (request
== VOCTRL_PAUSE
);
968 case VOCTRL_QUERY_FORMAT
:
969 return query_format(*((uint32_t*)data
));
970 case VOCTRL_GET_IMAGE
:
971 return get_image(data
);
972 case VOCTRL_DRAW_IMAGE
:
973 return draw_image(data
);
974 case VOCTRL_DRAW_EOSD
:
979 case VOCTRL_GET_EOSD_RES
:
981 mp_eosd_res_t
*r
= data
;
982 r
->mt
= r
->mb
= r
->ml
= r
->mr
= 0;
983 if (scaled_osd
) {r
->w
= image_width
; r
->h
= image_height
;}
985 r
->w
= vo_screenwidth
; r
->h
= vo_screenheight
;
986 r
->ml
= r
->mr
= ass_border_x
;
987 r
->mt
= r
->mb
= ass_border_y
;
989 r
->w
= vo_dwidth
; r
->h
= vo_dheight
;
993 case VOCTRL_GUISUPPORT
:
998 case VOCTRL_FULLSCREEN
:
1000 resize(vo_dwidth
, vo_dheight
);
1003 vo_gl_border(global_vo
);
1004 resize(vo_dwidth
, vo_dheight
);
1006 case VOCTRL_GET_PANSCAN
:
1007 if (!use_aspect
) return VO_NOTIMPL
;
1009 case VOCTRL_SET_PANSCAN
:
1010 if (!use_aspect
) return VO_NOTIMPL
;
1011 resize (vo_dwidth
, vo_dheight
);
1013 case VOCTRL_GET_EQUALIZER
:
1014 if (image_format
== IMGFMT_YV12
) {
1015 struct voctrl_get_equalizer_args
*args
= data
;
1017 for (i
= 0; eq_map
[i
].name
; i
++)
1018 if (strcmp(args
->name
, eq_map
[i
].name
) == 0) break;
1019 if (!(eq_map
[i
].supportmask
& (1 << use_yuv
)))
1021 *args
->valueptr
= *eq_map
[i
].value
;
1025 case VOCTRL_SET_EQUALIZER
:
1026 if (image_format
== IMGFMT_YV12
) {
1027 struct voctrl_set_equalizer_args
*args
= data
;
1029 for (i
= 0; eq_map
[i
].name
; i
++)
1030 if (strcmp(args
->name
, eq_map
[i
].name
) == 0) break;
1031 if (!(eq_map
[i
].supportmask
& (1 << use_yuv
)))
1033 *eq_map
[i
].value
= args
->value
;
1038 case VOCTRL_UPDATE_SCREENINFO
:
1039 update_xinerama_info();