Debian Codegraph @

lib/video/renderer.cpp

raw
1 /********************************************************
2 *
3 * Video renderers
4 * Copyright 2000 Eugene Kuznetsov ([email protected])
5 *
6 ********************************************************/
7
8
9 #include "rendlock.h"
10 #include "subrend.h"
11
12 #include "avm_fourcc.h"
13 #include "mmx.h"
14 #include "avm_output.h"
15 #define DECLARE_REGISTRY_SHORTCUT
16 #include "configfile.h"
17 #undef DECLARE_REGISTRY_SHORTCUT
18
19 #include <unistd.h>
20 #include <fcntl.h>
21 #include <sys/ioctl.h>
22 #include <sys/time.h>
23 #include <string.h>
24 #include <stdarg.h>
25 #include <langinfo.h>
26
27 bool avm::IVideoRenderer::allow_sw_yuv = true;//false;
28
29 #ifndef X_DISPLAY_MISSING
30
31 #include <errno.h>
32
33 #include <sys/ipc.h>
34 #include <sys/shm.h>
35
36 #include <X11/Xlib.h>
37 #include <X11/Xlocale.h>
38 #include <X11/Xutil.h>
39 #include <X11/extensions/XInput.h> //XGetExtensionVersion
40 #include <X11/extensions/XShm.h>
41
42 //#ifdef HAVE_LIBXXF86DGA
43 //#include <X11/extensions/xf86dga.h>
44 //#endif
45
46 #ifdef HAVE_LIBXXF86VM
47 #include <X11/extensions/xf86vmode.h>
48 #endif
49
50 //#undef HAVE_LIBXFT
51 #ifdef HAVE_LIBXFT
52 #include <X11/Xft/Xft.h>
53 //#include <X11/extensions/Xrender.h>
54 #endif
55
56 #ifdef HAVE_LIBXV
57 #include <X11/extensions/Xv.h>
58 #include <X11/extensions/Xvlib.h>
59 #endif
60
61 #include "VideoDPMS.h"
62
63 #include <ctype.h>
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <math.h>
67
68 #define __MODULE__ "VideoRenderer"
69
70 AVM_BEGIN_NAMESPACE;
71
72 static const char __RENDERER__[] = "renderer";
73
74 int GetPhysicalDepth(void* _dpy)
75 {
76 Display* dpy = (Display*) _dpy;
77 if (!dpy)
78 return 0;
79
80 int n, pixmap_bits = 0;
81 XPixmapFormatValues* pf = XListPixmapFormats(dpy, &n);
82 int screen = DefaultScreen(dpy);
83 int planes = DefaultDepth(dpy, screen);
84
85 for (int i = 0; i < n; ++i)
86 if (pf[i].depth == planes)
87 {
88 pixmap_bits = pf[i].bits_per_pixel;
89 break;
90 }
91
92 XFree(pf);
93
94 if (pixmap_bits == 16 && DefaultVisual(dpy, screen)->red_mask == 0x7c00)
95 pixmap_bits = 15;
96
97 return pixmap_bits;
98 }
99
100
101 #ifdef HAVE_LIBSDL
102
103 #include <SDL.h>
104 #include <SDL_video.h>
105 #include <SDL_syswm.h>
106 #include <SDL_mouse.h>
107
108 #ifndef SDL_VERSIONNUM
109 #define SDL_VERSIONNUM(X, Y, Z) (X)*1000 + (Y)*100 + (Z)
110 #endif
111
112 #define _SDL_VER SDL_VERSIONNUM(SDL_MAJOR_VERSION, SDL_MINOR_VERSION, SDL_PATCHLEVEL)
113
114 #ifndef SDL_DISABLE
115 #define SDL_DISABLE 0
116 #endif
117
118 #ifndef SDL_ENABLE
119 #define SDL_ENABLE 1
120 #endif
121
122 #ifndef SDL_BUTTON_LEFT
123 #define SDL_BUTTON_LEFT 1
124 #define SDL_BUTTON_MIDDLE 2
125 #define SDL_BUTTON_RIGHT 3
126 #endif
127
128 #ifndef SDL_BUTTON_WHEELUP
129 #define SDL_BUTTON_WHEELUP 4
130 #define SDL_BUTTON_WHEELDOWN 5
131 #endif
132
133 /* XPM */
134 static const char* const mouse_arrow[] =
135 {
136 /* width height num_colors chars_per_pixel */
137 " 32 32 3 1",
138 /* colors */
139 "X c #000000",
140 ". c #ffffff",
141 " c None",
142 /* pixels */
143 "X ",
144 "XX ",
145 "X.X ",
146 "X..X ",
147 "X...X ",
148 "X....X ",
149 "X.....X ",
150 "X......X ",
151 "X.......X ",
152 "X........X ",
153 "X.....XXXXX ",
154 "X..X..X ",
155 "X.X X..X ",
156 "XX X..X ",
157 "X X..X ",
158 " X..X ",
159 " X..X ",
160 " X..X ",
161 " XX ",
162 " ",
163 " ",
164 " ",
165 " ",
166 " ",
167 " ",
168 " ",
169 " ",
170 " ",
171 " ",
172 " ",
173 " ",
174 " ",
175 "0,0"
176 };
177
178 static const char* const mouse_zoomlu[] =
179 {
180 /* width height num_colors chars_per_pixel */
181 " 32 32 3 1",
182 /* colors */
183 "X c #000000",
184 ". c #ffffff",
185 " c None",
186 /* pixels */
187 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
188 "X..............................X",
189 "X..............................X",
190 "X..XXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
191 "X..X ",
192 "X..X ",
193 "X..X ",
194 "X..X ",
195 "X..X ",
196 "X..X ",
197 "X..X ",
198 "X..X ",
199 "X..X ",
200 "X..X ",
201 "X..X ",
202 "X..X ",
203 "X..X ",
204 "X..X ",
205 "X..X ",
206 "X..X ",
207 "X..X ",
208 "X..X ",
209 "X..X ",
210 "X..X ",
211 "X..X ",
212 "X..X ",
213 "X..X ",
214 "X..X ",
215 "X..X ",
216 "X..X ",
217 "X..X ",
218 "XXXX ",
219 "0,0"
220 };
221
222 static const char* const mouse_zoomrb[] =
223 {
224 /* width height num_colors chars_per_pixel */
225 " 32 32 3 1",
226 /* colors */
227 "X c #000000",
228 ". c #ffffff",
229 " c None",
230 /* pixels */
231 " XXXX",
232 " X..X",
233 " X..X",
234 " X..X",
235 " X..X",
236 " X..X",
237 " X..X",
238 " X..X",
239 " X..X",
240 " X..X",
241 " X..X",
242 " X..X",
243 " X..X",
244 " X..X",
245 " X..X",
246 " X..X",
247 " X..X",
248 " X..X",
249 " X..X",
250 " X..X",
251 " X..X",
252 " X..X",
253 " X..X",
254 " X..X",
255 " X..X",
256 " X..X",
257 " X..X",
258 " X..X",
259 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX..X",
260 "X..............................X",
261 "X..............................X",
262 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
263 "31,31"
264 };
265
266 static const char* const hidden_arrow[] =
267 {
268 /* width height num_colors chars_per_pixel */
269 " 8 1 3 1",
270 /* colors */
271 "X c #000000",
272 ". c #ffffff",
273 " c None",
274 /* pixels */
275 " ",
276 "0,0"
277 };
278
279 static SDL_Cursor* init_system_cursor(const char* const image[])
280 {
281 int i, row, col;
282 int hot_x, hot_y;
283 Uint8* data, *mask;
284 int rows, cols, colors, per_pix;
285 int s;
286 SDL_Cursor* cret;
287 sscanf(image[0], "%d %d %d %d", &cols, &rows, &colors, &per_pix);
288
289 //printf("col %d row %d colo: %d %d\n", cols, rows, colors, per_pix);
290 s = ((cols + 7) / 8) * rows;
291 data = new Uint8[s];
292 mask = new Uint8[s];
293 memset(data, 0, s);
294 memset(mask, 0, s);
295 colors++; // skip definition
296
297 i = -1;
298 for ( row=0; row<rows; ++row ) {
299 for ( col=0; col<cols; ++col ) {
300 if ( col % 8 ) {
301 data[i] = (unsigned char)(data[i] << 1);
302 mask[i] = (unsigned char)(mask[i] << 1);
303 } else {
304 ++i;
305 data[i] = mask[i] = 0;
306 }
307 switch (image[colors+row][col]) {
308 case 'X':
309 data[i] |= 0x01;
310 mask[i] |= 0x01;
311 break;
312 case '.':
313 mask[i] |= 0x01;
314 break;
315 case ' ':
316 break;
317 }
318 }
319 }
320 sscanf(image[colors + row], "%d,%d", &hot_x, &hot_y);
321 cret = SDL_CreateCursor(data, mask, cols, rows, hot_x, hot_y);
322 delete[] data;
323 delete[] mask;
324 return cret;
325 }
326
327 VideoMode::VideoMode(int w, int h, float f)
328 : width(w), height(h), freq(f)
329 {
330 name.sprintf(freq ? "%d x %d %dHz" : "%d x %d",
331 width, height, (int)(freq + 0.5));
332 }
333
334 class MouseKeeper
335 {
336 int64_t m_lMouseTime;
337 int64_t m_lMouseLast; // keep time of last mouse event
338 Uint16 m_iMouseX, m_iMouseY;
339 int m_iMouseOff;
340 Uint16 m_iX, m_iY;
341 SDL_Cursor* m_pCursor;// store actuall mouse cursor
342 SDL_Cursor* m_pCursorHide;
343 SDL_Cursor* m_pCursorZoomLU;
344 SDL_Cursor* m_pCursorZoomRB;
345 public:
346 MouseKeeper() : m_lMouseTime(0), m_lMouseLast(0),
347 m_iMouseX(0), m_iMouseY(0), m_iMouseOff(0),
348 m_pCursor(0), m_pCursorHide(0),
349 m_pCursorZoomLU(0), m_pCursorZoomRB(0)
350 {
351 }
352 ~MouseKeeper()
353 {
354 }
355 void Open()
356 {
357 m_pCursorHide = init_system_cursor(hidden_arrow);
358 m_pCursorZoomLU = init_system_cursor(mouse_zoomlu);
359 m_pCursorZoomRB = init_system_cursor(mouse_zoomrb);
360 }
361 void Close()
362 {
363 if (m_pCursorHide)
364 SDL_FreeCursor(m_pCursorHide);
365 if (m_pCursorZoomLU)
366 SDL_FreeCursor(m_pCursorZoomLU);
367 if (m_pCursorZoomRB)
368 SDL_FreeCursor(m_pCursorZoomRB);
369 }
370 void On() {
371 if (m_pCursor)
372 SDL_SetCursor(m_pCursor);
373 }
374 void Off() {
375 m_lMouseTime = 0;
376 m_iMouseOff = 3; // resistance
377 SDL_Cursor* tmp = SDL_GetCursor();
378 if (tmp != m_pCursorHide)
379 {
380 m_pCursor = tmp;
381 SDL_SetCursor(m_pCursorHide);
382 }
383 }
384 void Process() {
385 if (m_lMouseTime)
386 {
387 if (avm_get_time_diff(avm_get_time_us(), m_lMouseTime) > 1.0)
388 Off();
389 else if (m_lMouseTime != m_lMouseLast && --m_iMouseOff == 0)
390 On();
391 m_lMouseLast = m_lMouseTime;
392 }
393 }
394 void Set(Uint16 x, Uint16 y)
395 {
396 m_iMouseX = x;
397 m_iMouseY = y;
398 m_lMouseTime = avm_get_time_us();
399 }
400 void Store()
401 {
402 // save original mouse position
403 // using mouse motion event
404 m_iX = m_iMouseX;
405 m_iY = m_iMouseY;
406 }
407 void Restore()
408 {
409 // recover original mouse position
410 // called with LOCK
411 SDL_WarpMouse(m_iX, m_iY); // this seems to be doing same thing
412 }
413 };
414
415 #endif // HAVE_LIBSDL
416
417 #ifdef HAVE_ICONV
418 #include <iconv.h>
419
420 XFontSet XLoadQueryFontSet(Display* disp, const char* fontset_name)
421 {
422 int missing_charset_count;
423 char** missing_charset_list;
424 char* def_string;
425 XFontSet fontset = XCreateFontSet(disp, fontset_name,
426 &missing_charset_list,
427 &missing_charset_count,
428 &def_string);
429 if (missing_charset_count) {
430 AVM_WRITE(__RENDERER__, "Missing charsets in FontSet(%s) creation.\n",
431 fontset_name);
432 XFreeStringList(missing_charset_list);
433 }
434 return fontset;
435 }
436
437 #define XLoadQueryFont XLoadQueryFontSet
438 #define XFreeFont XFreeFontSet
439 #define XDrawString(d,w,gc,x,y,s,l) XmbDrawString(d,w,font,gc,x,y,s,l)
440 #define XDrawImageString(d,w,gc,x,y,s,l) XmbDrawImageString(d,w,font,gc,x,y,s,l)
441 #define XTextWidth XmbTextEscapement
442 #define XftTextExtents8 XftTextExtentsUtf8
443 #define XftDrawString8 XftDrawStringUtf8
444 #endif /* HAVE_ICONV */
445
446 VideoRendererWithLock::~VideoRendererWithLock()
447 {
448 }
449
450 void* VideoRendererWithLock::eventThread(void* arg)
451 {
452 VideoRendererWithLock* vr = (VideoRendererWithLock*) arg;
453 while (!vr->m_bQuit) {
454 //static int c = 0; printf("EventRunning %d\n", c++);
455 vr->processEvent();
456 }
457 return 0;
458 }
459
460 #if _SDL_VER > 1104
461 class SDLGRtConfig : public IRtConfig
462 {
463 VideoRendererWithLock* vr;
464 avm::vector<int> val;
465 public:
466 SDLGRtConfig(VideoRendererWithLock* _vr)
467 : vr(_vr)
468 {
469 m_Attrs.push_back(AttributeInfo("SDL_GAMMA_RED", "Gamma Red",
470 AttributeInfo::Integer,
471 0, 30, 10));
472 m_Attrs.push_back(AttributeInfo("SDL_GAMMA_GREEN", "Gamma Green",
473 AttributeInfo::Integer,
474 0, 30, 10));
475 m_Attrs.push_back(AttributeInfo("SDL_GAMMA_BLUE", "Gamma Blue",
476 AttributeInfo::Integer,
477 0, 30, 10));
478 val.resize(m_Attrs.size());
479 for (unsigned i = 0; i < m_Attrs.size(); ++i)
480 val[i] = RegReadInt("aviplay", m_Attrs[i].GetName(), 10);
481
482 setGamma();
483 }
484 virtual ~SDLGRtConfig()
485 {
486 // restore to defaults or at least those from the begining
487 for (unsigned i = 0; i < m_Attrs.size(); ++i)
488 RegWriteInt("aviplay", m_Attrs[i].GetName(), val[i]);
489
490 val[0] = val[1] = val[2] = 10;
491 setGamma();
492 }
493 // IRtConfig interface
494 virtual int GetValue(const char* attr, int* result) const
495 {
496 AVM_WRITE(__RENDERER__, 1, "sdlg: get %s\n", attr);
497 for (unsigned i = 0; i < m_Attrs.size(); ++i)
498 if (attr == m_Attrs[i].GetName())
499 {
500 *result = val[i];
501 return 0;
502 }
503
504 return -1;
505 }
506 virtual int SetValue(const char* attr, int value)
507 {
508 AVM_WRITE(__RENDERER__, 1, "sdlg: set %s value: %d\n", attr, value);
509 for (unsigned i = 0; i < m_Attrs.size(); ++i)
510 if (attr == m_Attrs[i].GetName())
511 {
512 val[i] = value;
513 setGamma();
514 return 0;
515 }
516
517 return -1;
518 }
519 private:
520 void setGamma()
521 {
522 vr->Lock();
523 SDL_SetGamma((float)val[0]/10.f, (float)val[1]/10.f, (float)val[2]/10.f);
524 vr->Unlock();
525 }
526 };
527 #else
528 class SDLGRtConfig : public IRtConfig
529 {
530 public:
531 virtual int GetValue(const char* attr, int* result) const { return 0; }
532 virtual int SetValue(const char* attr, int value) { return 0; }
533 };
534
535 #endif // _SDL_VER > 1104
536
537 #undef __MODULE__
538 #define __MODULE__ "Fullscreen renderer"
539
540 #ifdef HAVE_LIBSDL
541
542 // according to SDL we should not use this
543 // new XFree should have this fixed
544 //static EnvKeeper keeper;
545 static const char sdl_var[] = "SDL_VIDEO_X11_NODIRECTCOLOR";
546 class EnvKeeper
547 {
548 avm::string m_str;
549 public:
550 EnvKeeper() : m_str(getenv(sdl_var))
551 {
552 avm_setenv(sdl_var, "1", true);
553 }
554 ~EnvKeeper()
555 {
556 if (!m_str.size())
557 avm_unsetenv(sdl_var);
558 }
559 };
560
561 class FullscreenRenderer: public VideoRendererWithLock
562 {
563 protected:
564 Display* dpy;
565 GC xgc;
566 ISubRenderer* m_pSubRenderer;
567 SDL_Event keyrepev;
568 SDL_Rect cliprect;
569 SDL_SysWMinfo info;
570 SDL_Surface* screen;
571 SDLGRtConfig* m_pSdlgRtConfig;
572 MouseKeeper m_Mouse;
573 int max_w, max_h;
574 SDL_Rect m_Zoom;
575 int fs;
576 int bit_depth;
577 int bpp;
578 const CImage* image;
579 char* convbuf;
580 PthreadTask* eventchecker;
581 VideoDPMS* dpmsSafe;
582 size_t m_uiImages;
583 avm::vector<CImage*> sflist;
584 const subtitle_line_t* m_pSublineRef;
585 subtitle_line_t* m_pSubline;
586 char* charset;
587 #ifdef HAVE_ICONV
588 XFontSet font;
589 char* i18nfileencoding;
590 char* i18ndisplayencoding;
591 bool i18ncodeconvert;
592 #else
593 XFontStruct* font;
594 #endif
595 #ifdef HAVE_LIBXFT
596 XftDraw* xftdraw;
597 XftFont* xftfont;
598 XftColor* xftcolor;
599 #endif
600 Uint32 sdl_systems;
601
602 bool dga;
603 bool m_bDirty;
604 bool m_bResizeEnabled;
605 bool m_bSubRefresh;
606
607 public:
608 static const unsigned int SUBTITLE_SPACE = 3;
609
610 FullscreenRenderer(PlayerWidget* pw, Display* _dpy,
611 int width, int height, bool subt = false)
612 :VideoRendererWithLock(width, height, subt),
613 dpy(_dpy), xgc(0), m_pSubRenderer(0), screen(0),
614 m_pSdlgRtConfig(0),
615 max_w(0), max_h(0), fs(0), image(0),
616 convbuf(0), eventchecker(0), dpmsSafe(0),
617 m_uiImages(0), m_pSublineRef(0), m_pSubline(0),
618 charset(0), font(0),
619 #ifdef HAVE_LIBXFT
620 xftdraw(0), xftfont(0), xftcolor(0),
621 #endif
622 sdl_systems(0),
623 dga(false), m_bDirty(false), m_bResizeEnabled(true),
624 m_bSubRefresh(false)
625 {
626 m_pPw = pw;
627 m_Zoom.w = 0;
628 }
629 virtual ~FullscreenRenderer()
630 {
631 if (fs)
632 {
633 m_bQuit = true;
634 emutex.Lock();
635 econd.Broadcast();
636 emutex.Unlock();
637 delete m_pSdlgRtConfig;
638 Lock();
639 delete dpmsSafe;
640 #if _SDL_VER > 1102
641 if (screen && (screen->flags & SDL_FULLSCREEN))
642 SDL_WM_ToggleFullScreen(screen);
643 #endif
644 delete eventchecker;
645 delete[] convbuf;
646 m_Mouse.Close();
647
648 if (image)
649 image->Release();
650
651 while (sflist.size() > 0)
652 {
653 //printf("SFLIST FREE %p\n", sflist.back()->GetUserData());
654 SDL_FreeSurface((SDL_Surface*) sflist.back()->GetUserData());
655 sflist.pop_back();
656 }
657
658 if (m_pSubline)
659 subtitle_line_free(m_pSubline);
660 if (dpy)
661 freeFont();
662 if (xgc)
663 XFreeGC(dpy, xgc);
664 if (charset)
665 free(charset);
666 //if (screen) SDL_FreeSurface(screen);
667 Unlock();
668 #if _SDL_VER > 1102
669 SDL_QuitSubSystem(sdl_systems);
670 #else
671 SDL_Quit();
672 #endif
673 }
674 }
675 virtual int init()
676 {
677 if (VideoRendererWithLock::init() < 0)
678 return -1;
679
680 //avm_setenv("SDL_WINDOWID", "0x200000e", 1);
681 if (setlocale(LC_CTYPE, "") == 0)
682 AVM_WRITE(__RENDERER__, "Warning: Locale not supported by C library\n");
683 else
684 {
685 if (!XSupportsLocale())
686 {
687 AVM_WRITE(__RENDERER__, "warning: Locale not supported by Xlib\n");
688 setlocale(LC_CTYPE, "C");
689 }
690 charset = strdup(nl_langinfo(CODESET));
691 }
692
693 #if _SDL_VER < 1103
694 SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE);
695 atexit(SDL_Quit);
696 #else
697 Uint32 subsystem_init = SDL_WasInit(SDL_INIT_EVERYTHING);
698 if (!(subsystem_init & SDL_INIT_VIDEO))
699 {
700 if (subsystem_init == 0)
701 {
702 SDL_Init(SDL_INIT_NOPARACHUTE);
703 atexit(SDL_Quit);
704 }
705
706 if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) {
707 AVM_WRITE(__RENDERER__, "Failed to initialize SDL_VIDEO");
708 return -1;
709 }
710 sdl_systems |= SDL_INIT_VIDEO;
711 }
712 #endif
713 //fs = SDL_RESIZABLE | SDL_SWSURFACE | SDL_HWACCEL | SDL_ANYFORMAT;
714 fs = SDL_SWSURFACE | SDL_HWACCEL | SDL_ANYFORMAT;
715 #if _SDL_VER > 1103
716 fs |= SDL_RESIZABLE;
717
718 //fs |= SDL_DOUBLEBUF;
719 //fs |= SDL_ASYNCBLIT;
720 // Async blit makes this a bit faster - however we prefer
721 // to know when the image is drawn
722 // - Async uses XFlush, Sync calls XSync
723 // using double buffering would be great
724 // but for some reason it doesn't work for YUV
725 char vname[100];
726 SDL_VideoDriverName(vname, sizeof(vname));
727 AVM_WRITE(__RENDERER__, "SDL video driver: %s\n", vname);
728 #endif
729 const SDL_VideoInfo* vi = SDL_GetVideoInfo();
730 AVM_WRITE(__RENDERER__, 1, "VideoInfo: %s%s%s%s%s - %s%s%s - %s\n",
731 (vi->hw_available) ? "hw_available" : "",
732 (vi->wm_available) ? " wm_available" : "",
733 (vi->blit_hw) ? " blit_hw" : "",
734 (vi->blit_hw_CC) ? " blit_hw_CC" : "",
735 (vi->blit_hw_A) ? " blit_hw_A" : "",
736 (vi->blit_sw) ? " blit_sw" : "",
737 (vi->blit_sw_CC) ? " blit_sw_CC" : "",
738 (vi->blit_sw_A) ? " blit_sw_A" : "",
739 (vi->blit_fill) ? " blit_fill" : "");
740 #if 0
741 AVM_WRITE(__RENDERER__, 1, "VideoInfo: vmem:%d colorkey:0x%x alpha:0x%x\n",
742 vi->video_mem,
743 vi->vfmt->colorkey,
744 vi->vfmt->alpha);
745 #endif
746 pic_w = -1;
747 int dim_w = m_w;
748 int dim_h = m_h;
749 const char* e = getenv("SDL_VIDEODRIVER");
750 if (e && strcmp(e, "dga") == 0) {
751 const char* px = getenv("AVIPLAY_DGA_WIDTH");
752 const char* py = getenv("AVIPLAY_DGA_HEIGHT");
753 float ratio_x = (px == NULL) ? 800.f : (float) atof(px);
754 float ratio_y = (py == NULL) ? 600.f : (float) atof(py);
755 float ratio;
756 ratio_x = ratio_x / (float) m_w;
757 ratio_y = ratio_y / (float) m_h;
758 ratio = (ratio_x < ratio_y) ? ratio_x : ratio_y;
759
760 //dim_w = (int) rint(ratio * m_w);
761 //dim_h = (int) rint(ratio * m_h);
762 dga = true;
763 }
764 /*int newbpp = SDL_VideoModeOK(dim_w, dim_h, GetPhysicalDepth(dpy), fs);
765 if (!newbpp) {
766 AVM_WRITE(__RENDERER__, "Failed to set up video mode");
767 return -1;
768 }
769 */
770 doResize(dim_w, dim_h); // creates screen = SDL_SetVideoMode(dim_w, dim_h, 0, fs);
771 if (!screen) {
772 AVM_WRITE(__RENDERER__, "Failed to set up video mode");
773 return -1;
774 }
775
776 if (dga) {
777 m_bResizeEnabled = false;
778 dpy = 0;
779 }
780
781 SDL_EventState(SDL_SYSWMEVENT, SDL_ENABLE);
782
783 bit_depth = screen->format->BitsPerPixel;
784 if (bit_depth == 16 && screen->format->Rmask == 0x7c00)
785 bit_depth = 15;
786 bpp = (bit_depth + 7) / 8;
787
788 if (dpy)
789 {
790 SDL_VERSION(&info.version);
791 if (!SDL_GetWMInfo(&info)) {
792 AVM_WRITE(__RENDERER__, "Failed SDL GetWMInfo");
793 return -1;
794 }
795 dpy = info.info.x11.display;
796 Window& win = info.info.x11.window;
797
798 m_Mouse.Open();
799 dpmsSafe = new VideoDPMS(dpy);
800 keyrepev.type = 0;
801
802 #ifdef HAVE_LIBXXF86VM
803 int lines;
804 XF86VidModeModeInfo** modesinfo = 0;
805 Bool r = XF86VidModeGetAllModeLines(dpy, DefaultScreen(dpy),
806 &lines, &modesinfo);
807 if (r && modesinfo)
808 {
809 for (int i = 0; modesinfo[i] && i < lines; ++i)
810 modes.push_back(VideoMode(modesinfo[i]->hdisplay,
811 modesinfo[i]->vdisplay,
812 // some systems have zeroes here... ???
813 // ATI Radeon VE 32MB @ 1280x1024 some user
814 (modesinfo[i]->htotal && modesinfo[i]->vtotal)
815 ? ((float)modesinfo[i]->dotclock * 1000.f
816 / (float)modesinfo[i]->htotal
817 / (float)modesinfo[i]->vtotal) : 0.f)
818 );
819 XFree(modesinfo);
820 }
821 else
822 AVM_WRITE(__RENDERER__, "Can't resolve video modes...\n");
823 #else
824 // Get available fullscreen/hardware modes
825 // no call to FREE for smodes!!
826 const SDL_Rect** smodes = const_cast<const SDL_Rect**>(SDL_ListModes(0, SDL_FULLSCREEN | SDL_HWSURFACE));
827 // Check if there are any modes available
828 if (smodes)
829 {
830 /* Check if our resolution is restricted */
831 if (smodes != (SDL_Rect **)-1)
832 for (int i = 0; smodes[i]; ++i)
833 modes.push_back(VideoMode(smodes[i]->w, smodes[i]->h));
834 else
835 AVM_WRITE(__RENDERER__, "All resolutions available.\n");
836 }
837 else
838 AVM_WRITE(__RENDERER__, "Can't resolve video modes...\n");
839 #endif
840 XGCValues gcv;
841 gcv.graphics_exposures = False;
842 xgc = XCreateGC(dpy, win, GCGraphicsExposures, &gcv);
843 #if 0
844 #ifdef HAVE_FREETYPE
845 m_pSubRenderer = new SubRenderer_Ft();
846 #endif // HAVE_FREETYPE
847 #ifdef HAVE_XFT
848 if (!m_pSubRenderer)
849 m_pSubRenderer = new SubRenderer_Xft(dpy, win);
850 #endif // HAVE_XFT
851 if (!m_pSubRenderer)
852 m_pSubRenderer = new SubRenderer_X11(dpy, win);
853 #endif // 0
854 SetCaption("AviPlayer: M/F max/fullscreen", 0);
855 }
856 AVM_WRITE(__RENDERER__, "created surface: %dx%d %d bits\n", pic_w, pic_h, bit_depth);
857
858 m_pSdlgRtConfig = new SDLGRtConfig(this);
859 if (m_pPw)
860 eventchecker = new PthreadTask(0, eventThread, this);
861 return 0;
862 }
863 virtual int Set(...)
864 {
865 va_list args;
866 Property prop;
867
868 va_start(args, this);
869 while ((prop = (Property) va_arg(args, int)) != 0)
870 {
871
872 }
873 va_end(args);
874 return 0;
875 }
876 virtual int Get(...) const
877 {
878 va_list args;
879 Property prop;
880
881 //printf("Sizeof %d\n", sizeof(Property));
882 va_start(args, this);
883
884 va_end(args);
885
886 return 0;
887 }
888 virtual IRtConfig* GetRtConfig() const { return m_pSdlgRtConfig; }
889 virtual size_t GetImages() const { return m_uiImages; }
890 virtual CImage* ImageAlloc(const BITMAPINFOHEADER& bh, uint_t idx, size_t align)
891 {
892 // for direct rendering bitdepth must match!
893 m_bResizeEnabled = false; // not allowed for x11 resizing
894 BitmapInfo bi(m_w, m_h, bit_depth);
895
896 //printf("--ImageAlloc\n"); BitmapInfo(bh).Print();
897 //printf("--ImageAllocated\n"); bi.Print();
898 if (!(bi == BitmapInfo(bh)))
899 return 0;
900
901 SDL_Surface* sf = 0;
902
903 Lock();
904 while (idx >= sflist.size())
905 {
906 if (idx == 0)
907 sf = screen;
908 else if (idx > 10000) // disabled
909 {
910 SDL_Surface* cs = SDL_GetVideoSurface();
911 sf = SDL_CreateRGBSurface(SDL_HWSURFACE,
912 cs->w, cs->h,
913 cs->format->BitsPerPixel,
914 cs->format->Rmask,
915 cs->format->Gmask,
916 cs->format->Bmask,
917 cs->format->Amask);
918 //printf("SDL SURFACE %d %d\n", sf->w, sf->h);
919 }
920
921 if (!sf)
922 break;
923
924 CImage* ci = new CImage(&bi, (uint8_t*) sf->pixels, false);
925 ci->SetUserData(sf);
926 ci->SetAllocator(this);
927 sflist.push_back(ci);
928 }
929 Unlock();
930 if (!sf)
931 return 0;
932
933 if (m_uiImages <= idx)
934 m_uiImages = idx + 1;
935 return sflist[idx];
936 }
937 virtual void ReleaseImages() { m_uiImages = 0; }
938 virtual int Draw(const CImage* data)
939 {
940 // we have to prevent resizing of screen->pixels
941 #if 0
942 char* tmp_array=new char[m_w*m_h];
943 char* tmp_array2=new char[m_w*m_h];
944 uint_t t3=localcount();
945 memcpy(tmp_array2, tmp_array, m_w*m_h);
946 uint_t t4=localcount();
947 SDL_LockSurface(screen);
948 uint_t t1=localcount();
949 memcpy(screen->pixels, tmp_array, m_w*m_h);
950 uint_t t2=localcount();
951 SDL_UnlockSurface(screen);
952 delete[] tmp_array;
953 delete[] tmp_array2;
954 AVM_WRITE("Pixels: %f Mb/s System: %f Mb/s\n",
955 (m_w*m_h/1048576.)/((t2-t1)/freq/1000.),
956 (m_w*m_h/1048576.)/((t4-t3)/freq/1000.)
957 );
958 #endif
959 // if we do not want to block our renderer while
960 // keyboard event processing tries to lock this system
961 Lock();
962
963 if (!data)
964 {
965 if (!image) {
966 Unlock();
967 return -1;
968 }
969
970 data = image;
971 }
972
973 data->AddRef();
974 if (image)
975 image->Release();
976 image = data;
977
978 m_lLastDrawStamp = avm_get_time_us();
979 char* dest = (char*)screen->pixels;
980 if (pic_w != m_w || pic_h != m_h || dga)
981 {
982 if (!convbuf)
983 convbuf = new char[m_w * m_h * bpp];
984 dest = convbuf;
985 }
986
987 if (SDL_MUSTLOCK(screen))
988 SDL_LockSurface(screen);
989
990 if (!data->GetUserData() || !data->Format() != bit_depth)
991 {
992 int w = m_w;
993 int h = m_h;
994 if (m_Zoom.w)
995 {
996 w = m_Zoom.w;
997 h = m_Zoom.h;
998 //printf("%d %d %d %d\n", pic_w, w, pic_h, h);
999 if (pic_w == w && pic_h == h)
1000 dest = (char*) screen->pixels;
1001 }
1002
1003 // make a local non-const copy of data
1004 // so we could set Zoom
1005 const uint8_t* p[CIMAGE_MAX_PLANES] = {
1006 data->Data(0),
1007 data->Data(1),
1008 data->Data(2)
1009 };
1010 int s[CIMAGE_MAX_PLANES] = {
1011 data->Stride(0),
1012 data->Stride(1),
1013 data->Stride(2)
1014 };
1015 CImage datah(data->GetFmt(), p, s, false);
1016
1017 if (m_Zoom.w)
1018 datah.SetWindow(m_Zoom.x, m_Zoom.y, m_Zoom.w, m_Zoom.h);
1019
1020 BitmapInfo bi(w, h, bit_depth);
1021 //data->GetFmt()->Print();
1022 //bi.Print();
1023 // convert image into either buffer or
1024 // directly into the screen surface in case we do not need
1025 // more scaling
1026 //printf("SCREEN %d\n", screen->pitch);
1027 CImage ci(&bi, (const uint8_t*) dest, false);
1028 ci.Convert(&datah);
1029 //printf("COVERT %d -> %d %d,%d 0x%08x\n", data->GetFmt()->Bpp(), bit_depth,
1030 //data->GetFmt()->biHeight, bi.biHeight, data->GetFmt()->biCompression);
1031 m_bResizeEnabled = true;
1032 }
1033
1034 if (dest == convbuf)
1035 {
1036 //printf("Zoom %d %d %d h: %d %d\n", pic_w, screen->pitch, screen->pitch / (bit_depth / 8), pic_h, m_h);
1037 // scaling is necessary
1038 zoom((uint16_t*)screen->pixels, (const uint16_t*)dest,
1039 screen->pitch / (bit_depth / 8),
1040 //pic_w,
1041 pic_h,
1042 m_w, m_h, bit_depth);
1043
1044 //for (int i = 0; i < screen->h; ++i) for (int j = 0; j < 50; ++j)
1045 // ((char*)screen->pixels)[i * screen->pitch + j] = i * j;
1046 }
1047
1048 if (SDL_MUSTLOCK(screen))
1049 SDL_UnlockSurface(screen);
1050
1051 m_bDirty = true;
1052 Unlock();
1053 return 0;
1054 }
1055 virtual int Sync()
1056 {
1057 if (m_bDirty && !dga)
1058 {
1059 Lock();
1060 m_bDirty = false;
1061
1062 //SDL_Flip(screen);
1063 // equals for non double buffered surface
1064 //printf("Update %d %d %d %d\n",cliprect.x, cliprect.x,cliprect.w, cliprect.h);
1065 SDL_UpdateRect(screen, cliprect.x, cliprect.x,
1066 cliprect.w, cliprect.h);
1067 Unlock();
1068 }
1069
1070 //printf("FSMOD %x %d\n", screen->flags, screen->flags & SDL_FULLSCREEN);
1071 emutex.Lock();
1072 econd.Broadcast();
1073 emutex.Unlock();
1074 return 0;
1075 }
1076 virtual int ToggleFullscreen(bool maximize = false)
1077 {
1078 if (dga)
1079 return -1;
1080 Lock();
1081
1082 if (!(screen->flags & SDL_FULLSCREEN))
1083 {
1084 m_Mouse.Store();
1085 if (maximize && m_bResizeEnabled)
1086 {
1087 int w = max_w = pic_w; // set some defaults
1088 int h = pic_h + m_sub;
1089 max_h = pic_h;
1090 GetModeLine(w, h);
1091 //printf("GETMODELINE %dx%d %dx%d\n", w, h, pic_w, pic_h);
1092 float ratio_x = (float)w / (float)pic_w;
1093 float ratio_y = (float)(h - m_sub) / (float)pic_h;
1094 float ratio = (ratio_x < ratio_y) ? ratio_x : ratio_y;
1095 w = (int) rint(ratio * (float)pic_w);
1096 h = (int) rint(ratio * (float)pic_h);
1097 if (pic_w != w || pic_h != h)
1098 {
1099 // FIXME - needs to flush XQueue here to be able to move
1100 // this window before Toggle occures
1101 if (doResize(w, h) < 0)
1102 max_w = max_h = 0;
1103 }
1104 }
1105 }
1106 #if _SDL_VER > 1102
1107 SDL_WM_ToggleFullScreen(screen);
1108 fs = screen->flags & SDL_FULLSCREEN;
1109 #else
1110 screen->flags ^= SDL_FULLSCREEN;
1111 fs = screen->flags & SDL_FULLSCREEN;
1112 if (fs && max_w > 0)
1113 XMoveWindow(dpy, info.info.x11.window, 0, 0);
1114 #endif
1115 if (!fs)
1116 {
1117 m_Mouse.Restore();
1118 if (max_w && max_h)
1119 doResize(max_w, max_h);
1120 max_w = max_h = 0;
1121 }
1122 m_Mouse.Off();
1123 Unlock();
1124 Refresh();
1125 return fs;
1126 }
1127 virtual int DrawSubtitles(const subtitle_line_t* sl)
1128 {
1129 if (!dpy || !xgc)
1130 return -1;
1131
1132 Lock();
1133
1134 if ((!m_bSubRefresh && subtitle_line_equals(sl, m_pSubline))
1135 || (!sl && !m_pSubline) || !m_iFontHeight)
1136 {
1137 Unlock();
1138 return 0;
1139 }
1140 // prepare line - calc width of each line
1141 // append each overlap to next line
1142
1143 m_bSubRefresh = false;
1144 subtitle_line_t* nsl = (sl) ? subtitle_line_copy(sl) : NULL;
1145 subtitle_line_free(m_pSubline);
1146 m_pSubline = nsl;
1147
1148 //if (m_pSubRenderer) m_pSubRenderer->Draw(m_pSubline);
1149
1150 Window& win = info.info.x11.window;
1151 GC lxgc = xgc;
1152
1153 XSetForeground(dpy, lxgc, 0x0);
1154 XFillRectangle(dpy, win, lxgc, 0, pic_h, pic_w, m_sub);
1155 XSetForeground(dpy, lxgc, 0xFFFFFFFF);
1156
1157 if (!m_pSubline) {
1158 Unlock();
1159 return 0;
1160 }
1161
1162 char sub[1000];
1163 *sub = 0;
1164 bool copy = true;
1165 for (unsigned i = 0; i < SUBTITLE_SPACE; ++i)
1166 {
1167 if (copy && i < m_pSubline->lines)
1168 {
1169 strncat(sub, m_pSubline->line[i], sizeof(sub) - strlen(sub) - 2);
1170
1171 if ((m_sub / (unsigned)m_iFontHeight) < m_pSubline->lines)
1172 {
1173 for (unsigned j = 1; j < m_pSubline->lines; ++j)
1174 {
1175 // compress all subtitles lines into just one line
1176 strcat(sub, " ");
1177 strncat(sub, m_pSubline->line[j], sizeof(sub) - strlen(sub) - 2);
1178 }
1179 copy = false;
1180 }
1181 }
1182
1183 // XDrawString16(dpy, win, lxgc, dim_w/2-6*strlen(sub)/2,
1184 // dim_h-65+20*i,
1185 // (XChar2b*)(short*)QString::fromLocal8Bit(sub).unicode(),
1186 // strlen(sub));
1187 size_t slen = strlen(sub);
1188 if (!slen)
1189 break;
1190 int draw_h = pic_h + (i + 1) * m_iFontHeight;
1191 #ifdef HAVE_LIBXFT
1192 if (slen && xftfont)
1193 {
1194 size_t clen = slen;
1195 size_t nlen = slen;
1196 for (;;)
1197 {
1198 XGlyphInfo extents;
1199 XftTextExtents8(dpy, xftfont, (XftChar8*)sub, (int)clen, &extents);
1200 int fm_width_sub = extents.xOff;
1201 if (fm_width_sub > pic_w)
1202 {
1203 while (clen > 0 && !isspace(sub[clen - 1]))
1204 clen--;
1205 if (clen > 0 && clen < nlen)
1206 {
1207 nlen = clen;
1208 while (clen > 0 && isspace(sub[clen - 1]))
1209 clen--;
1210 if (clen > 0)
1211 continue; // try next for loop
1212 }
1213 clen = nlen;
1214 }
1215 XftDrawString8(xftdraw, xftcolor, xftfont,
1216 (pic_w - fm_width_sub) / 2,
1217 draw_h, (XftChar8*)sub, (int)clen);
1218 //printf("n:%d s:%d\n", nlen, slen);
1219 if (nlen != slen)
1220 {
1221 slen -= nlen;
1222 memcpy(sub, sub + nlen, slen);
1223 sub[slen++] = ' ';
1224 sub[slen] = 0;
1225 }
1226 else
1227 *sub = 0;
1228 break;
1229 }
1230 } else
1231 #endif /* HAVE_LIBXFT */
1232 if (slen && font)
1233 {
1234 #ifdef HAVE_ICONV
1235 size_t in_size = slen;
1236 size_t out_size = slen * 6;
1237 char* out_buffer = (char*)malloc(out_size);
1238 char* out_p = out_buffer;
1239 iconv_t icvsts = iconv_open(charset, "UTF-8");
1240 if (icvsts != (iconv_t)(-1)) {
1241 char* in_p = sub;
1242 while (in_size > 0 && out_size > 10)
1243 if ((size_t)(-1) == iconv(icvsts, (ICONV_CONST_CAST char**) &in_p, &in_size, &out_p, &out_size)
1244 && --in_size > 0)
1245 in_p++; // skip and try next
1246
1247 iconv_close(icvsts);
1248 }
1249 int out_count = (int)(out_p - out_buffer);
1250 //printf("OUTLEN %d - %d %d (%s)\n", out_size, out_count, slen, charset);
1251 //for (unsigned i =0; i < out_size / sizeof(wchar_t); ++i)
1252 // printf("char %d %d 0x%x\n", i, out_buffer[i], out_buffer[i]);
1253 int fm_width_sub = XTextWidth(font, out_buffer, out_count);
1254 XDrawString(dpy, win, lxgc, (pic_w - fm_width_sub) / 2,
1255 draw_h, out_buffer, out_count);
1256 free(out_buffer);
1257 #else /* HAVE_ICONV */
1258 int fm_width_sub = XTextWidth(font, sub, slen);
1259 //printf("Width %d, height %d %s\n", fm_width_sub, m_iFontHeight, sub);
1260 XDrawString(dpy, win, lxgc, (pic_w - fm_width_sub) / 2,
1261 draw_h, sub, slen);
1262 #endif /* HAVE_ICONV */
1263 }
1264 XFlush(dpy);
1265 }
1266 Unlock();
1267 return 0;
1268 }
1269 virtual int Resize(int& new_w, int& new_h)
1270 {
1271 if (dga)
1272 return -1;
1273
1274 Lock();
1275 int r = doResize(new_w, new_h);
1276 //printf("DORESIZE %d %d %d\n", new_w, new_h, r);
1277 Unlock();
1278 if (r == 0)
1279 Refresh();
1280 return r;
1281 }
1282 virtual int Zoom(int x, int y, int width, int height)
1283 {
1284 if (!m_bResizeEnabled)
1285 return -1;
1286 Lock();
1287 width = (width + 7) & ~7;
1288 height = (height + 1) & ~1;
1289 m_Zoom.x = (Sint16)(x & ~7);
1290 m_Zoom.y = (Sint16)(y & ~1);
1291 m_Zoom.w = (Uint16)width;
1292 m_Zoom.h = (Uint16)height;
1293 if (m_Zoom.w > 0 && m_Zoom.h > 0 && doResize(width, height) == 0)
1294 for (unsigned i = 0; i < sflist.size(); ++i)
1295 sflist[i]->SetWindow(m_Zoom.x, m_Zoom.y, m_Zoom.w, m_Zoom.h);
1296 else
1297 m_Zoom.w = m_Zoom.h = 0;
1298
1299 Unlock();
1300
1301 return 0;
1302 }
1303 virtual int Refresh()
1304 {
1305 if (avm_get_time_diff(avm_get_time_us(), m_lLastDrawStamp) > 0.01)
1306 {
1307 // wait until we could really refresh image
1308 Lock();
1309 subtitle_line_t* sl = m_pSubline;
1310 m_pSubline = 0;
1311 Unlock();
1312 // because of scaling we need to call Draw
1313 if (Draw(0) == 0)
1314 {
1315 if (sl)
1316 DrawSubtitles(sl);
1317 Sync();
1318 return 0;
1319 }
1320 }
1321 else
1322 m_bSubRefresh = true;
1323
1324 return -1;
1325 }
1326 virtual int SetCaption(const char* title, const char* icon)
1327 {
1328 if (!dpy)
1329 return -1;
1330
1331 Lock();
1332 SDL_WM_SetCaption(title, icon);
1333 Unlock();
1334 return 0;
1335 }
1336 virtual int GetPosition(int& x, int& y) const
1337 {
1338 if (!dpy)
1339 return -1;
1340
1341 Lock();
1342 doGetPosition(x, y);
1343 Unlock();
1344 //printf("Window position is %d %d s:%d\n", x, y, s);
1345 return 0;
1346 }
1347 virtual int SetPosition(int new_x, int new_y)
1348 {
1349 if (!dpy)
1350 return -1;
1351
1352 Lock();
1353 #if _SDL_VER > 1102
1354 Window& win = info.info.x11.wmwindow;
1355 #else
1356 Window& win = info.info.x11.window;
1357 #endif
1358 XMoveWindow(dpy, win, new_x, new_y);
1359 Unlock();
1360 Refresh();
1361
1362 return 0;
1363 }
1364 virtual int SetFont(const char* subfont)
1365 {
1366 if (!dpy)
1367 return -1;
1368
1369 avm::string lf = subfont;
1370 avm::string::size_type n = lf.find(":qtfont=");
1371 if (n != avm::string::npos)
1372 lf[n] = 0;
1373 if (lf.size() == 0)
1374 return 0;
1375 Lock();
1376 freeFont();
1377
1378 n = lf.find("-iso");
1379 if (n != avm::string::npos)
1380 {
1381 lf[n + 1] = '*';
1382 lf[n + 2] = 0;
1383 }
1384
1385 Window& win = info.info.x11.window;
1386 //XSetWindowAttributes attr;
1387 //XWindowAttributes src;
1388 //XGetWindowAttributes(dpy, info.info.x11.window, &src);
1389 //attr.event_mask=VisibilityChangeMask;
1390 //XChangeWindowAttributes(dpy, info.info.x11.wmwindow, CWEventMask, &attr);
1391 //XChangeWindowAttributes(dpy, info.info.x11.window, CWEventMask, &attr);
1392 //::wnd=info.info.x11.wmwindow;
1393
1394 if (lf.size())
1395 AVM_WRITE(__RENDERER__, "Loading font: \"%s\"\n", lf.c_str());
1396
1397 m_iFontHeight = 0;
1398
1399 //font = XLoadQueryFont(dpy, lf);
1400 #ifdef HAVE_LIBXFT
1401 xftfont = 0; // only truetype fonts
1402 if (XftDefaultHasRender(dpy) == 1 && lf.size())
1403 {
1404 xftcolor = new XftColor;
1405 xftcolor->color.red = 0xd700;
1406 xftcolor->color.green = 0xdc00;
1407 xftcolor->color.blue = 0xed00;
1408 xftcolor->color.alpha = 0xffff;
1409 xftcolor->pixel = 0xd7dced;
1410
1411 avm::string lfn = lf;
1412
1413 #ifdef HAVE_ICONV
1414 /* Check Font Name Style
1415 Type1. Single Font Style.
1416 "-sony-fixed-medium-r-normal--24-230-75-75-c-120-iso8859-1"
1417 Type2 Multi Font Style.
1418 "-sony-fixed-medium-r-normal--24-230-75-75-c-120-jisx0208.1983-0,-sony-fixed-medium-r-normal--24-230-75-75-c-120-jisx0201.1976-0,-sony-fixed-medium-r-normal--24-230-75-75-c-120-iso8859-1"
1419 Type 3 Xft Font Name Style
1420 "MS UI Gothic-16"
1421 */
1422
1423 XftPattern* pat = XftXlfdParse(lf.c_str(), 0, 1);
1424 if (pat)
1425 {
1426 XftResult res;
1427 XftPattern* match = XftFontMatch(dpy, DefaultScreen(dpy),
1428 pat, &res);
1429 if (match)
1430 {
1431 Bool bIsCore;
1432 XftPatternGetBool(match, XFT_CORE, 0, &bIsCore);
1433 if (!bIsCore)
1434 {
1435 AVM_WRITE(__RENDERER__, "XftFont %p\n", match);
1436 xftfont = XftFontOpenXlfd(dpy, DefaultScreen(dpy),
1437 lfn.c_str());
1438 }
1439 XftPatternDestroy(match);
1440 }
1441 AVM_WRITE(__RENDERER__, "XftFont Not Match\n");
1442 XftPatternDestroy(pat);
1443 }
1444 else
1445 {
1446 if (lf[0] != '-')
1447 xftfont = XftFontOpenName(dpy, DefaultScreen(dpy), lf.c_str());
1448 }
1449 #else /* HAVE_ICONV */
1450
1451 // stupid check for truetype font
1452 // FIXME:
1453 char* p = strstr(lfn.c_str(), "type-");
1454 if (p || strstr(lfn.c_str(), "ttf-"))
1455 {
1456 // the purpose of the following code is to replace last digit
1457 // in the fontname: *-iso8859-2 -> *-iso8859-*
1458 // it's necessary so the Xft renderer will work correctly
1459 p = strstr(lfn.c_str(), "iso8859-");
1460 if (p)
1461 {
1462 p += 9;
1463 *p = '*';
1464 AVM_WRITE(__RENDERER__, "Modified font name for iso8859 support\n");
1465 }
1466 #if 0
1467 xftfont = XftFontOpen(dpy, DefaultScreen(dpy),
1468 XFT_FAMILY, XftTypeString, "Arial",
1469 //XFT_ENCODING, XftTypeString, "iso10646",
1470 XFT_ENCODING, XftTypeString, "iso8859-2",
1471 XFT_SIZE, XftTypeDouble, 20.0,
1472 0);
1473 #endif // just testing family open
1474 xftfont = XftFontOpenXlfd(dpy, win, lfn.c_str());
1475 }
1476 //xftfont = XftFontOpenName(d, info.info.x11.window, "verdana:pixelsize=30");
1477 #endif /* HAVE_ICONV */
1478 }
1479
1480 int iDescent = 0;
1481 if (xftfont)
1482 {
1483 m_iFontHeight = xftfont->height;
1484 iDescent = xftfont->descent;
1485 xftdraw = XftDrawCreate(dpy, win, DefaultVisual(dpy, DefaultScreen(dpy)),
1486 DefaultColormap(dpy, DefaultScreen(dpy)));
1487 AVM_WRITE(__RENDERER__, "Successfully opened Xft font\n");
1488 }
1489 else
1490 {
1491 AVM_WRITE(__RENDERER__, "Failed to open Xft Font\n");
1492 }
1493 if (!xftfont)
1494 #endif // HAVE_LIBXFT
1495
1496 if (lf.size())
1497 {
1498 font = XLoadQueryFont(dpy, lf.c_str());
1499 if (!font)
1500 AVM_WRITE(__RENDERER__, "Failed to open X11 font\n");
1501 }
1502
1503 if (font)
1504 {
1505 #ifdef HAVE_ICONV
1506 XFontSetExtents *extent = XExtentsOfFontSet(font);
1507 m_iFontHeight = extent->max_logical_extent.height;
1508 #else /* HAVE_ICONV */
1509 XSetFont(dpy, xgc, font->fid);
1510 m_iFontHeight = font->max_bounds.ascent + font->max_bounds.descent;
1511 iDescent = font->max_bounds.descent;
1512 //printf("Max bounds: %d, %d\n",
1513 // _font->max_bounds.ascent, _font->max_bounds.descent);
1514 #endif /* HAVE_ICONV */
1515 }
1516
1517 m_sub = m_iFontHeight * SUBTITLE_SPACE + iDescent;
1518 //printf("MSUB %d %d\n", m_sub, m_iFontHeight);
1519 SDL_Surface* s = SDL_SetVideoMode(pic_w, pic_h + m_sub, 0, fs);
1520 if (s)
1521 screen = s;
1522 //printf("Max bounds: %d, %d\n",
1523 // _font->max_bounds.ascent, _font->max_bounds.descent);
1524 Unlock();
1525 return 0;
1526 }
1527 void GetModeLine(int& w, int& h)
1528 {
1529 // method to detect screen resolution in pixels
1530 // e.g. when user switches resolution with Ctrl Alt '+'
1531 if (!dpy)
1532 return;
1533 // this could be called either under SDL or
1534 // within SDL context - thus open/close new connectins
1535 // for this query - not very efficient but safe
1536 // and avoids mixture of Xevents with older XServers
1537 Display* d = XOpenDisplay(0);
1538 #ifdef HAVE_LIBXXF86VM
1539 int unused;
1540 XF86VidModeModeLine vidmode;
1541 XF86VidModeGetModeLine(d, DefaultScreen(d),
1542 &unused, &vidmode);
1543 w = vidmode.hdisplay;
1544 h = vidmode.vdisplay;
1545 #else
1546 // this will return just size of the whole screen
1547 // and I don't know other way then XF86VidMode
1548 lugins/libffmpeg // to read current screen size
1549 w = DisplayWidth(d, DefaultScreen(d));
1550 h = DisplayHeight(d, DefaultScreen(d));
1551 #endif // HAVE_LIBXXF86VM
1552 XCloseDisplay(d);
1553 }
1554 virtual int Lock() const
1555 {
1556 int r = VideoRendererWithLock::Lock();
1557 if (r == 0 && !dga && info.info.x11.lock_func)
1558 info.info.x11.lock_func();
1559 return r;
1560 }
1561 virtual int TryLock() const
1562 {
1563 int r = VideoRendererWithLock::TryLock();
1564 if (r == 0 && !dga && info.info.x11.lock_func)
1565 info.info.x11.lock_func();
1566 return r;
1567 }
1568 virtual int Unlock() const
1569 {
1570 if (!dga && info.info.x11.unlock_func)
1571 info.info.x11.unlock_func();
1572 return VideoRendererWithLock::Unlock();
1573 }
1574 virtual int processEvent()
1575 {
1576 /**
1577 *
1578 * This function processes events received by SDL window.
1579 *
1580 */
1581 static const char* const event_names[]=
1582 {
1583 "SDL_NOEVENT",
1584 "SDL_ACTIVEEVENT",
1585 "SDL_KEYDOWN",
1586 "SDL_KEYUP",
1587 "SDL_MOUSEMOTION",
1588 "SDL_MOUSEBUTTONDOWN",
1589 "SDL_MOUSEBUTTONUP",
1590 "SDL_JOYAXISMOTION",
1591 "SDL_JOYBALLMOTION",
1592 "SDL_JOYHATMOTION",
1593 "SDL_JOYBUTTONDOWN",
1594 "SDL_JOYBUTTONUP",
1595 "SDL_QUIT",
1596 "SDL_SYSWMEVENT",
1597 "SDL_EVENT_RESERVEDA",
1598 "SDL_EVENT_RESERVEDB",
1599 "SDL_VIDEORESIZE",
1600 "SDL_VIDEOEXPOSE",
1601 "SDL_EVENT_RESERVED2",
1602 "SDL_EVENT_RESERVED3",
1603 "SDL_EVENT_RESERVED4",
1604 "SDL_EVENT_RESERVED5",
1605 "SDL_EVENT_RESERVED6",
1606 "SDL_EVENT_RESERVED7"
1607 };
1608
1609 int r = 0;
1610 SDL_Event event;
1611 if (TryLock() == 0)
1612 {
1613 m_Mouse.Process(); // MouseOn/Off
1614 r = SDL_PollEvent(&event);
1615 Unlock();
1616 }
1617 if (!r || !m_pPw)
1618 {
1619 // in case there is no incomming event
1620 // check if there is some 'autorepeated' event
1621 if (m_pPw && keyrepev.type == SDL_USEREVENT)
1622 {
1623 // small initial resistance after keypress
1624 if (m_iAuto++ > 4)
1625 {
1626 switch (keyrepev.key.keysym.sym)
1627 {
1628 case SDLK_F1:
1629 case SDLK_F2:
1630 case SDLK_F3:
1631 case SDLK_F4:
1632 case SDLK_F5:
1633 case SDLK_F6:
1634 case SDLK_F7:
1635 case SDLK_F8:
1636 case SDLK_F9:
1637 case SDLK_F10:
1638 case SDLK_F11:
1639 case SDLK_F12:
1640 case SDLK_F13:
1641 case SDLK_F14:
1642 case SDLK_F15:
1643 case SDLK_9:
1644 case SDLK_0:
1645 m_iAuto = 5;
1646 //printf("SLOW\n");
1647 default:
1648 m_pPw->PW_key_func(keyrepev.key.keysym.sym, keyrepev.key.keysym.mod);
1649 if (m_iAuto >= 30)
1650 {
1651 m_iAuto = 30;
1652 //return 0; // without wait -> creates CPU busy loop
1653 }
1654 }
1655 }
1656 }
1657
1658 emutex.Lock();
1659 //AVM_WRITE(__RENDERER__, "nokey %d \n", m_iAuto);
1660 econd.Wait(emutex, 0.1f / (float)m_iAuto);
1661 emutex.Unlock();
1662
1663 return 0;
1664 }
1665 //printf("EvenType: %d %s\n", event.type, event_names[event.type]);
1666 if (event.type == SDL_KEYUP)
1667 {
1668 //printf("Key s:%d m:%d\n", (int) event.key.keysym.sym, event.key.keysym.mod);
1669 keyrepev.type = 0; // disable autorepeat
1670 m_iAuto = 1;
1671 }
1672 else if (event.type == SDL_KEYDOWN && keyrepev.type == 0)
1673 {
1674 //printf("KeyDown s:%d m:%d\n", (int) event.key.keysym.sym, event.key.keysym.mod);
1675 bool autorep = true;
1676 switch (event.key.keysym.sym)
1677 {
1678 case SDLK_RETURN:
1679 if (!(event.key.keysym.mod & (KMOD_ALT | KMOD_META)))
1680 break;
1681 // fall through - alt + enter
1682 case SDLK_ESCAPE:
1683 case SDLK_f:
1684 autorep = false;
1685 m_pPw->PW_fullscreen();
1686 return 1;
1687 case SDLK_m:
1688 autorep = false;
1689 m_pPw->PW_maximize_func();
1690 return 1;
1691 case SDLK_x:
1692 autorep = false;
1693 m_pPw->PW_stop_func();
1694 return 1;
1695 case SDLK_v:
1696 autorep = false;
1697 m_pPw->PW_play_func();
1698 return 1;
1699 case SDLK_q:
1700 autorep = false;
1701 m_pPw->PW_quit_func();
1702 return 1;
1703 case SDLK_p:
1704 case SDLK_c:
1705 case SDLK_SPACE:
1706 autorep = false;
1707 m_pPw->PW_pause_func();
1708 return 1;
1709 //break;
1710 case SDLK_LEFTBRACKET:
1711 case SDLK_RIGHTBRACKET:
1712 autorep = false;
1713 break;
1714 default:
1715 break;
1716 }
1717
1718 //printf("KEYFUNC %d\n",event.key.keysym.sym);
1719 // passing all keys
1720 m_pPw->PW_key_func(event.key.keysym.sym, event.key.keysym.mod);
1721 if (autorep && !keyrepev.type)
1722 {
1723 memcpy(&keyrepev, &event, sizeof(keyrepev));
1724 keyrepev.type = SDL_USEREVENT;
1725 }
1726 }
1727 #if _SDL_VER > 1102
1728 else if (event.type == SDL_VIDEORESIZE)
1729 {
1730 //printf("RESIZE EVENT %d %d\n", event.resize.w, event.resize.h);
1731 m_pPw->PW_resize(event.resize.w, event.resize.h - m_sub);
1732 }
1733 #endif
1734 else if (event.type == SDL_MOUSEBUTTONDOWN)
1735 {
1736 if (!(SDL_GetModState()
1737 & (KMOD_CTRL | KMOD_SHIFT | KMOD_ALT | KMOD_META)))
1738 {
1739 switch (event.button.button)
1740 {
1741 case SDL_BUTTON_RIGHT:
1742 if (SDL_GetVideoSurface()->flags & SDL_FULLSCREEN)
1743 m_pPw->PW_fullscreen();
1744 #if 0 // defined(HAVE_LIBQT) && (HAVE_LIBQT < 302 || HAVE_LIBQT > 303 )
1745 // fix race for Pointer grab
1746 Lock();
1747 XUngrabPointer(info.info.x11.display, CurrentTime);
1748 XUngrabKeyboard(info.info.x11.display, CurrentTime);
1749 Unlock();
1750 #endif
1751 m_pPw->PW_menu_slot();
1752 break;
1753 case SDL_BUTTON_MIDDLE:
1754 m_pPw->PW_middle_button();
1755 break;
1756 case SDL_BUTTON_LEFT:
1757 m_pPw->PW_pause_func();
1758 break;
1759 case SDL_BUTTON_WHEELUP:
1760 m_pPw->PW_key_func(SDLK_PAGEUP, 0);
1761 break;
1762 case SDL_BUTTON_WHEELDOWN:
1763 m_pPw->PW_key_func(SDLK_PAGEDOWN, 0);
1764 break;
1765 }
1766 }
1767 }
1768 #ifdef SDL_VIDEOEXPOSE
1769 else if (event.type == SDL_VIDEOEXPOSE)
1770 {
1771 // we are not receiving this event so far !!
1772 AVM_WRITE(__RENDERER__, "SDL_VIDEOEXPOSE arrived\n");
1773 }
1774 #endif
1775 else if (event.type == SDL_ACTIVEEVENT)
1776 {
1777 if (event.active.state == SDL_APPINPUTFOCUS)
1778 {
1779 //m_bKeyboardOn = (event.active.gain) ? true : false;
1780 m_pPw->PW_refresh();
1781 }
1782 // FIXME - check position change
1783 }
1784 else if (event.type == SDL_VIDEOEXPOSE)
1785 {
1786 m_pPw->PW_refresh();
1787 }
1788 else if (event.type == SDL_MOUSEMOTION)
1789 {
1790 m_Mouse.Set(event.motion.x, event.motion.y);
1791 }
1792 else if (event.type == SDL_QUIT)
1793 {
1794 m_pPw->PW_quit_func();
1795 }
1796
1797 return 1; // try once more again
1798 }
1799
1800 protected:
1801 virtual void doGetPosition(int& x, int& y) const
1802 {
1803 XWindowAttributes xwa;
1804 Window win;
1805 #if _SDL_VER > 1102
1806 win = info.info.x11.wmwindow;
1807 #else
1808 win = info.info.x11.window;
1809 #endif
1810 Status s = XGetWindowAttributes(info.info.x11.display, win, &xwa);
1811 XTranslateCoordinates(info.info.x11.display, win,
1812 xwa.root, -xwa.border_width, -xwa.border_width,
1813 &x, &y, &win);
1814 //printf("WINDOWS %ld %ld %ld\n", info.info.x11.window, info.info.x11.wmwindow, xwa.root);
1815 }
1816 virtual int doResize(int& new_w, int& new_h)
1817 {
1818 // allow original size
1819 if (new_w != m_w)
1820 new_w = (new_w + 7) & ~7;
1821 if (new_h != m_h)
1822 // some resizing algorithms requires this for now
1823 new_h = (new_h + 1) & ~1;
1824
1825 if (!m_bResizeEnabled)
1826 {
1827 AVM_WRITE(__RENDERER__, "Resize is unsupported for RGB Direct mode!\n");
1828 //printf("Disabled %dx%d %dx%d\n", new_w, new_h, pic_w, pic_h);
1829 new_w = m_w;
1830 new_h = m_h;
1831 return -1;
1832 }
1833
1834 if (new_w < 8 || new_h < 2)
1835 return -1;
1836
1837 if (pic_w != new_w || pic_h != new_h)
1838 {
1839 if (image && screen && image->Data() == screen->pixels)
1840 {
1841 AVM_WRITE(__RENDERER__, "Resize is unsupported for RGB Direct mode!\n");
1842 new_w = m_w;
1843 new_h = m_h;
1844 return -1;
1845 CImage* ci = new CImage(image);
1846 image->Release();
1847 // replace direct image with a new copy
1848 image = ci;
1849 }
1850 pic_w = new_w;
1851 pic_h = new_h;
1852 cliprect.x = cliprect.y = 0;
1853 cliprect.w = (Uint16)new_w;
1854 cliprect.h = (Uint16)new_h;
1855 // 0 is intentional -> use current screen depth and color model
1856 screen = SDL_SetVideoMode(pic_w, pic_h + m_sub, 0, fs);
1857 //printf("SetVideoMode pic_w: %d x %d %d\n", pic_w, pic_h, m_sub);
1858 delete[] convbuf;
1859 convbuf = 0;
1860 }
1861 return 0;
1862 }
1863 private:
1864 virtual void freeFont()
1865 {
1866 #ifdef HAVE_LIBXFT
1867 if (xftfont)
1868 XftFontClose(dpy, xftfont);
1869 xftfont = 0;
1870 if (xftdraw)
1871 XftDrawDestroy(xftdraw);
1872 xftdraw = 0;
1873 if (xftcolor)
1874 delete xftcolor;
1875 xftcolor = 0;
1876 #endif
1877 if (font)
1878 XFreeFont(dpy, font);
1879 font = 0;
1880 }
1881 #if 0
1882 // not really needed anymore
1883 void nvidiaCheck()
1884 {
1885 if (s_iTrickNvidia == -1)
1886 {
1887 int lines;
1888 //XExtensionVersion* ext = XGetExtensionVersion(dpy, "NVIDIA-GLX");
1889 int n = 0;
1890 char **extlist = XListExtensions(dpy, &n);
1891
1892 //AVM_WRITE (__RENDERER__, 1, "number of extensions: %d\n", n);
1893 s_iTrickNvidia = 0;
1894 if (extlist) {
1895 int i;
1896 int opcode, event, error;
1897
1898 for (i = 0; i < n; ++i)
1899 if (strcmp("NVIDIA-GLX", extlist[i]) == 0)
1900 {
1901 AVM_WRITE(__RENDERER__, 0, "Detected nVidia GLX driver\n");
1902 AVM_WRITE(__RENDERER__, 0, "If you need to clear two lowest lines set the shell variable AVIPLAY_NVIDIA_ENABLE\n");
1903 }
1904 /* ??? do not free ??? could Xlib depend on contents being unaltered */
1905 XFreeExtensionList(extlist);
1906 }
1907 }
1908 }
1909 #endif
1910 };
1911
1912 #undef __MODULE__
1913 #define __MODULE__ "YUV renderer"
1914
1915 #ifdef HAVE_LIBXV
1916
1917 static const char xvset[] = "XV_SET_DEFAULTS";
1918
1919 #if 0
1920 static void xv_write_attr(Display* dpy, int xv_port, const char* attr, int value)
1921 {
1922 if (attr->id == ATTR_ID_NORM || attr->id == ATTR_ID_INPUT) {
1923 if (attr->id == ATTR_ID_NORM)
1924 h->norm = value;
1925 if (attr->id == ATTR_ID_INPUT)
1926 h->input = value;
1927 for (i = 0; i < h->encodings; ++i) {
1928 if (h->enc_map[i].norm == h->norm &&
1929 h->enc_map[i].input == h->input) {
1930 h->enc = i;
1931 XvSetPortAttribute(dpy,h->vi_port,h->xv_encoding,h->enc);
1932 break;
1933 }
1934 }
1935 }
1936 /* needed for proper timing on the
1937 "mute - wait - switch - wait - unmute" channel switches */
1938 //XSync(dpy, False);
1939 }
1940 #endif // 0
1941
1942 static int xv_scan_attrs(avm::vector<AttributeInfo>& vattrs, Display* dpy, int* xv_port)
1943 {
1944 struct {
1945 const char* atom;
1946 const char* name;
1947 } const xvattrs[] = {
1948 { "XV_HUE", "Hue" },
1949 { "XV_BRIGHTNESS", "Brightness" },
1950 { "XV_CONTRAST", "Contrast" },
1951 { "XV_SATURATION", "Saturation" },
1952 { "XV_RED_INTENSITY", "Red intensity" },
1953 { "XV_GREEN_INTENSITY", "Green intensity" },
1954 { "XV_BLUE_INTENSITY", "Blue intensity" },
1955 { "XV_COLORKEY", "Colorkey" },
1956 { "XV_DOUBLE_BUFFER", "Double buffering" },
1957 { "XV_SYNC_TO_VBLANK", "Sync to VBlank" },
1958
1959 /* this shouldn't be exposed to the user
1960 { "XV_COLOR", "Color" },
1961 { "XV_MUTE", "Mute" },
1962 { "XV_VOLUME", "Volume" },
1963 { "XV_FREQ", "Freq" },
1964 { "XV_ENCODING", "Encoding" },
1965 { "XV_SET_DEFAULTS", "Set default" },
1966 { "XV_AUTOPAINT_COLORKEY", "Autopaint" },
1967 */
1968 { 0 }
1969 };
1970 int have_def = 0;
1971 if (*xv_port == 0)
1972 {
1973 unsigned int ver, rev, req, evn, err;
1974 if (Success != XvQueryExtension(dpy, &ver, &rev, &req, &evn, &err))
1975 return 0;
1976
1977 unsigned int adaptors;
1978 XvAdaptorInfo* ai;
1979 /* check for Xvideo support */
1980 if (Success != XvQueryAdaptors(dpy, DefaultRootWindow(dpy), &adaptors, &ai))
1981 {
1982 AVM_WRITE(__RENDERER__, "Xv: XvQueryAdaptors failed");
1983 return 0;
1984 }
1985 /* check adaptors */
1986 for (unsigned i = 0; i < adaptors && *xv_port == 0; ++i)
1987 {
1988 if ((ai[i].type & (XvInputMask | XvImageMask)))
1989 {
1990 for (unsigned p = (unsigned)ai[i].base_id; p < (unsigned)ai[i].base_id + ai[i].num_ports; ++p)
1991 {
1992 *xv_port = p;
1993 break;
1994 XvUngrabPort(dpy, p, CurrentTime);
1995 if (!XvGrabPort(dpy, p, CurrentTime)) {
1996 AVM_WRITE(__RENDERER__, "Adaptor %d; format list:\n", i);
1997 for (unsigned j = 0; j < ai[i].num_formats; ++j)
1998 {
1999 AVM_WRITE(__RENDERER__, "depth=%d, visual=%ld\n",
2000 ai[i].formats[j].depth,
2001 ai[i].formats[j].visual_id);
2002 }
2003 *xv_port = p;
2004 // 4616 HLA MISS 1 (az 18)
2005 break;
2006 } else {
2007 AVM_WRITE(__RENDERER__, "Xv: could not grab port %i\n", (int)p);
2008 }
2009 }
2010 }
2011 }
2012 }
2013 //printf(" attribute list for port %d\n", xv_port);
2014 int attributes;
2015 XvAttribute* at = XvQueryPortAttributes(dpy, *xv_port, &attributes);
2016 if (at)
2017 {
2018 for (int j = 0; j < attributes; ++j)
2019 {
2020 const char* nm = 0;//at[j].name;
2021 for (int n = 0; xvattrs[n].atom != 0; n++)
2022 {
2023 if (!strcmp(xvattrs[n].atom, xvset))
2024 have_def++;
2025 if (!strcmp(xvattrs[n].atom, at[j].name))
2026 {
2027 nm = xvattrs[n].name;
2028 break;
2029 }
2030 }
2031 if (at[j].flags & XvGettable && at[j].flags & XvSettable)
2032 {
2033 int result;
2034 Atom atom = XInternAtom(dpy, at[j].name, True);
2035 XvGetPortAttribute(dpy, *xv_port, atom, &result);
2036 AVM_WRITE(__RENDERER__, 0, "XV attribute: %s"
2037 " %s%s <%i, %i> = %d\n",
2038 at[j].name,
2039 (at[j].flags & XvGettable) ? "G" : "",
2040 (at[j].flags & XvSettable) ? "S" : "",
2041 at[j].min_value, at[j].max_value, result);
2042 if (nm)
2043 vattrs.push_back(AttributeInfo(at[j].name, nm,
2044 AttributeInfo::Integer,
2045 at[j].min_value,
2046 at[j].max_value,
2047 result));
2048 }
2049 }
2050 XFree(at);
2051 }
2052 return have_def;
2053 }
2054
2055 class XvRtConfig : public IRtConfig
2056 {
2057 VideoRendererWithLock* vr;
2058 avm::vector<int> vattrsorig;
2059 Display* dpy;
2060 int xv_port;
2061 int have_def;
2062 public:
2063 XvRtConfig(VideoRendererWithLock* _vr, Display* _dpy, int _xv_port)
2064 : vr(_vr), dpy(_dpy), xv_port(_xv_port)
2065 {
2066 AVM_WRITE(__RENDERER__, "XV port: %d\n", xv_port);
2067 vr->Lock();
2068 have_def = xv_scan_attrs(m_Attrs, dpy, &xv_port);
2069 vr->Unlock();
2070 vattrsorig.resize(m_Attrs.size());
2071 if (have_def)
2072 SetValue(xvset, 0);
2073 for (unsigned i = 0; i < m_Attrs.size(); ++i)
2074 SetValue(m_Attrs[i].GetName(),
2075 RegReadInt("aviplay", m_Attrs[i].GetName(), m_Attrs[i].GetDefault()));
2076 }
2077 virtual ~XvRtConfig()
2078 {
2079 // restore to defaults or at least those from the begining
2080 if (have_def)
2081 SetValue(xvset, 0);
2082 else
2083 for (unsigned i = 0; i < m_Attrs.size(); ++i)
2084 {
2085 int val = 0;
2086 GetValue(m_Attrs[i].GetName(), &val);
2087 RegWriteInt("aviplay", m_Attrs[i].GetName(), val);
2088 SetValue(m_Attrs[i].GetName(), m_Attrs[i].GetDefault());
2089 }
2090 }
2091 // IRtConfig interface
2092 virtual int GetValue(const char* attr, int* result) const
2093 {
2094 vr->Lock();
2095 Atom atom = XInternAtom(dpy, attr, True);
2096 XvGetPortAttribute(dpy, xv_port, atom, result);
2097 AVM_WRITE(__RENDERER__, 1, "xv: get %s:%d %d\n", attr, xv_port, *result);
2098 vr->Unlock();
2099 return 0;
2100 }
2101 virtual int SetValue(const char* attr, int value)
2102 {
2103 vr->Lock();
2104 Atom atom = XInternAtom(dpy, attr, True);
2105 XvSetPortAttribute(dpy, xv_port, atom, value);
2106 AVM_WRITE(__RENDERER__, 1, "xv: set %s:%d atom:%d value: %d\n", attr, xv_port, (int)atom, value);
2107 vr->Unlock();
2108 return 0;
2109 }
2110 };
2111 #else // HAVE_LIBXV
2112 class XvRtConfig : public IRtConfig
2113 {
2114 public:
2115 virtual int GetValue(const char* attr, int* result) const { return 0; }
2116 virtual int SetValue(const char* attr, int value) { return 0; }
2117 };
2118 #endif // HAVE_LIBXV
2119
2120 #if _SDL_VER > 1103
2121
2122 /**
2123 *
2124 * SDL YUV Renderer
2125 *
2126 * currently SDL is able to use only XShm XV extension
2127 *
2128 *
2129 *
2130 *
2131 */
2132 class YUVRenderer: public FullscreenRenderer
2133 {
2134 protected:
2135 SDL_Overlay* m_ov;
2136 SDL_Overlay* m_zoomov;
2137 fourcc_t m_fmt;
2138 avm::vector<CImage*> ovlist;
2139 XvRtConfig* m_pXvRtConfig;
2140 public:
2141 YUVRenderer(PlayerWidget* pw, Display* _dpy,
2142 int _width, int _height, fourcc_t yuvm_fmt, bool _subtitles = false) :
2143 FullscreenRenderer(pw, _dpy, _width, _height, _subtitles),
2144 m_ov(0), m_zoomov(0), m_fmt(yuvm_fmt), m_pXvRtConfig(0)
2145 {
2146 }
2147 ~YUVRenderer()
2148 {
2149 delete m_pXvRtConfig;
2150 if (screen)
2151 {
2152 ReleaseImages();
2153 Lock();
2154 if (m_zoomov)
2155 SDL_FreeYUVOverlay(m_zoomov);
2156 if (m_ov)
2157 SDL_FreeYUVOverlay(m_ov);
2158 Unlock();
2159 }
2160 }
2161 virtual int init()
2162 {
2163 if (dga) {
2164 AVM_WRITE(__MODULE__, "Requested DGA driver - YUV not available!");
2165 return -1;
2166 }
2167
2168 if (FullscreenRenderer::init() < 0)
2169 return -1;
2170
2171 Lock(); // event thread is already running
2172
2173 screen = SDL_SetVideoMode(pic_w, pic_h + m_sub, 0, fs);
2174 // fs &= ~SDL_DOUBLEBUF; // doesn't work with HW overlay
2175 // fs |= SDL_HWSURFACE;
2176 //screen->flags = fs;
2177 #if 0
2178 AVM_WRITE(__RENDERER__, 1, "ScreenInfo: colorkey:0x%x alpha:0x%x\n",
2179 screen->format->colorkey,
2180 screen->format->alpha);
2181 #endif
2182 #if 0
2183 char* tmp_array=new char[m_w*m_h*2];
2184 char* tmp_array2=new char[m_w*m_h*2];
2185 uint_t t1=localcount();
2186 memcpy(tmp_array2, tmp_array, m_w*m_h*2);
2187 uint_t t2=localcount();
2188 AVM_WRITE("Memory->memory copy: %f Mb/s\n",
2189 (_width*_height*3/2/1048576.)/((t2-t1)/freq/1000.));
2190 delete[] tmp_array2;
2191 SDL_Overlay* ovs[200];
2192 for (int i=0; i<200; ++i)
2193 {
2194 ovs[i]=SDL_CreateYUVOverlay(_width, _height, fccYV12, screen);
2195 SDL_LockYUVOverlay(ovs[i]);
2196 uint_t t=localcount();
2197 memcpy(ovs[i]->pixels[0], tmp_array, _width*_height*3/2);
2198 uint_t t2=localcount();
2199 SDL_UnlockYUVOverlay(ovs[i]);
2200 AVM_WRITE("overlay %d ( %f Mb used ): %f Mb/s\n",
2201 i, i*_width*_height*3/2/1048576.,
2202 (_width*_height*3/2/1048576.)/((t2-t1)/freq/1000.));
2203 }
2204 for (int i=0; i<200; ++i)
2205 SDL_FreeYUVOverlay(ovs[i]);
2206 delete[] tmp_array;
2207 #endif
2208 //printf("IMGFMT %x %.4s\n", m_fmt, (char*)&m_fmt);
2209
2210 Unlock();
2211 BitmapInfo bi(m_w, m_h, m_fmt);
2212 CImage* ci = ImageAlloc(bi, 0, 0); // creates m_ov if possible
2213 ci->Release();
2214 if (!m_ov
2215 #if _SDL_VER>1104
2216 // SDL <=1.1.5 does not have hw_overlay flag
2217 || !m_ov->hw_overlay
2218 #endif
2219 )
2220 {
2221 const char* errmsg = (!m_ov) ? "Failed to create overlay" :
2222 "No hardware YUV acceleration detected!";
2223
2224 AVM_WRITE(__RENDERER__, "%s\n", errmsg);
2225 #if _SDL_VER>1106
2226 if (m_ov && allow_sw_yuv)
2227 {
2228 AVM_WRITE(__RENDERER__,
2229 "*** Using SDL software YUV emulation ***\n"
2230 " Some codecs supports RGB modes natively - then you may\n"
2231 " achieve better performance with disabled YUV flag\n");
2232
2233 // do not bother user with Xlib warning messages
2234 avm_setenv("SDL_VIDEO_YUV_HWACCEL", "0", 1);
2235 }
2236 else
2237 #endif
2238 {
2239 if (m_ov)
2240 AVM_WRITE(__RENDERER__, "Your SDL library is too old and doesn't support software YUV emulation - upgrade SDL package!\n");
2241 return -1;
2242 }
2243 }
2244 else
2245 {
2246 delete m_pSdlgRtConfig;
2247 m_pSdlgRtConfig = 0;
2248 }
2249
2250 AVM_WRITE(__RENDERER__, 0, "created overlay: %dx%d %s\n",
2251 m_w, m_h, avm_img_format_name(m_fmt));
2252
2253 #ifdef HAVE_LIBXV
2254 if (!m_pSdlgRtConfig && m_ov->hw_overlay)
2255 {
2256 // hack - we want to get to the grabbed Xv port
2257 struct hack_privdata { int port; };
2258 int port = ((struct hack_privdata*) m_ov->hwdata)->port;
2259 m_pXvRtConfig = new XvRtConfig(this, dpy, port);
2260 }
2261 #endif
2262 return 0;
2263 }
2264 virtual IRtConfig* GetRtConfig() const { return m_pXvRtConfig ? (IRtConfig*) m_pXvRtConfig : (IRtConfig*) m_pSdlgRtConfig; }
2265 virtual CImage* ImageAlloc(const BITMAPINFOHEADER& bh, uint_t idx, size_t align)
2266 {
2267 //printf("IA 0x%x==0x%x %d %d %d %d align:%" PRIsz " idx:%d-%" PRIsz "\n",bh.biCompression, m_fmt,
2268 // m_w, bh.biWidth, m_h, bh.biHeight, align, idx, ovlist.size());
2269 align = 15; //HACK FIXME
2270 if (m_w != (int)bh.biWidth || m_h != (int)-bh.biHeight || bh.biCompression <= 32)
2271 return 0;
2272
2273 Lock();
2274 while (idx >= ovlist.size())
2275 {
2276 // ok here is little trick
2277 // we need to use aligned size - but for display
2278 // we do not want to show these extra pixels
2279 // so here we will modify the size a little bit
2280 int mh = m_h;
2281 switch (bh.biCompression) {
2282 case fccI420:
2283 case fccYV12:
2284 if (align > 0)
2285 mh = ((m_h + (int)align) & (int)~align);
2286 break;
2287 }
2288
2289 // make the image rounded for 16
2290 // width aligned by 8 - fix for my broken i855 YUV driver
2291 //SDL_Overlay* o = SDL_CreateYUVOverlay((m_w + 7) & ~7, mh+8, bh.biCompression, screen);
2292 SDL_Overlay* o = SDL_CreateYUVOverlay((m_w + (int)align) & (int)~align, mh, bh.biCompression, screen);
2293 if (!o || o->h < mh || o->w < m_w) {
2294 AVM_WRITE(__RENDERER__, "Failed to create SDL Overlay: %dx%d\n", m_w, mh);
2295 return 0;
2296 }
2297
2298 //printf("M_H:%d MH:%d H:%d W:%d M_W:%d 0x%x ,%d\n", m_h, mh, o->h, o->w, m_w, o->pixels[0], o->pitches[0]);
2299 if (m_h < mh)
2300 {
2301 #ifdef HAVE_LIBXV
2302 struct private_yuvhwdata { // copy from SDL header file
2303 int port;
2304 XShmSegmentInfo yuvshm;
2305 XvImage *image;
2306 };
2307 //((struct private_yuvhwdata*) o->hwdata)->image->height = m_h;
2308 #endif
2309 // now modify the size
2310 //o->h = m_h - 8; // HACK!!
2311 }
2312
2313 BitmapInfo bi(m_w, mh, bh.biCompression);
2314 // CHECKME return SDL1.1.3 compatibility
2315 const uint8_t* planes[3];
2316 int stride[3];
2317 planes[0] = o->pixels[0];
2318 stride[0] = o->pitches[0];
2319 switch (bh.biCompression) {
2320 case fccI420:
2321 case fccYV12:
2322 planes[1] = o->pixels[1];
2323 planes[2] = o->pixels[2];
2324 stride[1] = o->pitches[1];
2325 stride[2] = o->pitches[2];
2326 break;
2327 default:
2328 planes[1] = planes[2] = 0;
2329 stride[1] = stride[2] = 0;
2330 }
2331
2332 CImage* ci = new CImage(&bi, planes, stride, false);
2333 //printf("NEW DATA %p %x\n", ci, ci->Format());
2334 //AVM_WRITE("AFTERORIMGALO %d %d %p\n", idx, ovlist.size(), ovlist[idx]->pixels);
2335 ci->SetUserData(o);
2336 ci->SetAllocator(this);
2337 ci->Clear();
2338 ovlist.push_back(ci);
2339 if (!m_ov)
2340 {
2341 SDL_Rect rect = { 0, 0, (Uint16)o->w, (Uint16)o->h };
2342 // activate overlay so YUV preferencies
2343 // for brightness etc. can be set
2344 SDL_DisplayYUVOverlay(o, &rect);
2345 m_ov = o;
2346 }
2347 else if (idx == 0 && o)
2348 {
2349 // ok let's make it as a leading FMT
2350 SDL_FreeYUVOverlay(m_ov);
2351 m_ov = o;
2352 m_fmt = bh.biCompression;
2353 //printf("NEW GROUP %x\n", m_fmt);
2354 }
2355 }
2356 Unlock();
2357 if (idx >= ovlist.size())
2358 return 0;
2359 ovlist[idx]->AddRef();
2360 if (m_uiImages <= idx)
2361 m_uiImages = idx + 1;
2362
2363 return ovlist[idx];
2364 }
2365 virtual void ReleaseImages() {
2366 Lock();
2367 m_uiImages = 0;
2368 if (image)
2369 image->Release();
2370 image = 0;
2371 while (ovlist.size() > 0)
2372 {
2373 SDL_Overlay* o = (SDL_Overlay*) ovlist.back()->GetUserData();
2374 // leave initially allocate
2375 if (o != m_ov)
2376 SDL_FreeYUVOverlay(o);
2377 ovlist.back()->Release();
2378 ovlist.pop_back();
2379 }
2380 Unlock();
2381 }
2382 virtual int Draw(const CImage* data)
2383 {
2384 // if we do not want to block our renderer while
2385 // keyboard event processing tries to lock this system
2386 // using just normal MUTEX - we are not writing anything to Xfree
2387 Lock();
2388 if (!data)
2389 {
2390 if (!image) {
2391 Unlock();
2392 return -1;
2393 }
2394 data = image;
2395 }
2396
2397 m_lLastDrawStamp = avm_get_time_us();
2398
2399 data->AddRef();
2400 if (image)
2401 image->Release();
2402 image = data;
2403 //AVM_WRITE("DATA %x %x\n", data->GetFmt()->biCompression, m_fmt);
2404 //printf("ALLOCATOR %p %p %x %x %p\n", data->GetAllocator(),
2405 // data->GetUserData(), data->Format(), m_fmt, m_zoomov);
2406 if (m_zoomov || !data->GetUserData())
2407 {
2408 SDL_Overlay* o = m_ov;
2409 CImage* datah = 0;
2410 if (m_zoomov) {
2411 o = m_zoomov;
2412 if (data->m_Window.w != m_Zoom.w)
2413 {
2414 const uint8_t* p[CIMAGE_MAX_PLANES] = {
2415 data->Data(0), data->Data(1), data->Data(2)
2416 };
2417 int s[CIMAGE_MAX_PLANES] = {
2418 data->Stride(0), data->Stride(1), data->Stride(2)
2419 };
2420 datah = new CImage(data->GetFmt(), p, s, false);
2421 datah->SetWindow(m_Zoom.x, m_Zoom.y, m_Zoom.w, m_Zoom.h);
2422 }
2423 }
2424
2425 assert(o != 0);
2426 //SDL_LockYUVOverlay(o);
2427 BitmapInfo bi(o->w, o->h, m_fmt);
2428 //data->GetFmt()->Print();
2429 //bi.Print();
2430
2431 // CHECKME return SDL1.0 compatibility
2432 const int stride[3] = { o->pitches[0], o->pitches[1], o->pitches[2] };
2433 CImage ci(&bi, const_cast<const uint8_t**>(o->pixels), stride, false);
2434 //printf("Set %d %d %d %d\n", m_Zoom.x, m_Zoom.y, m_Zoom.w, m_Zoom.h);
2435 ci.SetWindow(0, 0, m_Zoom.w, m_Zoom.h);
2436 if (datah)
2437 {
2438 ci.Convert(datah);
2439 datah->Release();
2440 }
2441 else
2442 ci.Convert(data);
2443 //printf("CONVERT %p %x-%x %p\n", m_zoomov, data->Format(), m_fmt, data->GetUserData());
2444 //SDL_UnlockYUVOverlay(o);
2445 }
2446 Unlock();
2447
2448 return 0;
2449 }
2450 virtual int Sync()
2451 {
2452 Lock();
2453 //SDL_Rect rect = { -300, -300, (Uint16)pic_w*2, (Uint16)pic_h*2 };// + 1;
2454 SDL_Rect rect = { 0, 0, (Uint16)pic_w, (Uint16)pic_h };// + 1;
2455 SDL_Overlay* o = (m_zoomov) ? m_zoomov : (SDL_Overlay*) image->GetUserData();
2456 if (!o)
2457 o = m_ov;
2458 assert(o != 0);
2459 #if 0
2460 printf("OVERLA %p w:%d h:%d %d %d\n", o, o->w, o->h, pic_w, pic_h);
2461 // no surface lock as SDL documentation recommends!
2462 // the following operation might lock itself in XServer
2463 #ifdef HAVE_LIBXV
2464 struct private_yuvhwdata { // copy from SDL header file
2465 int port;
2466 XShmSegmentInfo yuvshm;
2467 XvImage *image;
2468 };
2469 ((struct private_yuvhwdata*) o->hwdata)->image->height--;
2470 ((struct private_yuvhwdata*) o->hwdata)->image->width--;
2471 #endif
2472 #endif
2473 // now modify the size
2474 //o->h = m_h - 8; // HACK!!
2475 SDL_DisplayYUVOverlay(o, &rect);
2476 /*
2477 for (int i = 0; i < pic_h + 50; ++i)
2478 for (int j = 0; j < pic_w; ++j)
2479 ((short*)screen->pixels)[i * pic_w + j] = 0xffff - j;
2480 */
2481 //SDL_UpdateRect(screen, rect.x, rect.y, rect.w, rect.h);
2482 //SDL_UpdateRect(screen, 0, 0, 0, 0);
2483 Unlock();
2484 emutex.Lock();
2485 econd.Broadcast();
2486 emutex.Unlock();
2487 return 0;
2488 }
2489 virtual int Zoom(int x, int y, int width, int height)
2490 {
2491 Lock();
2492 if (m_zoomov)
2493 {
2494 SDL_FreeYUVOverlay(m_zoomov);
2495 m_zoomov = 0;
2496 }
2497
2498 width = (width + 7) & ~7;
2499 height = (height + 1) & ~1;
2500
2501 m_Zoom.x = (Sint16)(x & ~7);
2502 m_Zoom.y = (Sint16)(y & ~1);
2503 m_Zoom.w = (Uint16)width;
2504 m_Zoom.h = (Uint16)height;
2505 //printf("ZOOOM x:%d y:%d w:%d h:%d 0x%x %p\n", m_Zoom.x, m_Zoom.y, m_Zoom.w, m_Zoom.h, m_fmt, screen);
2506 if ((width == m_w && height == m_h)
2507 || m_Zoom.w <= 0 || m_Zoom.h <= 0)
2508 m_Zoom.w = m_Zoom.h = 0; // no cropping needed
2509 else
2510 m_zoomov = SDL_CreateYUVOverlay(m_Zoom.w, m_Zoom.h, m_fmt, screen);
2511 //printf("%d %d %d %d %d %d\n", m_Zoom.x, m_Zoom.y, m_Zoom.w, m_Zoom.h, width, height);
2512 if (m_zoomov)
2513 for (unsigned i = 0; i < ovlist.size(); ++i)
2514 ovlist[i]->SetWindow(m_Zoom.x, m_Zoom.y, m_Zoom.w, m_Zoom.h);
2515
2516 //avm_vector_iterate(CImage*, ovlist, i)
2517 //(*i)->SetWindow(m_Zoom.x, m_Zoom.y, m_Zoom.w, m_Zoom.h);
2518 Unlock();
2519
2520 return 0;
2521 }
2522 protected:
2523 virtual int doResize(int& new_w, int& new_h)
2524 {
2525 if (new_w != m_w)
2526 new_w = (new_w + 7) & ~7;
2527 if (new_h != m_h)
2528 new_h = (new_h + 1) & ~1;
2529
2530 if (new_w >= 8 && new_h >= 2
2531 && (pic_w != new_w || pic_h != new_h))
2532 {
2533 #if _SDL_VER > 1103
2534 fs |= SDL_RESIZABLE;
2535 #endif
2536 screen = SDL_SetVideoMode(new_w, new_h + m_sub, 0, fs);
2537 if (screen) {
2538 pic_w = new_w;
2539 pic_h = new_h;
2540 return 0;
2541 }
2542 screen = SDL_SetVideoMode(pic_w, pic_h + m_sub, 0, fs);
2543 }
2544
2545 new_w = pic_w;
2546 new_h = pic_h;
2547 //printf("Failed SET SCREEN2 = %p %d,%d,%d fs:%d\n", screen, pic_w, pic_h, m_sub, fs);
2548 return -1;
2549 }
2550 };
2551
2552 #endif // _SDL_VER > 1103
2553
2554 #ifdef HAVE_VIDIX
2555 #include <vidix.h>
2556 #include <vidixlib.h>
2557
2558 #undef __MODULE__
2559 #define __MODULE__ "VIDIX renderer"
2560
2561 #define VIDIXCSTR(name) \
2562 static const char name[] = #name
2563
2564 VIDIXCSTR(VIDIX_BRIGHTNESS);
2565 VIDIXCSTR(VIDIX_CONTRAST);
2566 VIDIXCSTR(VIDIX_SATURATION);
2567 VIDIXCSTR(VIDIX_HUE);
2568
2569 class VidixRtConfig : public IRtConfig
2570 {
2571 VideoRendererWithLock* vr;
2572 VDL_HANDLE handler;
2573 mutable vidix_video_eq_t info;
2574 public:
2575 VidixRtConfig(VideoRendererWithLock* _vr, VDL_HANDLE _handler) : vr(_vr),
2576 handler(_handler)
2577 {
2578 vdlPlaybackGetEq(handler, &info);
2579 if (info.cap & VEQ_CAP_BRIGHTNESS)
2580 m_Attrs.push_back(AttributeInfo(VIDIX_BRIGHTNESS, "Brightness",
2581 AttributeInfo::Integer,
2582 -1000, 1000, info.brightness));
2583 if (info.cap & VEQ_CAP_CONTRAST)
2584 m_Attrs.push_back(AttributeInfo(VIDIX_CONTRAST, "Contrast",
2585 AttributeInfo::Integer,
2586 -1000, 1000, info.contrast));
2587 if (info.cap & VEQ_CAP_SATURATION)
2588 m_Attrs.push_back(AttributeInfo(VIDIX_SATURATION, "Saturation",
2589 AttributeInfo::Integer,
2590 -1000, 1000, info.saturation));
2591 if (info.cap & VEQ_CAP_HUE)
2592 m_Attrs.push_back(AttributeInfo(VIDIX_HUE, "Hue",
2593 AttributeInfo::Integer,
2594 -1000, 1000, info.hue));
2595 //for (avm::vector<avm::AttributeInfo>::const_iterator it = m_Attrs.begin(); it != m_Attrs.end(); ++it)
2596 //vector_iterate_const(avm::AttributeInfo, m_Attrs, i)
2597 // SetValue((*i).GetName(), RegReadInt("aviplay", (*i).GetName(), (*i).GetDefault()));
2598
2599 for (unsigned i = 0; i < m_Attrs.size(); ++i)
2600 SetValue(m_Attrs[i].GetName(),
2601 RegReadInt("aviplay", m_Attrs[i].GetName(), m_Attrs[i].GetDefault()));
2602 }
2603 virtual ~VidixRtConfig()
2604 {
2605 for (unsigned i = 0; i < m_Attrs.size(); ++i)
2606 {
2607 int val = 0;
2608 GetValue(m_Attrs[i].GetName(), &val);
2609 RegWriteInt("aviplay", m_Attrs[i].GetName(), val);
2610 //printf("WRITE %s %d\n", m_Attrs[i].GetName(), val);
2611 SetValue(m_Attrs[i].GetName(), m_Attrs[i].GetDefault());
2612 }
2613 }
2614 // IRtConfig interface
2615 virtual int GetValue(const char* attr, int* result) const
2616 {
2617 int r = 0;
2618 vr->Lock();
2619 if (vdlPlaybackGetEq(handler, &info) != 0)
2620 r = -1;
2621 else if (info.cap & VEQ_CAP_BRIGHTNESS
2622 && !strcasecmp(attr, VIDIX_BRIGHTNESS))
2623 *result = info.brightness;
2624 else if (info.cap & VEQ_CAP_CONTRAST
2625 && !strcasecmp(attr, VIDIX_CONTRAST))
2626 *result = info.contrast;
2627 else if (info.cap & VEQ_CAP_SATURATION
2628 && !strcasecmp(attr, VIDIX_SATURATION))
2629 *result = info.saturation;
2630 else if (info.cap & VEQ_CAP_HUE
2631 && !strcasecmp(attr, VIDIX_HUE))
2632 *result = info.hue;
2633 else
2634 r = -1;
2635 vr->Unlock();
2636 //printf("RESULT %s %d\n", attr, *result);
2637 return r;
2638 }
2639 virtual int SetValue(const char* attr, int value)
2640 {
2641 int r = 0;
2642 vr->Lock();
2643 if (!strcmp(attr, VIDIX_BRIGHTNESS))
2644 info.brightness = value;
2645 else if (!strcmp(attr, VIDIX_CONTRAST))
2646 info.contrast = value;
2647 else if (!strcmp(attr, VIDIX_SATURATION))
2648 info.saturation = value;
2649 else if (!strcmp(attr, VIDIX_HUE))
2650 info.hue = value;
2651 else
2652 r = -1;
2653
2654 if (r == 0 && vdlPlaybackSetEq(handler, &info) != 0)
2655 r = -1;
2656 vr->Unlock();
2657
2658 return r;
2659 }
2660 };
2661 #undef VIDIXCSTR
2662
2663 class VidixRenderer : public FullscreenRenderer
2664 {
2665 VDL_HANDLE m_vidix_handler;
2666 vidix_capability_t m_vidix_cap;
2667 vidix_playback_t m_vidix_play;
2668 vidix_fourcc_t m_vidix_fourcc;
2669 vidix_yuv_t dstrides;
2670 uint32_t apitch;
2671 uint8_t* vidix_mem;
2672 int next_frame;
2673 int image_Bpp;
2674 int x, y;
2675 uint32_t bgclr;
2676 VidixRtConfig* m_pVidixRtConfig;
2677 avm::vector<CImage*> ovlist;
2678
2679 public:
2680 VidixRenderer(PlayerWidget* pw, Display* _dpy,
2681 int _width, int _height, fourcc_t fmt, bool _subtitles = false) :
2682 FullscreenRenderer(pw, _dpy, _width, _height, _subtitles),
2683 next_frame(0), bgclr(0), m_pVidixRtConfig(0)
2684 {
2685 m_vidix_fourcc.fourcc = fmt;
2686 m_vidix_fourcc.srcw = m_w;
2687 m_vidix_fourcc.srch = m_h;
2688 }
2689 virtual ~VidixRenderer()
2690 {
2691 delete m_pVidixRtConfig;
2692 vdlPlaybackOff(m_vidix_handler);
2693 vdlClose(m_vidix_handler);
2694 }
2695 virtual int init()
2696 {
2697 if (vdlGetVersion() != VIDIX_VERSION)
2698 {
2699 AVM_WRITE(__MODULE__, "vidix incompatible library version");
2700 return -1;
2701 }
2702
2703 m_vidix_handler = vdlOpen(VIDIX_LIBDIR, 0, TYPE_OUTPUT, 0);
2704 //m_vidix_handler = vdlOpen(VIDIX_LIBDIR, "librage128.so", TYPE_OUTPUT, 0/*verbose*/);
2705 //m_vidix_handler = vdlOpen(VIDIX_LIBDIR, "libmga_crtc2.so", TYPE_OUTPUT, 0/*verbose*/);
2706 if (!m_vidix_handler)
2707 {
2708 AVM_WRITE(__MODULE__, "can't be opened");
2709 return -1;
2710 }
2711
2712 if (vdlGetCapability(m_vidix_handler, &m_vidix_cap) != 0)
2713 {
2714 AVM_WRITE(__MODULE__, "can't get capabilities");
2715 return -1;
2716 }
2717
2718 char fc[4];
2719 avm_set_le32(fc, m_vidix_fourcc.fourcc);
2720 AVM_WRITE(__MODULE__, "Opened vidix renderer - FCC: %.4s\n", fc);
2721 AVM_WRITE(__MODULE__, "Description: %s\n", m_vidix_cap.name);
2722 AVM_WRITE(__MODULE__, "Author: %s\n", m_vidix_cap.author);
2723
2724 if (((m_vidix_cap.maxwidth != -1) && (m_w > m_vidix_cap.maxwidth)) ||
2725 ((m_vidix_cap.minwidth != -1) && (m_w < m_vidix_cap.minwidth)) ||
2726 ((m_vidix_cap.maxheight != -1) && (m_h > m_vidix_cap.maxheight)) ||
2727 ((m_vidix_cap.minwidth != -1 ) && (m_h < m_vidix_cap.minheight)))
2728 {
2729 AVM_WRITE(__MODULE__, "unsupported resolution (min/max width/height)");
2730 return -1;
2731 }
2732
2733 if (vdlQueryFourcc(m_vidix_handler, &m_vidix_fourcc) != 0)
2734 {
2735 AVM_WRITE(__MODULE__, "unsupported fourcc");
2736 return -1;
2737 }
2738
2739 unsigned int err;
2740 switch (bit_depth)
2741 {
2742 case 1: err = VID_DEPTH_1BPP; break;
2743 case 2: err = VID_DEPTH_2BPP; break;
2744 case 4: err = VID_DEPTH_4BPP; break;
2745 case 8: err = VID_DEPTH_8BPP; break;
2746 case 12: err = VID_DEPTH_12BPP; break;
2747 case 15: err = VID_DEPTH_15BPP; break;
2748 case 16: err = VID_DEPTH_16BPP; break;
2749 case 24: err = VID_DEPTH_24BPP; break;
2750 case 32: err = VID_DEPTH_32BPP; break;
2751 default: err=~0U; break;
2752 }
2753 err = ((m_vidix_fourcc.depth & err) != err);
2754 if (m_vidix_fourcc.flags & VID_CAP_COLORKEY)
2755 {
2756 vidix_grkey_t gr_key;
2757 vdlGetGrKeys(m_vidix_handler, &gr_key);
2758 gr_key.key_op = KEYS_PUT;
2759 gr_key.ckey.op = CKEY_TRUE;
2760 gr_key.ckey.red = 4;
2761 gr_key.ckey.green = 0;
2762 gr_key.ckey.blue = 247;
2763 #if _SDL_VER > 1103
2764 bgclr = SDL_MapRGBA(screen->format, gr_key.ckey.red,
2765 gr_key.ckey.green, gr_key.ckey.blue, 0);
2766 #else
2767 #warning RGB map missing
2768 #endif
2769 vdlSetGrKeys(m_vidix_handler, &gr_key);
2770 }
2771 doResize(pic_w, pic_h);
2772
2773 delete m_pSdlgRtConfig;
2774 m_pSdlgRtConfig = 0;
2775 m_pVidixRtConfig = new VidixRtConfig(this, m_vidix_handler);
2776 return 0;
2777 }
2778 virtual IRtConfig* GetRtConfig() const { return m_pVidixRtConfig; }
2779 //virtual uint_t GetImages() const { return 0; }
2780 virtual CImage* ImageAlloc(const BITMAPINFOHEADER& bh, uint_t idx, uint_t align)
2781 {
2782 // vidix provides slow real graphics card memory
2783 // so it could be only usable with draw_band
2784 //printf("IMAGEALLOC %d %d\n", idx, m_vidix_play.num_frames);
2785 Locker locker(m_Mutex);
2786 if (0 || idx >= m_vidix_play.num_frames || bh.biCompression != IMG_FMT_YV12)
2787 return 0;
2788 if (idx >= ovlist.size())
2789 {
2790 BitmapInfo bi(m_w, m_h, m_vidix_fourcc.fourcc);
2791 const uint8_t* plane[3] =
2792 {
2793 vidix_mem + m_vidix_play.offsets[idx]
2794 + m_vidix_play.offset.y,
2795 vidix_mem + m_vidix_play.offsets[idx]
2796 + m_vidix_play.offset.u,
2797 vidix_mem + m_vidix_play.offsets[idx]
2798 + m_vidix_play.offset.v
2799 };
2800 const int stride[3] = { dstrides.y, dstrides.u, dstrides.v };
2801 //printf("stride %d %d %d\n", dstrides.y, dstrides.u, dstrides.v);
2802 CImage* ci = new CImage(&bi, plane, stride, false);
2803 m_uiImages = ++idx;
2804 ci->SetUserData((void*)idx);
2805 ci->SetAllocator(this);
2806 ovlist.push_back(ci);
2807 }
2808 return ovlist[idx];
2809 }
2810 virtual void ReleaseImages()
2811 {
2812 Locker locker(m_Mutex);
2813 m_uiImages = 0;
2814 if (image)
2815 image->Release();
2816 image = 0;
2817 while (ovlist.size() > 0)
2818 {
2819 ovlist.back()->Release();
2820 ovlist.pop_back();
2821 }
2822 }
2823 virtual int Draw(const CImage* data)
2824 {
2825 // if we do not want to block our renderer while
2826 // keyboard event processing tries to lock this system
2827 // using just normal MUTEX - we are not writing anything to Xfree
2828 Locker locker(m_Mutex);
2829 if (!data)
2830 {
2831 if (!image)
2832 return -1;
2833 data = image;
2834 }
2835 m_lLastDrawStamp = avm_get_time_us();
2836
2837 data->AddRef();
2838 if (image)
2839 image->Release();
2840
2841 if (data->GetAllocator() == this)
2842 {
2843 image = data;
2844 return 0;
2845 }
2846 //AVM_WRITE("DATA %x %x\n", data->GetFmt()->biCompression, m_fmt);
2847 BitmapInfo bi(m_w, m_h, m_vidix_fourcc.fourcc);
2848 //printf("CONVERT from to ############ %.4s\n", (char*)&m_vidix_fourcc.fourcc);
2849 //data->GetFmt()->Print();
2850 //bi.Print();
2851 const uint8_t* plane[3] =
2852 {
2853 vidix_mem + m_vidix_play.offsets[next_frame]
2854 + m_vidix_play.offset.y,
2855 vidix_mem + m_vidix_play.offsets[next_frame]
2856 + m_vidix_play.offset.u,
2857 vidix_mem + m_vidix_play.offsets[next_frame]
2858 + m_vidix_play.offset.v
2859 };
2860 const int stride[3] = { dstrides.y, dstrides.u, dstrides.v };
2861
2862 //printf("%.4s %d %d %d %p %p %p %p %p %p\n", (char*)&m_vidix_fourcc.fourcc, stride[0], stride[1], stride[2],
2863 //plane[0], plane[1], plane[2], data->Data(0), data->Data(1), data->Data(2));
2864 // FIXME direct memcopy should be handled with DMA here
2865
2866 // reading memory from MGA G400 is about 22 times slower then real RAM
2867 //int64_t ys = avm_get_time_us(); int b = 0; for(int i = 0; i < 100000; ++i) b += plane[0][i]; int64_t ye = avm_get_time_us();
2868 //int64_t xs = avm_get_time_us(); int a = 0; for(int i = 0; i < 100000; ++i) a += data->m_pPlane[0][i]; int64_t xe = avm_get_time_us();
2869 //printf("XF %d %lld %d %lld\n", a, xe - xs, b, ye - ys);
2870 CImage* ci = new CImage(&bi, plane, stride, false);
2871 ci->Convert(data);
2872 data->Release();
2873 image = ci;
2874
2875 return 0;
2876 }
2877 virtual int Sync()
2878 {
2879 if (m_vidix_play.num_frames > 1)
2880 {
2881 Locker locker(m_Mutex);
2882 if (image && image->GetAllocator() == this)
2883 {
2884 next_frame = (int)image->GetUserData() % m_uiImages;
2885 //printf("NEXT %d %d\n", next_frame, m_vidix_play.num_frames);
2886 }
2887 vdlPlaybackFrameSelect(m_vidix_handler, next_frame);
2888 next_frame = (next_frame + 1) % m_vidix_play.num_frames;
2889 }
2890 emutex.Lock();
2891 econd.Broadcast();
2892 emutex.Unlock();
2893 return 0;
2894 }
2895 virtual int Refresh()
2896 {
2897 Lock();
2898 int nx = 0, ny = 0;
2899 doGetPosition(nx, ny);
2900 //printf("XPO %d %d %d %d\n", nx, ny, x, y);
2901 if (x != nx || y != ny || screen->flags & SDL_FULLSCREEN)
2902 doResize(pic_w, pic_h);
2903 Unlock();
2904 return FullscreenRenderer::Refresh();
2905 }
2906 protected:
2907 virtual int doResize(int& new_w, int& new_h)
2908 {
2909 int i = FullscreenRenderer::doResize(new_w, new_h);
2910 if (reinit() != 0)
2911 return -1;
2912
2913 SDL_FillRect(screen, &cliprect, bgclr);
2914 SDL_UpdateRect(screen, cliprect.x, cliprect.x,
2915 cliprect.w, cliprect.h);
2916 return i;
2917 }
2918 int reinit()
2919 {
2920 //printf("FLAG %x %x\n", screen->flags & SDL_FULLSCREEN, screen->flags);
2921 if (screen->flags & SDL_FULLSCREEN)
2922 x = y = 0;
2923 else
2924 doGetPosition(x, y);
2925 //printf("POS X %d Y %d \n", x, y);
2926
2927 memset(&m_vidix_play, 0, sizeof(vidix_playback_t));
2928 m_vidix_play.fourcc = m_vidix_fourcc.fourcc;
2929 m_vidix_play.capability = m_vidix_cap.flags; /* every ;) */
2930 m_vidix_play.blend_factor = 0; /* for now */
2931
2932 m_vidix_play.src.x = (x < 0) ? 0 * -x : 0;
2933 m_vidix_play.src.y = (y < 0) ? 0 * -y : 0;
2934 m_vidix_play.src.w = (x < 0) ? m_w + 0*x : m_w;
2935 m_vidix_play.src.h = (y < 0) ? m_h + 0*y : m_h;
2936
2937 m_vidix_play.dest.x = (x < 0) ? 0 : x;
2938 m_vidix_play.dest.y = (y < 0) ? 0 : y;
2939 m_vidix_play.dest.w = (x < 0) ? pic_w + x : pic_w;
2940 m_vidix_play.dest.h = (y < 0) ? pic_h + y : pic_h;
2941 m_vidix_play.num_frames = 6;//vo_doublebuffering?NUM_FRAMES-1:1;
2942
2943 m_vidix_play.src.pitch.y =
2944 m_vidix_play.src.pitch.u =
2945 m_vidix_play.src.pitch.v = 0;
2946
2947 vidix_video_eq_t vinfo;
2948 vdlPlaybackGetEq(m_vidix_handler, &vinfo);
2949 vdlPlaybackOff(m_vidix_handler);
2950 if (vdlConfigPlayback(m_vidix_handler, &m_vidix_play) != 0)
2951 {
2952 AVM_WRITE(__MODULE__, "can't configure playback");
2953 return -1;
2954 }
2955
2956 vidix_mem = (uint8_t*) m_vidix_play.dga_addr;
2957
2958 // vdlPlaybackFrameSelect(vidix_handler,next_frame);
2959
2960 /* clear every frame with correct address and frame_size */
2961 for (unsigned i = 0; i < m_vidix_play.num_frames; ++i)
2962 memset(vidix_mem + m_vidix_play.offsets[i], 0x80,
2963 m_vidix_play.frame_size);
2964 vdlPlaybackOn(m_vidix_handler);
2965 vdlPlaybackSetEq(m_vidix_handler, &vinfo);
2966 //int err;
2967 //if ((err = vdlPlaybackOn(m_vidix_handler))!=0)
2968 // throw FATAL("can't start playback: %s\n", strerror(err));
2969 //printf("PITCH %d %d %d\n",m_vidix_play.dest.pitch.y,m_vidix_play.dest.pitch.u,m_vidix_play.dest.pitch.v);
2970 apitch = m_vidix_play.dest.pitch.y - 1;
2971 dstrides.y = (m_w + apitch) & ~apitch;
2972 dstrides.u = dstrides.v = 0;
2973
2974 switch(m_vidix_fourcc.fourcc)
2975 {
2976 case IMG_FMT_YV12:
2977 case IMG_FMT_I420:
2978 case IMG_FMT_IYUV:
2979 case IMG_FMT_YVU9:
2980 case IMG_FMT_IF09:
2981 case IMG_FMT_Y800:
2982 case IMG_FMT_Y8:
2983 dstrides.v = dstrides.u = dstrides.y;
2984 image_Bpp=1;
2985 break;
2986 case IMG_FMT_RGB32:
2987 case IMG_FMT_BGR32:
2988 dstrides.y *= 4;
2989 image_Bpp=4;
2990 break;
2991 case IMG_FMT_RGB24:
2992 case IMG_FMT_BGR24:
2993 dstrides.y *= 3;
2994 image_Bpp=3;
2995 break;
2996 default:
2997 dstrides.y *= 2;
2998 image_Bpp=2;
2999 break;
3000 }
3001
3002 switch(m_vidix_fourcc.fourcc)
3003 {
3004 case IMG_FMT_YVU9:
3005 case IMG_FMT_IF09:
3006 dstrides.u /= 4;
3007 dstrides.v /= 4;
3008 break;
3009 case IMG_FMT_I420:
3010 case IMG_FMT_IYUV:
3011 case IMG_FMT_YV12:
3012 dstrides.u /= 2;
3013 dstrides.v /= 2;
3014 break;
3015 }
3016
3017 //printf("strideB %d %d %d\n", dstrides.y, dstrides.u, dstrides.v);
3018 /* apitch = m_vidix_play.dest.pitch.y - 1;
3019 dstrides.y = (dstrides.y + apitch) & ~apitch;
3020 apitch = m_vidix_play.dest.pitch.v - 1;
3021 dstrides.v = (dstrides.v + apitch) & ~apitch;
3022 apitch = m_vidix_play.dest.pitch.u - 1;
3023 dstrides.u = (dstrides.u + apitch) & ~apitch;
3024 */
3025 //printf("strideC %d %d %d\n", dstrides.y, dstrides.u, dstrides.v);
3026 //printf("strideXX %d %d %d\n", m_vidix_play.dest.pitch.y, m_vidix_play.dest.pitch.v, m_vidix_play.dest.pitch.u);
3027 return 0;
3028 }
3029 };
3030 #endif /* HAVE_VIDIX */
3031
3032
3033 IVideoRenderer* CreateFullscreenRenderer(IPlayerWidget* pw, void* dpy,
3034 int width, int height, bool sub)
3035 {
3036
3037 FullscreenRenderer* fr = new FullscreenRenderer(pw, (Display*)dpy, width, height, sub);
3038 if (fr && (fr->init() == 0))
3039 return fr;
3040 delete fr;
3041 return 0;
3042 }
3043
3044 IVideoRenderer* CreateYUVRenderer(IPlayerWidget* pw, void* dpy,
3045 int width, int height,
3046 fourcc_t yuv_fmt, bool sub)
3047 {
3048 #ifdef HAVE_VIDIX
3049 if (geteuid() == 0) {
3050 // for root user try to use VIDIX
3051 VidixRenderer* vr = new VidixRenderer(pw, (Display*)dpy, width, height, yuv_fmt, sub);
3052 if (vr && (vr->init() == 0))
3053 return vr;
3054 delete vr;
3055 }
3056 #endif // HAVE_VIDIX
3057 #if _SDL_VER > 1103
3058 YUVRenderer* yr = new YUVRenderer(pw, (Display*)dpy, width, height, yuv_fmt, sub);
3059 if (yr && (yr->init() == 0))
3060 return yr;
3061 delete yr;
3062 #else
3063 AVM_WRITE(__RENDERER__, "SDL library too old - no XV support available\n");
3064 #endif // _SDL_VER
3065 return 0;
3066 }
3067
3068 IVideoRenderer* CreateVidixRenderer(IPlayerWidget* pw, void* dpy,
3069 int width, int height,
3070 fourcc_t yuv_fmt, bool sub)
3071 {
3072 #ifdef HAVE_VIDIX
3073 VidixRenderer* vr = new VidixRenderer(pw, (Display*)dpy, width, height, yuv_fmt, sub);
3074 if (vr && (vr->init() == 0))
3075 return vr;
3076 delete vr;
3077 #else // HAVE_VIDIX
3078 AVM_WRITE(__RENDERER__, "library compiled without VIDIX support - sorry no rendering\n");
3079 #endif // HAVE_VIDIX
3080 return 0;
3081 }
3082
3083 #else /* HAVE_LIBSDL */
3084
3085 static const char norend[] =
3086 "\n\n!!! libaviplay library has been compiled WITHOUT SDL support !!!\n"
3087 " sorry no rendering available - if you want to see VIDEO\n"
3088 " recompile library with SDL support - check why detection of SDL failed\n"
3089 " if you have problems send message to [email protected]\n\n";
3090
3091 IVideoRenderer* CreateFullscreenRenderer(IPlayerWidget* pw, void* dpy,
3092 int width, int height, bool sub)
3093 {
3094 AVM_WRITE(__RENDERER__, norend);
3095 return 0;
3096 }
3097
3098 IVideoRenderer* CreateYUVRenderer(IPlayerWidget* pw, void* dpy,
3099 int width, int height,
3100 fourcc_t yuv_fmt, bool sub)
3101 {
3102 AVM_WRITE(__RENDERER__, norend);
3103 return 0;
3104 }
3105
3106 #endif /* HAVE_LIBSDL */
3107
3108 #undef __MODULE__
3109
3110 AVM_END_NAMESPACE;
3111
3112 #endif /* X_DISPLAY_MISSING */