aboutsummaryrefslogtreecommitdiffstats
path: root/test/video.c
blob: fae421e3c1c519a516663bc53929d46ef966fa91 (plain)
1
2
3
4
5
6
7
8
9
10
11
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
# UK, Huntshaw Cross
# Auto-generated from http://www.dtg.org.uk/retailer/dtt_channels.html
# and http://www.ofcom.org.uk/static/reception_advice/index.asp.html
# T freq bw fec_hi fec_lo mod transmission-mode guard-interval hierarchy
T 737833000 8MHz 3/4 NONE QAM16 2k 1/32 NONE
T 769833000 8MHz 2
#define USAGE \
"\n" \
"\n A tiny video watching application, just starts capturing /dev/video" \
"\n into /dev/fb0." \
"\n Be shure to have >8Bit/pixel color resolution and r/w access for " \
"\n /dev/video0, /dev/fb0 and /dev/tty0 to let this work..." \
"\n" \
"\n  compile with" \
"\n" \
"\n  $ gcc -g -Wall -O2 -o video video.c -I../../ost/include" \
"\n"

#include <sys/mman.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <linux/fb.h>
#include <linux/videodev.h>

#define VIDEO_DEV "/dev/video0"
#define FB_DEV "/dev/fb0"
#define VT_DEV "/dev/tty0"

static char *video_devname = VIDEO_DEV;

#define min(a,b)	(a) < (b) ? (a) : (b)

static int zero = 0;
static int one = 1;

static struct fb_var_screeninfo fb_var;
static struct fb_fix_screeninfo fb_fix;


int init_fb (void)
{
	const char blankoff_str[] = "\033[9;0]";
	int fd, vt_fd;

	fd = open (FB_DEV, O_RDWR);
	if (fd < 0) {
		perror("Could not open " FB_DEV ", please check permissions\n");
		return 1;
	}

	if ((vt_fd = open( VT_DEV, O_RDWR )) < 0) {
		perror("Could not open " VT_DEV ", please check permissions\n");
		return 1;
	}

	write( vt_fd, blankoff_str, strlen(blankoff_str) );

	if (ioctl (fd, FBIOGET_VSCREENINFO, &fb_var) < 0) {
		perror("Could not get variable screen information (fb_var)\n");
		return 1;
	}

	if (ioctl (fd, FBIOGET_FSCREENINFO, &fb_fix) < 0) {
		perror("Could not get fixed screen information (fb_fix)\n");
		return 1;
	}

	close (fd);
	return 0;
}


int init_video (int stop)
{
	int fd;
	struct video_capability vcap;

	if ((fd = open (video_devname, O_RDWR)) < 0) {
		fprintf (stderr,
			 "%s: Could not open %s, please check permissions\n",
			 __FUNCTION__, video_devname);
		return -1;
	}

	ioctl(fd, VIDIOCGCAP, &vcap);

	if (ioctl(fd, VIDIOCCAPTURE, &zero) < 0) {
		perror("Could not stop capturing (VIDIOCCAPTURE failed)\n");
		return -2;
	}

	if (stop)
		return 0;

	{
		struct video_buffer b;
		b.base = (void*) fb_fix.smem_start;
		b.width = fb_var.xres;
		b.height = fb_var.yres;
		b.depth = fb_var.bits_per_pixel;
		b.bytesperline = fb_var.xres*((fb_var.bits_per_pixel+7)/8);
		if (ioctl(fd, VIDIOCSFBUF, &b) < 0) {
			fprintf(stderr, "VIDIOCSFBUF failed, must run as root?\n");
			return -3;
		}
	}

	{
		struct video_picture p;
		if (ioctl(fd, VIDIOCGPICT, &p) < 0) {
			perror("VIDIOCGPICT failed\n");
			return -4;
		}
		p.depth = fb_var.bits_per_pixel;
		switch (fb_var.bits_per_pixel) {
			case 16:
				p.palette = VIDEO_PALETTE_RGB565;
				break;
			case 24:
				p.palette = VIDEO_PALETTE_RGB24;
				break;
			case 32:
				p.palette = VIDEO_PALETTE_RGB32;
				break;
		}
		//p.contrast = 0x8000;
		//p.colour = 0x6000;
		if (ioctl(fd, VIDIOCSPICT, &p) < 0) {
			perror("VIDIOCSPICT failed\n");
			return -5;
		}
	}

	{
		struct video_window win;
		win.width = min((__u32) vcap.maxwidth, fb_var.xres);
		win.height = min((__u32) vcap.maxheight, fb_var.yres);
		win.x = 0;
		win.y = 0;
		win.flags = 0;
		win.clips = NULL;
		win.clipcount = 0;
		win.chromakey = 0;
		if (ioctl(fd, VIDIOCSWIN, &win) < 0) {
			perror("VIDIOCSWIN failed\n");
			return -6;
		}
	}

	if (ioctl(fd, VIDIOCCAPTURE, &one) < 0) {
		perror("Could not start capturing (VIDIOCCAPTURE failed)\n");
		return -7;
	}

	close (fd);

	return 0;
}

int main (int argc, char **argv)
{
	int err = 0, stop = 0;

	if ((err = init_fb()))
		return err;

	if ((argc == 2 && strcmp(argv[1], "stop") == 0) ||
	    (argc == 3 && strcmp(argv[2], "stop") == 0))
		stop = 1;

	if ((argc == 2 && !stop) || argc == 3)
		video_devname = argv[1];

	if (argc != 1 && argc != 2 && !(argc == 3 && stop)) {
		fprintf(stderr, "usage: %s <devname> <stop>\n" USAGE, argv[0]);
		exit (-1);
	}

	return init_video (stop);
}