1 /*
2  *  libzvbi -- Bit slicer
3  *
4  *  Copyright (C) 2000-2007 Michael H. Schimek
5  *
6  *  This library is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU Library General Public
8  *  License as published by the Free Software Foundation; either
9  *  version 2 of the License, or (at your option) any later version.
10  *
11  *  This library is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *  Library General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Library General Public
17  *  License along with this library; if not, write to the
18  *  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  *  Boston, MA  02110-1301  USA.
20  */
21 
22 /* $Id: bit_slicer.h,v 1.11 2008/02/24 14:18:13 mschimek Exp $ */
23 
24 #ifndef __ZVBI_BIT_SLICER_H__
25 #define __ZVBI_BIT_SLICER_H__
26 
27 #include "sampling_par.h"
28 
29 VBI_BEGIN_DECLS
30 
31 /**
32  * @addtogroup BitSlicer
33  * @{
34  */
35 
36 /**
37  * @brief Modulation used for VBI data transmission.
38  */
39 typedef enum {
40 	/**
41 	 * The data is 'non-return to zero' coded, logical '1' bits
42 	 * are described by high sample values, logical '0' bits by
43 	 * low values. The data is last significant bit first transmitted.
44 	 */
45 	VBI3_MODULATION_NRZ_LSB,
46 
47 	/**
48 	 * 'Non-return to zero' coded, most significant bit first
49 	 * transmitted.
50 	 */
51 	VBI3_MODULATION_NRZ_MSB,
52 
53 	/**
54 	 * The data is 'bi-phase' coded. Each data bit is described
55 	 * by two complementary signalling elements, a logical '1'
56 	 * by a sequence of '10' elements, a logical '0' by a '01'
57 	 * sequence. The data is last significant bit first transmitted.
58 	 */
59 	VBI3_MODULATION_BIPHASE_LSB,
60 
61 	/** 'Bi-phase' coded, most significant bit first transmitted. */
62 	VBI3_MODULATION_BIPHASE_MSB
63 } vbi3_modulation;
64 
65 /**
66  * @brief Bit slicer context.
67  *
68  * The contents of this structure are private.
69  * Call vbi3_bit_slicer_new() to allocate a bit slicer context.
70  */
71 typedef struct _vbi3_bit_slicer vbi3_bit_slicer;
72 
73 typedef enum {
74 	VBI3_CRI_BIT = 1,
75 	VBI3_FRC_BIT,
76 	VBI3_PAYLOAD_BIT
77 } vbi3_bit_slicer_bit;
78 
79 /**
80  * @brief Bit slicer sampling point.
81  *
82  * This structure contains information about
83  * a bit sampled by the bit slicer.
84  */
85 typedef struct {
86 	/** Whether this struct refers to a CRI, FRC or payload bit. */
87 	vbi3_bit_slicer_bit	kind;
88 
89 	/** Number of the sample times 256. */
90 	unsigned int		index;
91 
92 	/** Signal amplitude at this sample, in range 0 to 65535. */
93 	unsigned int		level;
94 
95 	/** 0/1 threshold at this sample, in range 0 to 65535. */
96 	unsigned int		thresh;
97 } vbi3_bit_slicer_point;
98 
99 extern vbi_bool
100 vbi3_bit_slicer_slice_with_points
101 				(vbi3_bit_slicer *	bs,
102 				 uint8_t *		buffer,
103 				 unsigned int		buffer_size,
104 				 vbi3_bit_slicer_point *points,
105 				 unsigned int *		n_points,
106 				 unsigned int		max_points,
107 				 const uint8_t *	raw)
108   _vbi_nonnull ((1, 2, 4, 5, 7));
109 extern vbi_bool
110 vbi3_bit_slicer_slice		(vbi3_bit_slicer *	bs,
111 				 uint8_t *		buffer,
112 				 unsigned int		buffer_size,
113 				 const uint8_t *	raw)
114   _vbi_nonnull ((1, 2, 4));
115 extern vbi_bool
116 vbi3_bit_slicer_set_params	(vbi3_bit_slicer *	bs,
117 				 vbi_pixfmt		sample_format,
118 				 unsigned int		sampling_rate,
119 				 unsigned int		sample_offset,
120 				 unsigned int		samples_per_line,
121 				 unsigned int		cri,
122 				 unsigned int		cri_mask,
123 				 unsigned int		cri_bits,
124 				 unsigned int		cri_rate,
125 				 unsigned int		cri_end,
126 				 unsigned int		frc,
127 				 unsigned int		frc_bits,
128 				 unsigned int		payload_bits,
129 				 unsigned int		payload_rate,
130 				 vbi3_modulation	modulation)
131   _vbi_nonnull ((1));
132 extern void
133 vbi3_bit_slicer_set_log_fn	(vbi3_bit_slicer *	bs,
134 				 vbi_log_mask		mask,
135 				 vbi_log_fn *		log_fn,
136 				 void *			user_data)
137   _vbi_nonnull ((1));
138 extern void
139 vbi3_bit_slicer_delete		(vbi3_bit_slicer *	bs);
140 extern vbi3_bit_slicer *
141 vbi3_bit_slicer_new		(void)
142   _vbi_alloc;
143 
144 /* Private */
145 
146 typedef vbi_bool
147 _vbi3_bit_slicer_fn		(vbi3_bit_slicer *	bs,
148 				 uint8_t *		buffer,
149 				 vbi3_bit_slicer_point *points,
150 				 unsigned int *		n_points,
151 				 const uint8_t *	raw);
152 
153 /** @internal */
154 struct _vbi3_bit_slicer {
155 	_vbi3_bit_slicer_fn *	func;
156 
157 	vbi_pixfmt		sample_format;
158 	unsigned int		cri;
159 	unsigned int		cri_mask;
160 	unsigned int		thresh;
161 	unsigned int		thresh_frac;
162 	unsigned int		cri_samples;
163 	unsigned int		cri_rate;
164 	unsigned int		oversampling_rate;
165 	unsigned int		phase_shift;
166 	unsigned int		step;
167 	unsigned int		frc;
168 	unsigned int		frc_bits;
169 	unsigned int		total_bits;
170 	unsigned int		payload;
171 	unsigned int		endian;
172 	unsigned int		bytes_per_sample;
173 	unsigned int		skip;
174 	unsigned int		green_mask;
175 
176 	_vbi_log_hook		log;
177 };
178 
179 extern void
180 _vbi3_bit_slicer_destroy	(vbi3_bit_slicer *	bs)
181   _vbi_nonnull ((1));
182 extern vbi_bool
183 _vbi3_bit_slicer_init		(vbi3_bit_slicer *	bs)
184   _vbi_nonnull ((1));
185 
186 /** @} */
187 
188 VBI_END_DECLS
189 
190 #endif /* __ZVBI_BIT_SLICER_H__ */
191 
192 /*
193 Local variables:
194 c-set-style: K&R
195 c-basic-offset: 8
196 End:
197 */
198