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