]> git.vomp.tv Git - vompclient-marten.git/blob - surfacemvp.cc
Portability
[vompclient-marten.git] / surfacemvp.cc
1 /*
2     Copyright 2004-2005 Chris Tallon
3     Portions copyright 2004 Jon Gettler
4
5     This file is part of VOMP.
6
7     VOMP is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11
12     VOMP is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License
18     along with VOMP; if not, write to the Free Software
19     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20 */
21
22 #include "surfacemvp.h"
23 #include "osd.h"
24
25 SurfaceMVP::SurfaceMVP(int id)
26 {
27   if (id == SCREEN) screen = this;
28
29   fdOsd = Osd::getInstance()->getFD();
30   memset(&surface, 0, sizeof(osd_surface_t));
31   memset(&surface.sfc, 0, sizeof(stbgfx_sfc_t));
32   memset(&surface.map, 0, sizeof(stbgfx_map_t));
33 }
34
35 SurfaceMVP::~SurfaceMVP()
36 {
37   for (int i = 0; i < 3; i++)
38   {
39     if (surface.base[i]) munmap(surface.base[i], surface.map.map[i].size);
40   }
41
42   int a;
43
44   a = ioctl(fdOsd, GFX_FB_SFC_UNMAP, surface.sfc.handle);
45   if (a) Log::getInstance()->log("Surface", Log::ERR, "Surface unmap failed");
46
47   a = ioctl(fdOsd, GFX_FB_SFC_FREE, surface.sfc.handle);
48   if (a) Log::getInstance()->log("Surface", Log::ERR, "Surface destroy failed");
49
50 }
51
52 int SurfaceMVP::create(UINT width, UINT height)
53 {
54   int r = 0;
55   int displayNumber = 0; // mvpmc iterates 0->15 till it gets one that works?
56
57   surface.sfc.width = width;
58   surface.sfc.height = height;
59   surface.sfc.flags = 0x3f1533;
60
61   surface.sfc.unknown = -1;
62
63 //  r = ioctl(fdOsd, GFX_FB_SET_OSD, &displayNumber);
64 //  if (r) return 0;
65
66   r = ioctl(fdOsd, GFX_FB_OSD_SURFACE, &displayNumber);
67   if (r) return 0;
68
69   r = ioctl(fdOsd, GFX_FB_SFC_ALLOC, &surface.sfc);
70   if (r) return 0;
71
72   surface.map.map[0].unknown = surface.sfc.handle;
73   r = ioctl(fdOsd, GFX_FB_MAP, &surface.map);
74   if (r) return 0;
75
76   surface.base[0] = (unsigned char *)mmap(NULL, surface.map.map[0].size, PROT_READ|PROT_WRITE, MAP_SHARED, fdOsd, surface.map.map[0].addr);
77   if (surface.base[0] == MAP_FAILED) return 0;
78
79   surface.base[1] = (unsigned char *)mmap(NULL, surface.map.map[1].size, PROT_READ|PROT_WRITE, MAP_SHARED, fdOsd, surface.map.map[1].addr);
80   if (surface.base[1] == MAP_FAILED) return 0;
81
82   surface.base[2] = (unsigned char *)mmap(NULL, surface.map.map[2].size, PROT_READ|PROT_WRITE, MAP_SHARED, fdOsd, surface.map.map[2].addr);
83   if (surface.base[2] == MAP_FAILED) return 0;
84
85 //  surface.display.num = displayNumber;
86 //  r = ioctl(fdOsd, GFX_FB_MOVE_DISPLAY, &surface.display);
87 //  if (r) return 0;
88
89 //  r = ioctl(fdOsd, GFX_FB_SET_DISPLAY, &surface.display);
90 //  if (r) return 0;
91
92   return 1;
93 }
94
95 void SurfaceMVP::display()
96 {
97   unsigned long fb_descriptor[2];
98
99   fb_descriptor[0] = surface.sfc.handle;
100   fb_descriptor[1] = 1;
101
102   ioctl(fdOsd, GFX_FB_ATTACH, fb_descriptor);
103 }
104
105 unsigned long SurfaceMVP::getSurfaceHandle()
106 {
107   return surface.sfc.handle;
108 }
109
110 // ----------------------------------------------------------------------------
111
112 // Now for the drawing functions
113
114 /*
115  * RGB to YUV conversion tables
116  */
117 int SurfaceMVP::conv_YB[256];
118 int SurfaceMVP::conv_YG[256];
119 int SurfaceMVP::conv_YR[256];
120 int SurfaceMVP::conv_UB[256];
121 int SurfaceMVP::conv_UG[256];
122 int SurfaceMVP::conv_UR[256];
123 int SurfaceMVP::conv_VB[256];
124 int SurfaceMVP::conv_VG[256];
125 int SurfaceMVP::conv_VR[256];
126
127 int SurfaceMVP::conv_BY[256];
128 int SurfaceMVP::conv_GY[256];
129 int SurfaceMVP::conv_RY[256];
130 int SurfaceMVP::conv_BU[256];
131 int SurfaceMVP::conv_GU[256];
132 int SurfaceMVP::conv_RU[256];
133 int SurfaceMVP::conv_BV[256];
134 int SurfaceMVP::conv_GV[256];
135 int SurfaceMVP::conv_RV[256];
136
137 /*
138  * gfx_init() - initialize the RGB to YUV conversion tables
139  */
140 void SurfaceMVP::initConversionTables(void)
141 {
142   int i;
143
144   for (i=0; i<256; i++) {
145     conv_YB[i] = (int)(0.299 * (double)i);
146     conv_BY[i] = i;
147   }
148   for (i=0; i<256; i++) {
149     conv_YG[i] = (int)(0.587 * (double)i);
150     conv_GY[i] = i;
151   }
152   for (i=0; i<256; i++) {
153     conv_YR[i] = (int)(0.114 * (double)i);
154     conv_RY[i] = i;
155   }
156
157   for (i=0; i<256; i++) {
158     conv_UB[i] = (int)(0.5 * (double)i);
159     conv_BU[i] = (int)(1.732 * (i - 128));
160   }
161   for (i=0; i<256; i++) {
162     conv_UG[i] = (int)(-0.33126 * (double)i);
163     conv_GU[i] = (int)(-0.338 * (i - 128));
164   }
165   for (i=0; i<256; i++) {
166     conv_UR[i] = (int)(-0.16874 * (double)i);
167     conv_RU[i] = 0;
168   }
169
170   for (i=0; i<256; i++) {
171     conv_VB[i] = (int)(-0.08131 * (double)i);
172     conv_BV[i] = 0;
173   }
174   for (i=0; i<256; i++) {
175     conv_VG[i] = (int)(-0.41869 * (double)i);
176     conv_GV[i] = (int)(-0.698 * (i - 128));
177   }
178   for (i=0; i<256; i++) {
179     conv_VR[i] = (int)(0.5 * (double)i);
180     conv_RV[i] = (int)(1.370 * ((double)i - 128));
181   }
182 }
183
184 /*
185  * rgb2yuv() - convert an RGB pixel to YUV
186  */
187  //inline me?
188 void SurfaceMVP::rgb2yuv(unsigned char r, unsigned char g, unsigned char b, unsigned char *y, unsigned char *u, unsigned char *v)
189 {
190   int Y, U, V;
191
192   Y = conv_YB[b] + conv_YG[g] + conv_YR[r];
193   U = conv_UB[b] + conv_UG[g] + conv_UR[r] + 128;
194   V = conv_VB[b] + conv_VG[g] + conv_VR[r] + 128;
195
196   if (Y > 255)
197     Y = 255;
198   else if (Y < 0)
199     Y = 0;
200   if (U > 255)
201     U = 255;
202   else if (U < 0)
203     U = 0;
204   if (V > 255)
205     V = 255;
206   else if (V < 0)
207     V = 0;
208
209   *y = Y;
210   *u = U;
211   *v = V;
212 }
213
214 int SurfaceMVP::fillblt(int x, int y, int width, int height, unsigned int c)
215 {
216   osd_fillblt_t fblt;
217
218   fblt.handle = surface.sfc.handle;
219   fblt.x = x;
220   fblt.y = y;
221   fblt.width = width;
222   fblt.height = height;
223   fblt.colour = c;
224
225   return ioctl(fdOsd, GFX_FB_OSD_FILLBLT, &fblt);
226 }
227
228 void SurfaceMVP::drawPixel(int x, int y, unsigned int c)
229 {
230   int offset;
231   unsigned char r, g, b, a, Y, U, V;
232   unsigned int line, remainder;
233
234   if ((x >= (int)surface.sfc.width) || (y >= (int)surface.sfc.height))
235     return;
236
237   c2rgba(c, &r, &g, &b, &a);
238
239   rgb2yuv(r, g, b, &Y, &U, &V);
240
241   remainder = (surface.sfc.width % 4);
242   if (remainder == 0)
243     line = surface.sfc.width;
244   else
245     line = surface.sfc.width + (4 - remainder);
246
247   offset = (y * line) + x;
248
249   *(surface.base[0] + offset) = Y;
250   *(surface.base[1] + (offset & 0xfffffffe)) = U;
251   *(surface.base[1] + (offset & 0xfffffffe) + 1) = V;
252   *(surface.base[2] + offset) = a;
253
254 }
255
256 void SurfaceMVP::drawHorzLine(int x1, int x2, int y, unsigned int c)
257 {
258   fillblt(x1, y, x2-x1, 1, c);
259 }
260
261 void SurfaceMVP::drawVertLine(int x, int y1, int y2, unsigned int c)
262 {
263   fillblt(x, y1, 1, y2-y1, c);
264 }
265
266
267   /* surface update to screen needs:
268   source x distance into this surface
269   source y distance into this surface
270   width of update
271   height of update
272   destination x on screen
273   destination y on screen
274   */
275 int SurfaceMVP::updateToScreen(int sx, int sy, int w, int h, int dx, int dy) // FIXME new, replace others with this FIXME
276 {
277   return blt(fdOsd, surface.sfc.handle, sx, sy, w, h, ((SurfaceMVP*)screen)->getSurfaceHandle(), dx, dy);
278 }
279
280 int SurfaceMVP::blt(int fd, unsigned long shandle, int sx, int sy, int width, int height, unsigned long dhandle, int dx, int dy)
281 {
282   osd_bitblt_t fblt;
283   memset(&fblt, 0, sizeof(fblt));
284
285   fblt.dst_handle = dhandle;
286   fblt.dst_x = dx;
287   fblt.dst_y = dy;
288
289   fblt.src_handle = shandle;
290   fblt.src_x = sx;
291   fblt.src_y = sy;
292
293   fblt.width = width;
294   fblt.height = height;
295
296   fblt.u1 = 1;
297   fblt.u2 = 0;
298   fblt.u3 = 0x0f;
299
300   return ioctl(fd, GFX_FB_OSD_BITBLT, &fblt);
301 }
302
303 void SurfaceMVP::screenShot(char* fileName)
304 {
305   Log* logger = Log::getInstance();
306
307   FILE* outfile = fopen(fileName, "w");
308   if (outfile == NULL)
309   {
310     logger->log("Surface", Log::ERR, "Can't open JPEG");
311     return;
312   }
313   logger->log("Surface", Log::DEBUG, "File opened %u %u", surface.sfc.height, surface.sfc.width);
314
315   struct jpeg_compress_struct cinfo;
316   struct jpeg_error_mgr jerr;
317   cinfo.err = jpeg_std_error(&jerr);
318   jpeg_create_compress(&cinfo);
319   jpeg_stdio_dest(&cinfo, outfile);
320   cinfo.image_width = surface.sfc.width;
321   cinfo.image_height = surface.sfc.height;
322   cinfo.input_components = 3;
323   cinfo.in_color_space = JCS_RGB;
324   jpeg_set_defaults(&cinfo);
325   jpeg_start_compress(&cinfo, TRUE);
326
327
328   unsigned char row[surface.sfc.width * 3];
329   unsigned char* prow = (unsigned char*)&row;
330   unsigned char r, g, b;
331
332   while (cinfo.next_scanline < cinfo.image_height)
333   {
334     for(unsigned int i = 0; i < surface.sfc.width; i++)
335     {
336       readPixel(i, cinfo.next_scanline, &r, &g, &b);
337       row[i * 3] = r;
338       row[(i * 3) + 1] = g;
339       row[(i * 3) + 2] = b;
340     }
341     jpeg_write_scanlines(&cinfo, (JSAMPLE **)&prow, 1);
342   }
343
344   jpeg_finish_compress(&cinfo);
345   jpeg_destroy_compress(&cinfo);
346   fclose(outfile);
347   logger->log("Surface", Log::DEBUG, "Jpeg saved");
348 }
349
350 void SurfaceMVP::readPixel(int x, int y, unsigned char* r, unsigned char* g, unsigned char* b)
351 {
352   int offset;
353   unsigned char a, Y, U, V;
354   unsigned int line, remainder;
355
356   if (((unsigned int)x >= surface.sfc.width) || ((unsigned int)y >= surface.sfc.height)) return;
357
358   remainder = (surface.sfc.width % 4);
359   if (remainder == 0)
360     line = surface.sfc.width;
361   else
362     line = surface.sfc.width + (4 - remainder);
363
364   offset = (y * line) + x;
365
366   Y = *(surface.base[0] + offset);
367   U = *(surface.base[1] + (offset & 0xfffffffe));
368   V = *(surface.base[1] + (offset & 0xfffffffe) + 1);
369   a = *(surface.base[2] + offset);
370
371   yuv2rgb(Y, U, V, r, g, b);
372 }
373
374 void SurfaceMVP::yuv2rgb(int y, int u, int v, unsigned char* pr, unsigned char* pg, unsigned char* pb)
375 {
376    // from http://www.fourcc.org/index.php?http%3A//www.fourcc.org/fccyvrgb.php
377
378 //   unsigned int pixel32;
379 //   unsigned char *pixel = (unsigned char *)&pixel32;
380    int r, g, b;
381
382
383    /*
384      One formula I found:  (not the right one)
385
386      R = 1.164(Y - 16) + 1.596(Cr - 128)
387      G = 1.164(Y - 16) - 0.813(Cr - 128) - 0.391(Cb - 128)
388      B = 1.164(Y - 16)                   + 2.018(Cb - 128)
389
390
391    r = (1.164 * (y - 16))
392       + (2.018 * (v - 128));
393    g = (1.164 * (y - 16))
394       - (0.813 * (u - 128))
395       - (0.391 * (v - 128));
396    b = (1.164 * (y - 16))
397       + (1.596 * (u - 128));
398
399
400      Another formula I found:  (seems to work)
401
402      R = Y + 1.370705 (V-128)
403      G = Y - 0.698001 (V-128) - 0.337633 (U-128)
404      B = Y + 1.732446 (U-128)
405    */
406
407    r = (int)( y + (1.370705 * (v-128)) );
408    g = (int)( y - (0.698001 * (v-128)) - (0.337633 * (u-128)) );
409    b = (int)( y + (1.732446 * (u-128)) );
410
411    // Even with proper conversion, some values still need clipping.
412    if (r > 255) r = 255;
413    if (g > 255) g = 255;
414    if (b > 255) b = 255;
415    if (r < 0) r = 0;
416    if (g < 0) g = 0;
417    if (b < 0) b = 0;
418
419    // Values only go from 0-220..  Why?
420 //   pixel[0] = r * 220 / 256;
421 //   pixel[1] = g * 220 / 256;
422 //   pixel[2] = b * 220 / 256;
423 //   pixel[3] = 0;
424
425    *pr = (unsigned char) (r * 220 / 256);
426    *pg = (unsigned char) (g * 220 / 256);
427    *pb = (unsigned char) (b * 220 / 256);
428
429    /* Debug
430    //printf("yuv2rgb(%i, %i, %i) -> %i, %i, %i  (0x%x)\n",
431     y, u, v,
432     pixel[0], pixel[1], pixel[2],
433     pixel32);
434    */
435
436 //   return pixel32;
437 }