~ubuntu-branches/ubuntu/maverick/zapping/maverick

« back to all changes in this revision

Viewing changes to libvbi/packet-830.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2005-03-08 23:19:08 UTC
  • mfrom: (2.1.1 sarge)
  • Revision ID: james.westby@ubuntu.com-20050308231908-oip7rfv6lcmo8c0e
Tags: 0.9.2-2ubuntu1
Rebuilt for Python transition (2.3 -> 2.4)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  libzvbi - Teletext packet decoder, packet 8/30
 
3
 *
 
4
 *  Copyright (C) 2003, 2004 Michael H. Schimek
 
5
 *
 
6
 *  This program is free software; you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License version 2 as
 
8
 *  published by the Free Software Foundation.
 
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., 675 Mass Ave, Cambridge, MA 02139, USA.
 
18
 */
 
19
 
 
20
/* $Id: packet-830.c,v 1.4 2005/01/31 07:21:34 mschimek Exp $ */
 
21
 
 
22
#ifdef HAVE_CONFIG_H
 
23
#  include "config.h"
 
24
#endif
 
25
 
 
26
#include <assert.h>
 
27
 
 
28
#include "bcd.h"                /* vbi3_bcd2dec() */
 
29
#include "hamm.h"               /* vbi3_rev16p(), vbi3_iham8() */
 
30
#include "packet-830.h"
 
31
 
 
32
/**
 
33
 * @addtogroup Packet830 Teletext Packet 8/30 Decoder
 
34
 * @ingroup LowDec
 
35
 * @brief Functions to decode Teletext packets 8/30 (ETS 300 706).
 
36
 */
 
37
 
 
38
/**
 
39
 * @param cni CNI of type VBI3_CNI_TYPE_8301 will be stored here.
 
40
 * @param buffer Teletext packet as defined for @c VBI3_SLICED_TELETEXT_B,
 
41
 *   i.e. 42 bytes without clock run-in and framing code.
 
42
 *
 
43
 * Decodes a Teletext packet 8/30 format 1 according to
 
44
 * ETS 300 706 section 9.8.1, returning the 16 bit Country and
 
45
 * Network Identifier in @a cni.
 
46
 *
 
47
 * @returns
 
48
 * Always @c TRUE, no error checking possible.
 
49
 */
 
50
vbi3_bool
 
51
vbi3_decode_teletext_8301_cni   (unsigned int *         cni,
 
52
                                 const uint8_t          buffer[42])
 
53
{
 
54
        assert (NULL != cni);
 
55
        assert (NULL != buffer);
 
56
 
 
57
        *cni = vbi3_rev16p (buffer + 9);
 
58
 
 
59
        return TRUE;
 
60
}
 
61
 
 
62
/**
 
63
 * @param tme UTC time will be stored here.
 
64
 * @param gmtoff Local time offset in seconds east of UTC is stored here,
 
65
 *   including daylight saving time, as in BSD and GNU struct tm tm_gmtoff.
 
66
 *   To get the local time of the network broadcasting this packet add
 
67
 *   @a gmtoff to @a time.
 
68
 * @param buffer Teletext packet as defined for @c VBI3_SLICED_TELETEXT_B,
 
69
 *   i.e. 42 bytes without clock run-in and framing code.
 
70
 * 
 
71
 * Decodes a Teletext packet 8/30 format 1 according to
 
72
 * ETS 300 706 section 9.8.1, returning the time data.
 
73
 *
 
74
 * Note a few stations incorrectly transmit local time instead of UTC,
 
75
 * with gmtoff zero.
 
76
 *
 
77
 * @returns
 
78
 * @c FALSE if the buffer contained incorrectable data, in this case
 
79
 * @a tme and @a gmtoff remain unchanged.
 
80
 */
 
81
vbi3_bool
 
82
vbi3_decode_teletext_8301_local_time
 
83
                                (time_t *               tme,
 
84
                                 int *                  gmtoff,
 
85
                                 const uint8_t          buffer[42])
 
86
{
 
87
        unsigned int mjd;
 
88
        unsigned int utc;
 
89
        int bcd;
 
90
        int t;
 
91
 
 
92
        assert (NULL != tme);
 
93
        assert (NULL != gmtoff);
 
94
        assert (NULL != buffer);
 
95
 
 
96
        bcd = (+ ((buffer[12] & 15) << 16)
 
97
               +  (buffer[13] << 8)
 
98
               +   buffer[14]
 
99
               - 0x11111);
 
100
 
 
101
        if (!vbi3_is_bcd (bcd))
 
102
                return FALSE;
 
103
 
 
104
        mjd = vbi3_bcd2dec (bcd);
 
105
 
 
106
        bcd = (+ (buffer[15] << 16)
 
107
               + (buffer[16] << 8)
 
108
               +  buffer[17]
 
109
               - 0x111111);
 
110
 
 
111
        if (vbi3_bcd_digits_greater (bcd, 0x295959))
 
112
                return FALSE;
 
113
 
 
114
        utc  = (bcd & 15)        + ((bcd >> 4) & 15) * 10;
 
115
        bcd >>= 8;
 
116
        utc += (bcd & 15) * 60   + ((bcd >> 4) & 15) * 600;
 
117
        bcd >>= 8;
 
118
        utc += (bcd & 15) * 3600 +  (bcd >> 4)       * 36000;
 
119
 
 
120
        if (utc >= 24 * 60 * 60)
 
121
                return FALSE;
 
122
 
 
123
        *tme = (mjd - 40587) * 86400 + utc;
 
124
 
 
125
        /* Local time offset in seconds east of UTC. */
 
126
 
 
127
        t = (buffer[11] & 0x3E) * (15 * 60);
 
128
 
 
129
        if (buffer[11] & 0x40)
 
130
                t = -t;
 
131
 
 
132
        /* 300 706 says 0x81 bits are reserved, some stations set,
 
133
           some clear, no apparent function (no UTC/local flag anyway). */
 
134
 
 
135
        *gmtoff = t;
 
136
 
 
137
        return TRUE;
 
138
}
 
139
 
 
140
/**
 
141
 * @param cni CNI of type VBI3_CNI_TYPE_8302 will be stored here.
 
142
 * @param buffer Teletext packet as defined for @c VBI3_SLICED_TELETEXT_B,
 
143
 *   i.e. 42 bytes without clock run-in and framing code.
 
144
 *
 
145
 * Decodes a Teletext packet 8/30 format 2 according to
 
146
 * ETS 300 706 section 9.8.2, returning the 16 bit Country and
 
147
 * Network Identifier in @a cni.
 
148
 *
 
149
 * @returns
 
150
 * @c FALSE if the buffer contained incorrectable data, in this case
 
151
 * @a cni remains unchanged.
 
152
 */
 
153
vbi3_bool
 
154
vbi3_decode_teletext_8302_cni   (unsigned int *         cni,
 
155
                                 const uint8_t          buffer[42])
 
156
{
 
157
        int b[13];
 
158
 
 
159
        assert (NULL != cni);
 
160
        assert (NULL != buffer);
 
161
 
 
162
        b[ 7] = vbi3_unham16p (buffer + 10);
 
163
        b[ 8] = vbi3_unham16p (buffer + 12);
 
164
        b[10] = vbi3_unham16p (buffer + 16);
 
165
        b[11] = vbi3_unham16p (buffer + 18);
 
166
 
 
167
        if ((b[7] | b[8] | b[10] | b[11]) < 0)
 
168
                return FALSE;
 
169
 
 
170
        b[ 7] = vbi3_rev8 (b[ 7]);
 
171
        b[ 8] = vbi3_rev8 (b[ 8]);
 
172
        b[10] = vbi3_rev8 (b[10]);
 
173
        b[11] = vbi3_rev8 (b[11]);
 
174
 
 
175
        *cni = (+ ((b[ 7] & 0x0F) << 12)
 
176
                + ((b[10] & 0x03) << 10)
 
177
                + ((b[11] & 0xC0) << 2)
 
178
                +  (b[ 8] & 0xC0)
 
179
                +  (b[11] & 0x3F));
 
180
 
 
181
        return TRUE;
 
182
}
 
183
 
 
184
/**
 
185
 * @param pid PDC data will be stored here.
 
186
 * @param buffer Teletext packet as defined for @c VBI3_SLICED_TELETEXT_B,
 
187
 *   i.e. 42 bytes without clock run-in and framing code.
 
188
 * 
 
189
 * Decodes a Teletext packet 8/30 format 2 according to
 
190
 * ETS 300 231, storing PDC recording-control data in @a pid.
 
191
 *
 
192
 * @returns
 
193
 * @c FALSE if the buffer contained incorrectable data, in this case
 
194
 * @a pid remains unchanged.
 
195
 */
 
196
vbi3_bool
 
197
vbi3_decode_teletext_8302_pdc   (vbi3_program_id *      pid,
 
198
                                 const uint8_t          buffer[42])
 
199
{
 
200
        uint8_t b[13];
 
201
        unsigned int i;
 
202
        int error;
 
203
 
 
204
        assert (NULL != pid);
 
205
        assert (NULL != buffer);
 
206
 
 
207
        error = vbi3_unham8 (buffer[10]);
 
208
        b[ 6] = error;
 
209
 
 
210
        for (i = 7; i <= 12; ++i) {
 
211
                int t;
 
212
 
 
213
                t = vbi3_unham16p (buffer + i * 2 - 4);
 
214
                error |= t;
 
215
                b[i] = vbi3_rev8 (t);
 
216
        }
 
217
 
 
218
        if (error < 0)
 
219
                return FALSE;
 
220
 
 
221
        pid->cni_type   = VBI3_CNI_TYPE_8302;
 
222
        pid->cni        = (+ ((b[ 7] & 0x0F) << 12)
 
223
                           + ((b[10] & 0x03) << 10)
 
224
                           + ((b[11] & 0xC0) << 2)
 
225
                           +  (b[ 8] & 0xC0)
 
226
                           +  (b[11] & 0x3F));
 
227
 
 
228
        pid->channel    = VBI3_PID_CHANNEL_LCI_0 + ((b[6] >> 2) & 3);
 
229
 
 
230
        pid->luf        = !!(b[6] & 2);
 
231
        pid->prf        = b[6] & 1;
 
232
 
 
233
        pid->pcs_audio  = b[7] >> 6;
 
234
 
 
235
        pid->mi         = !!(b[7] & 0x20);
 
236
 
 
237
        pid->pil        = (+ ((b[ 8] & 0x3F) << 14)
 
238
                           +  (b[ 9] << 6)
 
239
                           +  (b[10] >> 2));
 
240
 
 
241
        pid->month      = VBI3_PIL_MONTH (pid->pil) - 1; 
 
242
        pid->day        = VBI3_PIL_DAY (pid->pil) - 1; 
 
243
        pid->hour       = VBI3_PIL_HOUR (pid->pil); 
 
244
        pid->minute     = VBI3_PIL_MINUTE (pid->pil); 
 
245
 
 
246
        pid->length     = 0; /* unknown */
 
247
 
 
248
        pid->pty        = b[12];
 
249
 
 
250
        pid->tape_delayed = FALSE;
 
251
 
 
252
        return TRUE;
 
253
}