aboutsummaryrefslogtreecommitdiffstats
path: root/common/vbi-dvb.c
blob: fd43982c12b9ee7337f2486d8a705621da9f2cb8 (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
/*
 * libzvbi -- dvb driver interface
 *
 * (c) 2003 Gerd Knorr <kraxel@bytesex.org> [SUSE Labs]
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
#include "config.h"
#define _GNU_SOURCE /* for glibc memmem() */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <ctype.h>
#include <inttypes.h>

#include <sys/select.h>
#include <sys/ioctl.h>

#include <libzvbi.h>

#if (VBI_VERSION_MAJOR == 0) && (VBI_VERSION_MINOR <= 2) && (VBI_VERSION_MICRO <= 5)
#warning you should upgrade zvbi

#include "vbi-dvb.h"
#include "misc.h"

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

/* WARNING: not public -- from zvbi-0.2.5/src/io.h */
struct vbi_capture {
    vbi_bool		(* read)(vbi_capture *, vbi_capture_buffer **,
				 vbi_capture_buffer **, struct timeval *);
    vbi_raw_decoder *	(* parameters)(vbi_capture *);
    int			(* get_fd)(vbi_capture *);
    void	       	(* _delete)(vbi_capture *);
};

struct vbi_capture_dvb {
    vbi_capture	   cap;
    int            fd;
    int            debug;
    int            resync;
    unsigned char  buffer[1024*8];
    unsigned int   bbytes;
    unsigned int   psize;
};

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

static unsigned char bitswap[256];

static void __attribute__ ((constructor)) init_bitswap(void)
{
    int i,bit;

    for (i = 0; i < 256; i++)
	for (bit = 0; bit < 8; bit++)
	    if (i & (1 << bit))
		bitswap[i] |= (1 << (7-bit));
}

static int dvb_payload(struct vbi_capture_dvb *dvb,
		       unsigned char *buf, unsigned int len,
		       vbi_sliced *dest)
{
    int i,j,line;
    int slices = 0;

    if (dvb->debug > 1)
	fprintf(stderr,"dvb-vbi: new PES packet\n");
    if (buf[0] < 0x10 || buf[0] > 0x1f)
	return slices;  /* no EBU teletext data */

    for (i = 1; i < len; i += buf[i+1]+2) {
	line = buf[i+2] & 0x1f;
	if (buf[i+2] & 0x20)
	    line += 312;
	if (dvb->debug > 2)
	    fprintf(stderr,"dvb-vbi: id 0x%02x len %d | line %3d framing 0x%02x\n",
		    buf[i], buf[i+1], line, buf[i+3]);
	switch (buf[i]) {
	case 0x02:
	    dest[slices].id   = VBI_SLICED_TELETEXT_B;
	    dest[slices].line = line;
	    for (j = 0; j < sizeof(dest[slices].data) && j < buf[i+1]-2; j++)
		dest[slices].data[j] = bitswap[buf[i+j+4]];
	    slices++;
	    break;
	}
    }
    return slices;
}

static int dvb_wait(int fd, struct timeval *timeout, int debug)
{
    struct timeval tv = *timeout;
    int rc;
    
    fd_set set;
    FD_ZERO(&set);
    FD_SET(fd,&set);
    rc = select(fd+1,&set,NULL,NULL,&tv);
    switch (rc) {
    case -1:
	perror("dvb-vbi: select");
	return -1;
    case 0:
	if (debug)
	    fprintf(stderr,"dvb-vbi: timeout\n");
	return -1;
    default:
	return 0;
    }
}

static struct vbi_capture_dvb* dvb_init(char *dev, int debug)
{
    struct vbi_capture_dvb *dvb;

    dvb = malloc(sizeof(*dvb));
    if (NULL == dvb)
	return NULL;
    memset(dvb,0,sizeof(*dvb));

    dvb->debug = debug;
    dvb->fd = open(dev, O_RDWR | O_NONBLOCK);
    if (-1 == dvb->fd) {
	fprintf(stderr,"open %s: %s\n",dev,strerror(errno));
	free(dvb);
	return NULL;
    }
    if (dvb->debug)
	fprintf(stderr,"dvb-vbi: opened device %s\n",dev);
    return dvb;
}

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

static int dvb_read(vbi_capture *cap,
		    vbi_capture_buffer **raw,
		    vbi_capture_buffer **sliced,
		    struct timeval *timeout)
{
    struct vbi_capture_dvb *dvb = (struct vbi_capture_dvb*)cap;
    static unsigned char peshdr[4] = { 0x00, 0x00, 0x01, 0xbd };
    vbi_sliced *dest = (vbi_sliced *)(*sliced)->data;
    unsigned int off;
    int lines = 0;
    int ret = 0;
    int rc;

    if (0 != dvb_wait(dvb->fd,timeout,dvb->debug))
	return -1;
    rc = read(dvb->fd, dvb->buffer + dvb->bbytes,
	      sizeof(dvb->buffer) - dvb->bbytes);
    switch (rc) {
    case -1:
	perror("read");
	return -1;
    case 0:
	fprintf(stderr,"EOF\n");
	return -1;
    };
    if (dvb->debug > 1)
	fprintf(stderr,"dvb-vbi: read %d bytes (@%d)\n",rc,dvb->bbytes);
    dvb->bbytes += rc;

    if (dvb->resync) {
	/* grep for start code in resync mode */
	unsigned char *start;
	start = memmem(dvb->buffer,dvb->bbytes,peshdr,4);
	if (NULL != start) {
	    if (dvb->debug)
		fprintf(stderr,"vbi-dvb: start code found, RESYNC cleared\n");
	    dvb->bbytes -= (start - dvb->buffer);
	    memmove(dvb->buffer,start,dvb->bbytes);
	    dvb->resync = 0;
	} else {
	    if (dvb->debug)
		fprintf(stderr,"vbi-dvb: RESYNC [still no pes start magic]\n");
	    dvb->bbytes = 0;
	    dvb->resync = 1;
	    return ret;
	}
    }

    for (;;) {
	if (6 > dvb->bbytes) {
	    /* need header */
	    return ret;
	}
	if (0 != memcmp(dvb->buffer,peshdr,4)) {
	    if (dvb->debug)
		fprintf(stderr,"vbi-dvb: RESYNC [no pes start magic]\n");
	    dvb->bbytes = 0;
	    dvb->resync = 1;
	    return ret;
	}
	dvb->psize = dvb->buffer[4] << 8 | dvb->buffer[5];
	if (dvb->psize+6 > sizeof(dvb->buffer)) {
	    if (dvb->debug)
		fprintf(stderr,"vbi-dvb: RESYNC [insane payload size %d]\n",dvb->psize);
	    dvb->bbytes = 0;
	    dvb->resync = 1;
	    return ret;
	}
	if (dvb->psize+6 > dvb->bbytes) {
	    /* need more data */
	    return ret;
	}

	/* ok, have a complete PES packet, pass it on ... */
	off = dvb->buffer[8]+9;
	rc = dvb_payload(dvb, dvb->buffer+off, dvb->psize-off, dest + lines);
	if (rc > 0) {
	    struct timeval tv;
	    ret = 1;
	    lines += rc;
	    gettimeofday(&tv, NULL);
	    (*sliced)->size      = lines * sizeof(vbi_sliced);
	    (*sliced)->timestamp = tv.tv_sec + tv.tv_usec * (1 / 1e6);
	}

	dvb->bbytes -= (dvb->psize+6);
	if (dvb->bbytes) {
	    /* shift buffer */
	    memmove(dvb->buffer,dvb->buffer + dvb->psize+6,dvb->bbytes);
	}
    }
}

static vbi_raw_decoder* dvb_parameters(vbi_capture *cap)
{
    static vbi_raw_decoder raw = {
	.count[0] = 128,
    };
    return &raw;
}

static void
dvb_delete(vbi_capture *cap)
{
    struct vbi_capture_dvb *dvb = (struct vbi_capture_dvb*)cap;

    if (dvb->fd != -1)
	close(dvb->fd);
    free(dvb);
}

static int
dvb_fd(vbi_capture *cap)
{
    struct vbi_capture_dvb *dvb = (struct vbi_capture_dvb*)cap;
    return dvb->fd;
}

/* ----------------------------------------------------------------------- */
/* public interface                                                        */

#if HAVE_DVB

#include <linux/dvb/frontend.h>
#include <linux/dvb/dmx.h>

int vbi_capture_dvb_filter(vbi_capture *cap, int pid)
{
    struct vbi_capture_dvb *dvb = (struct vbi_capture_dvb*)cap;
    struct dmx_pes_filter_params filter;

    memset(&filter, 0, sizeof(filter));
    filter.pid = pid;
    filter.input = DMX_IN_FRONTEND;
    filter.output = DMX_OUT_TAP;
    filter.pes_type = DMX_PES_OTHER;
    filter.flags = DMX_IMMEDIATE_START;
    if (0 != ioctl(dvb->fd, DMX_SET_PES_FILTER, &filter)) {
	perror("ioctl DMX_SET_PES_FILTER");
	return -1;
    }
    if (dvb->debug)
	fprintf(stderr,"dvb-vbi: filter setup done | fd %d pid %d\n",
		dvb->fd, pid);
    return 0;
}

#else

int vbi_capture_dvb_filter(vbi_capture *cap, int pid)
{
    fprintf(stderr,"built without dvb support, sorry\n");
    return -1;
}


#endif

vbi_capture*
vbi_capture_dvb_new(char *dev, int scanning,
		    unsigned int *services, int strict,
		    char **errstr, vbi_bool trace)
{
    struct vbi_capture_dvb *dvb;

    dvb = dvb_init(dev,trace);
    if (NULL == dvb)
	return NULL;

    dvb->cap.parameters = dvb_parameters;
    dvb->cap.read       = dvb_read;
    dvb->cap.get_fd     = dvb_fd;
    dvb->cap._delete    = dvb_delete;

    return &dvb->cap;
}

#endif /* libzvbi version */

Privacy Policy