ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Video-Capture-V4l/VBI/VBI.xs
Revision: 1.6
Committed: Tue Nov 1 16:10:29 2005 UTC (18 years, 8 months ago) by root
Branch: MAIN
CVS Tags: rel-0_9, rel-0_902, HEAD
Changes since 1.5: +11 -20 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 pcg 1.1 #include "EXTERN.h"
2     #include "perl.h"
3     #include "XSUB.h"
4    
5     #include <sys/types.h>
6     #include <unistd.h>
7     #include <sys/mman.h>
8    
9     #include "../gppport.h"
10    
11     /* loosely based on the program vbidecode.cc by Ralph Metzler */
12    
13     typedef unsigned int UI;
14     typedef unsigned char u8;
15     typedef U16 u16;
16    
17     /* calculates odd parity, medium-efficient */
18     static int
19     parodd(U32 data)
20     {
21     u8 p4[16] = { 0,1,1,0, 1,0,0,1, 1,0,0,1, 0,1,1,0 };
22     int parity = 1;
23    
24     do {
25     parity ^= p4[data & 15];
26     data >>= 4;
27     } while (data);
28    
29     return parity;
30     }
31    
32     static u8 invtab[256] = {
33     0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
34     0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
35     0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8,
36     0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,
37     0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4,
38     0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
39     0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec,
40     0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc,
41     0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2,
42     0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2,
43     0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea,
44     0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa,
45     0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6,
46     0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6,
47     0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee,
48     0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe,
49     0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1,
50     0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1,
51     0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9,
52     0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9,
53     0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5,
54     0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5,
55     0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed,
56     0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd,
57     0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3,
58     0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3,
59     0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb,
60     0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb,
61     0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7,
62     0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
63     0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef,
64     0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff,
65     };
66    
67     static u8 unhamtab[256] = {
68     0x01, 0xff, 0x81, 0x01, 0xff, 0x00, 0x01, 0xff,
69     0xff, 0x02, 0x01, 0xff, 0x0a, 0xff, 0xff, 0x07,
70     0xff, 0x00, 0x01, 0xff, 0x00, 0x80, 0xff, 0x00,
71     0x06, 0xff, 0xff, 0x0b, 0xff, 0x00, 0x03, 0xff,
72     0xff, 0x0c, 0x01, 0xff, 0x04, 0xff, 0xff, 0x07,
73     0x06, 0xff, 0xff, 0x07, 0xff, 0x07, 0x07, 0x87,
74     0x06, 0xff, 0xff, 0x05, 0xff, 0x00, 0x0d, 0xff,
75     0x86, 0x06, 0x06, 0xff, 0x06, 0xff, 0xff, 0x07,
76     0xff, 0x02, 0x01, 0xff, 0x04, 0xff, 0xff, 0x09,
77     0x02, 0x82, 0xff, 0x02, 0xff, 0x02, 0x03, 0xff,
78     0x08, 0xff, 0xff, 0x05, 0xff, 0x00, 0x03, 0xff,
79     0xff, 0x02, 0x03, 0xff, 0x03, 0xff, 0x83, 0x03,
80     0x04, 0xff, 0xff, 0x05, 0x84, 0x04, 0x04, 0xff,
81     0xff, 0x02, 0x0f, 0xff, 0x04, 0xff, 0xff, 0x07,
82     0xff, 0x05, 0x05, 0x85, 0x04, 0xff, 0xff, 0x05,
83     0x06, 0xff, 0xff, 0x05, 0xff, 0x0e, 0x03, 0xff,
84     0xff, 0x0c, 0x01, 0xff, 0x0a, 0xff, 0xff, 0x09,
85     0x0a, 0xff, 0xff, 0x0b, 0x8a, 0x0a, 0x0a, 0xff,
86     0x08, 0xff, 0xff, 0x0b, 0xff, 0x00, 0x0d, 0xff,
87     0xff, 0x0b, 0x0b, 0x8b, 0x0a, 0xff, 0xff, 0x0b,
88     0x0c, 0x8c, 0xff, 0x0c, 0xff, 0x0c, 0x0d, 0xff,
89     0xff, 0x0c, 0x0f, 0xff, 0x0a, 0xff, 0xff, 0x07,
90     0xff, 0x0c, 0x0d, 0xff, 0x0d, 0xff, 0x8d, 0x0d,
91     0x06, 0xff, 0xff, 0x0b, 0xff, 0x0e, 0x0d, 0xff,
92     0x08, 0xff, 0xff, 0x09, 0xff, 0x09, 0x09, 0x89,
93     0xff, 0x02, 0x0f, 0xff, 0x0a, 0xff, 0xff, 0x09,
94     0x88, 0x08, 0x08, 0xff, 0x08, 0xff, 0xff, 0x09,
95     0x08, 0xff, 0xff, 0x0b, 0xff, 0x0e, 0x03, 0xff,
96     0xff, 0x0c, 0x0f, 0xff, 0x04, 0xff, 0xff, 0x09,
97     0x0f, 0xff, 0x8f, 0x0f, 0xff, 0x0e, 0x0f, 0xff,
98     0x08, 0xff, 0xff, 0x05, 0xff, 0x0e, 0x0d, 0xff,
99     0xff, 0x0e, 0x0f, 0xff, 0x0e, 0x8e, 0xff, 0x0e,
100     };
101    
102     #define VBI_BPL 2048
103    
104     #define FREQ_PAL 35.468950
105     #define FREQ_NTSC 28.636363
106     #define FREQ FREQ_PAL
107    
108     #define FREQ_VT_PAL 6.9375
109     #define FREQ_VT_NTSC 5.72725
110 pcg 1.2 #define FREQ_VT FREQ_VT_PAL /* Replace by FREQ_VT_NTSC and pray that it works */
111 pcg 1.1 /*#define FREQ_VT 6.165*/ /* teletext-like signal on france, 0xe7 instead of 0x27 */
112     #define FREQ_CRYPT 4.5 /* found on premiere */
113     #define FREQ_VPS 2.5 /* probably only pal */
114     #define FREQ_VDAT 2.0 /* videodat */
115     #define FREQ_VC 0.77 /* videocrypt, just ignore */
116    
117     #define VBI_VT 0x0001
118     #define VBI_VPS 0x0002
119     #define VBI_VDAT 0x0004
120     #define VBI_VC 0x0008
121     #define VBI_OTHER 0x0010
122     #define VBI_EMPTY 0x8000
123    
124     typedef long FP;
125    
126     #define FP_BITS 16
127     #define FP_0_5 D2FP(0.5)
128    
129     #define D2FP(d) ((FP)((d) * (1<<FP_BITS) + 0.5))
130     #define I2FP(i) ((FP)(i) << FP_BITS)
131     #define FP2I(fp) (((fp) + FP_0_5) >> FP_BITS)
132    
133     #define VT_COLS 40
134     #define VT_LINES 36
135    
136     typedef struct {
137     UI types; /* required types */
138    
139     int offset; /* signal offset */
140     int did_agc : 1; /* did we already do agc this frame? */
141    
142     int y; /* the line number */
143     u8 *line; /* the current line */
144     FP step; /* the bit step */
145     FP pos; /* position */
146     } decoder;
147    
148     static void
149     decoder_init (decoder *dec, UI types)
150     {
151     dec->types = types;
152     dec->did_agc = 0;
153     }
154    
155     static void
156     decoder_scan_start (decoder *dec, UI a, UI b)
157     {
158     u8 *p = dec->line + a;
159     UI med = 128 - dec->offset;
160     do
161     {
162     if (*p >= med)
163     break;
164     }
165     while (++p < dec->line + b);
166    
167     /* find maximum */
168     while (p[1] > p[0])
169     p++;
170    
171     dec->pos = I2FP (p - dec->line);
172     }
173    
174    
175     static u8
176     get_byte (decoder *dec)
177     {
178     u8 byte;
179     int bit = 8;
180    
181     /* if the next bit is a one bit, try to re-center the decoder on it */
182     if ((dec->offset + dec->line[FP2I(dec->pos)]) & 0x80)
183     {
184     /*if (dec->line[FP2I(dec->pos)] < dec->line[FP2I(dec->pos)+1])
185     dec->pos += I2FP(1);*/ /* why is this casuing checksum errors? */
186     /*if (dec->line[FP2I(dec->pos)] < dec->line[FP2I(dec->pos)-1])
187     dec->pos -= I2FP(1);*/
188     }
189    
190     byte=0;
191     do
192     {
193     byte >>= 1;
194     byte |= ((dec->offset + dec->line[FP2I(dec->pos)]) & 0x80);
195     dec->pos += dec->step;
196     }
197     while (--bit);
198    
199     return byte;
200     }
201    
202     /* get shift-encoded byte */
203     static u8
204     get_byte_SE (decoder *dec)
205     {
206     u8 byte;
207     int bit = 8;
208    
209     do
210     {
211     byte >>= 1;
212     byte |= (dec->line[FP2I(dec->pos)]
213     > dec->line[FP2I(dec->pos + dec->step/2)]) << 7;
214     dec->pos += dec->step;
215     }
216     while (--bit);
217    
218     /* if the next bit is a one bit, try to re-center the decoder on it */
219     if (dec->line[FP2I(dec->pos)] > 128-dec->offset)
220     {
221     if (dec->line[FP2I(dec->pos)] > dec->line[FP2I(dec->pos)+1])
222     dec->pos += I2FP(1);
223     if (dec->line[FP2I(dec->pos)] < dec->line[FP2I(dec->pos)-1])
224     dec->pos -= I2FP(1);
225     }
226    
227     return byte;
228     }
229    
230     static u8
231     unham4(u8 a)
232     {
233     return unhamtab[a] & 15;
234     }
235    
236     static u8
237     unham8(u8 a, u8 b)
238     {
239     u8 c1 = unhamtab[a];
240     u8 c2 = unhamtab[b];
241    
242     if ( (c1|c2) & 0x40)
243     /* 2 bit error */;
244    
245     return (c1 & 15)
246     | (c2 << 4);
247     }
248    
249     static u16 unham16(u8 a, u8 b, u8 c)
250     {
251     U32 d = (((c << 8) | b) << 8) | c;
252     int A = parodd (d & 0x555555);
253     int B = parodd (d & 0x666666);
254     int C = parodd (d & 0x787878);
255     int D = parodd (d & 0x007f80);
256     int E = parodd (d & 0x7f8000);
257     int F = parodd (d & 0xffffff);
258     int bit;
259    
260     d = (((d >> 16) & 0x7f) << 11)
261     | (((d >> 8) & 0x7f) << 4)
262     | (((d >> 4) & 0x07) << 1)
263     | (((d >> 2) & 0x01) );
264    
265     if (A&B&C&D&E)
266     return d;
267     if (F)
268     return -1; /* double bit error */
269    
270     /* correct the single bit error */
271     return d ^ (1 << (31 - 16*E + 8*D + 4*C + 2*B + A));
272     }
273    
274 root 1.6 #define rev(byte) (invtab [(u8)(byte)])
275 pcg 1.1
276     static SV *
277 root 1.5 decode_vps (u8 *data)
278 pcg 1.1 {
279     AV *av = newAV ();
280    
281 root 1.6 char name = rev (data[3]);
282    
283 pcg 1.1 av_push (av, newSViv (VBI_VPS));
284 root 1.6 av_push (av, newSVpvn (&name, 1));
285 pcg 1.1 av_push (av, newSViv (data[4] & 3)); /* "unknown", "stereo ", "mono ", "dual " */
286     /* ch, day, mon, hour, min */
287 root 1.6 av_push (av, newSViv (data[ 4] <<12 & 0xf000
288     | data[10] & 0x00c0
289     | data[12] <<10 & 0x0c00
290     | data[13] << 2 & 0x0300
291     | data[13] & 0x003f));
292 pcg 1.1 av_push (av, newSViv (rev (data[10]) >> 1 & 31));
293     av_push (av, newSViv (rev (data[10]) << 3 & 8 | rev (data[11]) >> 5));
294     av_push (av, newSViv (rev (data[11]) & 31));
295     av_push (av, newSViv (rev (data[12]) >> 2));
296     av_push (av, newSViv (rev (data[14])));
297    
298     return newRV_noinc ((SV*)av);
299     }
300    
301     static SV *
302 root 1.5 decode_vt (u8 *data)
303 pcg 1.1 {
304     AV *av = newAV ();
305     u8 mpag = unham8 (data[3], data[4]);
306     u8 mag = mpag & 7;
307     u8 pack = (mpag & 0xf8) >> 3;
308    
309     av_push (av, newSViv (VBI_VT));
310     av_push (av, newSViv (mag));
311     av_push (av, newSViv (pack));
312    
313     switch (pack)
314     {
315     /* ets300-706 */
316     case 0:
317     av_push (av, newSVpvn (data+5, 40));
318     av_push (av, newSViv (unham8 (data[5], data[6]) | (mag << 8)));
319     av_push (av, newSViv (unham8 (data[7], data[8])
320     | (unham8 (data[9], data[10]) << 8)
321     | (unham8 (data[11], data[12]) << 16)));
322     break;
323     case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 10:
324     case 11: case 12: case 13: case 14: case 15: case 16: case 17: case 18: case 19: case 20:
325     case 21: case 22: case 23: case 24: case 25:
326     av_push (av, newSVpvn (data+5, 40));
327     break;
328     case 26: case 27: case 28: case 29: /* enhancement packets */
329     av_push (av, newSViv (unham4 (data[5])));
330     av_push (av, newSViv (unham16 (data[ 6], data[ 7], data[ 8])));
331     av_push (av, newSViv (unham16 (data[ 9], data[10], data[11])));
332     av_push (av, newSViv (unham16 (data[12], data[13], data[14])));
333     av_push (av, newSViv (unham16 (data[15], data[16], data[17])));
334     av_push (av, newSViv (unham16 (data[18], data[19], data[20])));
335     av_push (av, newSViv (unham16 (data[21], data[22], data[23])));
336     av_push (av, newSViv (unham16 (data[24], data[25], data[26])));
337     av_push (av, newSViv (unham16 (data[27], data[28], data[29])));
338     av_push (av, newSViv (unham16 (data[30], data[31], data[32])));
339     av_push (av, newSViv (unham16 (data[33], data[34], data[35])));
340     av_push (av, newSViv (unham16 (data[36], data[37], data[38])));
341     av_push (av, newSViv (unham16 (data[39], data[40], data[41])));
342     av_push (av, newSViv (unham16 (data[42], data[43], data[44])));
343     break;
344     /* ets300-706 & ets300-231 */
345     case 30:
346     {
347     UI dc = unham4 (data[5]);
348     av_push (av, newSViv (dc));
349     if ((dc >> 1) == 0)
350     {
351     av_push (av, newSViv (unham8 (data[6], data[7]) | (mag << 8)));
352     av_push (av, newSViv (unham8 (data[8], data[9])
353     | unham8 (data[10], data[11]) << 8));
354     av_push (av, newSViv (rev (data[12]) << 8 | rev (data[13])));
355     }
356     else if ((dc >> 1) == 8)
357     {
358     /* pdc */
359     }
360     }
361     break;
362     case 31:
363     {
364     UI ft = unham4 (data[5]);
365     UI al = unham4 (data[6]);
366     UI i, addr = 0;
367    
368     /* ets300-708, IDL */
369     /* http://sunsite.cnlab-switch.ch/ftp/mirror/internet-drafts/draft-ietf-ipvbi-tv-signal-00.txt */
370    
371     for(i=0; i<al&7; i++)
372     addr = addr << 4 | unham4 (data[i+7]);
373    
374     av_push (av, newSViv (ft));
375     av_push (av, newSViv (addr));
376     break;
377     }
378     default:
379     av_push (av, newSVpvn (data+5, 40));
380     }
381     return newRV_noinc ((SV*)av);
382     }
383    
384     static SV *
385     decoder_decode_other (decoder *dec)
386     {
387     AV *av = newAV ();
388     FP pos = dec->pos;
389     u8 data[6];
390    
391     av_push (av, newSViv (VBI_OTHER));
392    
393     dec->step = D2FP (FREQ / FREQ_CRYPT); /* found on premiere */
394     data [0] = get_byte (dec);
395     data [1] = get_byte (dec);
396     data [2] = get_byte (dec);
397     if (data[0] == 0x55 && data[1] == 0xd0 && data[2] == 0x18)
398     {
399     /* premiere */
400     av_push (av, newSViv (1));
401     }
402     else
403     av_push (av, newSViv (0));
404    
405     return newRV_noinc ((SV*)av);
406     }
407    
408     static SV *
409 root 1.5 decode_empty ()
410 pcg 1.1 {
411     AV *av = newAV ();
412     av_push (av, newSViv (VBI_EMPTY));
413     return newRV_noinc ((SV*)av);
414     }
415    
416     static SV *
417     decoder_decode (decoder *dec, UI y, u8 *line)
418     {
419     UI type;
420     u8 data[45]; /* 45 bytes per line are max. */
421     UI i;
422     int did_agc;
423    
424     type = VBI_VT | VBI_EMPTY | VBI_OTHER | VBI_VC; /* can be everywhere */
425     if (y == 16 - 7) type |= VBI_VPS;
426     if (y > 17 - 7) type |= VBI_VDAT;
427    
428     type &= dec->types;
429    
430     /* don't do anything unless we need to */
431     if (type)
432     {
433     dec->line = line;
434     dec->y = y;
435     dec->pos = 0;
436    
437     did_agc = dec->did_agc;
438    
439     /* maybe do agc? */
440     if (!dec->did_agc || y == 20-7)
441     {
442     u8 *n = line + 120;
443     u8 max = 0, min = 255;
444     do
445     {
446     if (*n > max) max = *n;
447     if (*n < min) min = *n;
448     }
449     while (++n < line + 300);
450    
451     if (max > min + 30)
452     {
453     dec->offset = 128 - (((int)max + (int)min) >> 1);
454     dec->did_agc = 1;
455     }
456     }
457    
458     if (dec->did_agc)
459     {
460     if (type & VBI_VT)
461     {
462     dec->step = D2FP (FREQ / FREQ_VT);
463     decoder_scan_start (dec, 50, 350);
464    
465     data[0] = get_byte (dec);
466     if ((data[0] & 0xfe) == 0x54)
467     {
468     data[1] = get_byte (dec);
469     switch (data[1])
470     {
471     case 0x27:
472     dec->pos -= dec->step * 2;
473     case 0x4e:
474     dec->pos -= dec->step * 2;
475     case 0x9d:
476     dec->pos -= dec->step * 2;
477     case 0x75:
478     dec->pos -= dec->step * 2;
479     case 0xd5:
480     dec->pos -= dec->step * 2;
481     data[1] = 0x55;
482     case 0x55:
483     break;
484     default:
485 root 1.4 ; /* no teletext page */
486 pcg 1.1 }
487    
488     if (data[1] == 0x55)
489     {
490     data[2] = get_byte (dec);
491     if (data[2] == 0x27 || data[2] == 0xe7)
492     {
493     for (i = 3; i < 45; i++)
494     data[i] = get_byte (dec);
495 root 1.5 return decode_vt (data);
496 pcg 1.1 }
497     }
498     }
499     }
500    
501     if (type & VBI_VPS)
502     {
503     decoder_scan_start (dec, 150, 260);
504     dec->step = D2FP (FREQ / FREQ_VPS); /* shift encoding, two "pixels"/bit (skip the 2nd) */
505     data[0] = get_byte_SE (dec);
506     if (data[0] == 0xff)
507     {
508     data[1] = get_byte_SE (dec);
509     if (data[1] == 0x5d || data[1] == 0x5f)
510     {
511     for (i = 2; i < 16; i++)
512     data[i] = get_byte_SE (dec);
513 root 1.5 return decode_vps (data);
514 pcg 1.1 }
515     }
516     }
517    
518     #if 0
519     if (type & VBI_VDAT)
520     {
521     dec->step = D2FP (FREQ / FREQ_VDAT);
522     decoder_scan_start (dec, 150, 200);
523     }
524    
525     if (type & VBI_VC)
526     {
527     dec->step = D2FP (FREQ / FREQ_VC);
528     decoder_scan_start (dec, 150, 200);
529     }
530     #endif
531    
532 root 1.3 /* watch out for empty lines, test signals etc.. */
533 pcg 1.1 if (type & VBI_OTHER)
534     {
535     dec->did_agc = did_agc; /* other signals do not affect agc, yet */
536    
537     dec->step = D2FP (FREQ);
538     decoder_scan_start (dec, 100, 500);
539     if (dec->pos < I2FP (450))
540     return decoder_decode_other (dec);
541     }
542     }
543    
544     dec->did_agc = did_agc;
545    
546     if (type & VBI_EMPTY)
547 root 1.5 return decode_empty ();
548 pcg 1.1 }
549    
550     return 0;
551     }
552    
553     /* vtx decoding routines taken from videotext-0.6.971023,
554     * Copyright (c) 1994-96 Martin Buck */
555    
556     #define VTX_COLMASK 0x07
557     #define VTX_BGMASK (0x07 << 3)
558     #define VTX_G1 (1 << 6)
559     #define VTX_GRSEP (1 << 8)
560     #define VTX_HIDDEN (1 << 9)
561     #define VTX_BOX (1 << 10)
562     #define VTX_FLASH (1 << 11)
563     #define VTX_DOUBLE1 (1 << 12)
564     #define VTX_DOUBLE2 (1 << 13)
565     #define VTX_INVERT (1 << 14)
566     #define VTX_DOUBLE (VTX_DOUBLE1 | VTX_DOUBLE2)
567    
568     static const u8 g0_to_iso_table[256] =
569     " "
570 root 1.4 " !\"#$%&'()*+,-./0123456789:;<=>?"
571     "@ABCDEFGHIJKLMNOPQRSTUVWXYZAOU^#"
572     "-abcdefghijklmnopqrstuvwxyzaous#"
573 pcg 1.1 " "
574     " "
575     " "
576     " ";
577    
578     /* one-to-one copy */
579     static int
580     decode_vtpage (u8 *src, UI lines, u8 *chr, u16 *attr)
581     {
582     UI line, col, pos, graphics, grhold, doubleht, nextattr = 0;
583     u16 *lastattr, default_attrib = 7, next_attrib;
584     u8 c, *lastchr, default_chr = ' ';
585     UI lang;
586    
587     lang = 4;
588     pos = 0;
589     doubleht = 0;
590    
591     for (line = 0; line < lines; line++) {
592     lastchr = &default_chr;
593     lastattr = &default_attrib;
594     graphics = grhold = 0;
595     if (doubleht && pos > 40) {
596     for (col = 0; col <= 39; col++) {
597     if (attr[pos - 40] & VTX_DOUBLE1) {
598     chr[pos] = chr[pos - 40];
599     chr[pos - 40] = ' ';
600     attr[pos] = (attr[pos - 40] & ~VTX_DOUBLE1) | VTX_DOUBLE2;
601     } else {
602     chr[pos] = ' ';
603     attr[pos] = attr[pos - 40];
604     }
605     pos++;
606     }
607     doubleht = 0;
608     } else {
609     for (col = 0; col <= 39; col++) {
610     c = src[pos];
611     if (parodd(c)) {
612     chr[pos] = 254; /* Parity error */
613     attr[pos] = 7;
614     /* return 0 */ /*?*/
615     } else if ((c & 0x7f) >= 32) { /* Normal character */
616     c &= 0x7f;
617     if (!graphics || (c >= 64 && c <= 95)) {
618     chr[pos] = g0_to_iso_table [c];
619     attr[pos] = *lastattr;
620     } else {
621     chr[pos] = c + (c >= 96 ? 64 : 96);
622     attr[pos] = *lastattr | VTX_G1;
623     }
624     } else {
625     c &= 0x7f;
626     chr[pos] = ((grhold && graphics ) ? *lastchr : ' ');
627     if (c <= 7) { /* Set alphanumerics-color */
628     attr[pos] = *lastattr;
629     next_attrib = (*lastattr & ~(VTX_COLMASK | VTX_HIDDEN)) + c;
630     nextattr = 1;
631     graphics = 0;
632     } else if (c == 8 || c == 9) { /* Flash on/off */
633     attr[pos] = (*lastattr & ~VTX_FLASH) + VTX_FLASH * (c == 8);
634     } else if (c == 10 || c == 11) { /* End/start box */
635     attr[pos] = (*lastattr & ~VTX_BOX) + VTX_BOX * (c == 11);
636     } else if (c == 12 || c == 13) { /* Normal/double height */
637     attr[pos] = (*lastattr & ~VTX_DOUBLE1) + VTX_DOUBLE1 * (c == 13);
638     if (c == 13)
639     doubleht = 1;
640     } else if (c == 14 || c == 15 || c == 27) { /* SO, SI, ESC (ignored) */
641     attr[pos] = *lastattr;
642     } else if (c >= 16 && c <= 23) { /* Set graphics-color */
643     attr[pos] = *lastattr;
644     next_attrib = (*lastattr & ~(VTX_COLMASK | VTX_HIDDEN)) + c - 16;
645     nextattr = 1;
646     graphics = 1;
647     } else if (c == 24) { /* Conceal display */
648     attr[pos] = *lastattr | VTX_HIDDEN;
649     } else if (c == 25 || c == 26) { /* Contiguous/separated graphics */
650     attr[pos] = (*lastattr & ~VTX_GRSEP) + VTX_GRSEP * (c == 26);
651     } else if (c == 28) { /* Black background */
652     attr[pos] = *lastattr & ~VTX_BGMASK;
653     } else if (c == 29) { /* Set background */
654     attr[pos] = (*lastattr & ~VTX_BGMASK) + ((*lastattr & VTX_COLMASK) << 3);
655     } else if (c == 30 || c == 31) { /* Hold/release graphics */
656     attr[pos] = *lastattr;
657     grhold = (c == 30);
658     if (grhold && graphics)
659     chr[pos] = *lastchr;
660     } else {
661     return 0;
662     }
663     }
664     lastchr = chr + pos;
665     if (nextattr) {
666     lastattr = &next_attrib;
667     nextattr = 0;
668     } else {
669     lastattr = attr + pos;
670     }
671     pos++;
672     }
673     }
674     }
675     return 1;
676     }
677    
678     static SV *
679     decode_ansi (u8 *chr, u16 *atr)
680     {
681     UI x;
682     SV *sv = newSVpvn ("", 0);
683     u16 o;
684    
685 root 1.4 for (x=0; x < VT_COLS; x++)
686 pcg 1.1 {
687     u16 a = *atr++;
688 root 1.4 if (x == 0 || (a & 0x07) != (o & 0x07))
689     sv_catpvf (sv, "\x1b[3%dm", a & 7);
690     if (x == 0 || (a & 0x38) != (o & 0x38))
691     sv_catpvf (sv, "\x1b[4%dm", (o & 0x38)>>3);
692     if (x == 0 || (a & VTX_FLASH) != (o & VTX_FLASH))
693     sv_catpvf (sv, "\x1b[%sm", a & VTX_FLASH ? "7" : "");
694 pcg 1.1
695 root 1.4 sv_catpvf (sv, "%c", a & VTX_G1 ? 'x' : *chr);
696 pcg 1.1
697     chr++;
698     o = a;
699     }
700    
701     sv_catpv (sv, "\x1b[37;40;0m");
702    
703     return sv;
704     }
705    
706     #define valid_packet(sv) (SvPOK(packet) && SvCUR(packet) == 40)
707     #define consume_byte(store) \
708     if (bp >= 39) \
709     { \
710     SV **sv = av_fetch(stream, pi, 0); pi++; \
711     if (!sv) \
712     goto eostream; \
713     packet = *sv; \
714     if (!valid_packet (pascket)) \
715     goto skip; \
716     p = SvPV_nolen (packet); \
717     bp = 0; \
718     } \
719     (store) = p[++bp]
720    
721     static void
722     decode_stream (AV *stream)
723     {
724     dSP;
725    
726     while (av_len (stream) >= 0)
727     {
728     UI pi = 1;
729     SV *packet = *av_fetch(stream, 0, 1);
730    
731     if (valid_packet (packet))
732     {
733     u8 *p = SvPV_nolen(packet);
734     u8 bp = p[0] == 0xff ? p[1] : unham4 (p[0])*3+1;
735    
736     if (bp <= 12*3+1 && p[bp] == 0xa1)
737     {
738     u8 buf[4];
739    
740     consume_byte (buf[0]); consume_byte (buf[1]);
741     consume_byte (buf[2]); consume_byte (buf[3]);
742    
743     {
744     u16 sh = unham8 (buf[0], buf[1]) | unham8 (buf[2], buf[3]) << 8;
745     u8 bt = sh & 0x1f;
746     u16 bl = sh >> 5;
747     SV *block = sv_2mortal (newSVpvn (&bt, 1));
748    
749     while (bl--)
750     {
751     consume_byte (buf[0]);
752     sv_catpvn (block, buf, 1);
753     }
754    
755     EXTEND (SP, 1);
756     PUSHs (block);
757    
758     /* optimize, do only when still in first packet! */
759     do {
760     if (bp >= 39)
761     break;
762    
763     bp++;
764     if (p[bp] == 0xa1)
765     {
766     p[0] = 0xff;
767     p[1] = bp;
768     pi--;
769     break;
770     }
771     } while (p[bp] = 0x5e);
772     }
773     }
774     }
775    
776     skip:
777     while (pi--)
778     SvREFCNT_dec(av_shift(stream));
779     }
780     eostream:
781    
782     PUTBACK;
783     }
784    
785     static u8 *
786     unham_block (u8 *src, UI len, u8 *dst, UI dlen)
787     {
788     u16 sh = *src | (len-1)<<5;
789     u8 sum = ( sh & 15)
790     + ((sh >> 4) & 15)
791     + ((sh >> 8) & 15)
792     + ((sh >> 12) & 15);
793    
794     if (len < 5)
795     return 0;
796    
797     sum += unham8 (src[1], src[2]);
798     src += 3; len -= 3;
799    
800     dlen--;
801    
802     if (len < dlen)
803     return 0;
804    
805     while (dlen)
806     {
807     *dst = unham8 (src[0], src[1]);
808     sum += (*dst >> 4) + (*dst & 15);
809     #if 0
810     printf ("%02x ", *dst);
811     #endif
812     dst++; src += 2; dlen--;
813     }
814     #if 0
815     printf ("\n");
816     printf ("sh = %04x, len = %02x, sum = %02x\n", sh, len, sum);
817     #endif
818     if (sum)
819     return 0;
820    
821     return src;
822     }
823    
824     /* must not be a macro, see nvec */
825     static U32
826     vec(u8 *d, UI bit, UI len)
827     {
828     U32 word = bit >> 3;
829    
830     /*printf ("vec(%d,%d) %d (%d)\n",bit,len,(bit & 7) + len,(bit & 7) + len < 32);*/
831     assert ((bit & 7) + len < 32);
832    
833     word = (d[word] )
834     | (d[word+1] << 8)
835     | (d[word+2] << 16)
836     | (d[word+3] << 24);
837    
838     return (word >> (bit & 7)) & (((U32)1 << len) - 1);
839     }
840    
841     #define hv_store_sv(name,value) hv_store (hv, #name, strlen(#name), (SV*)(value), 0)
842     #define hv_store_iv(name,value) hv_store_sv (name, newSViv (value))
843     #define nvec(bits) vec (c, (ofs += (bits)) - (bits), (bits))
844    
845     #define decode_escape_sequences(av) \
846     { \
847     UI no_escapes = nvec (8); \
848     av = newAV (); \
849     while (no_escapes--) \
850     { \
851     AV *esc = newAV (); \
852     av_push (esc, newSViv (nvec (10))); \
853     av_push (esc, newSViv (nvec (6))); \
854     av_push (esc, newSViv (nvec (8))); \
855     av_push (av, newRV_noinc((SV*)esc)); \
856     } \
857     }
858    
859     #define decode_transparent_string(sv, len) \
860     { \
861     UI l = len; \
862     sv = newSVpvn (s, l); \
863     s += l; \
864     while (l--) \
865     SvPV_nolen (sv)[l] &= 0x7f; \
866     }
867    
868     #define decode_descriptor_loop(av, ll) \
869     av = newAV (); \
870     while (ll--) \
871     { \
872     AV *desc = newAV (); \
873     av_push (desc, newSViv (nvec (6))); \
874     av_push (desc, newSViv (nvec (6))); \
875     av_push (desc, newSViv (nvec (8))); \
876     av_push (av, newRV_noinc((SV*)desc)); \
877     }
878    
879     static void
880     decode_epg (HV *hv, UI appid, u8 *c, u8 *s)
881     {
882     UI ofs = 16;
883     UI len;
884     AV *av;
885     SV *sv;
886    
887     hv_store_iv (ca_mode, nvec(2));
888     hv_store_iv (_copyright, nvec(1));
889    
890     ofs++; /* reserved */
891    
892     switch (appid)
893     {
894     case 1:
895     {
896     hv_store_iv (epg_version, nvec( 6));
897     hv_store_iv (epg_version_swo, nvec( 6));
898     hv_store_iv (no_navigation, nvec(16));
899     hv_store_iv (no_osd, nvec(16));
900     hv_store_iv (no_message, nvec(16));
901     hv_store_iv (no_navigation_swo,nvec(16));
902     hv_store_iv (no_osd_swo, nvec(16));
903     hv_store_iv (no_message_swo, nvec(16));
904     len = nvec(8);
905     hv_store_iv (this_network_op, nvec( 8));
906     decode_transparent_string (sv, nvec (5));
907     hv_store_sv (service_name, sv);
908     hv_store_iv (no_updates, nvec( 1));
909    
910     ofs += 2;
911    
912     av = newAV ();
913     while (len--)
914     {
915     HV *hv = newHV ();
916     int LTO;
917     hv_store_iv (cni, nvec (16));
918     LTO = nvec (7) * 15;
919     if (nvec(1))
920     LTO = -LTO;
921     hv_store_iv (lto, LTO);
922     hv_store_iv (no_days, nvec( 5));
923     decode_transparent_string (sv, nvec( 5));
924     hv_store_sv (netwop_name, sv);
925     hv_store_iv (default_alphabet, nvec( 7));
926     hv_store_iv (prog_start_no, nvec(16));
927     hv_store_iv (prog_stop_no, nvec(16));
928     hv_store_iv (prog_stop_no_swo, nvec(16));
929     hv_store_iv (network_add_info, nvec(11));
930    
931     av_push (av, newRV_noinc ((SV*)hv));
932     }
933     hv_store_sv (networks, newRV_noinc ((SV*)av));
934     break;
935     }
936     case 2:
937     {
938     UI background_reuse;
939    
940     hv_store_iv (block_no, nvec(16));
941     hv_store_iv (audio_mode, vec(c, ofs, 12) & 3);
942     hv_store_iv (feature_flags, nvec(12));
943     hv_store_iv (netwop_no, nvec( 8));
944     hv_store_iv (start_time, nvec(16));
945     hv_store_iv (start_date, nvec(16));
946     hv_store_iv (stop_time, nvec(16));
947     hv_store_iv (_pil, nvec(20));
948     hv_store_iv (parental_rating, nvec( 4));
949     hv_store_iv (editorial_rating,nvec( 3));
950     {
951     UI no_themes = nvec( 3);
952     UI no_sortcrit = nvec( 3);
953     UI descriptor_looplength = nvec( 6);
954     background_reuse = nvec( 1);
955    
956     av = newAV ();
957     while (no_themes--)
958     av_push (av, newSViv (nvec (8)));
959     hv_store_sv (themes, newRV_noinc((SV*)av));
960    
961     av = newAV ();
962     while (no_sortcrit--)
963     av_push (av, newSViv (nvec (8)));
964     hv_store_sv (sortcrits, newRV_noinc((SV*)av));
965    
966     decode_descriptor_loop (av, descriptor_looplength);
967     hv_store_sv (descriptors, newRV_noinc((SV*)av));
968    
969     ofs = (ofs+7) & ~7;
970    
971     decode_escape_sequences (av);
972     hv_store_sv (title_escape_sequences, newRV_noinc((SV*)av));
973     decode_transparent_string (sv, nvec (8));
974     hv_store_sv (title, sv);
975     }
976    
977     if (background_reuse)
978     hv_store_iv (title_length, nvec (16));
979     else
980     {
981     decode_escape_sequences (av);
982     hv_store_sv (shortinfo_escape_sequences, newRV_noinc((SV*)av));
983     decode_transparent_string (sv, nvec (8));
984     hv_store_sv (shortinfo, sv);
985    
986     len = nvec (3);
987     ofs += 2;
988     switch (len)
989     {
990     case 0:
991     decode_escape_sequences (av);
992     hv_store_sv (longinfo_escape_sequences, newRV_noinc((SV*)av));
993     decode_transparent_string (sv, nvec (8));
994     hv_store_sv (longinfo, sv);
995     break;
996     case 1:
997     decode_escape_sequences (av);
998     hv_store_sv (longinfo_escape_sequences, newRV_noinc((SV*)av));
999     decode_transparent_string (sv, nvec (10));
1000     hv_store_sv (longinfo, sv);
1001     break;
1002     default:
1003     printf ("UNKNOWN LONGINFO TYPE %d\n", len);
1004     }
1005     }
1006    
1007     break;
1008     }
1009     case 3:
1010     {
1011     UI descriptor_ll;
1012     hv_store_iv (block_no, nvec(16));
1013     hv_store_iv (header_size, nvec( 2));
1014     len = nvec(4);
1015     hv_store_iv (message_size, nvec( 3));
1016     ofs++;
1017     descriptor_ll = nvec(6);
1018     decode_descriptor_loop (av, descriptor_ll);
1019     hv_store_sv (descriptors, newRV_noinc((SV*)av));
1020     ofs = (ofs+7) & ~7;
1021    
1022     decode_escape_sequences (av);
1023     hv_store_sv (header_escape_sequences, newRV_noinc((SV*)av));
1024    
1025     decode_transparent_string (sv, nvec(8));
1026     hv_store_sv (header, sv);
1027    
1028     hv_store_iv (message_attribute,nvec(8));
1029    
1030     av = newAV ();
1031     while (len--)
1032     {
1033     AV *av2;
1034     UI len;
1035     HV *hv = newHV ();
1036    
1037     hv_store_iv (next_id, nvec(16));
1038     hv_store_iv (next_type, nvec( 4));
1039    
1040     len = nvec(4);
1041     av2 = newAV ();
1042     while (len--)
1043     {
1044     UI kind = nvec(8);
1045     av_push (av2, newSViv (kind));
1046     switch (kind)
1047     {
1048     case 0x02: case 0x10: case 0x11: case 0x18:
1049     case 0x20: case 0x21: case 0x22: case 0x23: case 0x24: case 0x25: case 0x26: case 0x27:
1050     case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: case 0x35: case 0x36: case 0x37:
1051     case 0x40: case 0x41:
1052     av_push (av2, newSViv (nvec ( 8)));
1053     break;
1054    
1055     case 0x80: case 0x81:
1056     av_push (av2, newSViv (nvec (16)));
1057     break;
1058    
1059     case 0xc0:
1060     av_push (av2, newSViv (nvec (12)));
1061     av_push (av2, newSViv (nvec (12)));
1062     break;
1063    
1064     case 0xc8: case 0xc9:
1065     av_push (av2, newSViv (nvec (24)));
1066     break;
1067    
1068     default:
1069     abort ();
1070     }
1071     }
1072     hv_store_sv (attributes, newRV_noinc((SV*)av2));
1073    
1074     decode_escape_sequences (av2);
1075     hv_store_sv (header_escape_sequences, newRV_noinc((SV*)av2));
1076     decode_transparent_string (sv, nvec(8));
1077     hv_store_sv (event, sv);
1078    
1079     av_push (av, newRV_noinc((SV*)hv));
1080     }
1081     hv_store_sv (events, newRV_noinc((SV*)av));
1082    
1083     break;
1084     }
1085     case 4:
1086     {
1087     UI block_no = nvec (16);
1088     UI descriptor_ll;
1089     UI msg_len;
1090     hv_store_iv (block_no, block_no);
1091     hv_store_iv (message_attribute,nvec(8));
1092     hv_store_iv (header_size, nvec(3));
1093     hv_store_iv (message_size, nvec(3));
1094     descriptor_ll = nvec(6);
1095     if (!block_no)
1096     {
1097     decode_escape_sequences (av);
1098     hv_store_sv (message_escape_sequences, newRV_noinc((SV*)av));
1099     msg_len = nvec(10);
1100     ofs += 6;
1101     }
1102    
1103     decode_escape_sequences (av);
1104     hv_store_sv (header_escape_sequences, newRV_noinc((SV*)av));
1105    
1106     decode_transparent_string (sv, nvec(8));
1107     hv_store_sv (header, sv);
1108    
1109     if (!block_no)
1110     {
1111     decode_transparent_string (sv, msg_len);
1112     hv_store_sv (message, sv);
1113     }
1114    
1115     decode_descriptor_loop (av, descriptor_ll);
1116     hv_store_sv (descriptors, newRV_noinc((SV*)av));
1117     break;
1118     }
1119     case 5:
1120     {
1121     UI descriptor_ll;
1122     hv_store_iv (block_no, nvec(16));
1123     descriptor_ll = nvec(6);
1124     decode_descriptor_loop (av, descriptor_ll);
1125     hv_store_sv (descriptors, newRV_noinc((SV*)av));
1126     ofs = (ofs+7) & ~7;
1127    
1128     decode_escape_sequences (av);
1129     hv_store_sv (message_escape_sequences, newRV_noinc((SV*)av));
1130    
1131     decode_transparent_string (sv, nvec(10));
1132     hv_store_sv (message, sv);
1133    
1134     break;
1135     }
1136     default:
1137     printf ("UNKNOWN EPG DATATYPE ($%02x)\n", appid);
1138     }
1139     }
1140    
1141     static SV *
1142     decode_block(u8 *b, UI len, AV *bi)
1143     {
1144     dSP;
1145     u8 ctrl[1024];
1146     u8 bt = *b;
1147    
1148     if (bt == 0)
1149     {
1150     if ((b = unham_block (b, len, ctrl, (len-1)>>1 )))
1151     {
1152     UI app_no = ctrl[0];
1153     UI app;
1154     av_clear (bi);
1155     for (app=1; app<=app_no; app++)
1156     av_store (bi, app, newSViv (vec (ctrl, app*16-8, 16)));
1157     }
1158     }
1159     else if (len >= 5)
1160     {
1161     if (bt <= av_len (bi) && SvOK(*av_fetch (bi, bt, 0)))
1162     {
1163     u16 appid = SvIV (*av_fetch (bi, bt, 0));
1164     if (appid == 0) /* EPG */
1165     {
1166     if ((b = unham_block (b, len, ctrl, unham8 (b[3], b[4]) | (unham4 (b[5]) << 8) & 1023)))
1167     {
1168     HV *hv = newHV ();
1169     /* _now_ we have an epg structure. decode it */
1170    
1171     appid = vec(ctrl,10,6);
1172     EXTEND (SP, 2);
1173     PUSHs (sv_2mortal (newSViv (appid)));
1174     PUSHs (sv_2mortal (newRV_noinc ((SV*)hv)));
1175    
1176     decode_epg (hv, appid, ctrl, b);
1177     }
1178     else
1179     printf ("checksum error found block %d, len %d, appid = %d (clen 0)\n", bt, len, appid);
1180     }
1181     else
1182     /* other applications not defined (to my knowledge) */;
1183     }
1184     else
1185     /* no bundle info: can't parse yet */;
1186     }
1187    
1188     PUTBACK;
1189     }
1190    
1191     MODULE = Video::Capture::VBI PACKAGE = Video::Capture::VBI
1192    
1193     PROTOTYPES: ENABLE
1194    
1195     int
1196     unham4(data)
1197     SV * data
1198     CODE:
1199     STRLEN len;
1200     unsigned char *d = (unsigned char *)SvPV (data, len);
1201    
1202     if (len < 1)
1203     croak ("unham4: length must be at least 1");
1204    
1205     RETVAL = unham4 (*d);
1206     OUTPUT:
1207     RETVAL
1208    
1209     int
1210     unham8(data)
1211     SV * data
1212     CODE:
1213     STRLEN len;
1214     unsigned char *d = (unsigned char *)SvPV (data, len);
1215    
1216     if (len < 2)
1217     croak ("unham8: length must be at least 2");
1218    
1219     RETVAL = unham8 (d[0], d[1]);
1220     OUTPUT:
1221     RETVAL
1222    
1223     void
1224     decode_field(field, types)
1225     SV * field
1226     unsigned int types
1227     PPCODE:
1228 root 1.5 {
1229 pcg 1.1 UI lines = SvCUR(field) / VBI_BPL;
1230     UI line;
1231     decoder dec;
1232    
1233     decoder_init (&dec, types);
1234    
1235     EXTEND (SP, lines);
1236     for (line = 0; line < lines; line++)
1237     {
1238     SV *sv = decoder_decode (&dec, line, ((u8*)SvPV_nolen(field)) + line * VBI_BPL);
1239     if (sv)
1240     PUSHs (sv_2mortal (sv));
1241     }
1242 root 1.5 }
1243    
1244     SV *
1245 root 1.6 decode_vps(char *data)
1246 root 1.5
1247     SV *
1248 root 1.6 decode_vt(char *data)
1249 pcg 1.1
1250     void
1251     decode_vtpage(data)
1252     SV * data
1253     PPCODE:
1254 root 1.5 {
1255 pcg 1.1 u8 chr[VT_COLS*VT_LINES];
1256     u16 atr[VT_COLS*VT_LINES];
1257     UI lines;
1258    
1259     if (!SvPOK(data))
1260     XSRETURN_EMPTY;
1261    
1262     lines = SvCUR(data) / VT_COLS;
1263    
1264     if (lines > VT_LINES)
1265     croak ("videotext cannot have more than %d lines (argument has %d lines)\n", VT_LINES, lines);
1266    
1267     Zero(chr, VT_COLS*VT_LINES, u8);
1268     Zero(atr, VT_COLS*VT_LINES, u16);
1269     if (decode_vtpage (SvPV_nolen(data), lines, chr, atr))
1270     {
1271     AV *av = newAV ();
1272     UI n;
1273    
1274     for (n = 0; n < VT_COLS*lines; n++)
1275     av_push (av, newSViv (atr[n]));
1276    
1277     EXTEND (SP, 2);
1278     PUSHs (sv_2mortal (newSVpvn (chr, VT_COLS*lines)));
1279     PUSHs (sv_2mortal (newRV_noinc ((SV*)av)));
1280     }
1281 root 1.5 }
1282 pcg 1.1
1283     PROTOTYPES: DISABLE
1284    
1285     void
1286     decode_ansi(chr, atr)
1287     SV * chr
1288     SV * atr
1289     PPCODE:
1290 root 1.5 {
1291 pcg 1.1 UI lines = SvCUR(chr) / VT_COLS;
1292     UI attr_i = 0;
1293     u8 *_chr = SvPV_nolen (chr);
1294     u16 _atr[VT_COLS];
1295    
1296     EXTEND (SP, lines);
1297    
1298     while (lines--)
1299     {
1300     UI attr_j;
1301     for(attr_j = 0; attr_j < VT_COLS; attr_j++)
1302     _atr[attr_j] = SvIV (*av_fetch ((AV*)SvRV (atr), attr_i+attr_j, 1));
1303    
1304     PUSHs (sv_2mortal (decode_ansi (_chr, _atr)));
1305    
1306     _chr += VT_COLS;
1307     attr_i += VT_COLS;
1308     }
1309 root 1.5 }
1310 pcg 1.1
1311     unsigned int
1312     bcd2dec(bcd)
1313     unsigned int bcd
1314     CODE:
1315 root 1.5 {
1316 pcg 1.1 UI digit = 1;
1317     RETVAL = 0;
1318     while (bcd)
1319     {
1320     if ((bcd & 15) > 9)
1321     XSRETURN_EMPTY;
1322    
1323     RETVAL += (bcd & 15) * digit;
1324     digit *= 10;
1325     bcd >>= 4;
1326     }
1327 root 1.5 }
1328 pcg 1.1 OUTPUT:
1329     RETVAL
1330    
1331     PROTOTYPES: ENABLE
1332    
1333     MODULE = Video::Capture::VBI PACKAGE = Video::Capture::VBI::EPG
1334    
1335     void
1336     decode_stream(stream)
1337     SV * stream
1338     PPCODE:
1339     if (!SvROK(stream) || SvTYPE(SvRV(stream)) != SVt_PVAV)
1340     croak ("decode_epg stream works on arrayrefs");
1341    
1342     PUTBACK;
1343     decode_stream ((AV*)SvRV(stream));
1344     SPAGAIN;
1345    
1346     SV *
1347     decode_block(block, bundle)
1348     SV * block
1349     SV * bundle
1350     PPCODE:
1351     if (!SvROK(bundle) || SvTYPE(SvRV(bundle)) != SVt_PVAV)
1352     croak ("bundle info must be arrayref");
1353    
1354     PUTBACK;
1355     decode_block (SvPV_nolen(block), SvCUR(block), (AV*)SvRV(bundle));
1356     SPAGAIN;
1357    
1358     BOOT:
1359     {
1360     HV *stash = gv_stashpvn("Video::Capture::VBI", 19, TRUE);
1361    
1362     newCONSTSUB(stash,"VBI_VT", newSViv(VBI_VT));
1363     newCONSTSUB(stash,"VBI_VPS", newSViv(VBI_VPS));
1364     newCONSTSUB(stash,"VBI_VDAT", newSViv(VBI_VDAT));
1365     newCONSTSUB(stash,"VBI_VC", newSViv(VBI_VC));
1366     newCONSTSUB(stash,"VBI_EMPTY", newSViv(VBI_EMPTY));
1367     newCONSTSUB(stash,"VBI_OTHER", newSViv(VBI_OTHER));
1368    
1369     newCONSTSUB(stash,"VTX_COLMASK",newSViv(VTX_COLMASK));
1370     newCONSTSUB(stash,"VTX_GRSEP", newSViv(VTX_GRSEP));
1371     newCONSTSUB(stash,"VTX_HIDDEN", newSViv(VTX_HIDDEN));
1372     newCONSTSUB(stash,"VTX_BOX", newSViv(VTX_BOX));
1373     newCONSTSUB(stash,"VTX_FLASH", newSViv(VTX_FLASH));
1374     newCONSTSUB(stash,"VTX_DOUBLE1",newSViv(VTX_DOUBLE1));
1375     newCONSTSUB(stash,"VTX_DOUBLE2",newSViv(VTX_DOUBLE2));
1376     newCONSTSUB(stash,"VTX_INVERT", newSViv(VTX_INVERT));
1377     newCONSTSUB(stash,"VTX_DOUBLE", newSViv(VTX_DOUBLE));
1378     }
1379