Mercurial > mplayer.hg
annotate libvo/vo_fbdev.c @ 856:258884190f83
ehh...
author | arpi_esp |
---|---|
date | Thu, 24 May 2001 01:30:51 +0000 |
parents | c3fad5e1698e |
children | 83919c1b9924 |
rev | line source |
---|---|
305 | 1 /* |
2 * Video driver for Framebuffer device | |
3 * by Szabolcs Berecz <szabi@inf.elte.hu> | |
359 | 4 * (C) 2001 |
305 | 5 * |
6 * Some idea and code borrowed from Chris Lawrence's ppmtofb-0.27 | |
7 */ | |
8 | |
393 | 9 #define FBDEV "fbdev: " |
10 | |
225 | 11 #include <stdio.h> |
12 #include <stdlib.h> | |
13 #include <string.h> | |
14 #include <fcntl.h> | |
15 #include <unistd.h> | |
16 #include <errno.h> | |
359 | 17 #include <ctype.h> |
18 #include <assert.h> | |
225 | 19 |
20 #include <sys/mman.h> | |
21 #include <sys/ioctl.h> | |
22 #include <linux/fb.h> | |
23 | |
24 #include "config.h" | |
25 #include "video_out.h" | |
26 #include "video_out_internal.h" | |
658 | 27 #include "fastmemcpy.h" |
633 | 28 #include "osd.h" |
29 #include "sub.h" | |
225 | 30 #include "yuv2rgb.h" |
311 | 31 extern void rgb15to16_mmx(char *s0, char *d0, int count); |
225 | 32 |
33 LIBVO_EXTERN(fbdev) | |
34 | |
35 static vo_info_t vo_info = { | |
36 "Framebuffer Device", | |
37 "fbdev", | |
38 "Szabolcs Berecz <szabi@inf.elte.hu>", | |
39 "" | |
40 }; | |
41 | |
379 | 42 extern int verbose; |
43 | |
359 | 44 /****************************** |
393 | 45 * fb.modes support * |
359 | 46 ******************************/ |
47 | |
48 /* | |
49 * read the fb.modes manual page! | |
50 */ | |
51 | |
52 typedef struct { | |
53 char *name; | |
54 uint32_t xres, yres, vxres, vyres, depth; | |
55 uint32_t pixclock, left, right, upper, lower, hslen, vslen; | |
56 uint32_t sync; | |
57 uint32_t vmode; | |
58 } fb_mode_t; | |
59 | |
60 #define PRINT_LINENUM printf(" at line %d\n", line_num) | |
61 | |
62 #define MAX_NR_TOKEN 16 | |
63 | |
64 #define MAX_LINE_LEN 1000 | |
65 | |
66 #define RET_EOF -1 | |
67 #define RET_EOL -2 | |
68 | |
69 static int validate_mode(fb_mode_t *m) | |
70 { | |
71 if (!m->xres) { | |
72 printf("needs geometry "); | |
73 return 0; | |
74 } | |
75 if (!m->pixclock) { | |
76 printf("needs timings "); | |
77 return 0; | |
78 } | |
79 return 1; | |
80 } | |
81 | |
82 static FILE *fp; | |
83 static int line_num = 0; | |
84 static char *line; | |
85 static char *token[MAX_NR_TOKEN]; | |
86 | |
87 static int get_token(int num) | |
88 { | |
89 static int read_nextline = 1; | |
90 static int line_pos; | |
91 int i; | |
92 char c; | |
93 | |
94 if (num >= MAX_NR_TOKEN) { | |
95 printf("get_token(): max >= MAX_NR_TOKEN!"); | |
96 goto out_eof; | |
97 } | |
98 | |
99 if (read_nextline) { | |
100 if (!fgets(line, MAX_LINE_LEN, fp)) | |
101 goto out_eof; | |
102 line_pos = 0; | |
103 ++line_num; | |
104 read_nextline = 0; | |
105 } | |
106 for (i = 0; i < num; i++) { | |
107 while (isspace(line[line_pos])) | |
108 ++line_pos; | |
109 if (line[line_pos] == '\0' || line[line_pos] == '#') { | |
110 read_nextline = 1; | |
111 if (i == num) | |
112 goto out_ok; | |
113 goto out_eol; | |
114 } | |
115 token[i] = line + line_pos; | |
116 c = line[line_pos]; | |
117 if (c == '"' || c == '\'') { | |
118 token[i]++; | |
119 while (line[++line_pos] != c && line[line_pos]) | |
120 /* NOTHING */; | |
121 } else { | |
122 for (/* NOTHING */; !isspace(line[line_pos]) && | |
123 line[line_pos]; line_pos++) | |
124 /* NOTHING */; | |
125 } | |
126 if (!line[line_pos]) { | |
127 read_nextline = 1; | |
128 if (i == num - 1) | |
129 goto out_ok; | |
130 goto out_eol; | |
131 } | |
132 line[line_pos] = '\0'; | |
133 line_pos++; | |
134 } | |
135 out_ok: | |
136 return i; | |
137 out_eof: | |
138 return RET_EOF; | |
139 out_eol: | |
140 return RET_EOL; | |
141 } | |
142 | |
143 static fb_mode_t *fb_modes = NULL; | |
144 static int nr_modes = 0; | |
145 | |
146 static int parse_fbmode_cfg(char *cfgfile) | |
147 { | |
148 fb_mode_t *mode = NULL; | |
149 char *endptr; // strtoul()... | |
150 int tmp, i; | |
151 | |
152 #ifdef DEBUG | |
153 assert(cfgfile != NULL); | |
154 #endif | |
155 | |
156 printf("Reading %s: ", cfgfile); | |
157 | |
158 if ((fp = fopen(cfgfile, "r")) == NULL) { | |
159 printf("can't open '%s': %s\n", cfgfile, strerror(errno)); | |
160 return -1; | |
161 } | |
162 | |
163 if ((line = (char *) malloc(MAX_LINE_LEN + 1)) == NULL) { | |
164 printf("can't get memory for 'line': %s\n", strerror(errno)); | |
165 return -2; | |
166 } | |
167 | |
168 /* | |
169 * check if the cfgfile starts with 'mode' | |
170 */ | |
171 while ((tmp = get_token(1)) == RET_EOL) | |
172 /* NOTHING */; | |
173 if (tmp == RET_EOF) | |
174 goto out; | |
175 if (!strcmp(token[0], "mode")) | |
176 goto loop_enter; | |
177 goto err_out_parse_error; | |
178 | |
179 while ((tmp = get_token(1)) != RET_EOF) { | |
180 if (tmp == RET_EOL) | |
181 continue; | |
182 if (!strcmp(token[0], "mode")) { | |
183 if (!validate_mode(mode)) | |
184 goto err_out_not_valid; | |
185 loop_enter: | |
186 if (!(fb_modes = (fb_mode_t *) realloc(fb_modes, | |
187 sizeof(fb_mode_t) * (nr_modes + 1)))) { | |
188 printf("can't realloc 'fb_modes': %s\n", strerror(errno)); | |
189 goto err_out; | |
190 } | |
191 mode=fb_modes + nr_modes; | |
192 ++nr_modes; | |
193 memset(mode,0,sizeof(fb_mode_t)); | |
225 | 194 |
359 | 195 if (get_token(1) < 0) |
196 goto err_out_parse_error; | |
197 for (i = 0; i < nr_modes - 1; i++) { | |
198 if (!strcmp(token[0], fb_modes[i].name)) { | |
199 printf("mode name '%s' isn't unique", token[0]); | |
200 goto err_out_print_linenum; | |
201 } | |
202 } | |
203 if (!(mode->name = strdup(token[0]))) { | |
204 printf("can't strdup -> 'name': %s\n", strerror(errno)); | |
205 goto err_out; | |
206 } | |
207 } else if (!strcmp(token[0], "geometry")) { | |
208 if (get_token(5) < 0) | |
209 goto err_out_parse_error; | |
210 mode->xres = strtoul(token[0], &endptr, 0); | |
211 if (*endptr) | |
212 goto err_out_parse_error; | |
213 mode->yres = strtoul(token[1], &endptr, 0); | |
214 if (*endptr) | |
215 goto err_out_parse_error; | |
216 mode->vxres = strtoul(token[2], &endptr, 0); | |
217 if (*endptr) | |
218 goto err_out_parse_error; | |
219 mode->vyres = strtoul(token[3], &endptr, 0); | |
220 if (*endptr) | |
221 goto err_out_parse_error; | |
222 mode->depth = strtoul(token[4], &endptr, 0); | |
223 if (*endptr) | |
224 goto err_out_parse_error; | |
225 } else if (!strcmp(token[0], "timings")) { | |
226 if (get_token(7) < 0) | |
227 goto err_out_parse_error; | |
228 mode->pixclock = strtoul(token[0], &endptr, 0); | |
229 if (*endptr) | |
230 goto err_out_parse_error; | |
231 mode->left = strtoul(token[1], &endptr, 0); | |
232 if (*endptr) | |
233 goto err_out_parse_error; | |
234 mode->right = strtoul(token[2], &endptr, 0); | |
235 if (*endptr) | |
236 goto err_out_parse_error; | |
237 mode->upper = strtoul(token[3], &endptr, 0); | |
238 if (*endptr) | |
239 goto err_out_parse_error; | |
240 mode->lower = strtoul(token[4], &endptr, 0); | |
241 if (*endptr) | |
242 goto err_out_parse_error; | |
243 mode->hslen = strtoul(token[5], &endptr, 0); | |
244 if (*endptr) | |
245 goto err_out_parse_error; | |
246 mode->vslen = strtoul(token[6], &endptr, 0); | |
247 if (*endptr) | |
248 goto err_out_parse_error; | |
249 } else if (!strcmp(token[0], "endmode")) { | |
250 /* NOTHING for now*/ | |
383 | 251 } else if (!strcmp(token[0], "accel")) { |
252 if (get_token(1) < 0) | |
253 goto err_out_parse_error; | |
393 | 254 /* |
255 * it's only used for text acceleration | |
256 * so we just ignore it. | |
257 */ | |
359 | 258 } else if (!strcmp(token[0], "hsync")) { |
259 if (get_token(1) < 0) | |
260 goto err_out_parse_error; | |
261 if (!strcmp(token[0], "low")) | |
262 mode->sync &= ~FB_SYNC_HOR_HIGH_ACT; | |
263 else if(!strcmp(token[0], "high")) | |
264 mode->sync |= FB_SYNC_HOR_HIGH_ACT; | |
265 else | |
266 goto err_out_parse_error; | |
267 } else if (!strcmp(token[0], "vsync")) { | |
268 if (get_token(1) < 0) | |
269 goto err_out_parse_error; | |
270 if (!strcmp(token[0], "low")) | |
271 mode->sync &= ~FB_SYNC_VERT_HIGH_ACT; | |
272 else if(!strcmp(token[0], "high")) | |
273 mode->sync |= FB_SYNC_VERT_HIGH_ACT; | |
274 else | |
275 goto err_out_parse_error; | |
276 } else if (!strcmp(token[0], "csync")) { | |
277 if (get_token(1) < 0) | |
278 goto err_out_parse_error; | |
279 if (!strcmp(token[0], "low")) | |
280 mode->sync &= ~FB_SYNC_COMP_HIGH_ACT; | |
281 else if(!strcmp(token[0], "high")) | |
282 mode->sync |= FB_SYNC_COMP_HIGH_ACT; | |
283 else | |
284 goto err_out_parse_error; | |
285 } else if (!strcmp(token[0], "extsync")) { | |
286 if (get_token(1) < 0) | |
287 goto err_out_parse_error; | |
288 if (!strcmp(token[0], "false")) | |
289 mode->sync &= ~FB_SYNC_EXT; | |
290 else if(!strcmp(token[0], "true")) | |
291 mode->sync |= FB_SYNC_EXT; | |
292 else | |
293 goto err_out_parse_error; | |
294 } else if (!strcmp(token[0], "laced")) { | |
295 if (get_token(1) < 0) | |
296 goto err_out_parse_error; | |
297 if (!strcmp(token[0], "false")) | |
298 mode->vmode = FB_VMODE_NONINTERLACED; | |
299 else if (!strcmp(token[0], "true")) | |
300 mode->vmode = FB_VMODE_INTERLACED; | |
301 else | |
302 goto err_out_parse_error; | |
479 | 303 } else if (!strcmp(token[0], "double")) { |
359 | 304 if (get_token(1) < 0) |
305 goto err_out_parse_error; | |
306 if (!strcmp(token[0], "false")) | |
307 ; | |
308 else if (!strcmp(token[0], "true")) | |
309 mode->vmode = FB_VMODE_DOUBLE; | |
310 else | |
311 goto err_out_parse_error; | |
312 } else | |
313 goto err_out_parse_error; | |
314 } | |
315 if (!validate_mode(mode)) | |
316 goto err_out_not_valid; | |
317 out: | |
318 printf("%d modes\n", nr_modes); | |
319 free(line); | |
320 fclose(fp); | |
321 return nr_modes; | |
322 err_out_parse_error: | |
323 printf("parse error"); | |
324 err_out_print_linenum: | |
325 PRINT_LINENUM; | |
326 err_out: | |
393 | 327 if (fb_modes) { |
359 | 328 free(fb_modes); |
393 | 329 fb_modes = NULL; |
330 } | |
331 nr_modes = 0; | |
359 | 332 free(line); |
333 free(fp); | |
334 return -2; | |
335 err_out_not_valid: | |
336 printf("mode is not definied correctly"); | |
337 goto err_out_print_linenum; | |
338 } | |
339 | |
340 static fb_mode_t *find_mode_by_name(char *name) | |
341 { | |
342 int i; | |
343 | |
658 | 344 for (i = 0; i < nr_modes; i++) |
359 | 345 if (!strcmp(name, fb_modes[i].name)) |
346 return fb_modes + i; | |
347 return NULL; | |
348 } | |
349 | |
393 | 350 static float dcf(fb_mode_t *m) //driving clock frequency |
351 { | |
519 | 352 return 1e12f / m->pixclock; |
393 | 353 } |
354 | |
355 static float hsf(fb_mode_t *m) //horizontal scan frequency | |
356 { | |
357 int htotal = m->left + m->xres + m->right + m->hslen; | |
358 return dcf(m) / htotal; | |
359 } | |
360 | |
361 static float vsf(fb_mode_t *m) //vertical scan frequency | |
362 { | |
363 int vtotal = m->upper + m->yres + m->lower + m->vslen; | |
364 return hsf(m) / vtotal; | |
365 } | |
366 | |
367 typedef struct { | |
368 float min; | |
369 float max; | |
418 | 370 } range_t; |
393 | 371 |
418 | 372 static int in_range(range_t *r, float f) |
393 | 373 { |
658 | 374 for (/* NOTHING */; (r->min != -1 && r->max != -1); r++) |
393 | 375 if (f >= r->min && f <= r->max) |
376 return 1; | |
377 return 0; | |
378 } | |
379 | |
418 | 380 static fb_mode_t *find_best_mode(int xres, int yres, range_t *hfreq, |
381 range_t *vfreq, range_t *dotclock) | |
393 | 382 { |
383 int i; | |
384 fb_mode_t *best = fb_modes; | |
519 | 385 fb_mode_t *curr; |
386 | |
387 /* find first working mode */ | |
388 for (i = nr_modes - 1; i; i--, best++) { | |
389 if (in_range(hfreq, hsf(best)) && in_range(vfreq, vsf(best)) && | |
390 in_range(dotclock, dcf(best))) | |
391 break; | |
392 if (verbose > 1) | |
393 printf(FBDEV "can't set %dx%d\n", best->xres, best->yres); | |
394 } | |
395 | |
396 if (!i) | |
397 return NULL; | |
398 if (i == 1) | |
399 return best; | |
393 | 400 |
519 | 401 for (curr = best + 1; i; i--, curr++) { |
402 if (!in_range(hfreq, hsf(curr))) | |
403 continue; | |
404 if (!in_range(vfreq, vsf(curr))) | |
405 continue; | |
406 if (!in_range(dotclock, dcf(curr))) | |
407 continue; | |
408 if (verbose > 1) | |
409 printf(FBDEV "%dx%d ", curr->xres, curr->yres); | |
410 if ((best->xres < xres || best->yres < yres) && | |
411 (curr->xres > best->xres || | |
412 curr->yres > best->yres)) { | |
413 if (verbose > 1) | |
414 printf("better than %dx%d\n", best->xres, | |
415 best->yres); | |
416 best = curr; | |
417 } else if (curr->xres >= xres && curr->yres >= yres) { | |
393 | 418 if (curr->xres < best->xres && curr->yres < best->yres) { |
519 | 419 if (verbose > 1) |
420 printf("smaller than %dx%d\n", | |
421 best->xres, best->yres); | |
393 | 422 best = curr; |
519 | 423 } else if (curr->xres == best->xres && |
424 curr->yres == best->yres && | |
425 (vsf(curr) > vsf(best))) { | |
426 if (verbose > 1) | |
427 printf("faster screen refresh\n"); | |
428 best = curr; | |
429 } else if (verbose > 1) | |
430 printf("\n"); | |
431 } else if (verbose > 1) | |
432 printf("is too small\n"); | |
393 | 433 } |
434 return best; | |
435 } | |
436 | |
418 | 437 static void set_bpp(struct fb_var_screeninfo *p, int bpp) |
438 { | |
476 | 439 p->bits_per_pixel = (bpp + 1) & ~1; |
418 | 440 p->red.msb_right = p->green.msb_right = p->blue.msb_right = 0; |
563 | 441 p->transp.offset = p->transp.length = 0; |
418 | 442 switch (bpp) { |
443 case 32: | |
563 | 444 p->transp.offset = 24; |
445 p->transp.length = 8; | |
418 | 446 case 24: |
447 p->red.offset = 16; | |
448 p->red.length = 8; | |
449 p->green.offset = 8; | |
450 p->green.length = 8; | |
550 | 451 p->blue.length = 8; |
418 | 452 p->blue.offset = 0; |
453 break; | |
454 case 16: | |
455 p->red.offset = 11; | |
550 | 456 p->green.length = 6; |
418 | 457 p->red.length = 5; |
458 p->green.offset = 5; | |
550 | 459 p->blue.length = 5; |
418 | 460 p->blue.offset = 0; |
461 break; | |
462 case 15: | |
463 p->red.offset = 10; | |
550 | 464 p->green.length = 5; |
418 | 465 p->red.length = 5; |
466 p->green.offset = 5; | |
550 | 467 p->blue.length = 5; |
418 | 468 p->blue.offset = 0; |
469 break; | |
470 } | |
471 } | |
472 | |
473 static void fb_mode2fb_vinfo(fb_mode_t *m, struct fb_var_screeninfo *v) | |
474 { | |
475 v->xres = m->xres; | |
476 v->yres = m->yres; | |
477 v->xres_virtual = m->vxres; | |
478 v->yres_virtual = m->vyres; | |
479 set_bpp(v, m->depth); | |
480 v->pixclock = m->pixclock; | |
481 v->left_margin = m->left; | |
482 v->right_margin = m->right; | |
483 v->upper_margin = m->upper; | |
484 v->lower_margin = m->lower; | |
485 v->hsync_len = m->hslen; | |
486 v->vsync_len = m->vslen; | |
487 v->sync = m->sync; | |
488 v->vmode = m->vmode; | |
489 } | |
490 | |
491 static range_t *str2range(char *s) | |
492 { | |
493 float tmp_min, tmp_max; | |
494 char *endptr = s; // to start the loop | |
495 range_t *r = NULL; | |
496 int i, j; | |
497 | |
498 if (!s) | |
499 return NULL; | |
500 for (i = 0; *endptr; i++) { | |
501 if (*s == ',') | |
502 goto out_err; | |
538 | 503 if (!(r = (range_t *) realloc(r, sizeof(*r) * (i + 2)))) { |
418 | 504 printf("can't realloc 'r'\n"); |
505 return NULL; | |
506 } | |
507 tmp_min = strtod(s, &endptr); | |
508 if (*endptr == 'k' || *endptr == 'K') { | |
509 tmp_min *= 1000.0; | |
510 endptr++; | |
511 } else if (*endptr == 'm' || *endptr == 'M') { | |
512 tmp_min *= 1000000.0; | |
513 endptr++; | |
514 } | |
515 if (*endptr == '-') { | |
516 tmp_max = strtod(endptr + 1, &endptr); | |
517 if (*endptr == 'k' || *endptr == 'K') { | |
518 tmp_max *= 1000.0; | |
519 endptr++; | |
520 } else if (*endptr == 'm' || *endptr == 'M') { | |
521 tmp_max *= 1000000.0; | |
522 endptr++; | |
523 } | |
524 if (*endptr != ',' && *endptr) | |
525 goto out_err; | |
526 } else if (*endptr == ',' || !*endptr) { | |
527 tmp_max = tmp_min; | |
528 } else | |
529 goto out_err; | |
530 r[i].min = tmp_min; | |
531 r[i].max = tmp_max; | |
532 s = endptr + 1; | |
533 } | |
534 /* check if we have negative numbers... */ | |
535 for (j = 0; j < i; j++) | |
536 if (r[j].min < 0 || r[j].max < 0) | |
537 goto out_err; | |
538 r[i].min = r[i].max = -1; | |
539 return r; | |
540 out_err: | |
541 if (r) | |
542 free(r); | |
543 return NULL; | |
544 } | |
545 | |
359 | 546 /****************************** |
547 * vo_fbdev * | |
548 ******************************/ | |
549 | |
658 | 550 /* command line/config file options */ |
393 | 551 char *fb_dev_name = NULL; |
552 char *fb_mode_cfgfile = "/etc/fb.modes"; | |
553 char *fb_mode_name = NULL; | |
418 | 554 char *monitor_hfreq_str = NULL; |
555 char *monitor_vfreq_str = NULL; | |
556 char *monitor_dotclock_str = NULL; | |
557 | |
658 | 558 /* fb.modes related variables */ |
418 | 559 range_t *monitor_hfreq = NULL; |
560 range_t *monitor_vfreq = NULL; | |
561 range_t *monitor_dotclock = NULL; | |
658 | 562 static fb_mode_t *fb_mode = NULL; |
393 | 563 |
658 | 564 /* vo_fbdev related variables */ |
393 | 565 static int fb_preinit_done = 0; |
359 | 566 static int fb_works = 0; |
225 | 567 static int fb_dev_fd; |
568 static size_t fb_size; | |
569 static uint8_t *frame_buffer; | |
658 | 570 static uint8_t *L123123875; /* thx .so :) */ |
359 | 571 static struct fb_fix_screeninfo fb_finfo; |
572 static struct fb_var_screeninfo fb_orig_vinfo; | |
573 static struct fb_var_screeninfo fb_vinfo; | |
481 | 574 static struct fb_cmap fb_oldcmap; |
575 static int fb_cmap_changed = 0; | |
359 | 576 static int fb_pixel_size; // 32: 4 24: 3 16: 2 15: 2 |
577 static int fb_real_bpp; // 32: 24 24: 24 16: 16 15: 15 | |
578 static int fb_bpp; // 32: 32 24: 24 16: 16 15: 15 | |
393 | 579 static int fb_bpp_we_want; // 32: 32 24: 24 16: 16 15: 15 |
359 | 580 static int fb_screen_width; |
658 | 581 static void (*draw_alpha_p)(int w, int h, unsigned char *src, |
582 unsigned char *srca, int stride, unsigned char *dst, | |
583 int dstride); | |
359 | 584 |
585 static uint8_t *next_frame; | |
225 | 586 static int in_width; |
587 static int in_height; | |
588 static int out_width; | |
589 static int out_height; | |
590 static uint32_t pixel_format; | |
804 | 591 static int fs; |
592 static int flip; | |
225 | 593 |
305 | 594 /* |
595 * Note: this function is completely cut'n'pasted from | |
596 * Chris Lawrence's code. | |
311 | 597 * (modified a bit to fit in my code...) |
305 | 598 */ |
599 struct fb_cmap *make_directcolor_cmap(struct fb_var_screeninfo *var) | |
600 { | |
601 /* Hopefully any DIRECTCOLOR device will have a big enough palette | |
602 * to handle mapping the full color depth. | |
603 * e.g. 8 bpp -> 256 entry palette | |
604 * | |
605 * We could handle some sort of gamma here | |
606 */ | |
607 int i, cols, rcols, gcols, bcols; | |
608 uint16_t *red, *green, *blue; | |
609 struct fb_cmap *cmap; | |
610 | |
611 rcols = 1 << var->red.length; | |
612 gcols = 1 << var->green.length; | |
613 bcols = 1 << var->blue.length; | |
614 | |
615 /* Make our palette the length of the deepest color */ | |
616 cols = (rcols > gcols ? rcols : gcols); | |
617 cols = (cols > bcols ? cols : bcols); | |
618 | |
619 red = malloc(cols * sizeof(red[0])); | |
620 if(!red) { | |
621 printf("Can't allocate red palette with %d entries.\n", cols); | |
622 return NULL; | |
623 } | |
624 for(i=0; i< rcols; i++) | |
625 red[i] = (65535/(rcols-1)) * i; | |
626 | |
627 green = malloc(cols * sizeof(green[0])); | |
628 if(!green) { | |
629 printf("Can't allocate green palette with %d entries.\n", cols); | |
630 free(red); | |
631 return NULL; | |
632 } | |
633 for(i=0; i< gcols; i++) | |
634 green[i] = (65535/(gcols-1)) * i; | |
635 | |
636 blue = malloc(cols * sizeof(blue[0])); | |
637 if(!blue) { | |
638 printf("Can't allocate blue palette with %d entries.\n", cols); | |
639 free(red); | |
640 free(green); | |
641 return NULL; | |
642 } | |
643 for(i=0; i< bcols; i++) | |
644 blue[i] = (65535/(bcols-1)) * i; | |
645 | |
646 cmap = malloc(sizeof(struct fb_cmap)); | |
647 if(!cmap) { | |
648 printf("Can't allocate color map\n"); | |
649 free(red); | |
650 free(green); | |
651 free(blue); | |
652 return NULL; | |
653 } | |
654 cmap->start = 0; | |
655 cmap->transp = 0; | |
656 cmap->len = cols; | |
657 cmap->red = red; | |
658 cmap->blue = blue; | |
659 cmap->green = green; | |
660 cmap->transp = NULL; | |
661 | |
662 return cmap; | |
663 } | |
225 | 664 |
393 | 665 static int fb_preinit(void) |
225 | 666 { |
393 | 667 if (!fb_dev_name && !(fb_dev_name = getenv("FRAMEBUFFER"))) |
668 fb_dev_name = "/dev/fb0"; | |
669 if (verbose > 0) | |
670 printf(FBDEV "using %s\n", fb_dev_name); | |
225 | 671 |
393 | 672 if ((fb_dev_fd = open(fb_dev_name, O_RDWR)) == -1) { |
673 printf(FBDEV "Can't open %s: %s\n", fb_dev_name, strerror(errno)); | |
674 goto err_out; | |
675 } | |
676 if (ioctl(fb_dev_fd, FBIOGET_VSCREENINFO, &fb_vinfo)) { | |
677 printf(FBDEV "Can't get VSCREENINFO: %s\n", strerror(errno)); | |
678 goto err_out_fd; | |
679 } | |
418 | 680 fb_orig_vinfo = fb_vinfo; |
393 | 681 |
663 | 682 fb_bpp = fb_vinfo.bits_per_pixel; |
683 | |
684 /* 16 and 15 bpp is reported 16 bpp */ | |
685 if (fb_bpp == 16) | |
686 fb_bpp = fb_vinfo.red.length + fb_vinfo.green.length + | |
687 fb_vinfo.blue.length; | |
688 | |
418 | 689 if (vo_dbpp) { |
690 if (vo_dbpp != 15 && vo_dbpp != 16 && vo_dbpp != 24 && | |
691 vo_dbpp != 32) { | |
692 printf(FBDEV "can't switch to %d bpp\n", vo_dbpp); | |
383 | 693 goto err_out; |
359 | 694 } |
418 | 695 fb_bpp = vo_dbpp; |
359 | 696 } |
245
cb4c682746c0
disabled scrollback buffer (virtual fb size set to real screen size)
szabii
parents:
231
diff
changeset
|
697 |
393 | 698 fb_preinit_done = 1; |
699 fb_works = 1; | |
700 return 0; | |
701 err_out_fd: | |
702 close(fb_dev_fd); | |
703 fb_dev_fd = -1; | |
704 err_out: | |
705 fb_preinit_done = 1; | |
706 return 1; | |
707 } | |
245
cb4c682746c0
disabled scrollback buffer (virtual fb size set to real screen size)
szabii
parents:
231
diff
changeset
|
708 |
633 | 709 static void lots_of_printf(void) |
710 { | |
711 if (verbose > 0) { | |
712 printf(FBDEV "var info:\n"); | |
713 printf(FBDEV "xres: %u\n", fb_vinfo.xres); | |
714 printf(FBDEV "yres: %u\n", fb_vinfo.yres); | |
715 printf(FBDEV "xres_virtual: %u\n", fb_vinfo.xres_virtual); | |
716 printf(FBDEV "yres_virtual: %u\n", fb_vinfo.yres_virtual); | |
717 printf(FBDEV "xoffset: %u\n", fb_vinfo.xoffset); | |
718 printf(FBDEV "yoffset: %u\n", fb_vinfo.yoffset); | |
719 printf(FBDEV "bits_per_pixel: %u\n", fb_vinfo.bits_per_pixel); | |
720 printf(FBDEV "grayscale: %u\n", fb_vinfo.grayscale); | |
721 printf(FBDEV "red: %lu %lu %lu\n", | |
722 (unsigned long) fb_vinfo.red.offset, | |
723 (unsigned long) fb_vinfo.red.length, | |
724 (unsigned long) fb_vinfo.red.msb_right); | |
725 printf(FBDEV "green: %lu %lu %lu\n", | |
726 (unsigned long) fb_vinfo.green.offset, | |
727 (unsigned long) fb_vinfo.green.length, | |
728 (unsigned long) fb_vinfo.green.msb_right); | |
729 printf(FBDEV "blue: %lu %lu %lu\n", | |
730 (unsigned long) fb_vinfo.blue.offset, | |
731 (unsigned long) fb_vinfo.blue.length, | |
732 (unsigned long) fb_vinfo.blue.msb_right); | |
733 printf(FBDEV "transp: %lu %lu %lu\n", | |
734 (unsigned long) fb_vinfo.transp.offset, | |
735 (unsigned long) fb_vinfo.transp.length, | |
736 (unsigned long) fb_vinfo.transp.msb_right); | |
737 printf(FBDEV "nonstd: %u\n", fb_vinfo.nonstd); | |
738 if (verbose > 1) { | |
739 printf(FBDEV "activate: %u\n", fb_vinfo.activate); | |
740 printf(FBDEV "height: %u\n", fb_vinfo.height); | |
741 printf(FBDEV "width: %u\n", fb_vinfo.width); | |
742 printf(FBDEV "accel_flags: %u\n", fb_vinfo.accel_flags); | |
743 printf(FBDEV "timing:\n"); | |
744 printf(FBDEV "pixclock: %u\n", fb_vinfo.pixclock); | |
745 printf(FBDEV "left_margin: %u\n", fb_vinfo.left_margin); | |
746 printf(FBDEV "right_margin: %u\n", fb_vinfo.right_margin); | |
747 printf(FBDEV "upper_margin: %u\n", fb_vinfo.upper_margin); | |
748 printf(FBDEV "lower_margin: %u\n", fb_vinfo.lower_margin); | |
749 printf(FBDEV "hsync_len: %u\n", fb_vinfo.hsync_len); | |
750 printf(FBDEV "vsync_len: %u\n", fb_vinfo.vsync_len); | |
751 printf(FBDEV "sync: %u\n", fb_vinfo.sync); | |
752 printf(FBDEV "vmode: %u\n", fb_vinfo.vmode); | |
753 } | |
754 printf(FBDEV "fix info:\n"); | |
755 printf(FBDEV "framebuffer size: %d bytes\n", fb_finfo.smem_len); | |
756 printf(FBDEV "type: %lu\n", (unsigned long) fb_finfo.type); | |
757 printf(FBDEV "type_aux: %lu\n", (unsigned long) fb_finfo.type_aux); | |
758 printf(FBDEV "visual: %lu\n", (unsigned long) fb_finfo.visual); | |
759 printf(FBDEV "line_length: %lu bytes\n", (unsigned long) fb_finfo.line_length); | |
760 if (verbose > 1) { | |
761 printf(FBDEV "id: %.16s\n", fb_finfo.id); | |
762 printf(FBDEV "smem_start: %p\n", (void *) fb_finfo.smem_start); | |
763 printf(FBDEV "xpanstep: %u\n", fb_finfo.xpanstep); | |
764 printf(FBDEV "ypanstep: %u\n", fb_finfo.ypanstep); | |
765 printf(FBDEV "ywrapstep: %u\n", fb_finfo.ywrapstep); | |
766 printf(FBDEV "mmio_start: %p\n", (void *) fb_finfo.mmio_start); | |
767 printf(FBDEV "mmio_len: %u bytes\n", fb_finfo.mmio_len); | |
768 printf(FBDEV "accel: %u\n", fb_finfo.accel); | |
769 } | |
770 } | |
771 } | |
658 | 772 |
393 | 773 static uint32_t init(uint32_t width, uint32_t height, uint32_t d_width, |
774 uint32_t d_height, uint32_t fullscreen, char *title, | |
775 uint32_t format) | |
776 { | |
418 | 777 #define FS (fullscreen & 0x01) |
778 #define VM (fullscreen & 0x02) | |
779 #define ZOOM (fullscreen & 0x04) | |
804 | 780 #define FLIP (fullscreen & 0x08) |
418 | 781 |
393 | 782 struct fb_cmap *cmap; |
783 | |
804 | 784 fs = FS; |
393 | 785 if (!fb_preinit_done) |
786 if (fb_preinit()) | |
787 return 1; | |
788 if (!fb_works) | |
789 return 1; | |
245
cb4c682746c0
disabled scrollback buffer (virtual fb size set to real screen size)
szabii
parents:
231
diff
changeset
|
790 |
804 | 791 flip = FLIP; |
792 | |
418 | 793 if (ZOOM) { |
794 printf(FBDEV "-zoom is not supported\n"); | |
795 return 1; | |
796 } | |
797 if (fb_mode_name && !VM) { | |
798 printf(FBDEV "-fbmode can be used only with -vm" | |
799 " (is it the right behaviour?)\n"); | |
800 return 1; | |
801 } | |
802 if (VM) | |
803 if (parse_fbmode_cfg(fb_mode_cfgfile) < 0) | |
804 return 1; | |
804 | 805 if (d_width && (fs || VM)) { |
418 | 806 out_width = d_width; |
807 out_height = d_height; | |
808 } else { | |
809 out_width = width; | |
810 out_height = height; | |
811 } | |
812 in_width = width; | |
813 in_height = height; | |
814 pixel_format = format; | |
815 | |
393 | 816 if (fb_mode_name) { |
418 | 817 if (!(fb_mode = find_mode_by_name(fb_mode_name))) { |
818 printf(FBDEV "can't find requested video mode\n"); | |
819 return 1; | |
820 } | |
821 fb_mode2fb_vinfo(fb_mode, &fb_vinfo); | |
822 } else if (VM) { | |
823 monitor_hfreq = str2range(monitor_hfreq_str); | |
824 monitor_vfreq = str2range(monitor_vfreq_str); | |
825 monitor_dotclock = str2range(monitor_dotclock_str); | |
826 if (!monitor_hfreq || !monitor_vfreq || !monitor_dotclock) { | |
827 printf(FBDEV "you have to specify the capabilities of" | |
828 " the monitor.\n"); | |
829 return 1; | |
830 } | |
831 if (!(fb_mode = find_best_mode(out_width, out_height, | |
832 monitor_hfreq, monitor_vfreq, | |
833 monitor_dotclock))) { | |
834 printf(FBDEV "can't find best video mode\n"); | |
835 return 1; | |
836 } | |
519 | 837 printf(FBDEV "using mode %dx%d @ %.1fHz\n", fb_mode->xres, |
838 fb_mode->yres, vsf(fb_mode)); | |
418 | 839 fb_mode2fb_vinfo(fb_mode, &fb_vinfo); |
359 | 840 } |
418 | 841 fb_bpp_we_want = fb_bpp; |
842 set_bpp(&fb_vinfo, fb_bpp); | |
359 | 843 fb_vinfo.xres_virtual = fb_vinfo.xres; |
844 fb_vinfo.yres_virtual = fb_vinfo.yres; | |
245
cb4c682746c0
disabled scrollback buffer (virtual fb size set to real screen size)
szabii
parents:
231
diff
changeset
|
845 |
359 | 846 if (ioctl(fb_dev_fd, FBIOPUT_VSCREENINFO, &fb_vinfo)) { |
393 | 847 printf(FBDEV "Can't put VSCREENINFO: %s\n", strerror(errno)); |
848 return 1; | |
849 } | |
618 | 850 if (ioctl(fb_dev_fd, FBIOGET_FSCREENINFO, &fb_finfo)) { |
851 printf(FBDEV "Can't get FSCREENINFO: %s\n", strerror(errno)); | |
852 return 1; | |
853 } | |
633 | 854 |
855 lots_of_printf(); | |
245
cb4c682746c0
disabled scrollback buffer (virtual fb size set to real screen size)
szabii
parents:
231
diff
changeset
|
856 |
633 | 857 if (fb_finfo.type != FB_TYPE_PACKED_PIXELS) { |
858 printf(FBDEV "type %d not supported\n", fb_finfo.type); | |
859 return 1; | |
305 | 860 } |
618 | 861 |
379 | 862 switch (fb_finfo.visual) { |
393 | 863 case FB_VISUAL_TRUECOLOR: |
864 break; | |
865 case FB_VISUAL_DIRECTCOLOR: | |
866 if (verbose > 0) | |
867 printf(FBDEV "creating cmap for directcolor\n"); | |
481 | 868 if (ioctl(fb_dev_fd, FBIOGETCMAP, &fb_oldcmap)) { |
393 | 869 printf(FBDEV "can't get cmap: %s\n", |
870 strerror(errno)); | |
871 return 1; | |
872 } | |
873 if (!(cmap = make_directcolor_cmap(&fb_vinfo))) | |
874 return 1; | |
875 if (ioctl(fb_dev_fd, FBIOPUTCMAP, cmap)) { | |
876 printf(FBDEV "can't put cmap: %s\n", | |
877 strerror(errno)); | |
878 return 1; | |
879 } | |
481 | 880 fb_cmap_changed = 1; |
393 | 881 free(cmap->red); |
882 free(cmap->green); | |
883 free(cmap->blue); | |
884 free(cmap); | |
885 break; | |
886 default: | |
887 printf(FBDEV "visual: %d not yet supported\n", | |
888 fb_finfo.visual); | |
889 return 1; | |
245
cb4c682746c0
disabled scrollback buffer (virtual fb size set to real screen size)
szabii
parents:
231
diff
changeset
|
890 } |
618 | 891 |
804 | 892 if (VM || fs) { |
418 | 893 out_width = fb_vinfo.xres; |
894 out_height = fb_vinfo.yres; | |
895 } | |
896 if (out_width < in_width || out_height < in_height) { | |
897 printf(FBDEV "screensize is smaller than video size\n"); | |
898 return 1; | |
899 } | |
245
cb4c682746c0
disabled scrollback buffer (virtual fb size set to real screen size)
szabii
parents:
231
diff
changeset
|
900 |
359 | 901 fb_pixel_size = fb_vinfo.bits_per_pixel / 8; |
902 fb_real_bpp = fb_vinfo.red.length + fb_vinfo.green.length + | |
903 fb_vinfo.blue.length; | |
904 fb_bpp = (fb_pixel_size == 4) ? 32 : fb_real_bpp; | |
393 | 905 if (fb_bpp_we_want != fb_bpp) |
418 | 906 printf(FBDEV "requested %d bpp, got %d bpp)!!!\n", |
393 | 907 fb_bpp_we_want, fb_bpp); |
359 | 908 fb_screen_width = fb_finfo.line_length; |
909 fb_size = fb_finfo.smem_len; | |
225 | 910 if ((frame_buffer = (uint8_t *) mmap(0, fb_size, PROT_READ | PROT_WRITE, |
911 MAP_SHARED, fb_dev_fd, 0)) == (uint8_t *) -1) { | |
393 | 912 printf(FBDEV "Can't mmap %s: %s\n", fb_dev_name, strerror(errno)); |
913 return 1; | |
225 | 914 } |
519 | 915 L123123875 = frame_buffer + (out_width - in_width) * fb_pixel_size / |
916 2 + (out_height - in_height) * fb_screen_width / 2; | |
229 | 917 |
393 | 918 if (verbose > 0) { |
919 printf(FBDEV "other:\n"); | |
519 | 920 if (verbose > 1) { |
393 | 921 printf(FBDEV "frame_buffer @ %p\n", frame_buffer); |
519 | 922 printf(FBDEV "L123123875 @ %p\n", L123123875); |
923 } | |
393 | 924 printf(FBDEV "fb_bpp: %d\n", fb_bpp); |
925 printf(FBDEV "fb_real_bpp: %d\n", fb_real_bpp); | |
926 printf(FBDEV "fb_pixel_size: %d bytes\n", fb_pixel_size); | |
927 printf(FBDEV "pixel per line: %d\n", fb_screen_width / fb_pixel_size); | |
379 | 928 } |
225 | 929 |
658 | 930 switch (fb_bpp) { |
931 case 32: | |
932 draw_alpha_p = vo_draw_alpha_rgb32; | |
933 break; | |
934 case 24: | |
935 draw_alpha_p = vo_draw_alpha_rgb24; | |
936 break; | |
937 case 16: | |
938 draw_alpha_p = vo_draw_alpha_rgb16; | |
939 break; | |
940 case 15: | |
941 draw_alpha_p = vo_draw_alpha_rgb15; | |
942 break; | |
943 } | |
944 if (verbose > 1) | |
945 printf(FBDEV "draw_alpha_p:%dbpp = %p\n", fb_bpp, draw_alpha_p); | |
946 | |
278 | 947 if (!(next_frame = (uint8_t *) malloc(in_width * in_height * fb_pixel_size))) { |
393 | 948 printf(FBDEV "Can't malloc next_frame: %s\n", strerror(errno)); |
225 | 949 return 1; |
950 } | |
951 | |
804 | 952 if (flip & (((pixel_format & 0xff) + 7) / 8) != fb_pixel_size) { |
953 printf(FBDEV "Flipped output with depth conversion is not " | |
954 "supported\n"); | |
955 return 1; | |
956 } | |
225 | 957 if (format == IMGFMT_YV12) |
359 | 958 yuv2rgb_init(fb_bpp, MODE_RGB); |
225 | 959 return 0; |
960 } | |
961 | |
962 static uint32_t query_format(uint32_t format) | |
963 { | |
658 | 964 int ret = 0x4; /* osd/sub is supported on every bpp */ |
519 | 965 |
393 | 966 if (!fb_preinit_done) |
967 if (fb_preinit()) | |
229 | 968 return 0; |
305 | 969 if (!fb_works) |
970 return 0; | |
971 | |
311 | 972 if ((format & IMGFMT_BGR_MASK) == IMGFMT_BGR) { |
973 int bpp = format & 0xff; | |
519 | 974 |
359 | 975 if (bpp == fb_bpp) |
519 | 976 return ret|0x2; |
359 | 977 else if (bpp == 15 && fb_bpp == 16) |
519 | 978 return ret|0x1; |
359 | 979 else if (bpp == 24 && fb_bpp == 32) |
519 | 980 return ret|0x1; |
311 | 981 } |
982 if (format == IMGFMT_YV12) | |
658 | 983 return ret|0x1; |
311 | 984 return 0; |
225 | 985 } |
986 | |
987 static const vo_info_t *get_info(void) | |
988 { | |
989 return &vo_info; | |
990 } | |
991 | |
992 static void draw_alpha(int x0, int y0, int w, int h, unsigned char *src, | |
993 unsigned char *srca, int stride) | |
994 { | |
658 | 995 unsigned char *dst = next_frame + (in_width * y0 + x0) * fb_pixel_size; |
359 | 996 int dstride = in_width * fb_pixel_size; |
997 | |
658 | 998 (*draw_alpha_p)(w, h, src, srca, stride, dst, dstride); |
225 | 999 } |
1000 | |
1001 static uint32_t draw_frame(uint8_t *src[]) | |
1002 { | |
1003 if (pixel_format == IMGFMT_YV12) { | |
1004 yuv2rgb(next_frame, src[0], src[1], src[2], in_width, | |
278 | 1005 in_height, in_width * fb_pixel_size, |
225 | 1006 in_width, in_width / 2); |
804 | 1007 } else if (flip) { |
1008 int h = in_height; | |
1009 int len = in_width * fb_pixel_size; | |
1010 char *d = next_frame + (in_height - 1) * len; | |
1011 char *s = src[0]; | |
1012 while (h--) { | |
1013 memcpy(d, s, len); | |
1014 s += len; | |
1015 d -= len; | |
1016 } | |
311 | 1017 } else { |
1018 int sbpp = ((pixel_format & 0xff) + 7) / 8; | |
1019 char *d = next_frame; | |
1020 char *s = src[0]; | |
1021 if (sbpp == fb_pixel_size) { | |
359 | 1022 if (fb_real_bpp == 16 && pixel_format == (IMGFMT_BGR|15)) { |
311 | 1023 #ifdef HAVE_MMX |
1024 rgb15to16_mmx(s, d, 2 * in_width * in_height); | |
1025 #else | |
1026 unsigned short *s1 = (unsigned short *) s; | |
1027 unsigned short *d1 = (unsigned short *) d; | |
1028 unsigned short *e = s1 + in_width * in_height; | |
1029 while (s1<e) { | |
1030 register x = *(s1++); | |
1031 *(d1++) = (x&0x001f)|((x&0x7fe0)<<1); | |
1032 } | |
1033 #endif | |
1034 } else | |
1035 memcpy(d, s, sbpp * in_width * in_height); | |
1036 } | |
1037 } | |
225 | 1038 return 0; |
1039 } | |
1040 | |
1041 static uint32_t draw_slice(uint8_t *src[], int stride[], int w, int h, int x, | |
1042 int y) | |
1043 { | |
1044 uint8_t *dest; | |
1045 | |
278 | 1046 dest = next_frame + (in_width * y + x) * fb_pixel_size; |
1047 yuv2rgb(dest, src[0], src[1], src[2], w, h, in_width * fb_pixel_size, | |
225 | 1048 stride[0], stride[1]); |
1049 return 0; | |
1050 } | |
1051 | |
1052 static void check_events(void) | |
1053 { | |
1054 } | |
1055 | |
519 | 1056 static void put_frame(void) |
225 | 1057 { |
1058 int i, out_offset = 0, in_offset = 0; | |
1059 | |
1060 for (i = 0; i < in_height; i++) { | |
519 | 1061 memcpy(L123123875 + out_offset, next_frame + in_offset, |
423 | 1062 in_width * fb_pixel_size); |
359 | 1063 out_offset += fb_screen_width; |
278 | 1064 in_offset += in_width * fb_pixel_size; |
225 | 1065 } |
1066 } | |
1067 | |
246 | 1068 static void flip_page(void) |
1069 { | |
1070 vo_draw_text(in_width, in_height, draw_alpha); | |
1071 check_events(); | |
519 | 1072 put_frame(); |
246 | 1073 } |
1074 | |
225 | 1075 static void uninit(void) |
1076 { | |
379 | 1077 if (verbose > 0) |
393 | 1078 printf(FBDEV "uninit\n"); |
481 | 1079 if (fb_cmap_changed) { |
1080 if (ioctl(fb_dev_fd, FBIOPUTCMAP, &fb_oldcmap)) | |
393 | 1081 printf(FBDEV "Can't restore original cmap\n"); |
481 | 1082 fb_cmap_changed = 0; |
306 | 1083 } |
503 | 1084 free(next_frame); |
1085 if (ioctl(fb_dev_fd, FBIOGET_VSCREENINFO, &fb_vinfo)) | |
1086 printf(FBDEV "ioctl FBIOGET_VSCREENINFO: %s\n", strerror(errno)); | |
1087 fb_orig_vinfo.xoffset = fb_vinfo.xoffset; | |
1088 fb_orig_vinfo.yoffset = fb_vinfo.yoffset; | |
379 | 1089 if (ioctl(fb_dev_fd, FBIOPUT_VSCREENINFO, &fb_orig_vinfo)) |
503 | 1090 printf(FBDEV "Can't reset original fb_var_screeninfo: %s\n", strerror(errno)); |
359 | 1091 close(fb_dev_fd); |
225 | 1092 munmap(frame_buffer, fb_size); |
1093 } |