|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the Qt Mobility Components. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include <QtGui/qx11info_x11.h> |
|
43 #include <QtCore/qdebug.h> |
|
44 #include <QtCore/qvariant.h> |
|
45 #include <qvideosurfaceformat.h> |
|
46 |
|
47 #include "qxvideosurface_maemo5_p.h" |
|
48 |
|
49 struct XvFormatRgb |
|
50 { |
|
51 QVideoFrame::PixelFormat pixelFormat; |
|
52 int bits_per_pixel; |
|
53 int format; |
|
54 int num_planes; |
|
55 |
|
56 int depth; |
|
57 unsigned int red_mask; |
|
58 unsigned int green_mask; |
|
59 unsigned int blue_mask; |
|
60 |
|
61 }; |
|
62 |
|
63 bool operator ==(const XvImageFormatValues &format, const XvFormatRgb &rgb) |
|
64 { |
|
65 return format.type == XvRGB |
|
66 && format.bits_per_pixel == rgb.bits_per_pixel |
|
67 && format.format == rgb.format |
|
68 && format.num_planes == rgb.num_planes |
|
69 && format.depth == rgb.depth |
|
70 && format.red_mask == rgb.red_mask |
|
71 && format.blue_mask == rgb.blue_mask; |
|
72 } |
|
73 |
|
74 static const XvFormatRgb qt_xvRgbLookup[] = |
|
75 { |
|
76 { QVideoFrame::Format_ARGB32, 32, XvPacked, 1, 32, 0x00FF0000, 0x0000FF00, 0x000000FF }, |
|
77 { QVideoFrame::Format_RGB32 , 32, XvPacked, 1, 24, 0x00FF0000, 0x0000FF00, 0x000000FF }, |
|
78 { QVideoFrame::Format_RGB24 , 24, XvPacked, 1, 24, 0x00FF0000, 0x0000FF00, 0x000000FF }, |
|
79 { QVideoFrame::Format_RGB565, 16, XvPacked, 1, 16, 0x0000F800, 0x000007E0, 0x0000001F }, |
|
80 { QVideoFrame::Format_BGRA32, 32, XvPacked, 1, 32, 0xFF000000, 0x00FF0000, 0x0000FF00 }, |
|
81 { QVideoFrame::Format_BGR32 , 32, XvPacked, 1, 24, 0x00FF0000, 0x0000FF00, 0x000000FF }, |
|
82 { QVideoFrame::Format_BGR24 , 24, XvPacked, 1, 24, 0x00FF0000, 0x0000FF00, 0x000000FF }, |
|
83 { QVideoFrame::Format_BGR565, 16, XvPacked, 1, 16, 0x0000F800, 0x000007E0, 0x0000001F } |
|
84 }; |
|
85 |
|
86 struct XvFormatYuv |
|
87 { |
|
88 QVideoFrame::PixelFormat pixelFormat; |
|
89 int bits_per_pixel; |
|
90 int format; |
|
91 int num_planes; |
|
92 |
|
93 unsigned int y_sample_bits; |
|
94 unsigned int u_sample_bits; |
|
95 unsigned int v_sample_bits; |
|
96 unsigned int horz_y_period; |
|
97 unsigned int horz_u_period; |
|
98 unsigned int horz_v_period; |
|
99 unsigned int vert_y_period; |
|
100 unsigned int vert_u_period; |
|
101 unsigned int vert_v_period; |
|
102 char component_order[32]; |
|
103 }; |
|
104 |
|
105 bool operator ==(const XvImageFormatValues &format, const XvFormatYuv &yuv) |
|
106 { |
|
107 return format.type == XvYUV |
|
108 && format.bits_per_pixel == yuv.bits_per_pixel |
|
109 && format.format == yuv.format |
|
110 && format.num_planes == yuv.num_planes |
|
111 && format.y_sample_bits == yuv.y_sample_bits |
|
112 && format.u_sample_bits == yuv.u_sample_bits |
|
113 && format.v_sample_bits == yuv.v_sample_bits |
|
114 && format.horz_y_period == yuv.horz_y_period |
|
115 && format.horz_u_period == yuv.horz_u_period |
|
116 && format.horz_v_period == yuv.horz_v_period |
|
117 && format.horz_y_period == yuv.vert_y_period |
|
118 && format.vert_u_period == yuv.vert_u_period |
|
119 && format.vert_v_period == yuv.vert_v_period |
|
120 && qstrncmp(format.component_order, yuv.component_order, 32) == 0; |
|
121 } |
|
122 |
|
123 static const XvFormatYuv qt_xvYuvLookup[] = |
|
124 { |
|
125 { QVideoFrame::Format_YUV444 , 24, XvPacked, 1, 8, 8, 8, 1, 1, 1, 1, 1, 1, "YUV" }, |
|
126 { QVideoFrame::Format_YUV420P, 12, XvPlanar, 3, 8, 8, 8, 1, 2, 2, 1, 2, 2, "YUV" }, |
|
127 { QVideoFrame::Format_YV12 , 12, XvPlanar, 3, 8, 8, 8, 1, 2, 2, 1, 2, 2, "YVU" }, |
|
128 { QVideoFrame::Format_UYVY , 16, XvPacked, 1, 8, 8, 8, 1, 2, 2, 1, 1, 1, "UYVY" }, |
|
129 { QVideoFrame::Format_YUYV , 16, XvPacked, 1, 8, 8, 8, 1, 2, 2, 1, 1, 1, "YUY2" }, |
|
130 { QVideoFrame::Format_YUYV , 16, XvPacked, 1, 8, 8, 8, 1, 2, 2, 1, 1, 1, "YUYV" }, |
|
131 { QVideoFrame::Format_NV12 , 12, XvPlanar, 2, 8, 8, 8, 1, 2, 2, 1, 2, 2, "YUV" }, |
|
132 { QVideoFrame::Format_NV12 , 12, XvPlanar, 2, 8, 8, 8, 1, 2, 2, 1, 2, 2, "YVU" }, |
|
133 { QVideoFrame::Format_Y8 , 8 , XvPlanar, 1, 8, 0, 0, 1, 0, 0, 1, 0, 0, "Y" } |
|
134 }; |
|
135 |
|
136 QXVideoSurface::QXVideoSurface(QObject *parent) |
|
137 : QAbstractVideoSurface(parent) |
|
138 , m_winId(0) |
|
139 , m_portId(0) |
|
140 , m_gc(0) |
|
141 , m_image(0) |
|
142 , m_colorKey(16,7,2) |
|
143 { |
|
144 } |
|
145 |
|
146 QXVideoSurface::~QXVideoSurface() |
|
147 { |
|
148 if (m_gc) |
|
149 XFreeGC(QX11Info::display(), m_gc); |
|
150 |
|
151 if (m_portId != 0) |
|
152 XvUngrabPort(QX11Info::display(), m_portId, 0); |
|
153 } |
|
154 |
|
155 WId QXVideoSurface::winId() const |
|
156 { |
|
157 return m_winId; |
|
158 } |
|
159 |
|
160 void QXVideoSurface::setWinId(WId id) |
|
161 { |
|
162 if (id == m_winId) |
|
163 return; |
|
164 |
|
165 if (m_image) |
|
166 XFree(m_image); |
|
167 |
|
168 if (m_gc) { |
|
169 XFreeGC(QX11Info::display(), m_gc); |
|
170 m_gc = 0; |
|
171 } |
|
172 |
|
173 if (m_portId != 0) |
|
174 XvUngrabPort(QX11Info::display(), m_portId, 0); |
|
175 |
|
176 m_supportedPixelFormats.clear(); |
|
177 m_formatIds.clear(); |
|
178 |
|
179 m_winId = id; |
|
180 |
|
181 if (m_winId && findPort()) { |
|
182 querySupportedFormats(); |
|
183 |
|
184 m_gc = XCreateGC(QX11Info::display(), m_winId, 0, 0); |
|
185 |
|
186 if (m_image) { |
|
187 m_image = 0; |
|
188 |
|
189 if (!start(surfaceFormat())) |
|
190 QAbstractVideoSurface::stop(); |
|
191 } |
|
192 } else if (m_image) { |
|
193 m_image = 0; |
|
194 |
|
195 QAbstractVideoSurface::stop(); |
|
196 } |
|
197 |
|
198 emit supportedFormatsChanged(); |
|
199 } |
|
200 |
|
201 QRect QXVideoSurface::displayRect() const |
|
202 { |
|
203 return m_displayRect; |
|
204 } |
|
205 |
|
206 void QXVideoSurface::setDisplayRect(const QRect &rect) |
|
207 { |
|
208 m_displayRect = rect; |
|
209 } |
|
210 |
|
211 QColor QXVideoSurface::colorKey() const |
|
212 { |
|
213 return m_colorKey; |
|
214 } |
|
215 |
|
216 void QXVideoSurface::setColorKey(QColor key) |
|
217 { |
|
218 m_colorKey = key; |
|
219 } |
|
220 |
|
221 int QXVideoSurface::getAttribute(const char *attribute) const |
|
222 { |
|
223 if (m_portId != 0) { |
|
224 Display *display = QX11Info::display(); |
|
225 |
|
226 Atom atom = XInternAtom(display, attribute, True); |
|
227 |
|
228 int value = 0; |
|
229 |
|
230 XvGetPortAttribute(display, m_portId, atom, &value); |
|
231 |
|
232 return value; |
|
233 } else { |
|
234 return 0; |
|
235 } |
|
236 } |
|
237 |
|
238 void QXVideoSurface::setAttribute(const char *attribute, int value) |
|
239 { |
|
240 if (m_portId != 0) { |
|
241 Display *display = QX11Info::display(); |
|
242 |
|
243 Atom atom = XInternAtom(display, attribute, True); |
|
244 |
|
245 XvSetPortAttribute(display, m_portId, atom, value); |
|
246 } |
|
247 } |
|
248 |
|
249 QList<QVideoFrame::PixelFormat> QXVideoSurface::supportedPixelFormats( |
|
250 QAbstractVideoBuffer::HandleType handleType) const |
|
251 { |
|
252 if ( handleType == QAbstractVideoBuffer::NoHandle || |
|
253 handleType == QAbstractVideoBuffer::XvShmImageHandle ) |
|
254 return m_supportedPixelFormats; |
|
255 else |
|
256 return QList<QVideoFrame::PixelFormat>(); |
|
257 } |
|
258 |
|
259 bool QXVideoSurface::start(const QVideoSurfaceFormat &format) |
|
260 { |
|
261 //qDebug() << "QXVideoSurface::start" << format; |
|
262 |
|
263 m_lastFrame = QVideoFrame(); |
|
264 |
|
265 if (m_image) |
|
266 XFree(m_image); |
|
267 |
|
268 m_xvFormatId = 0; |
|
269 for (int i = 0; i < m_supportedPixelFormats.count(); ++i) { |
|
270 if (m_supportedPixelFormats.at(i) == format.pixelFormat()) { |
|
271 m_xvFormatId = m_formatIds.at(i); |
|
272 break; |
|
273 } |
|
274 } |
|
275 |
|
276 if (m_xvFormatId == 0) { |
|
277 setError(UnsupportedFormatError); |
|
278 } else { |
|
279 XvImage *image = XvShmCreateImage( |
|
280 QX11Info::display(), |
|
281 m_portId, |
|
282 m_xvFormatId, |
|
283 0, |
|
284 format.frameWidth(), |
|
285 format.frameHeight(), |
|
286 &m_shminfo |
|
287 ); |
|
288 |
|
289 if (!image) { |
|
290 setError(ResourceError); |
|
291 return false; |
|
292 } |
|
293 |
|
294 m_shminfo.shmid = shmget(IPC_PRIVATE, image->data_size, IPC_CREAT | 0777); |
|
295 m_shminfo.shmaddr = image->data = (char*)shmat(m_shminfo.shmid, 0, 0); |
|
296 m_shminfo.readOnly = False; |
|
297 |
|
298 if (!XShmAttach(QX11Info::display(), &m_shminfo)) { |
|
299 //qDebug() << "XShmAttach failed"; |
|
300 return false; |
|
301 } |
|
302 |
|
303 if (!image) { |
|
304 setError(ResourceError); |
|
305 } else { |
|
306 m_viewport = format.viewport(); |
|
307 m_image = image; |
|
308 |
|
309 quint32 c = m_colorKey.rgb(); |
|
310 quint16 colorKey16 = ((c >> 3) & 0x001f) |
|
311 | ((c >> 5) & 0x07e0) |
|
312 | ((c >> 8) & 0xf800); |
|
313 |
|
314 setAttribute("XV_AUTOPAINT_COLORKEY", 0); |
|
315 setAttribute("XV_COLORKEY", colorKey16); |
|
316 setAttribute("XV_OMAP_VSYNC", 1); |
|
317 setAttribute("XV_DOUBLE_BUFFER", 0); |
|
318 |
|
319 QVideoSurfaceFormat newFormat = format; |
|
320 newFormat.setProperty("portId", QVariant(quint64(m_portId))); |
|
321 newFormat.setProperty("xvFormatId", m_xvFormatId); |
|
322 newFormat.setProperty("dataSize", image->data_size); |
|
323 |
|
324 return QAbstractVideoSurface::start(newFormat); |
|
325 } |
|
326 } |
|
327 |
|
328 if (m_image) { |
|
329 m_image = 0; |
|
330 |
|
331 QAbstractVideoSurface::stop(); |
|
332 } |
|
333 |
|
334 return false; |
|
335 } |
|
336 |
|
337 void QXVideoSurface::stop() |
|
338 { |
|
339 if (m_image) { |
|
340 XFree(m_image); |
|
341 m_image = 0; |
|
342 m_lastFrame = QVideoFrame(); |
|
343 |
|
344 QAbstractVideoSurface::stop(); |
|
345 } |
|
346 } |
|
347 |
|
348 bool QXVideoSurface::present(const QVideoFrame &frame) |
|
349 { |
|
350 if (!m_image) { |
|
351 setError(StoppedError); |
|
352 return false; |
|
353 } else if (m_image->width != frame.width() || m_image->height != frame.height()) { |
|
354 setError(IncorrectFormatError); |
|
355 return false; |
|
356 } else { |
|
357 m_lastFrame = frame; |
|
358 |
|
359 if (!m_lastFrame.map(QAbstractVideoBuffer::ReadOnly)) { |
|
360 qWarning() << "Failed to map video frame"; |
|
361 setError(IncorrectFormatError); |
|
362 return false; |
|
363 } else { |
|
364 bool presented = false; |
|
365 |
|
366 if (frame.handleType() != QAbstractVideoBuffer::XvShmImageHandle && |
|
367 m_image->data_size > m_lastFrame.mappedBytes()) { |
|
368 qWarning("Insufficient frame buffer size"); |
|
369 setError(IncorrectFormatError); |
|
370 } else if (frame.handleType() != QAbstractVideoBuffer::XvShmImageHandle && |
|
371 m_image->num_planes > 0 && |
|
372 m_image->pitches[0] != m_lastFrame.bytesPerLine()) { |
|
373 qWarning("Incompatible frame pitches"); |
|
374 setError(IncorrectFormatError); |
|
375 } else { |
|
376 XvImage *img = 0; |
|
377 |
|
378 if (frame.handleType() == QAbstractVideoBuffer::XvShmImageHandle) { |
|
379 img = frame.handle().value<XvImage*>(); |
|
380 } else { |
|
381 img = m_image; |
|
382 memcpy(m_image->data, m_lastFrame.bits(), qMin(m_lastFrame.mappedBytes(), m_image->data_size)); |
|
383 } |
|
384 |
|
385 if (img) |
|
386 XvShmPutImage( |
|
387 QX11Info::display(), |
|
388 m_portId, |
|
389 m_winId, |
|
390 m_gc, |
|
391 img, |
|
392 m_viewport.x(), |
|
393 m_viewport.y(), |
|
394 m_viewport.width(), |
|
395 m_viewport.height(), |
|
396 m_displayRect.x(), |
|
397 m_displayRect.y(), |
|
398 m_displayRect.width(), |
|
399 m_displayRect.height(), |
|
400 false); |
|
401 |
|
402 presented = true; |
|
403 } |
|
404 |
|
405 m_lastFrame.unmap(); |
|
406 |
|
407 return presented; |
|
408 } |
|
409 } |
|
410 } |
|
411 |
|
412 void QXVideoSurface::repaintLastFrame() |
|
413 { |
|
414 if (m_lastFrame.isValid()) |
|
415 present(QVideoFrame(m_lastFrame)); |
|
416 } |
|
417 |
|
418 bool QXVideoSurface::findPort() |
|
419 { |
|
420 unsigned int count = 0; |
|
421 XvAdaptorInfo *adaptors = 0; |
|
422 bool portFound = false; |
|
423 |
|
424 if (XvQueryAdaptors(QX11Info::display(), m_winId, &count, &adaptors) == Success) { |
|
425 for (unsigned int i = 0; i < count && !portFound; ++i) { |
|
426 if (adaptors[i].type & XvImageMask) { |
|
427 m_portId = adaptors[i].base_id; |
|
428 |
|
429 for (unsigned int j = 0; j < adaptors[i].num_ports && !portFound; ++j, ++m_portId) |
|
430 portFound = XvGrabPort(QX11Info::display(), m_portId, 0) == Success; |
|
431 } |
|
432 } |
|
433 XvFreeAdaptorInfo(adaptors); |
|
434 } |
|
435 |
|
436 return portFound; |
|
437 } |
|
438 |
|
439 void QXVideoSurface::querySupportedFormats() |
|
440 { |
|
441 int count = 0; |
|
442 if (XvImageFormatValues *imageFormats = XvListImageFormats( |
|
443 QX11Info::display(), m_portId, &count)) { |
|
444 const int rgbCount = sizeof(qt_xvRgbLookup) / sizeof(XvFormatRgb); |
|
445 const int yuvCount = sizeof(qt_xvYuvLookup) / sizeof(XvFormatYuv); |
|
446 |
|
447 for (int i = 0; i < count; ++i) { |
|
448 switch (imageFormats[i].type) { |
|
449 case XvRGB: |
|
450 for (int j = 0; j < rgbCount; ++j) { |
|
451 if (imageFormats[i] == qt_xvRgbLookup[j]) { |
|
452 m_supportedPixelFormats.append(qt_xvRgbLookup[j].pixelFormat); |
|
453 m_formatIds.append(imageFormats[i].id); |
|
454 break; |
|
455 } |
|
456 } |
|
457 break; |
|
458 case XvYUV: |
|
459 for (int j = 0; j < yuvCount; ++j) { |
|
460 //skip YUV420P and YV12 formats, they don't work correctly and slow, |
|
461 //YUV2 == YUYV is just slow |
|
462 if (imageFormats[i] == qt_xvYuvLookup[j] && |
|
463 qt_xvYuvLookup[j].pixelFormat != QVideoFrame::Format_YUV420P && |
|
464 qt_xvYuvLookup[j].pixelFormat != QVideoFrame::Format_YV12) { |
|
465 m_supportedPixelFormats.append(qt_xvYuvLookup[j].pixelFormat); |
|
466 m_formatIds.append(imageFormats[i].id); |
|
467 break; |
|
468 } |
|
469 } |
|
470 break; |
|
471 } |
|
472 } |
|
473 XFree(imageFormats); |
|
474 } |
|
475 } |