225
|
1 #include <stdio.h>
|
|
2 #include <stdlib.h>
|
|
3 #include <string.h>
|
|
4 #include <fcntl.h>
|
|
5 #include <unistd.h>
|
|
6 #include <errno.h>
|
|
7
|
|
8 #include <sys/mman.h>
|
|
9 #include <sys/ioctl.h>
|
|
10 #include <linux/fb.h>
|
|
11 #include <linux/vt.h>
|
|
12
|
|
13 #include "config.h"
|
|
14 #include "video_out.h"
|
|
15 #include "video_out_internal.h"
|
|
16
|
|
17 #include "yuv2rgb.h"
|
|
18
|
|
19 LIBVO_EXTERN(fbdev)
|
|
20
|
|
21 //#include "yuv2rgb.h"
|
|
22
|
|
23 static vo_info_t vo_info = {
|
|
24 "Framebuffer Device",
|
|
25 "fbdev",
|
|
26 "Szabolcs Berecz <szabi@inf.elte.hu>",
|
|
27 ""
|
|
28 };
|
|
29
|
|
30 static int vt_active;
|
|
31 static int vt_fd;
|
|
32
|
|
33 char *fb_dev_name = NULL;
|
|
34 static int fb_dev_fd;
|
|
35 static size_t fb_size;
|
|
36 static uint8_t *frame_buffer;
|
|
37 static int fb_bpp;
|
229
|
38 struct fb_fix_screeninfo fb_fix_info;
|
|
39 struct fb_var_screeninfo fb_var_info;
|
225
|
40
|
|
41 static int in_width;
|
|
42 static int in_height;
|
|
43 static int out_width;
|
|
44 static int out_height;
|
|
45 static uint8_t *next_frame;
|
|
46 static int screen_width;
|
|
47 static uint32_t pixel_format;
|
|
48
|
229
|
49 static int fb_init_done = 0;
|
225
|
50
|
229
|
51 static int fb_init(void)
|
225
|
52 {
|
|
53 int fd, vt;
|
|
54 char vt_name[11];
|
|
55 struct vt_stat vt_state;
|
|
56 struct vt_mode vt_mode;
|
|
57
|
229
|
58 #if 0
|
225
|
59 /* get a free vt */
|
|
60 if ((fd = open("/dev/tty0", O_WRONLY, 0)) == -1) {
|
|
61 printf("Can't open /dev/tty0: %s\n", strerror(errno));
|
|
62 return 1;
|
|
63 }
|
|
64 if (ioctl(fd, VT_OPENQRY, &vt) < 0 || vt == -1) {
|
|
65 printf("Can't open a free VT: %s\n", strerror(errno));
|
|
66 return 1;
|
|
67 }
|
|
68 close(fd);
|
229
|
69 #endif
|
|
70 #if 0
|
225
|
71 /* open the vt */
|
|
72 snprintf(vt_name, 10, "/dev/tty%d", vt);
|
|
73 if ((vt_fd = open(vt_name, O_RDWR | O_NONBLOCK, 0)) == -1) {
|
|
74 printf("Can't open %s: %s\n", vt_name, strerror(errno));
|
|
75 return 1;
|
|
76 }
|
|
77
|
|
78 /* save the current vtnum */
|
|
79 if (!ioctl(vt_fd, VT_GETSTATE, &vt_state))
|
|
80 vt_active = vt_state.v_active;
|
|
81
|
|
82 /* detach the controlling tty */
|
|
83 if ((fd = open("/dev/tty", O_RDWR)) >= 0) {
|
|
84 ioctl(fd, TIOCNOTTY, 0);
|
|
85 close(fd);
|
|
86 }
|
229
|
87 #endif
|
225
|
88 #if 0
|
|
89 /* switch to the new vt */
|
|
90 if (ioctl(vt_fd, VT_ACTIVATE, vt_active))
|
|
91 printf("ioctl VT_ACTIVATE: %s\n", strerror(errno));
|
|
92 if (ioctl(vt_fd, VT_WAITACTIVE, vt_active))
|
|
93 printf("ioctl VT_WAITACTIVE: %s\n", strerror(errno));
|
|
94 if (ioctl(vt_fd, VT_GETMODE, &vt_mode) < 0) {
|
|
95 printf("ioctl VT_GETMODE: %s\n", strerror(errno));
|
|
96 return 1;
|
|
97 }
|
|
98 signal(SIGUSR1, vt_request);
|
|
99 vt_mode.mode = VT_PROCESS;
|
|
100 vt_mode.relsig = SIGUSR1;
|
|
101 vt_mode.acqsig = SIGUSR1;
|
|
102 if (ioctl(vt_fd, VT_SETMODE, &vt_mode) < 0) {
|
|
103 printf("ioctl VT_SETMODE: %s\n", strerror(errno));
|
|
104 return 1;
|
|
105 }
|
|
106 #endif
|
|
107 if (!fb_dev_name && !(fb_dev_name = getenv("FRAMEBUFFER")))
|
|
108 fb_dev_name = "/dev/fb0";
|
229
|
109 printf("fb_init: using %s\n", fb_dev_name);
|
225
|
110 if ((fb_dev_fd = open(fb_dev_name, O_RDWR)) == -1) {
|
229
|
111 printf("fb_init: Can't open %s: %s\n", fb_dev_name, strerror(errno));
|
225
|
112 return 1;
|
|
113 }
|
229
|
114 if (ioctl(fb_dev_fd, FBIOGET_VSCREENINFO, &fb_var_info)) {
|
|
115 printf("fb_init: Can't get VSCREENINFO: %s\n", strerror(errno));
|
225
|
116 return 1;
|
|
117 }
|
229
|
118 if (ioctl(fb_dev_fd, FBIOGET_FSCREENINFO, &fb_fix_info)) {
|
|
119 printf("fb_init: Can't get VSCREENINFO: %s\n", strerror(errno));
|
225
|
120 return 1;
|
|
121 }
|
229
|
122 switch (fb_fix_info.type) {
|
225
|
123 case FB_TYPE_VGA_PLANES:
|
229
|
124 printf("fb_init: FB_TYPE_VGA_PLANES not supported.\n");
|
225
|
125 return 1;
|
|
126 break;
|
|
127 case FB_TYPE_PLANES:
|
229
|
128 printf("fb_init: FB_TYPE_PLANES not supported.\n");
|
225
|
129 return 1;
|
|
130 break;
|
|
131 case FB_TYPE_INTERLEAVED_PLANES:
|
229
|
132 printf("fb_init: FB_TYPE_INTERLEAVED_PLANES not supported.\n");
|
225
|
133 return 1;
|
|
134 break;
|
|
135 #ifdef FB_TYPE_TEXT
|
|
136 case FB_TYPE_TEXT:
|
229
|
137 printf("fb_init: FB_TYPE_TEXT not supported.\n");
|
225
|
138 return 1;
|
|
139 break;
|
|
140 #endif
|
|
141 case FB_TYPE_PACKED_PIXELS:
|
|
142 /* OK */
|
229
|
143 printf("fb_init: FB_TYPE_PACKED_PIXELS: OK\n");
|
225
|
144 break;
|
|
145 default:
|
229
|
146 printf("fb_init: unknown FB_TYPE: %d\n", fb_fix_info.type);
|
225
|
147 return 1;
|
|
148 }
|
229
|
149 fb_bpp = fb_var_info.bits_per_pixel;
|
|
150 screen_width = fb_fix_info.line_length;
|
|
151 fb_size = fb_fix_info.smem_len;
|
225
|
152 if ((frame_buffer = (uint8_t *) mmap(0, fb_size, PROT_READ | PROT_WRITE,
|
|
153 MAP_SHARED, fb_dev_fd, 0)) == (uint8_t *) -1) {
|
229
|
154 printf("fb_init: Can't mmap %s: %s\n", fb_dev_name, strerror(errno));
|
225
|
155 return 1;
|
|
156 }
|
|
157 close(fb_dev_fd);
|
229
|
158
|
|
159 printf("fb_init: framebuffer @ %p\n", frame_buffer);
|
|
160 printf("fb_init: framebuffer size: %d bytes\n", fb_size);
|
|
161 printf("fb_init: bpp: %d\n", fb_bpp);
|
|
162 printf("fb_init: pixel per line: %d\n", screen_width / (fb_bpp / 8));
|
|
163 printf("fb_init: visual: %d\n", fb_fix_info.visual);
|
225
|
164
|
229
|
165 fb_init_done = 1;
|
|
166 return 0;
|
|
167 }
|
|
168
|
|
169 static uint32_t init(uint32_t width, uint32_t height, uint32_t d_width,
|
|
170 uint32_t d_height, uint32_t fullscreen, char *title,
|
|
171 uint32_t format)
|
|
172 {
|
|
173 if (!fb_init_done)
|
|
174 if (fb_init())
|
|
175 return 1;
|
225
|
176
|
|
177 in_width = width;
|
|
178 in_height = height;
|
|
179 out_width = width;
|
|
180 out_height = height;
|
|
181 pixel_format = format;
|
|
182 if (!(next_frame = (uint8_t *) malloc(in_width * in_height * (fb_bpp / 8)))) {
|
|
183 printf("Can't malloc next_frame: %s\n", strerror(errno));
|
|
184 return 1;
|
|
185 }
|
|
186
|
|
187 if (format == IMGFMT_YV12)
|
|
188 yuv2rgb_init(fb_bpp, MODE_RGB);
|
|
189 return 0;
|
|
190 }
|
|
191
|
|
192 static uint32_t query_format(uint32_t format)
|
|
193 {
|
229
|
194 if (!fb_init_done)
|
|
195 if (fb_init())
|
|
196 return 0;
|
|
197 printf("vo_fbdev: query_format(%#x): ", format);
|
|
198 if (format & IMGFMT_BGR_MASK == IMGFMT_BGR)
|
|
199 goto not_supported;
|
225
|
200 switch (format) {
|
|
201 case IMGFMT_YV12:
|
229
|
202 goto supported;
|
225
|
203 case IMGFMT_RGB32:
|
|
204 if (fb_bpp == 32)
|
229
|
205 goto supported;
|
225
|
206 break;
|
|
207 case IMGFMT_RGB24:
|
|
208 if (fb_bpp == 24)
|
229
|
209 goto supported;
|
225
|
210 break;
|
|
211 case IMGFMT_RGB16:
|
|
212 if (fb_bpp == 16)
|
229
|
213 goto supported;
|
225
|
214 break;
|
|
215 case IMGFMT_RGB15:
|
|
216 if (fb_bpp == 15)
|
229
|
217 goto supported;
|
|
218 break;
|
|
219 case IMGFMT_BGR|32:
|
|
220 if (fb_bpp == 32)
|
|
221 goto supported;
|
|
222 break;
|
|
223 case IMGFMT_BGR|24:
|
|
224 if (fb_bpp == 24)
|
|
225 goto supported;
|
|
226 break;
|
|
227 case IMGFMT_BGR|16:
|
|
228 if (fb_bpp == 16)
|
|
229 goto supported;
|
|
230 break;
|
|
231 case IMGFMT_BGR|15:
|
|
232 if (fb_bpp == 15)
|
|
233 goto supported;
|
225
|
234 break;
|
|
235 }
|
229
|
236 not_supported:
|
|
237 printf("not_supported\n");
|
225
|
238 return 0;
|
229
|
239 supported:
|
|
240 printf("supported\n");
|
|
241 return 1;
|
225
|
242 }
|
|
243
|
|
244 static const vo_info_t *get_info(void)
|
|
245 {
|
|
246 return &vo_info;
|
|
247 }
|
|
248
|
|
249 static void draw_alpha(int x0, int y0, int w, int h, unsigned char *src,
|
|
250 unsigned char *srca, int stride)
|
|
251 {
|
|
252 int x, y;
|
|
253 uint8_t *dst;
|
|
254
|
|
255 if (pixel_format == IMGFMT_YV12) {
|
|
256 for (y = 0; y < h; y++){
|
|
257 dst = next_frame + (in_width * (y0 + y) + x0) * (fb_bpp / 8);
|
|
258 for (x = 0; x < w; x++) {
|
|
259 if (srca[x]) {
|
|
260 dst[0] = (dst[0]*(srca[x]^255)+src[x]*(srca[x]))>>8;
|
|
261 dst[1] = (dst[1]*(srca[x]^255)+src[x]*(srca[x]))>>8;
|
|
262 dst[2] = (dst[2]*(srca[x]^255)+src[x]*(srca[x]))>>8;
|
|
263 }
|
|
264 dst += fb_bpp / 8;
|
|
265 }
|
|
266 src += stride;
|
|
267 srca += stride;
|
|
268 }
|
|
269 }
|
|
270 }
|
|
271
|
|
272 static uint32_t draw_frame(uint8_t *src[])
|
|
273 {
|
|
274 if (pixel_format == IMGFMT_YV12) {
|
|
275 yuv2rgb(next_frame, src[0], src[1], src[2], in_width,
|
|
276 in_height, in_width * (fb_bpp / 8),
|
|
277 in_width, in_width / 2);
|
229
|
278 } else if ((pixel_format & IMGFMT_RGB_MASK) == IMGFMT_RGB) {
|
225
|
279 int i;
|
229
|
280 uint8_t *dst = next_frame;
|
|
281 uint8_t *s = src[0];
|
|
282 for (i = 0; i < in_height; i++) {
|
|
283 memcpy(next_frame, s, in_width * (fb_bpp / 8));
|
|
284 dst += screen_width;
|
|
285 s += in_width * (fb_bpp / 8);
|
|
286 }
|
|
287 } else {
|
225
|
288 }
|
|
289 return 0;
|
|
290 }
|
|
291
|
|
292 static uint32_t draw_slice(uint8_t *src[], int stride[], int w, int h, int x,
|
|
293 int y)
|
|
294 {
|
|
295 uint8_t *dest;
|
|
296
|
|
297 dest = next_frame + (in_width * y + x) * (fb_bpp / 8);
|
|
298 yuv2rgb(dest, src[0], src[1], src[2], w, h, in_width * (fb_bpp / 8),
|
|
299 stride[0], stride[1]);
|
|
300 return 0;
|
|
301 }
|
|
302
|
|
303 static void check_events(void)
|
|
304 {
|
|
305 }
|
|
306
|
|
307 static void flip_page(void)
|
|
308 {
|
|
309 int i, out_offset = 0, in_offset = 0;
|
|
310
|
|
311 vo_draw_text(in_width, in_height, draw_alpha);
|
|
312 check_events();
|
|
313 for (i = 0; i < in_height; i++) {
|
|
314 memcpy(frame_buffer + out_offset, next_frame + in_offset,
|
|
315 in_width * (fb_bpp / 8));
|
|
316 out_offset += screen_width;
|
|
317 in_offset += in_width * (fb_bpp / 8);
|
|
318 }
|
|
319 }
|
|
320
|
|
321 static void uninit(void)
|
|
322 {
|
|
323 if (vt_active >= 0)
|
|
324 ioctl(vt_fd, VT_ACTIVATE, vt_active);
|
|
325 printf("vo_fbdev: uninit\n");
|
|
326 free(next_frame);
|
|
327 munmap(frame_buffer, fb_size);
|
|
328 }
|