~ubuntu-branches/ubuntu/precise/v4l-utils/precise

« back to all changes in this revision

Viewing changes to contrib/test/sliced-vbi-detect.c

  • Committer: Bazaar Package Importer
  • Author(s): Gregor Jasny
  • Date: 2010-02-28 19:44:15 UTC
  • Revision ID: james.westby@ubuntu.com-20100228194415-067hdj8rvawj91zw
Tags: upstream-0.7.90
ImportĀ upstreamĀ versionĀ 0.7.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Sliced vbi autodetection demonstration utility
 
3
    Copyright (C) 2004  Hans Verkuil  <hverkuil@xs4all.nl>
 
4
 
 
5
    This program is free software; you can redistribute it and/or modify
 
6
    it under the terms of the GNU General Public License as published by
 
7
    the Free Software Foundation; either version 2 of the License, or
 
8
    (at your option) any later version.
 
9
 
 
10
    This program is distributed in the hope that it will be useful,
 
11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
    GNU General Public License for more details.
 
14
 
 
15
    You should have received a copy of the GNU General Public License
 
16
    along with this program; if not, write to the Free Software
 
17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
 */
 
19
 
 
20
/* This simple utility detects which VBI types are transmitted on
 
21
   each field/line. It serves both as example source and as a tool to
 
22
   test the sliced VBI implementation.
 
23
 
 
24
   Usage: sliced-vbi-detect [device]
 
25
   Without a device name as argument it will fallback to /dev/vbi0.
 
26
 */
 
27
 
 
28
#include <unistd.h>
 
29
#include <features.h>           /* Uses _GNU_SOURCE to define getsubopt in stdlib.h */
 
30
#include <stdlib.h>
 
31
#include <stdio.h>
 
32
#include <string.h>
 
33
#include <inttypes.h>
 
34
#include <getopt.h>
 
35
#include <sys/types.h>
 
36
#include <sys/stat.h>
 
37
#include <fcntl.h>
 
38
#include <errno.h>
 
39
#include <sys/ioctl.h>
 
40
#include <sys/time.h>
 
41
#include <math.h>
 
42
 
 
43
#include <linux/videodev2.h>
 
44
 
 
45
static void detect(int fh, struct v4l2_sliced_vbi_format *fmt)
 
46
{
 
47
        struct v4l2_sliced_vbi_data *buf = malloc(fmt->io_size);
 
48
        int cnt;
 
49
 
 
50
        for (cnt = 0; cnt < 5; cnt++) {
 
51
                int size = read(fh, buf, fmt->io_size);
 
52
                unsigned i;
 
53
 
 
54
                if (size <= 0) {
 
55
                        printf("size = %d\n", size);
 
56
                        break;
 
57
                }
 
58
                if (cnt == 0)
 
59
                        continue;
 
60
                for (i = 0; i < size / sizeof(*buf); i++) {
 
61
                        int field, line;
 
62
 
 
63
                        line = buf[i].line;
 
64
                        field = buf[i].field;
 
65
                        if (buf[i].id == 0)
 
66
                                continue;
 
67
                        if (line < 0 || line >= 24) {
 
68
                                printf("line %d out of range\n", line);
 
69
                                free(buf);
 
70
                                return;
 
71
                        }
 
72
                        fmt->service_lines[field][line] |= buf[i].id;
 
73
                }
 
74
        }
 
75
        free(buf);
 
76
}
 
77
 
 
78
static void v2s(int id)
 
79
{
 
80
        switch (id) {
 
81
                case V4L2_SLICED_TELETEXT_B: printf(" TELETEXT"); break;
 
82
                case V4L2_SLICED_CAPTION_525: printf(" CC"); break;
 
83
                case V4L2_SLICED_WSS_625: printf(" WSS"); break;
 
84
                case V4L2_SLICED_VPS: printf(" VPS"); break;
 
85
                default: printf(" UNKNOWN %x", id); break;
 
86
        }
 
87
}
 
88
 
 
89
int main(int argc, char **argv)
 
90
{
 
91
        char *device = "/dev/vbi0";
 
92
        struct v4l2_format vbifmt;
 
93
        struct v4l2_sliced_vbi_format vbiresult;
 
94
        int fh;
 
95
        int f, i, b;
 
96
 
 
97
        if (argc == 2)
 
98
                device = argv[1];
 
99
        fh = open(device, O_RDONLY);
 
100
 
 
101
        if (fh == -1) {
 
102
                fprintf(stderr, "cannot open %s\n", device);
 
103
                return 1;
 
104
        }
 
105
        memset(&vbiresult, 0, sizeof(vbiresult));
 
106
        for (i = 0; i < 16; i++) {
 
107
                int l;
 
108
                int set = 0;
 
109
 
 
110
                memset(&vbifmt, 0, sizeof(vbifmt));
 
111
                vbifmt.type = V4L2_BUF_TYPE_SLICED_VBI_CAPTURE;
 
112
                for (l = 0; l < 24; l++) {
 
113
                        vbifmt.fmt.sliced.service_lines[0][l] = 1 << i;
 
114
                        vbifmt.fmt.sliced.service_lines[1][l] = 1 << i;
 
115
                }
 
116
                if (ioctl(fh, VIDIOC_S_FMT, &vbifmt) < 0) {
 
117
                        if (errno == EINVAL)
 
118
                                continue;
 
119
                        perror("IVTV_IOC_S_VBI_FMT");
 
120
                        exit(-1);
 
121
                }
 
122
                vbiresult.io_size = vbifmt.fmt.sliced.io_size;
 
123
                for (l = 0; l < 24; l++) {
 
124
                        set |= vbifmt.fmt.sliced.service_lines[0][l] |
 
125
                               vbifmt.fmt.sliced.service_lines[1][l];
 
126
                }
 
127
                detect(fh, &vbiresult);
 
128
        }
 
129
        close(fh);
 
130
        for (f = 0; f < 2; f++) {
 
131
                printf("Field %d:\n", f);
 
132
                for (i = 6; i < 24; i++) {
 
133
                        unsigned set = vbiresult.service_lines[f][i];
 
134
 
 
135
                        printf("  Line %2d:", i);
 
136
                        for (b = 0; b < 16; b++) {
 
137
                                if (set & (1 << b))
 
138
                                        v2s(1 << b);
 
139
                        }
 
140
                        printf("\n");
 
141
                }
 
142
        }
 
143
        return 0;
 
144
}