aboutsummaryrefslogtreecommitdiffstats
path: root/fbtv.c
blob: 2a8f940746e841c9b6517768915c7982e67bf32f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <fcntl.h>
#include <termios.h>
#include <signal.h>
#include <ctype.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <linux/fb.h>
#include <X11/Intrinsic.h>
#include <curses.h>

#include "config.h"

#ifdef HAVE_LIBJPEG
# include "jpeglib.h"
#endif

#include "mixer.h"
#include "channel.h"
#include "channels.h"
#include "grab.h"

#define MAX(x,y)        ((x)>(y)?(x):(y))
#define MIN(x,y)        ((x)<(y)?(x):(y))

#define DEFAULT_DEVICE  "/dev/fb0"

/* ---------------------------------------------------------------------- */
/* framebuffer                                                            */

char                       *fbdev = NULL;
int                        fd;
struct fb_fix_screeninfo   fix;
struct fb_var_screeninfo   var;
unsigned char              *map;

unsigned short red[256],  green[256],  blue[256];
unsigned short ored[256], ogreen[256], oblue[256];
struct fb_cmap cmap  = { 0, 256, red,  green,  blue };
struct fb_cmap ocmap = { 0, 256, ored, ogreen, oblue };

int x11_native_format,have_dga=1;
int debug,sig;
int fs_width,fs_height,fs_xoff,fs_yoff,pix_width,pix_height;
int ww,hh;

/* ---------------------------------------------------------------------- */

/*                            PAL  NTSC SECAM */
static int    maxwidth[]  = { 768, 640, 768 };
static int    maxheight[] = { 576, 480, 576 };
static int    maxx,maxy /* current */;

/*--- drivers -------------------------------------------------------------*/

extern struct GRABBER grab_v4l;
struct GRABBER *grabbers[] = {
    &grab_v4l,
};

int grabber;

/*--- channels ------------------------------------------------------------*/

struct STRTAB *cmenu = NULL;
char   title[256];

int cur_color;
int cur_bright;
int cur_hue;
int cur_contrast;
int cur_capture;

int cur_mute   = 0;
int cur_volume = 65535;

char              *ppmfile;
char              *jpegfile;

/* ---------------------------------------------------------------------- */

void
fb_gray_palette()
{
    int             i;

    for (i = 0; i < 256; i++) {
	red[i]   = i * 255;
	green[i] = i * 255;
	blue[i]  = i * 255;
    }
}

void
fb_dither_palette(int r, int g, int b)
{
    int             rs, gs, bs, i;

    rs = 256 / (r - 1);
    gs = 256 / (g - 1);
    bs = 256 / (b - 1);
    for (i = 0; i < 256-16; i++) {
	green[i+16] = (gs * ((i / (r * b)) % g)) * 255;
	red[i+16]   = (rs * ((i / b) % r)) * 255;
	blue[i+16]  = (bs * ((i) % b)) * 255;
    }
}

void
fb_cleanup(void)
{
    if (var.bits_per_pixel == 8)
	if (-1 == ioctl(fd,FBIOPUTCMAP,&ocmap))
	    perror("ioctl FBIOPUTCMAP");
    endwin();
}

void
fb_init(int gray)
{
    /* init framebuffer */
    if (-1 == (fd = open(fbdev,O_RDWR))) {
	fprintf(stderr,"open %s: %s\n",fbdev,strerror(errno));
	exit(1);
    }
    if (-1 == ioctl(fd,FBIOGET_FSCREENINFO,&fix)) {
	perror("ioctl FBIOGET_FSCREENINFO");
	exit(1);
    }
    if (-1 == ioctl(fd,FBIOGET_VSCREENINFO,&var)) {
	perror("ioctl FBIOGET_VSCREENINFO");
	exit(1);
    }
    if (fix.type != FB_TYPE_PACKED_PIXELS) {
	fprintf(stderr,"can handle only packed pixel frame buffers\n");
	exit(1);
    }
    map = mmap(NULL,fix.smem_len,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
    if (-1 == (int)map) {
	perror("mmap");
	exit(1);
    }
    switch (var.bits_per_pixel) {
    case 8:
	if (-1 == ioctl(fd,FBIOGETCMAP,&ocmap))
	    perror("ioctl FBIOGETCMAP");
	if (-1 == ioctl(fd,FBIOGETCMAP,&cmap))
	    perror("ioctl FBIOGETCMAP");
	if (gray) {
	    fb_gray_palette();
	    x11_native_format = VIDEO_GRAY;
	} else {
	    fb_dither_palette(5,9,5);
	    x11_native_format = VIDEO_RGB08;
	}
	if (-1 == ioctl(fd,FBIOPUTCMAP,&cmap))
	    perror("ioctl FBIOPUTCMAP");
	break;
    case 15:
    case 16:
	x11_native_format = (var.green.length == 6) ?
	    VIDEO_RGB16 : VIDEO_RGB15;
	break;
    case 24:
	x11_native_format = VIDEO_RGB24;
	break;
    case 32:
	x11_native_format = VIDEO_RGB32;
	break;
    default:
	fprintf(stderr, "Oops: %i bit/pixel ???\n",
		var.bits_per_pixel);
	exit(1);
    }

    initscr();
    cbreak();
    noecho();
    keypad(stdscr,1);
    refresh();
}

/* ---------------------------------------------------------------------- */

void
ctrlc(int signal)
{
    sig=1;
}

void
change_audio(int mode)
{
    if (grabbers[grabber]->grab_audio)
	grabbers[grabber]->grab_audio(-1,-1,&mode);
}

void set_capture(int j)
{
    int x,y,w,h;
    cur_capture = j;

    if (j) {
	if (ww && hh) {
	    w = ww;
	    h = hh;
	    x = var.xres_virtual-w;
	    y = 0;
	} else {
	    w = MIN(var.xres_virtual,maxx);
	    h = MIN(var.yres_virtual,maxy);
	    x = (var.xres_virtual-w)/2;
	    y = (var.yres_virtual-h)/2;
	}
	grabbers[grabber]->grab_overlay(x,y,w,h,x11_native_format,NULL,0);
    } else
	grabbers[grabber]->grab_overlay(0,0,0,0,0,NULL,0);
}

void set_norm(int j)
{
    cur_norm = j;

    grabbers[grabber]->grab_input(-1,cur_norm);
    maxx = maxwidth[cur_norm];
    maxy = maxheight[cur_norm];
}

void set_source(int j)
{
    cur_input = j;
    
    grabbers[grabber]->grab_input(cur_input,-1);
}

void set_freqtab(int j)
{
    chan_tab = j;
}

void
set_picparams(int color, int bright, int hue, int contrast)
{
    if (color != -1)
	cur_color = color;
    if (bright != -1)
	cur_bright = bright;
    if (hue != -1)
	cur_hue = hue;
    if (contrast != -1)
	cur_contrast = contrast;
    grabbers[grabber]->grab_picture(cur_color,cur_bright,cur_hue,cur_contrast);
}

void
set_channel(struct CHANNEL *channel)
{
    /* image parameters */
    set_picparams(channel->color, channel->bright,
		  channel->hue, channel->contrast);

    /* input source */
    if (cur_input   != channel->source)
	set_source(channel->source);
    if (cur_norm    != channel->norm)
	set_norm(channel->norm);

    /* station */
    cur_channel  = channel->channel;
    cur_fine     = channel->fine;
    grabbers[grabber]->grab_tune(channel->freq);

    set_capture(channel->capture);
}

void
set_volume()
{
    int vol;
    
    if (have_mixer) {
	/* sound card */
	vol = cur_volume * 100 / 65536;
	mixer_set_volume(vol);
	cur_mute ? mixer_mute() : mixer_unmute();
    } else {
	/* v4l */
	grabbers[grabber]->grab_audio(cur_mute,cur_volume,NULL);
    }
}

void
channel_menu()
{
    int  i,f,max,len;
    char key[32],ctrl[16];

    cmenu = malloc((count+1)*sizeof(struct STRTAB));
    memset(cmenu,0,(count+1)*sizeof(struct STRTAB));
    for (i = 0, max = 0; i < count; i++) {
	len = strlen(channels[i]->name);
	if (max < len)
	    max = len;
    }
    for (i = 0; i < count; i++) {
	cmenu[i].nr      = i+1;
	cmenu[i].str     = channels[i]->name;
	if (channels[i]->key) {
	    if (2 != sscanf(channels[i]->key,"%15[A-Za-z0-9_]+%31[A-Za-z0-9_]",
			    ctrl,key))
		strcpy(key,channels[i]->key);
	    if (1 == sscanf(key,"F%d",&f)) {
		channels[i]->ckey = KEY_F(f);              /* Function keys */
	    } else if (strlen(key) == 1) {
		if (isalpha(key[0]))
		    key[0] = tolower(key[0]);
		channels[i]->ckey = (int)key[0];           /* single letter/digit */
	    }
	}
    }
}

/*--- grab (single frames) to file ---------------------------------------*/

void
patch_up(char *name)
{
    char *ptr;
    
    for (ptr = name+strlen(name); ptr >= name; ptr--)
	if (isdigit(*ptr))
	    break;
    if (ptr < name)
	return;
    while (*ptr == '9' && ptr >= name)
	*(ptr--) = '0';
    if (ptr < name)
	return;
    if (isdigit(*ptr))
	(*ptr)++;
}

#ifdef HAVE_LIBJPEG
static int write_jpeg(char *filename, char *data, int width, int height)
{
    struct jpeg_compress_struct cinfo;
    struct jpeg_error_mgr jerr;
    FILE *fp;
    int i;
    unsigned char *line;

    if (NULL == (fp = fopen(filename,"w"))) {
	fprintf(stderr,"grab: can't open %s: %s\n",filename,strerror(errno));
	return -1;
    }

    cinfo.err = jpeg_std_error(&jerr);
    jpeg_create_compress(&cinfo);
    jpeg_stdio_dest(&cinfo, fp);
    cinfo.image_width = width;
    cinfo.image_height = height;
    cinfo.input_components = 3;
    cinfo.in_color_space = JCS_RGB;
    jpeg_set_defaults(&cinfo);
    jpeg_set_quality(&cinfo, 75, TRUE);
    jpeg_start_compress(&cinfo, TRUE);

    for (i = 0, line = data; i < height; i++, line += width*3)
	jpeg_write_scanlines(&cinfo, &line, 1);
    
    jpeg_finish_compress(&(cinfo));
    jpeg_destroy_compress(&(cinfo));
    fclose(fp);

    return 0;
}
#endif

static int write_ppm(char *filename, char *data, int width, int height)
{
    FILE *fp;
    
    if (NULL == (fp = fopen(filename,"w"))) {
	fprintf(stderr,"grab: can't open %s: %s\n",filename,strerror(errno));
	return -1;
    }
    fprintf(fp,"P6\n%d %d\n255\n",width,height);
    fwrite(data,height,3*width,fp);
    fclose(fp);

    return 0;
}

void
snap(int jpeg)
{
    void *buffer;
    Dimension width  = maxwidth[cur_norm];
    Dimension height = maxheight[cur_norm];

    if (!grabbers[grabber]->grab_one)
	return;

    if (NULL == jpegfile) {
	jpegfile = malloc(32);
	strcpy(jpegfile, "snap0000.jpeg");
    }
    if (NULL == ppmfile) {
	ppmfile = malloc(32);
	strcpy(ppmfile, "snap0000.ppm");
    }
    
    mvprintw(0,0,"grabbing image to %s ... ", jpeg ? jpegfile : ppmfile);
    refresh();
    
    if (NULL == (buffer = grabbers[grabber]->grab_one(width,height))) {
	printw("FAILED");
    } else {
	if (jpeg) {
#ifdef HAVE_LIBJPEG
	    write_jpeg(jpegfile,buffer,width,height);
	    patch_up(jpegfile);
#endif
	} else {
	    write_ppm(ppmfile,buffer,width,height);
	    patch_up(ppmfile);
	}
	printw("OK");
    }
    refresh();
    sleep(1);
}

/*--- main ---------------------------------------------------------------*/

static void
grabber_init()
{
    int sw,sh;
    void *base;

    sw   = var.xres_virtual;
    sh   = var.yres_virtual;
    base = fix.smem_start;
    for (grabber = 0; grabber < sizeof(grabbers)/sizeof(struct GRABBERS*);
	 grabber++) {
	if (-1 != grabbers[grabber]->grab_open
	    (NULL,sw,sh,x11_native_format,x11_native_format,base,sw))
	    break;
    }
    if (grabber == sizeof(grabbers)/sizeof(struct GRABBERS*)) {
	fprintf(stderr,"no video grabber device available\n");
	exit(1);
    }
}

int
main(int argc, char *argv[])
{
    int             freq=0,key,i,c,gray=0;
    char            *line;

    if (NULL != (line = getenv("FRAMEBUFFER"))) {
	fbdev = line;
    }

    for (;;) {
	c = getopt(argc, argv, "gvd:o:j:s:");
	if (c == -1)
	    break;
	switch (c) {
	case 'g':
	    gray = 1;
	    break;
	case 'v':
	    debug = 1;
	    break;
	case 'd':
	    fbdev = optarg;
	    break;
	case 'o':
	    ppmfile = strdup(optarg);
	    break;
	case 'j':
	    jpegfile = strdup(optarg);
	    break;
	case 's':
	    sscanf(optarg,"%dx%d",&ww,&hh);
	    break;
	default:
	    exit(1);
	}
    }

    if (NULL == fbdev)
	fbdev = DEFAULT_DEVICE;

    switch (system("v4l-conf -q")) {
    case -1: /* can't run */
	fprintf(stderr,"could'nt start v4l-conf\n");
	break;
    case 0: /* ok */
	break;
    default: /* non-zero return */
	fprintf(stderr,"v4l-conf had some trouble, "
		"trying to continue anyway\n");
	break;
    }

    fb_init(gray);
    atexit(fb_cleanup);
    signal(SIGINT,ctrlc);
    
    grabber_init();
    read_config();
    set_freqtab(chan_tab);
    channel_menu();
    if (have_mixer)
	cur_volume = mixer_get_volume() * 65535/100;
    set_volume();
    cur_capture = 0;

    if (optind < argc)
	for (i = 0; i < count; i++)
	    if (0 == strcasecmp(channels[i]->name,argv[optind]))
		cur_sender = i;
    if (count) {
	if ((cur_sender < 0) || (cur_sender >= count))
	    cur_sender = 0;
	set_channel(channels[cur_sender]);
    } else {
	set_channel(&defaults);
    }

    for (;!sig;) {
	clear();
	if (cur_sender != -1) {
	    printw("Framebuffer TV  -  %s\n",channels[cur_sender]->name);
	} else {
	    printw("Framebuffer TV  -  channel %s", tvtuner[cur_channel].name);
	    if (cur_fine)
		printw(" (%+d)",cur_fine);
	    printw("  freq %.3f\n",(float)freq/16);
	}
	refresh();
	switch (key = getch()) {
	case 27: /* ESC */
	case 'q':
	case 'Q':
	    sig=1;
	    break;

	case '+':
	    cur_volume += 512;
	    if (cur_volume > 65535)
		cur_volume = 65535;
	    set_volume();
	    break;
	case '-':
	    cur_volume -= 512;
	    if (cur_volume < 0)
		cur_volume = 0;
	    set_volume();
	    break;
	case 10: /* ENTER/RETURN */
	case KEY_ENTER:
	    cur_mute = !cur_mute;
	    set_volume();
	    break;

	case 'G':
	case 'g':
	    snap(0);
	    break;
	case 'J':
	case 'j':
	    snap(1);
	    break;

	case 339 /* PgUp  --  is'nt in curses.h ??? */:
	    cur_sender = (cur_sender+1)%count;
	    set_channel(channels[cur_sender]);
	    break;
	case 338 /* PgDown */:
	    cur_sender = (cur_sender+count-1)%count;
	    set_channel(channels[cur_sender]);
	    break;

	case KEY_UP:
	    do {
		cur_channel = (cur_channel+1) % CHAN_ENTRIES;
	    } while (!tvtuner[cur_channel].freq[chan_tab]);
	    cur_fine = 0;
	    cur_sender = -1;
	    freq = get_freq(cur_channel)+cur_fine;
	    grabbers[grabber]->grab_tune(freq);
	    break;
	case KEY_DOWN:
	    do {
		cur_channel = (cur_channel+CHAN_ENTRIES-1) % CHAN_ENTRIES;
	    } while (!tvtuner[cur_channel].freq[chan_tab]);
	    cur_fine = 0;
	    cur_sender = -1;
	    freq = get_freq(cur_channel)+cur_fine;
	    grabbers[grabber]->grab_tune(freq);
	    break;
	case KEY_LEFT:
	    cur_fine--;
	    cur_sender = -1;
	    freq = get_freq(cur_channel)+cur_fine;
	    grabbers[grabber]->grab_tune(freq);
	    break;
	case KEY_RIGHT:
	    cur_fine++;
	    cur_sender = -1;
	    freq = get_freq(cur_channel)+cur_fine;
	    grabbers[grabber]->grab_tune(freq);
	    break;

	case -1:
	    break;

	default:
	    if (isalpha(key))
		key = tolower(key);
	    for (i = 0; i < count; i++)
		if (channels[i]->ckey == key) {
		    cur_sender = i;
		    break;
		}
	    if (i < count) {
		set_channel(channels[cur_sender]);
	    } else {
		printw("key: %d 0x%x ",key,key);
		if (key > 0x20 && key < 128) printw("'%c' ",key);
		refresh();
		sleep(2);
	    }
	}
    }
    grabbers[grabber]->grab_close();
    fprintf(stderr,"\033[H\033[J");
    exit(0);
}

Privacy Policy