Mercurial > mplayer.hg
annotate libvo/vo_fbdev.c @ 745:c653430f0789
<sigh> links replaced again </sigh>
author | gabucino |
---|---|
date | Wed, 09 May 2001 23:41:01 +0000 |
parents | 121cb2047c08 |
children | c3fad5e1698e |
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; | |
591 | |
305 | 592 /* |
593 * Note: this function is completely cut'n'pasted from | |
594 * Chris Lawrence's code. | |
311 | 595 * (modified a bit to fit in my code...) |
305 | 596 */ |
597 struct fb_cmap *make_directcolor_cmap(struct fb_var_screeninfo *var) | |
598 { | |
599 /* Hopefully any DIRECTCOLOR device will have a big enough palette | |
600 * to handle mapping the full color depth. | |
601 * e.g. 8 bpp -> 256 entry palette | |
602 * | |
603 * We could handle some sort of gamma here | |
604 */ | |
605 int i, cols, rcols, gcols, bcols; | |
606 uint16_t *red, *green, *blue; | |
607 struct fb_cmap *cmap; | |
608 | |
609 rcols = 1 << var->red.length; | |
610 gcols = 1 << var->green.length; | |
611 bcols = 1 << var->blue.length; | |
612 | |
613 /* Make our palette the length of the deepest color */ | |
614 cols = (rcols > gcols ? rcols : gcols); | |
615 cols = (cols > bcols ? cols : bcols); | |
616 | |
617 red = malloc(cols * sizeof(red[0])); | |
618 if(!red) { | |
619 printf("Can't allocate red palette with %d entries.\n", cols); | |
620 return NULL; | |
621 } | |
622 for(i=0; i< rcols; i++) | |
623 red[i] = (65535/(rcols-1)) * i; | |
624 | |
625 green = malloc(cols * sizeof(green[0])); | |
626 if(!green) { | |
627 printf("Can't allocate green palette with %d entries.\n", cols); | |
628 free(red); | |
629 return NULL; | |
630 } | |
631 for(i=0; i< gcols; i++) | |
632 green[i] = (65535/(gcols-1)) * i; | |
633 | |
634 blue = malloc(cols * sizeof(blue[0])); | |
635 if(!blue) { | |
636 printf("Can't allocate blue palette with %d entries.\n", cols); | |
637 free(red); | |
638 free(green); | |
639 return NULL; | |
640 } | |
641 for(i=0; i< bcols; i++) | |
642 blue[i] = (65535/(bcols-1)) * i; | |
643 | |
644 cmap = malloc(sizeof(struct fb_cmap)); | |
645 if(!cmap) { | |
646 printf("Can't allocate color map\n"); | |
647 free(red); | |
648 free(green); | |
649 free(blue); | |
650 return NULL; | |
651 } | |
652 cmap->start = 0; | |
653 cmap->transp = 0; | |
654 cmap->len = cols; | |
655 cmap->red = red; | |
656 cmap->blue = blue; | |
657 cmap->green = green; | |
658 cmap->transp = NULL; | |
659 | |
660 return cmap; | |
661 } | |
225 | 662 |
393 | 663 static int fb_preinit(void) |
225 | 664 { |
393 | 665 if (!fb_dev_name && !(fb_dev_name = getenv("FRAMEBUFFER"))) |
666 fb_dev_name = "/dev/fb0"; | |
667 if (verbose > 0) | |
668 printf(FBDEV "using %s\n", fb_dev_name); | |
225 | 669 |
393 | 670 if ((fb_dev_fd = open(fb_dev_name, O_RDWR)) == -1) { |
671 printf(FBDEV "Can't open %s: %s\n", fb_dev_name, strerror(errno)); | |
672 goto err_out; | |
673 } | |
674 if (ioctl(fb_dev_fd, FBIOGET_VSCREENINFO, &fb_vinfo)) { | |
675 printf(FBDEV "Can't get VSCREENINFO: %s\n", strerror(errno)); | |
676 goto err_out_fd; | |
677 } | |
418 | 678 fb_orig_vinfo = fb_vinfo; |
393 | 679 |
663 | 680 fb_bpp = fb_vinfo.bits_per_pixel; |
681 | |
682 /* 16 and 15 bpp is reported 16 bpp */ | |
683 if (fb_bpp == 16) | |
684 fb_bpp = fb_vinfo.red.length + fb_vinfo.green.length + | |
685 fb_vinfo.blue.length; | |
686 | |
418 | 687 if (vo_dbpp) { |
688 if (vo_dbpp != 15 && vo_dbpp != 16 && vo_dbpp != 24 && | |
689 vo_dbpp != 32) { | |
690 printf(FBDEV "can't switch to %d bpp\n", vo_dbpp); | |
383 | 691 goto err_out; |
359 | 692 } |
418 | 693 fb_bpp = vo_dbpp; |
359 | 694 } |
245
cb4c682746c0
disabled scrollback buffer (virtual fb size set to real screen size)
szabii
parents:
231
diff
changeset
|
695 |
393 | 696 fb_preinit_done = 1; |
697 fb_works = 1; | |
698 return 0; | |
699 err_out_fd: | |
700 close(fb_dev_fd); | |
701 fb_dev_fd = -1; | |
702 err_out: | |
703 fb_preinit_done = 1; | |
704 return 1; | |
705 } | |
245
cb4c682746c0
disabled scrollback buffer (virtual fb size set to real screen size)
szabii
parents:
231
diff
changeset
|
706 |
519 | 707 static void clear_bg(void) |
708 { | |
709 int i, offset = 0; | |
710 | |
711 for (i = 0; i < out_height; i++, offset += fb_screen_width) | |
712 memset(frame_buffer + offset, 0x0, out_width * fb_pixel_size); | |
713 } | |
418 | 714 |
633 | 715 static void lots_of_printf(void) |
716 { | |
717 if (verbose > 0) { | |
718 printf(FBDEV "var info:\n"); | |
719 printf(FBDEV "xres: %u\n", fb_vinfo.xres); | |
720 printf(FBDEV "yres: %u\n", fb_vinfo.yres); | |
721 printf(FBDEV "xres_virtual: %u\n", fb_vinfo.xres_virtual); | |
722 printf(FBDEV "yres_virtual: %u\n", fb_vinfo.yres_virtual); | |
723 printf(FBDEV "xoffset: %u\n", fb_vinfo.xoffset); | |
724 printf(FBDEV "yoffset: %u\n", fb_vinfo.yoffset); | |
725 printf(FBDEV "bits_per_pixel: %u\n", fb_vinfo.bits_per_pixel); | |
726 printf(FBDEV "grayscale: %u\n", fb_vinfo.grayscale); | |
727 printf(FBDEV "red: %lu %lu %lu\n", | |
728 (unsigned long) fb_vinfo.red.offset, | |
729 (unsigned long) fb_vinfo.red.length, | |
730 (unsigned long) fb_vinfo.red.msb_right); | |
731 printf(FBDEV "green: %lu %lu %lu\n", | |
732 (unsigned long) fb_vinfo.green.offset, | |
733 (unsigned long) fb_vinfo.green.length, | |
734 (unsigned long) fb_vinfo.green.msb_right); | |
735 printf(FBDEV "blue: %lu %lu %lu\n", | |
736 (unsigned long) fb_vinfo.blue.offset, | |
737 (unsigned long) fb_vinfo.blue.length, | |
738 (unsigned long) fb_vinfo.blue.msb_right); | |
739 printf(FBDEV "transp: %lu %lu %lu\n", | |
740 (unsigned long) fb_vinfo.transp.offset, | |
741 (unsigned long) fb_vinfo.transp.length, | |
742 (unsigned long) fb_vinfo.transp.msb_right); | |
743 printf(FBDEV "nonstd: %u\n", fb_vinfo.nonstd); | |
744 if (verbose > 1) { | |
745 printf(FBDEV "activate: %u\n", fb_vinfo.activate); | |
746 printf(FBDEV "height: %u\n", fb_vinfo.height); | |
747 printf(FBDEV "width: %u\n", fb_vinfo.width); | |
748 printf(FBDEV "accel_flags: %u\n", fb_vinfo.accel_flags); | |
749 printf(FBDEV "timing:\n"); | |
750 printf(FBDEV "pixclock: %u\n", fb_vinfo.pixclock); | |
751 printf(FBDEV "left_margin: %u\n", fb_vinfo.left_margin); | |
752 printf(FBDEV "right_margin: %u\n", fb_vinfo.right_margin); | |
753 printf(FBDEV "upper_margin: %u\n", fb_vinfo.upper_margin); | |
754 printf(FBDEV "lower_margin: %u\n", fb_vinfo.lower_margin); | |
755 printf(FBDEV "hsync_len: %u\n", fb_vinfo.hsync_len); | |
756 printf(FBDEV "vsync_len: %u\n", fb_vinfo.vsync_len); | |
757 printf(FBDEV "sync: %u\n", fb_vinfo.sync); | |
758 printf(FBDEV "vmode: %u\n", fb_vinfo.vmode); | |
759 } | |
760 printf(FBDEV "fix info:\n"); | |
761 printf(FBDEV "framebuffer size: %d bytes\n", fb_finfo.smem_len); | |
762 printf(FBDEV "type: %lu\n", (unsigned long) fb_finfo.type); | |
763 printf(FBDEV "type_aux: %lu\n", (unsigned long) fb_finfo.type_aux); | |
764 printf(FBDEV "visual: %lu\n", (unsigned long) fb_finfo.visual); | |
765 printf(FBDEV "line_length: %lu bytes\n", (unsigned long) fb_finfo.line_length); | |
766 if (verbose > 1) { | |
767 printf(FBDEV "id: %.16s\n", fb_finfo.id); | |
768 printf(FBDEV "smem_start: %p\n", (void *) fb_finfo.smem_start); | |
769 printf(FBDEV "xpanstep: %u\n", fb_finfo.xpanstep); | |
770 printf(FBDEV "ypanstep: %u\n", fb_finfo.ypanstep); | |
771 printf(FBDEV "ywrapstep: %u\n", fb_finfo.ywrapstep); | |
772 printf(FBDEV "mmio_start: %p\n", (void *) fb_finfo.mmio_start); | |
773 printf(FBDEV "mmio_len: %u bytes\n", fb_finfo.mmio_len); | |
774 printf(FBDEV "accel: %u\n", fb_finfo.accel); | |
775 } | |
776 } | |
777 } | |
658 | 778 |
393 | 779 static uint32_t init(uint32_t width, uint32_t height, uint32_t d_width, |
780 uint32_t d_height, uint32_t fullscreen, char *title, | |
781 uint32_t format) | |
782 { | |
418 | 783 #define FS (fullscreen & 0x01) |
784 #define VM (fullscreen & 0x02) | |
785 #define ZOOM (fullscreen & 0x04) | |
786 | |
393 | 787 struct fb_cmap *cmap; |
788 | |
789 if (!fb_preinit_done) | |
790 if (fb_preinit()) | |
791 return 1; | |
792 if (!fb_works) | |
793 return 1; | |
245
cb4c682746c0
disabled scrollback buffer (virtual fb size set to real screen size)
szabii
parents:
231
diff
changeset
|
794 |
418 | 795 if (ZOOM) { |
796 printf(FBDEV "-zoom is not supported\n"); | |
797 return 1; | |
798 } | |
799 if (fb_mode_name && !VM) { | |
800 printf(FBDEV "-fbmode can be used only with -vm" | |
801 " (is it the right behaviour?)\n"); | |
802 return 1; | |
803 } | |
804 if (VM) | |
805 if (parse_fbmode_cfg(fb_mode_cfgfile) < 0) | |
806 return 1; | |
658 | 807 #if 0 |
418 | 808 if ((!d_width + !d_height) == 1) { |
809 printf(FBDEV "use both -x and -y, or none of them\n"); | |
810 return 1; | |
811 } | |
658 | 812 #endif |
418 | 813 if (d_width) { |
814 out_width = d_width; | |
815 out_height = d_height; | |
816 } else { | |
817 out_width = width; | |
818 out_height = height; | |
819 } | |
820 in_width = width; | |
821 in_height = height; | |
822 pixel_format = format; | |
823 | |
393 | 824 if (fb_mode_name) { |
418 | 825 if (!(fb_mode = find_mode_by_name(fb_mode_name))) { |
826 printf(FBDEV "can't find requested video mode\n"); | |
827 return 1; | |
828 } | |
829 fb_mode2fb_vinfo(fb_mode, &fb_vinfo); | |
830 } else if (VM) { | |
831 monitor_hfreq = str2range(monitor_hfreq_str); | |
832 monitor_vfreq = str2range(monitor_vfreq_str); | |
833 monitor_dotclock = str2range(monitor_dotclock_str); | |
834 if (!monitor_hfreq || !monitor_vfreq || !monitor_dotclock) { | |
835 printf(FBDEV "you have to specify the capabilities of" | |
836 " the monitor.\n"); | |
837 return 1; | |
838 } | |
839 if (!(fb_mode = find_best_mode(out_width, out_height, | |
840 monitor_hfreq, monitor_vfreq, | |
841 monitor_dotclock))) { | |
842 printf(FBDEV "can't find best video mode\n"); | |
843 return 1; | |
844 } | |
519 | 845 printf(FBDEV "using mode %dx%d @ %.1fHz\n", fb_mode->xres, |
846 fb_mode->yres, vsf(fb_mode)); | |
418 | 847 fb_mode2fb_vinfo(fb_mode, &fb_vinfo); |
359 | 848 } |
418 | 849 fb_bpp_we_want = fb_bpp; |
850 set_bpp(&fb_vinfo, fb_bpp); | |
359 | 851 fb_vinfo.xres_virtual = fb_vinfo.xres; |
852 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
|
853 |
359 | 854 if (ioctl(fb_dev_fd, FBIOPUT_VSCREENINFO, &fb_vinfo)) { |
393 | 855 printf(FBDEV "Can't put VSCREENINFO: %s\n", strerror(errno)); |
856 return 1; | |
857 } | |
618 | 858 if (ioctl(fb_dev_fd, FBIOGET_FSCREENINFO, &fb_finfo)) { |
859 printf(FBDEV "Can't get FSCREENINFO: %s\n", strerror(errno)); | |
860 return 1; | |
861 } | |
633 | 862 |
863 lots_of_printf(); | |
245
cb4c682746c0
disabled scrollback buffer (virtual fb size set to real screen size)
szabii
parents:
231
diff
changeset
|
864 |
633 | 865 if (fb_finfo.type != FB_TYPE_PACKED_PIXELS) { |
866 printf(FBDEV "type %d not supported\n", fb_finfo.type); | |
867 return 1; | |
305 | 868 } |
618 | 869 |
379 | 870 switch (fb_finfo.visual) { |
393 | 871 case FB_VISUAL_TRUECOLOR: |
872 break; | |
873 case FB_VISUAL_DIRECTCOLOR: | |
874 if (verbose > 0) | |
875 printf(FBDEV "creating cmap for directcolor\n"); | |
481 | 876 if (ioctl(fb_dev_fd, FBIOGETCMAP, &fb_oldcmap)) { |
393 | 877 printf(FBDEV "can't get cmap: %s\n", |
878 strerror(errno)); | |
879 return 1; | |
880 } | |
881 if (!(cmap = make_directcolor_cmap(&fb_vinfo))) | |
882 return 1; | |
883 if (ioctl(fb_dev_fd, FBIOPUTCMAP, cmap)) { | |
884 printf(FBDEV "can't put cmap: %s\n", | |
885 strerror(errno)); | |
886 return 1; | |
887 } | |
481 | 888 fb_cmap_changed = 1; |
393 | 889 free(cmap->red); |
890 free(cmap->green); | |
891 free(cmap->blue); | |
892 free(cmap); | |
893 break; | |
633 | 894 // case FB_VISUAL_PSEUDOCOLOR: |
895 // printf(FBDEV "visual is FB_VISUAL_PSEUDOCOLOR." | |
896 // "it's not tested!\n"); | |
897 // break; | |
393 | 898 default: |
899 printf(FBDEV "visual: %d not yet supported\n", | |
900 fb_finfo.visual); | |
901 return 1; | |
245
cb4c682746c0
disabled scrollback buffer (virtual fb size set to real screen size)
szabii
parents:
231
diff
changeset
|
902 } |
618 | 903 |
418 | 904 if (FS || (d_width && VM)) { |
905 out_width = fb_vinfo.xres; | |
906 out_height = fb_vinfo.yres; | |
907 } | |
908 if (out_width < in_width || out_height < in_height) { | |
909 printf(FBDEV "screensize is smaller than video size\n"); | |
910 return 1; | |
911 } | |
245
cb4c682746c0
disabled scrollback buffer (virtual fb size set to real screen size)
szabii
parents:
231
diff
changeset
|
912 |
359 | 913 fb_pixel_size = fb_vinfo.bits_per_pixel / 8; |
914 fb_real_bpp = fb_vinfo.red.length + fb_vinfo.green.length + | |
915 fb_vinfo.blue.length; | |
916 fb_bpp = (fb_pixel_size == 4) ? 32 : fb_real_bpp; | |
393 | 917 if (fb_bpp_we_want != fb_bpp) |
418 | 918 printf(FBDEV "requested %d bpp, got %d bpp)!!!\n", |
393 | 919 fb_bpp_we_want, fb_bpp); |
359 | 920 fb_screen_width = fb_finfo.line_length; |
921 fb_size = fb_finfo.smem_len; | |
225 | 922 if ((frame_buffer = (uint8_t *) mmap(0, fb_size, PROT_READ | PROT_WRITE, |
923 MAP_SHARED, fb_dev_fd, 0)) == (uint8_t *) -1) { | |
393 | 924 printf(FBDEV "Can't mmap %s: %s\n", fb_dev_name, strerror(errno)); |
925 return 1; | |
225 | 926 } |
519 | 927 L123123875 = frame_buffer + (out_width - in_width) * fb_pixel_size / |
928 2 + (out_height - in_height) * fb_screen_width / 2; | |
229 | 929 |
393 | 930 if (verbose > 0) { |
931 printf(FBDEV "other:\n"); | |
519 | 932 if (verbose > 1) { |
393 | 933 printf(FBDEV "frame_buffer @ %p\n", frame_buffer); |
519 | 934 printf(FBDEV "L123123875 @ %p\n", L123123875); |
935 } | |
393 | 936 printf(FBDEV "fb_bpp: %d\n", fb_bpp); |
937 printf(FBDEV "fb_real_bpp: %d\n", fb_real_bpp); | |
938 printf(FBDEV "fb_pixel_size: %d bytes\n", fb_pixel_size); | |
939 printf(FBDEV "pixel per line: %d\n", fb_screen_width / fb_pixel_size); | |
379 | 940 } |
225 | 941 |
658 | 942 switch (fb_bpp) { |
943 case 32: | |
944 draw_alpha_p = vo_draw_alpha_rgb32; | |
945 break; | |
946 case 24: | |
947 draw_alpha_p = vo_draw_alpha_rgb24; | |
948 break; | |
949 case 16: | |
950 draw_alpha_p = vo_draw_alpha_rgb16; | |
951 break; | |
952 case 15: | |
953 draw_alpha_p = vo_draw_alpha_rgb15; | |
954 break; | |
955 } | |
956 if (verbose > 1) | |
957 printf(FBDEV "draw_alpha_p:%dbpp = %p\n", fb_bpp, draw_alpha_p); | |
958 | |
278 | 959 if (!(next_frame = (uint8_t *) malloc(in_width * in_height * fb_pixel_size))) { |
393 | 960 printf(FBDEV "Can't malloc next_frame: %s\n", strerror(errno)); |
225 | 961 return 1; |
962 } | |
963 | |
964 if (format == IMGFMT_YV12) | |
359 | 965 yuv2rgb_init(fb_bpp, MODE_RGB); |
519 | 966 clear_bg(); |
225 | 967 return 0; |
968 } | |
969 | |
970 static uint32_t query_format(uint32_t format) | |
971 { | |
658 | 972 int ret = 0x4; /* osd/sub is supported on every bpp */ |
519 | 973 |
393 | 974 if (!fb_preinit_done) |
975 if (fb_preinit()) | |
229 | 976 return 0; |
305 | 977 if (!fb_works) |
978 return 0; | |
979 | |
311 | 980 if ((format & IMGFMT_BGR_MASK) == IMGFMT_BGR) { |
981 int bpp = format & 0xff; | |
519 | 982 |
359 | 983 if (bpp == fb_bpp) |
519 | 984 return ret|0x2; |
359 | 985 else if (bpp == 15 && fb_bpp == 16) |
519 | 986 return ret|0x1; |
359 | 987 else if (bpp == 24 && fb_bpp == 32) |
519 | 988 return ret|0x1; |
311 | 989 } |
990 if (format == IMGFMT_YV12) | |
658 | 991 return ret|0x1; |
311 | 992 return 0; |
225 | 993 } |
994 | |
995 static const vo_info_t *get_info(void) | |
996 { | |
997 return &vo_info; | |
998 } | |
999 | |
1000 static void draw_alpha(int x0, int y0, int w, int h, unsigned char *src, | |
1001 unsigned char *srca, int stride) | |
1002 { | |
658 | 1003 unsigned char *dst = next_frame + (in_width * y0 + x0) * fb_pixel_size; |
359 | 1004 int dstride = in_width * fb_pixel_size; |
1005 | |
658 | 1006 (*draw_alpha_p)(w, h, src, srca, stride, dst, dstride); |
225 | 1007 } |
1008 | |
1009 static uint32_t draw_frame(uint8_t *src[]) | |
1010 { | |
1011 if (pixel_format == IMGFMT_YV12) { | |
1012 yuv2rgb(next_frame, src[0], src[1], src[2], in_width, | |
278 | 1013 in_height, in_width * fb_pixel_size, |
225 | 1014 in_width, in_width / 2); |
311 | 1015 } else { |
1016 int sbpp = ((pixel_format & 0xff) + 7) / 8; | |
1017 char *d = next_frame; | |
1018 char *s = src[0]; | |
1019 if (sbpp == fb_pixel_size) { | |
359 | 1020 if (fb_real_bpp == 16 && pixel_format == (IMGFMT_BGR|15)) { |
311 | 1021 #ifdef HAVE_MMX |
1022 rgb15to16_mmx(s, d, 2 * in_width * in_height); | |
1023 #else | |
1024 unsigned short *s1 = (unsigned short *) s; | |
1025 unsigned short *d1 = (unsigned short *) d; | |
1026 unsigned short *e = s1 + in_width * in_height; | |
1027 while (s1<e) { | |
1028 register x = *(s1++); | |
1029 *(d1++) = (x&0x001f)|((x&0x7fe0)<<1); | |
1030 } | |
1031 #endif | |
1032 } else | |
1033 memcpy(d, s, sbpp * in_width * in_height); | |
1034 } | |
1035 } | |
225 | 1036 return 0; |
1037 } | |
1038 | |
1039 static uint32_t draw_slice(uint8_t *src[], int stride[], int w, int h, int x, | |
1040 int y) | |
1041 { | |
1042 uint8_t *dest; | |
1043 | |
278 | 1044 dest = next_frame + (in_width * y + x) * fb_pixel_size; |
1045 yuv2rgb(dest, src[0], src[1], src[2], w, h, in_width * fb_pixel_size, | |
225 | 1046 stride[0], stride[1]); |
1047 return 0; | |
1048 } | |
1049 | |
1050 static void check_events(void) | |
1051 { | |
1052 } | |
1053 | |
519 | 1054 static void put_frame(void) |
225 | 1055 { |
1056 int i, out_offset = 0, in_offset = 0; | |
1057 | |
1058 for (i = 0; i < in_height; i++) { | |
519 | 1059 memcpy(L123123875 + out_offset, next_frame + in_offset, |
423 | 1060 in_width * fb_pixel_size); |
359 | 1061 out_offset += fb_screen_width; |
278 | 1062 in_offset += in_width * fb_pixel_size; |
225 | 1063 } |
1064 } | |
1065 | |
246 | 1066 static void flip_page(void) |
1067 { | |
1068 vo_draw_text(in_width, in_height, draw_alpha); | |
1069 check_events(); | |
519 | 1070 put_frame(); |
246 | 1071 } |
1072 | |
225 | 1073 static void uninit(void) |
1074 { | |
379 | 1075 if (verbose > 0) |
393 | 1076 printf(FBDEV "uninit\n"); |
481 | 1077 if (fb_cmap_changed) { |
1078 if (ioctl(fb_dev_fd, FBIOPUTCMAP, &fb_oldcmap)) | |
393 | 1079 printf(FBDEV "Can't restore original cmap\n"); |
481 | 1080 fb_cmap_changed = 0; |
306 | 1081 } |
503 | 1082 free(next_frame); |
1083 if (ioctl(fb_dev_fd, FBIOGET_VSCREENINFO, &fb_vinfo)) | |
1084 printf(FBDEV "ioctl FBIOGET_VSCREENINFO: %s\n", strerror(errno)); | |
1085 fb_orig_vinfo.xoffset = fb_vinfo.xoffset; | |
1086 fb_orig_vinfo.yoffset = fb_vinfo.yoffset; | |
379 | 1087 if (ioctl(fb_dev_fd, FBIOPUT_VSCREENINFO, &fb_orig_vinfo)) |
503 | 1088 printf(FBDEV "Can't reset original fb_var_screeninfo: %s\n", strerror(errno)); |
359 | 1089 close(fb_dev_fd); |
225 | 1090 munmap(frame_buffer, fb_size); |
1091 } |