1 /*
2  *  libzvbi -- Old raw VBI decoder
3  *
4  *  Copyright (C) 2000, 2001, 2002 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: decoder.h,v 1.11 2008-02-19 00:35:15 mschimek Exp $ */
23 
24 #ifndef DECODER_H
25 #define DECODER_H
26 
27 #ifdef HAVE_CONFIG_H
28 #  include "config.h"
29 #endif
30 
31 #include "bcd.h"
32 #include "sliced.h"
33 
34 /* Public */
35 
36 #include <glib.h>
37 
38 /* Bit slicer */
39 
40 /**
41  * @ingroup Rawdec
42  * @brief Image format used as source to vbi_bit_slice() and vbi_raw_decode().
43  *
44  * @htmlonly
45 <table border=1>
46 <tr><th>Symbol</th><th>Byte&nbsp;0</th><th>Byte&nbsp;1</th><th>Byte&nbsp;2</th><th>Byte&nbsp;3</th></tr>
47 <tr><td colspan=5>Planar YUV 4:2:0 data.</td></tr>
48 <tr><td>VBI_PIXFMT_YUV420</td><td colspan=4>
49  <table>
50   <tr><th>Y plane</th><th>U plane</th><th>V plane</th></tr>
51   <tr><td><table border=1>
52    <tr><td>Y00</td><td>Y01</td><td>Y02</td><td>Y03</td></tr>
53    <tr><td>Y10</td><td>Y11</td><td>Y12</td><td>Y13</td></tr>
54    <tr><td>Y20</td><td>Y21</td><td>Y22</td><td>Y23</td></tr>
55    <tr><td>Y30</td><td>Y31</td><td>Y32</td><td>Y33</td></tr>
56   </table></td>
57   <td><table border=1>
58    <tr><td>Cb00</td><td>Cb01</td></tr>
59    <tr><td>Cb10</td><td>Cb11</td></tr>
60   </table></td>
61   <td><table border=1>
62    <tr><td>Cr00</td><td>Cr01</td></tr>
63    <tr><td>Cr10</td><td>Cr11</td></tr>
64   </table></td>
65  </tr></table></td>
66 </tr>
67 <tr><td colspan=5>Packed YUV 4:2:2 data.</td></tr>
68 <tr><td>VBI_PIXFMT_YUYV</td><td>Y0</td><td>Cb</td><td>Y1</td><td>Cr</td></tr>
69 <tr><td>VBI_PIXFMT_YVYU</td><td>Y0</td><td>Cr</td><td>Y1</td><td>Cb</td></tr>
70 <tr><td>VBI_PIXFMT_UYVY</td><td>Cb</td><td>Y0</td><td>Cr</td><td>Y1</td></tr>
71 <tr><td>VBI_PIXFMT_VYUY</td><td>Cr</td><td>Y0</td><td>Cb</td><td>Y1</td></tr>
72 <tr><td colspan=5>Packed 32 bit RGB data.</td></tr>
73 <tr><td>VBI_PIXFMT_RGBA32_LE VBI_PIXFMT_ARGB32_BE</td>
74 <td>r7&nbsp;...&nbsp;r0</td><td>g7&nbsp;...&nbsp;g0</td>
75 <td>b7&nbsp;...&nbsp;b0</td><td>a7&nbsp;...&nbsp;a0</td></tr>
76 <tr><td>VBI_PIXFMT_BGRA32_LE VBI_PIXFMT_ARGB32_BE</td>
77 <td>b7&nbsp;...&nbsp;b0</td><td>g7&nbsp;...&nbsp;g0</td>
78 <td>r7&nbsp;...&nbsp;r0</td><td>a7&nbsp;...&nbsp;a0</td></tr>
79 <tr><td>VBI_PIXFMT_ARGB32_LE VBI_PIXFMT_BGRA32_BE</td>
80 <td>a7&nbsp;...&nbsp;a0</td><td>r7&nbsp;...&nbsp;r0</td>
81 <td>g7&nbsp;...&nbsp;g0</td><td>b7&nbsp;...&nbsp;b0</td></tr>
82 <tr><td>VBI_PIXFMT_ABGR32_LE VBI_PIXFMT_RGBA32_BE</td>
83 <td>a7&nbsp;...&nbsp;a0</td><td>b7&nbsp;...&nbsp;b0</td>
84 <td>g7&nbsp;...&nbsp;g0</td><td>r7&nbsp;...&nbsp;r0</td></tr>
85 <tr><td colspan=5>Packed 24 bit RGB data.</td></tr>
86 <tr><td>VBI_PIXFMT_RGBA24</td>
87 <td>r7&nbsp;...&nbsp;r0</td><td>g7&nbsp;...&nbsp;g0</td>
88 <td>b7&nbsp;...&nbsp;b0</td><td>&nbsp;</td></tr>
89 <tr><td>VBI_PIXFMT_BGRA24</td>
90 <td>b7&nbsp;...&nbsp;b0</td><td>g7&nbsp;...&nbsp;g0</td>
91 <td>r7&nbsp;...&nbsp;r0</td><td>&nbsp;</td></tr>
92 <tr><td colspan=5>Packed 16 bit RGB data.</td></tr>
93 <tr><td>VBI_PIXFMT_RGB16_LE</td>
94 <td>g2&nbsp;g1&nbsp;g0&nbsp;r4&nbsp;r3&nbsp;r2&nbsp;r1&nbsp;r0</td>
95 <td>b4&nbsp;b3&nbsp;b2&nbsp;b1&nbsp;b0&nbsp;g5&nbsp;g4&nbsp;g3</td>
96 <td>&nbsp;</td><td>&nbsp;</td></tr><tr><td>VBI_PIXFMT_BGR16_LE</td>
97 <td>g2&nbsp;g1&nbsp;g0&nbsp;b4&nbsp;b3&nbsp;b2&nbsp;b1&nbsp;b0</td>
98 <td>r4&nbsp;r3&nbsp;r2&nbsp;r1&nbsp;r0&nbsp;g5&nbsp;g4&nbsp;g3</td>
99 <td>&nbsp;</td><td>&nbsp;</td></tr><tr><td>VBI_PIXFMT_RGB16_BE</td>
100 <td>b4&nbsp;b3&nbsp;b2&nbsp;b1&nbsp;b0&nbsp;g5&nbsp;g4&nbsp;g3</td>
101 <td>g2&nbsp;g1&nbsp;g0&nbsp;r4&nbsp;r3&nbsp;r2&nbsp;r1&nbsp;r0</td>
102 <td>&nbsp;</td><td>&nbsp;</td></tr><tr><td>VBI_PIXFMT_BGR16_BE</td>
103 <td>r4&nbsp;r3&nbsp;r2&nbsp;r1&nbsp;r0&nbsp;g5&nbsp;g4&nbsp;g3</td>
104 <td>g2&nbsp;g1&nbsp;g0&nbsp;b4&nbsp;b3&nbsp;b2&nbsp;b1&nbsp;b0</td>
105 <td>&nbsp;</td><td>&nbsp;</td></tr>
106 <tr><td colspan=5>Packed 15 bit RGB data.</td></tr>
107 <tr><td>VBI_PIXFMT_RGBA15_LE</td>
108 <td>g2&nbsp;g1&nbsp;g0&nbsp;r4&nbsp;r3&nbsp;r2&nbsp;r1&nbsp;r0</td>
109 <td>a0&nbsp;b4&nbsp;b3&nbsp;b2&nbsp;b1&nbsp;b0&nbsp;g4&nbsp;g3</td>
110 <td>&nbsp;</td><td>&nbsp;</td></tr><tr><td>VBI_PIXFMT_BGRA15_LE</td>
111 <td>g2&nbsp;g1&nbsp;g0&nbsp;b4&nbsp;b3&nbsp;b2&nbsp;b1&nbsp;b0</td>
112 <td>a0&nbsp;r4&nbsp;r3&nbsp;r2&nbsp;r1&nbsp;r0&nbsp;g4&nbsp;g3</td>
113 <td>&nbsp;</td><td>&nbsp;</td></tr><tr><td>VBI_PIXFMT_ARGB15_LE</td>
114 <td>g1&nbsp;g0&nbsp;r4&nbsp;r3&nbsp;r2&nbsp;r1&nbsp;r0&nbsp;a0</td>
115 <td>b4&nbsp;b3&nbsp;b2&nbsp;b1&nbsp;b0&nbsp;g4&nbsp;g3&nbsp;g2</td>
116 <td>&nbsp;</td><td>&nbsp;</td></tr><tr><td>VBI_PIXFMT_ABGR15_LE</td>
117 <td>g1&nbsp;g0&nbsp;b4&nbsp;b3&nbsp;b2&nbsp;b1&nbsp;b0&nbsp;a0</td>
118 <td>r4&nbsp;r3&nbsp;r2&nbsp;r1&nbsp;r0&nbsp;g4&nbsp;g3&nbsp;g2</td>
119 <td>&nbsp;</td><td>&nbsp;</td></tr><tr><td>VBI_PIXFMT_RGBA15_BE</td>
120 <td>a0&nbsp;b4&nbsp;b3&nbsp;b2&nbsp;b1&nbsp;b0&nbsp;g4&nbsp;g3</td>
121 <td>g2&nbsp;g1&nbsp;g0&nbsp;r4&nbsp;r3&nbsp;r2&nbsp;r1&nbsp;r0</td>
122 <td>&nbsp;</td><td>&nbsp;</td></tr><tr><td>VBI_PIXFMT_BGRA15_BE</td>
123 <td>a0&nbsp;r4&nbsp;r3&nbsp;r2&nbsp;r1&nbsp;r0&nbsp;g4&nbsp;g3</td>
124 <td>g2&nbsp;g1&nbsp;g0&nbsp;b4&nbsp;b3&nbsp;b2&nbsp;b1&nbsp;b0</td>
125 <td>&nbsp;</td><td>&nbsp;</td></tr><tr><td>VBI_PIXFMT_ARGB15_BE</td>
126 <td>b4&nbsp;b3&nbsp;b2&nbsp;b1&nbsp;b0&nbsp;g4&nbsp;g3&nbsp;g2</td>
127 <td>g1&nbsp;g0&nbsp;r4&nbsp;r3&nbsp;r2&nbsp;r1&nbsp;r0&nbsp;a0</td>
128 <td>&nbsp;</td><td>&nbsp;</td></tr><tr><td>VBI_PIXFMT_ABGR15_BE</td>
129 <td>r4&nbsp;r3&nbsp;r2&nbsp;r1&nbsp;r0&nbsp;g4&nbsp;g3&nbsp;g2</td>
130 <td>g1&nbsp;g0&nbsp;b4&nbsp;b3&nbsp;b2&nbsp;b1&nbsp;b0&nbsp;a0</td>
131 <td>&nbsp;</td><td>&nbsp;</td></tr>
132 </table>
133 @endhtmlonly */
134 /* Attn: keep this in sync with rte, don't change order */
135 typedef enum {
136 	VBI_PIXFMT_YUV420 = 1,
137 	VBI_PIXFMT_YUYV,
138 	VBI_PIXFMT_YVYU,
139 	VBI_PIXFMT_UYVY,
140 	VBI_PIXFMT_VYUY,
141         VBI_PIXFMT_PAL8,
142 	VBI_PIXFMT_RGBA32_LE = 32,
143 	VBI_PIXFMT_RGBA32_BE,
144 	VBI_PIXFMT_BGRA32_LE,
145 	VBI_PIXFMT_BGRA32_BE,
146 	VBI_PIXFMT_ABGR32_BE = 32, /* synonyms */
147 	VBI_PIXFMT_ABGR32_LE,
148 	VBI_PIXFMT_ARGB32_BE,
149 	VBI_PIXFMT_ARGB32_LE,
150 	VBI_PIXFMT_RGB24,
151 	VBI_PIXFMT_BGR24,
152 	VBI_PIXFMT_RGB16_LE,
153 	VBI_PIXFMT_RGB16_BE,
154 	VBI_PIXFMT_BGR16_LE,
155 	VBI_PIXFMT_BGR16_BE,
156 	VBI_PIXFMT_RGBA15_LE,
157 	VBI_PIXFMT_RGBA15_BE,
158 	VBI_PIXFMT_BGRA15_LE,
159 	VBI_PIXFMT_BGRA15_BE,
160 	VBI_PIXFMT_ARGB15_LE,
161 	VBI_PIXFMT_ARGB15_BE,
162 	VBI_PIXFMT_ABGR15_LE,
163 	VBI_PIXFMT_ABGR15_BE
164 } vbi_pixfmt;
165 
166 /* Private */
167 
168 typedef uint64_t vbi_pixfmt_set;
169 
170 #define VBI_MAX_PIXFMTS 64
171 #define VBI_PIXFMT_SET(pixfmt) (((vbi_pixfmt_set) 1) << (pixfmt))
172 #define VBI_PIXFMT_SET_YUV (VBI_PIXFMT_SET (VBI_PIXFMT_YUV420) |	\
173 			    VBI_PIXFMT_SET (VBI_PIXFMT_YUYV) |		\
174 			    VBI_PIXFMT_SET (VBI_PIXFMT_YVYU) |		\
175 			    VBI_PIXFMT_SET (VBI_PIXFMT_UYVY) |		\
176 			    VBI_PIXFMT_SET (VBI_PIXFMT_VYUY))
177 #define VBI_PIXFMT_SET_RGB (VBI_PIXFMT_SET (VBI_PIXFMT_RGBA32_LE) |	\
178 			    VBI_PIXFMT_SET (VBI_PIXFMT_RGBA32_BE) |	\
179 			    VBI_PIXFMT_SET (VBI_PIXFMT_BGRA32_LE) |	\
180 			    VBI_PIXFMT_SET (VBI_PIXFMT_BGRA32_BE) |	\
181 			    VBI_PIXFMT_SET (VBI_PIXFMT_RGB24) |		\
182 			    VBI_PIXFMT_SET (VBI_PIXFMT_BGR24) |		\
183 			    VBI_PIXFMT_SET (VBI_PIXFMT_RGB16_LE) |	\
184 			    VBI_PIXFMT_SET (VBI_PIXFMT_RGB16_BE) |	\
185 			    VBI_PIXFMT_SET (VBI_PIXFMT_BGR16_LE) |	\
186 			    VBI_PIXFMT_SET (VBI_PIXFMT_BGR16_BE) |	\
187 			    VBI_PIXFMT_SET (VBI_PIXFMT_RGBA15_LE) |	\
188 			    VBI_PIXFMT_SET (VBI_PIXFMT_RGBA15_BE) |	\
189 			    VBI_PIXFMT_SET (VBI_PIXFMT_BGRA15_LE) |	\
190 			    VBI_PIXFMT_SET (VBI_PIXFMT_BGRA15_BE) |	\
191 			    VBI_PIXFMT_SET (VBI_PIXFMT_ARGB15_LE) |	\
192 			    VBI_PIXFMT_SET (VBI_PIXFMT_ARGB15_BE) |	\
193 			    VBI_PIXFMT_SET (VBI_PIXFMT_ABGR15_LE) |	\
194 			    VBI_PIXFMT_SET (VBI_PIXFMT_ABGR15_BE))
195 #define VBI_PIXFMT_SET_ALL (VBI_PIXFMT_SET_YUV |			\
196 			    VBI_PIXFMT_SET_RGB)
197 
198 #define VBI_PIXFMT_BPP(fmt)						\
199 	(((fmt) == VBI_PIXFMT_YUV420) ? 1 :				\
200 	 (((fmt) >= VBI_PIXFMT_RGBA32_LE				\
201 	   && (fmt) <= VBI_PIXFMT_BGRA32_BE) ? 4 :			\
202 	  (((fmt) == VBI_PIXFMT_RGB24					\
203 	    || (fmt) == VBI_PIXFMT_BGR24) ? 3 : 2)))
204 
205 /* Public */
206 
207 /**
208  * @ingroup Rawdec
209  * @brief Modulation used for VBI data transmission.
210  */
211 typedef enum {
212 	/**
213 	 * The data is 'non-return to zero' coded, logical '1' bits
214 	 * are described by high sample values, logical '0' bits by
215 	 * low values. The data is last significant bit first transmitted.
216 	 */
217 	VBI_MODULATION_NRZ_LSB,
218 	/**
219 	 * 'Non-return to zero' coded, most significant bit first
220 	 * transmitted.
221 	 */
222 	VBI_MODULATION_NRZ_MSB,
223 	/**
224 	 * The data is 'bi-phase' coded. Each data bit is described
225 	 * by two complementary signalling elements, a logical '1'
226 	 * by a sequence of '10' elements, a logical '0' by a '01'
227 	 * sequence. The data is last significant bit first transmitted.
228 	 */
229 	VBI_MODULATION_BIPHASE_LSB,
230 	/**
231 	 * 'Bi-phase' coded, most significant bit first transmitted.
232 	 */
233 	VBI_MODULATION_BIPHASE_MSB
234 } vbi_modulation;
235 
236 #if 0
237 /**
238  * @ingroup Rawdec
239  * @brief Bit slicer context.
240  *
241  * The contents of this structure are private,
242  * use vbi_bit_slicer_init() to initialize.
243  */
244 typedef struct vbi_bit_slicer {
245 	vbi_bool	(* func)(struct vbi_bit_slicer *slicer,
246 				 uint8_t *raw, uint8_t *buf);
247 	unsigned int	cri;
248 	unsigned int	cri_mask;
249 	int		thresh;
250 	int		cri_bytes;
251 	int		cri_rate;
252 	int		oversampling_rate;
253 	int		phase_shift;
254 	int		step;
255 	unsigned int	frc;
256 	int		frc_bits;
257 	int		payload;
258 	int		endian;
259 	int		skip;
260 } vbi_bit_slicer;
261 
262 /**
263  * @addtogroup Rawdec
264  * @{
265  */
266 extern void		vbi_bit_slicer_init(vbi_bit_slicer *slicer,
267 					    int raw_samples, int sampling_rate,
268 					    int cri_rate, int bit_rate,
269 					    unsigned int cri_frc, unsigned int cri_mask,
270 					    int cri_bits, int frc_bits, int payload,
271 					    vbi_modulation modulation, vbi_pixfmt fmt);
272 /**
273  * @param slicer Pointer to initialized vbi_bit_slicer object.
274  * @param raw Input data. At least the number of pixels or samples
275  *  given as @a raw_samples to vbi_bit_slicer_init().
276  * @param buf Output data. The buffer must be large enough to store
277  *   the number of bits given as @a payload to vbi_bit_slicer_init().
278  *
279  * Decode one scan line of raw vbi data. Note the bit slicer tries
280  * to adapt to the average signal amplitude, you should avoid
281  * using the same vbi_bit_slicer object for data from different
282  * devices.
283  *
284  * @note As a matter of speed this function does not lock the
285  * @a slicer. When you want to share a vbi_bit_slicer object between
286  * multiple threads you must implement your own locking mechanism.
287  *
288  * @return
289  * @c FALSE if the raw data does not contain the expected
290  * information, i. e. the CRI/FRC has not been found. This may also
291  * result from a too weak or noisy signal. Error correction must be
292  * implemented at a higher layer.
293  */
294 _vbi_inline vbi_bool
295 vbi_bit_slice(vbi_bit_slicer *slicer, uint8_t *raw, uint8_t *buf)
296 {
297 	return slicer->func(slicer, raw, buf);
298 }
299 /** @} */
300 #endif
301 /**
302  * @ingroup Rawdec
303  * @brief Raw vbi decoder context.
304  *
305  * Only the sampling parameters are public. See
306  * vbi_raw_decoder_parameters() and vbi_raw_decoder_add_services()
307  * for usage.
308  */
309 typedef struct vbi_raw_decoder {
310 	/* Sampling parameters */
311 
312 	/**
313 	 * Either 525 (M/NTSC, M/PAL) or 625 (PAL, SECAM), describing the
314 	 * scan line system all line numbers refer to.
315 	 */
316 	int			scanning;
317 	/**
318 	 * Format of the raw vbi data.
319 	 */
320 	vbi_pixfmt		sampling_format;
321 	/**
322 	 * Sampling rate in Hz, the number of samples or pixels
323 	 * captured per second.
324 	 */
325 	int			sampling_rate;		/* Hz */
326 	/**
327 	 * Number of samples or pixels captured per scan line,
328 	 * in bytes. This determines the raw vbi image width and you
329 	 * want it large enough to cover all data transmitted in the line (with
330 	 * headroom).
331 	 */
332 	int			bytes_per_line;
333 	/**
334 	 * The distance from 0H (leading edge hsync, half amplitude point)
335 	 * to the first sample (pixel) captured, in samples (pixels). You want
336 	 * an offset small enough not to miss the start of the data
337 	 * transmitted.
338 	 */
339 	int			offset;			/* 0H, samples */
340 	/**
341 	 * First scan line to be captured, first and second field
342 	 * respectively, according to the ITU-R line numbering scheme
343 	 * (see vbi_sliced). Set to zero if the exact line number isn't
344 	 * known.
345 	 */
346 	int			start[2];		/* ITU-R numbering */
347 	/**
348 	 * Number of scan lines captured, first and second
349 	 * field respectively. This can be zero if only data from one
350 	 * field is required. The sum @a count[0] + @a count[1] determines the
351 	 * raw vbi image height.
352 	 */
353 	int			count[2];		/* field lines */
354 	/**
355 	 * In the raw vbi image, normally all lines of the second
356 	 * field are supposed to follow all lines of the first field. When
357 	 * this flag is set, the scan lines of first and second field
358 	 * will be interleaved in memory. This implies @a count[0] and @a count[1]
359 	 * are equal.
360 	 */
361 	vbi_bool		interlaced;
362 	/**
363 	 * Fields must be stored in temporal order, i. e. as the
364 	 * lines have been captured. It is assumed that the first field is
365 	 * also stored first in memory, however if the hardware cannot reliable
366 	 * distinguish fields this flag shall be cleared, which disables
367 	 * decoding of data services depending on the field number.
368 	 */
369 	vbi_bool		synchronous;
370 
371 	/*< private >*/
372 
373 	GMutex			mutex;
374 
375 	unsigned int		services;
376 #if 0				/* DISABLED LEGACY DECODER */
377 	int			num_jobs;
378 #endif
379 
380 	int8_t *		pattern; /* The real vbi3_raw_decoder */
381 #if 0		/* DISABLED LEGACY DECODER */
382 	struct _vbi_raw_decoder_job {
383 		unsigned int		id;
384 		int			offset;
385 		vbi_bit_slicer		slicer;
386 	}			jobs[8];
387 #endif
388 } vbi_raw_decoder;
389 
390 /**
391  * @addtogroup Rawdec
392  * @{
393  */
394 extern void		vbi_raw_decoder_init(vbi_raw_decoder *rd);
395 extern void		vbi_raw_decoder_reset(vbi_raw_decoder *rd);
396 extern void		vbi_raw_decoder_destroy(vbi_raw_decoder *rd);
397 extern unsigned int	vbi_raw_decoder_add_services(vbi_raw_decoder *rd,
398 						     unsigned int services,
399 						     int strict);
400 extern unsigned int     vbi_raw_decoder_check_services(vbi_raw_decoder *rd,
401 						     unsigned int services, int strict);
402 extern unsigned int	vbi_raw_decoder_remove_services(vbi_raw_decoder *rd,
403 							unsigned int services);
404 extern void             vbi_raw_decoder_resize( vbi_raw_decoder *rd,
405 						int * start, unsigned int * count );
406 extern unsigned int	vbi_raw_decoder_parameters(vbi_raw_decoder *rd, unsigned int services,
407 						   int scanning, int *max_rate);
408 extern int		vbi_raw_decode(vbi_raw_decoder *rd, uint8_t *raw, vbi_sliced *out);
409 
410 void vbi_initialize_gst_debug (void);
411 /** @} */
412 
413 /* Private */
414 
415 #endif /* DECODER_H */
416 
417 /*
418 Local variables:
419 c-set-style: K&R
420 c-basic-offset: 8
421 End:
422 */
423