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