summaryrefslogtreecommitdiff
path: root/client/xvaccelrenderer.cc
blob: 208ea040a151866eb77115e7433c7b60bde3446a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
                          gui_xv.cpp  -  description
                             -------------------

	This part is strongly derivated from xine/mplayer/mpeg2dec

    begin                : Tue Jan 1 2002
    copyright            : (C) 2002 by mean
    email                : fixounet@free.fr
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
#include "xvaccelrenderer.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <QX11Info>

// Use this define for verbose output
#define VERBOSE_XV

//________________Wrapper around Xv_______________
XvAccelRender::XvAccelRender(QWidget *window, uint32_t w, uint32_t h)
{
	xvimage = NULL;

#ifdef VERBOSE_XV
	printf("Xv start\n");
#endif
	width = w;
	height = h;
  GUI_XvInit(window, w, h);
}

XvAccelRender::~XvAccelRender()
{
	GUI_XvEnd( );
#ifdef VERBOSE_XV
	printf("Xv end\n");
#endif
}

char *XvAccelRender::getDisplayData()
{
  if(xvimage) return xvimage->data;
  else return NULL;
}

uint8_t XvAccelRender::display(uint32_t w, uint32_t h)
{
	return GUI_XvDisplay(w, h);
}
//________________Wrapper around Xv_______________


//
//	Free all ressources allocated by xv
//


void XvAccelRender::GUI_XvEnd( void )
{
	ADM_assert(xv_port);
 	ADM_assert(xv_display);


#ifdef VERBOSE_XV
	printf("\n Releasing Xv Port\n");
#endif
	XLockDisplay (xv_display);
	if(XvUngrabPort(xv_display,xv_port,0)!=Success)
		printf("\n Trouble releasing port...\n");
	XUnlockDisplay (xv_display);


	xvimage=NULL;
	xv_display=NULL;
	xv_port=0;

}

//------------------------------------
uint8_t XvAccelRender::GUI_XvDisplay(uint32_t w, uint32_t h)
{

	if (xvimage)
		{

			// put image in shared segment

			// for YV12, 4 bits for Y 4 bits for u, 4 bits for v
			// total 1.5*

      //      memcpy(xvimage->data, src, (w*h*3)>>1);
      //memcpy(xvimage->data, src, (w*h*4)>>1); // We decodee directly to the buffer, so we don't need to copy it.

			XLockDisplay (xv_display);
			// And display it !
#if 1
			XvShmPutImage(xv_display, xv_port, xv_win, xv_gc, xvimage, 
										0, 0, w, h, // src
										//										0, 0, w / 4, h / 4, // dst
										0, 0, width, height, // dst
										False);
#else
			XvPutImage(xv_display, xv_port, xv_win, xv_gc, xvimage, 
								 0, 0, w, h,	// src
								 0, 0, w, h	// dst
								 );

#endif
			//XSetForeground (xv_display, xv_gc, 0);

			XSync(xv_display, False);
			XUnlockDisplay (xv_display);
			//GUI_XvExpose();

		}
	return 1;

}
/*
uint8_t XvAccelRender::GUI_XvSync(void)
{
	if(xv_display)
		XSync(xv_display, False);
	return 1;		
}
*/
//------------------------------------
//
//------------------------------------
uint8_t XvAccelRender::GUI_XvInit(QWidget * window, uint32_t w, uint32_t h)
{
	unsigned int ver, rel, req, ev, err;
	unsigned int port, adaptors;
	static XvAdaptorInfo *ai;
	static XvAdaptorInfo *curai;
   

	//    win = gtk_widget_get_parent_window(window);
	xv_display = window->x11Info().display();//GDK_WINDOW_XDISPLAY(win);
	//      xv_win= RootWindow(xv_display,0);
	xv_win = window->winId();//GDK_WINDOW_XWINDOW(GTK_WIDGET(window)->window);
#define WDN xv_display
	xv_port = 0;

	if (Success != XvQueryExtension(WDN, &ver, &rel, &req, &ev, &err))
		{
			printf("\n Query Extension failed\n");
			goto failed;
		}
	/* check for Xvideo support */
	if (Success != XvQueryAdaptors(WDN,
																 DefaultRootWindow(WDN), &adaptors, &ai))
		{
			printf("\n Query Adaptor failed\n");
			goto failed;
		}
	curai = ai;
	XvFormat *formats;
	// Dump infos
	port = 0;
	for (uint16_t i = 0; (!port) && (i < adaptors); i++)
		{
			/*
				XvPortID base_id;
				unsigned long num_ports;
				char type;
				char *name;
				unsigned long num_formats;
				XvFormat *formats;
				unsigned long num_adaptors;
			*/
#ifdef VERBOSE_XV
			printf("\n_______________________________\n");
			printf("\n Adaptator  : %d", i);
			printf("\n Base ID    : %ld", curai->base_id);
			printf("\n Nb Port    : %lu", curai->num_ports);
			printf("\n Type       : %d,", curai->type);

#define CHECK(x) if(curai->type & x) printf("|"#x);
			CHECK(XvInputMask);
			CHECK(XvOutputMask);
			CHECK(XvVideoMask);
			CHECK(XvStillMask);
			CHECK(XvImageMask);

			printf("\n Name       : %s", curai->name);
			printf("\n Num Adap   : %lu", curai->num_adaptors);
			printf("\n Num fmt    : %lu", curai->num_formats);
#endif	  
			formats = curai->formats;

			//
			uint16_t k;

			for (k = 0; (k < curai->num_ports) && !port; k++)
				{
					if (GUI_XvList(WDN, k + curai->base_id, &xv_format))
						port = k + curai->base_id + 0; // FIXME: TODO: HACK: It works when +1 is applied! But why!?
				}


			curai++;
		}
	//
	if (!port)
		{
			printf("\n no port found");
			goto failed;
		}
#ifdef 	COLORSPACE_YV12
#ifdef VERBOSE_XV
	printf("\n Xv YV12 found at port :%d, format : %d", port, xv_format);
#endif
#else
#ifdef VERBOSE_XV
	printf("\n Xv YUY2 found at port :%d, format : %d", port, xv_format);
#endif
#endif

	if (Success != XvGrabPort(WDN, port, 0))
		goto failed;
	{
	
		xv_port = port;
		/*
			Display *display,
			XvPortID port,
			int id,
			char* data,
			int width,
			int height,
			XShmSegmentInfo *shminfo

		*/
		xvimage = XvShmCreateImage(WDN, xv_port,
															 xv_format, 0, w, h, &Shminfo);

		Shminfo.shmid = shmget(IPC_PRIVATE, xvimage->data_size,
													 IPC_CREAT | 0777);
		Shminfo.shmaddr = (char *) shmat(Shminfo.shmid, 0, 0);
		Shminfo.readOnly = False;
		xvimage->data = Shminfo.shmaddr;
		XShmAttach(WDN, &Shminfo);
		XSync(WDN, False);
		shmctl(Shminfo.shmid, IPC_RMID, 0);
		memset(xvimage->data, 0, xvimage->data_size);

		xv_xgc.graphics_exposures = False;

		xv_gc = XCreateGC(xv_display, xv_win, 0L, &xv_xgc);
	
		//ADM_assert(BadWindow!=XSelectInput(xv_display, xv_win, ExposureMask | VisibilityChangeMask));

	}
#ifdef VERBOSE_XV
	printf("\n Xv init succeedeed\n");
#endif
	return 1;
 failed:
	printf("\n Xv init failed..\n");
	return 0;
}

// _________________________________________________
//
// _________________________________________________
uint8_t XvAccelRender::GUI_XvList(Display * dis, uint32_t port, uint32_t * fmt)
{
	XvImageFormatValues *formatValues;
	int imgfmt;
	int k, f = 0;

	formatValues = XvListImageFormats(dis, port, &imgfmt);
	if (formatValues)
		for (k = 0; !f || (k < imgfmt); k++)
			{
#ifdef VERVOSE_XV
	      printf("\n %lx %d --> %s", port, formatValues[k].id,
							 formatValues[k].guid);
#endif
#ifdef 	COLORSPACE_YV12
	      if (!strcmp(formatValues[k].guid, "YV12"))
#else
					if (!strcmp(formatValues[k].guid, "YUY2"))
#endif
						{
							f = 1;
							*fmt = formatValues[k].id;
						}
			} else
				f = 0;
	XFree(formatValues);
	return f;
}