4 * Copyright (C) 2000-2007 Michael H. Schimek
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.
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.
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.
22 /* $Id: bit_slicer.c,v 1.16 2008-02-19 00:35:14 mschimek Exp $ */
29 #include "bit_slicer.h"
31 # define VBI_PIXFMT_Y8 VBI_PIXFMT_YUV420
32 # define VBI_PIXFMT_RGB24_LE VBI_PIXFMT_RGB24
33 # define VBI_PIXFMT_BGR24_LE VBI_PIXFMT_BGR24
34 # define VBI_PIXFMT_RGBA24_LE VBI_PIXFMT_RGBA32_LE
35 # define VBI_PIXFMT_BGRA24_LE VBI_PIXFMT_BGRA32_LE
36 # define VBI_PIXFMT_RGBA24_BE VBI_PIXFMT_RGBA32_BE
37 # define VBI_PIXFMT_BGRA24_BE VBI_PIXFMT_BGRA32_BE
38 # define vbi_pixfmt_bytes_per_pixel VBI_PIXFMT_BPP
41 * $addtogroup BitSlicer Bit Slicer
43 * $brief Converting a single scan line of raw VBI
44 * data to sliced VBI data.
46 * These are low level functions most useful if you want to decode
47 * data services not covered by libzvbi. Usually you will want to
48 * use the raw VBI decoder, converting several lines of different
49 * data services at once.
52 /* This is time critical, tinker with care.
54 What about all these macros? They are templates to avoid a
55 pixel format switch within time critical loops. Instead we
56 compile bit slicer functions for different pixel formats.
58 I would use inline functions for proper type checking, but
59 there's no guarantee the compiler really will inline. */
61 /* Read a green sample, e.g. rrrrrggg gggbbbbb. endian is const. */
62 #define GREEN2(raw, endian) \
63 (((raw)[0 + endian] + (raw)[1 - endian] * 256) & bs->green_mask)
65 /* Read a sample with pixfmt conversion. pixfmt is const. */
66 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
68 ((VBI_PIXFMT_RGB16_LE == pixfmt) ? \
69 *(const uint16_t *)(raw) & bs->green_mask : \
70 ((VBI_PIXFMT_RGB16_BE == pixfmt) ? \
73 #elif G_BYTE_ORDER == G_BIG_ENDIAN
75 ((VBI_PIXFMT_RGB16_LE == pixfmt) ? \
77 ((VBI_PIXFMT_RGB16_BE == pixfmt) ? \
78 *(const uint16_t *)(raw) & bs->green_mask : \
82 ((VBI_PIXFMT_RGB16_LE == pixfmt) ? \
84 ((VBI_PIXFMT_RGB16_BE == pixfmt) ? \
89 /* raw0 = raw[index >> 8], linear interpolated. */
90 #define SAMPLE(_kind) \
94 r = raw + (i >> 8) * bpp; \
96 raw1 = GREEN (r + bpp); \
97 raw0 = (int)(raw1 - raw0) * (i & 255) + (raw0 << 8); \
98 if (collect_points) { \
99 points->kind = _kind; \
100 points->index = (raw - raw_start) * 256 + i; \
101 points->level = raw0; \
102 points->thresh = tr; \
109 i = bs->phase_shift; /* current bit position << 8 */ \
113 for (j = bs->frc_bits; j > 0; --j) { \
114 SAMPLE (VBI3_FRC_BIT); \
115 c = c * 2 + (raw0 >= tr); \
116 i += bs->step; /* next bit */ \
122 switch (bs->endian) { \
123 case 3: /* bitwise, lsb first */ \
124 for (j = 0; j < bs->payload; ++j) { \
125 SAMPLE (VBI3_PAYLOAD_BIT); \
126 c = (c >> 1) + ((raw0 >= tr) << 7); \
131 *buffer = c >> ((8 - bs->payload) & 7); \
134 case 2: /* bitwise, msb first */ \
135 for (j = 0; j < bs->payload; ++j) { \
136 SAMPLE (VBI3_PAYLOAD_BIT); \
137 c = c * 2 + (raw0 >= tr); \
142 *buffer = c & ((1 << (bs->payload & 7)) - 1); \
145 case 1: /* octets, lsb first */ \
146 for (j = bs->payload; j > 0; --j) { \
147 for (k = 0, c = 0; k < 8; ++k) { \
148 SAMPLE (VBI3_PAYLOAD_BIT); \
149 c += (raw0 >= tr) << k; \
156 default: /* octets, msb first */ \
157 for (j = bs->payload; j > 0; --j) { \
158 for (k = 0; k < 8; ++k) { \
159 SAMPLE (VBI3_PAYLOAD_BIT); \
160 c = c * 2 + (raw0 >= tr); \
172 unsigned char b; /* current bit */ \
174 tavg = (t + (oversampling / 2)) / oversampling; \
177 if (unlikely (b ^ b1)) { \
178 cl = bs->oversampling_rate >> 1; \
180 cl += bs->cri_rate; \
182 if (cl >= bs->oversampling_rate) { \
183 if (collect_points) { \
184 points->kind = VBI3_CRI_BIT; \
185 points->index = (raw - raw_start) << 8; \
186 points->level = tavg << 8; \
187 points->thresh = tr << 8; \
191 cl -= bs->oversampling_rate; \
193 if ((c & bs->cri_mask) == bs->cri) { \
195 if (collect_points) { \
206 if (oversampling > 1) \
212 const uint8_t *raw_start; \
213 unsigned int i, j, k; \
214 unsigned int cl; /* clock */ \
215 unsigned int thresh0; /* old 0/1 threshold */ \
216 unsigned int tr; /* current threshold */ \
217 unsigned int c; /* current byte */ \
218 unsigned int t; /* t = raw[0] * j + raw[1] * (1 - j) */ \
219 unsigned int raw0; /* oversampling temporary */ \
221 unsigned char b1; /* previous bit */ \
223 thresh0 = bs->thresh; \
231 for (i = bs->cri_samples; i > 0; --i) { \
232 tr = bs->thresh >> thresh_frac; \
233 raw0 = GREEN (raw); \
234 raw1 = GREEN (raw + bpp); \
236 bs->thresh += (int)(raw0 - tr) * (int) ABS ((int) raw1); \
237 t = raw0 * oversampling; \
239 for (j = oversampling; j > 0; --j) \
245 bs->thresh = thresh0; \
247 if (collect_points) \
248 *n_points = points - points_start; \
253 #define BIT_SLICER(fmt, os, tf) \
255 bit_slicer_ ## fmt (vbi3_bit_slicer * bs, \
257 vbi3_bit_slicer_point *points, \
258 unsigned int * n_points, \
259 const uint8_t * raw) \
261 static const vbi_pixfmt pixfmt = VBI_PIXFMT_ ## fmt; \
263 vbi_pixfmt_bytes_per_pixel (VBI_PIXFMT_ ## fmt); \
264 static const unsigned int oversampling = os; \
265 static const vbi3_bit_slicer_point *points_start = NULL; \
266 static const vbi_bool collect_points = FALSE; \
267 unsigned int thresh_frac = tf; \
272 #define DEF_THR_FRAC 9
274 BIT_SLICER (Y8, 4, DEF_THR_FRAC) /* any format with 0 bytes between Y or G */
275 BIT_SLICER (YUYV, 4, DEF_THR_FRAC) /* 1 byte */
276 BIT_SLICER (RGB24_LE, 4, DEF_THR_FRAC) /* 2 bytes */
277 BIT_SLICER (RGBA24_LE, 4, DEF_THR_FRAC) /* 3 bytes */
278 BIT_SLICER (RGB16_LE, 4, bs->thresh_frac)
279 BIT_SLICER (RGB16_BE, 4, bs->thresh_frac)
281 static const unsigned int LP_AVG = 4;
284 low_pass_bit_slicer_Y8 (vbi3_bit_slicer * bs,
286 vbi3_bit_slicer_point * points, unsigned int *n_points, const uint8_t * raw)
288 vbi3_bit_slicer_point *points_start;
289 const uint8_t *raw_start;
290 unsigned int i, j, k, m;
291 unsigned int cl; /* clock */
292 unsigned int thresh0; /* old 0/1 threshold */
293 unsigned int tr; /* current threshold */
294 unsigned int c; /* current byte */
295 unsigned int raw0; /* oversampling temporary */
296 unsigned char b1; /* previous bit */
298 unsigned int raw0sum;
300 points_start = points;
305 bps = bs->bytes_per_sample;
307 thresh0 = bs->thresh;
314 for (m = bps; m < (bps << LP_AVG); m += bps) {
321 unsigned char b; /* current bit */
323 tr = bs->thresh >> bs->thresh_frac;
325 raw0sum = raw0sum + raw[bps << LP_AVG]
328 bs->thresh += (int) (raw0 - tr)
329 * (int) ABS ((int) (raw0sum - raw0));
333 if (unlikely (b ^ b1)) {
334 cl = bs->oversampling_rate >> 1;
338 if (cl >= bs->oversampling_rate) {
339 if (unlikely (NULL != points)) {
340 points->kind = VBI3_CRI_BIT;
341 points->index = (raw - raw_start)
342 * 256 / bs->bytes_per_sample + (1 << LP_AVG) * 128;
343 points->level = raw0 << (8 - LP_AVG);
344 points->thresh = tr << (8 - LP_AVG);
348 cl -= bs->oversampling_rate;
350 if ((c & bs->cri_mask) == bs->cri) {
359 bs->thresh = thresh0;
361 if (unlikely (NULL != points))
362 *n_points = points - points_start;
368 #define LP_SAMPLE(_kind) \
370 unsigned int ii = (i >> 8) * bps; \
373 for (m = bps; m < (bps << LP_AVG); m += bps) \
374 raw0 += raw[ii + m]; \
375 if (unlikely (NULL != points)) { \
376 points->kind = _kind; \
377 points->index = (raw - raw_start) \
378 * 256 / bs->bytes_per_sample \
379 + (1 << LP_AVG) * 128 \
381 points->level = raw0 << (8 - LP_AVG); \
382 points->thresh = tr << (8 - LP_AVG); \
387 i = bs->phase_shift; /* current bit position << 8 */
390 for (j = bs->frc_bits; j > 0; --j) {
391 LP_SAMPLE (VBI3_FRC_BIT);
392 c = c * 2 + (raw0 >= tr);
393 i += bs->step; /* next bit */
401 switch (bs->endian) {
402 case 3: /* bitwise, lsb first */
403 for (j = 0; j < bs->payload; ++j) {
404 LP_SAMPLE (VBI3_PAYLOAD_BIT);
405 c = (c >> 1) + ((raw0 >= tr) << 7);
410 *buffer = c >> ((8 - bs->payload) & 7);
413 case 2: /* bitwise, msb first */
414 for (j = 0; j < bs->payload; ++j) {
415 LP_SAMPLE (VBI3_PAYLOAD_BIT);
416 c = c * 2 + (raw0 >= tr);
421 *buffer = c & ((1 << (bs->payload & 7)) - 1);
424 case 1: /* octets, lsb first */
427 for (k = 0; k < 8; ++k) {
428 LP_SAMPLE (VBI3_PAYLOAD_BIT);
429 c = (c >> 1) + ((raw0 >= tr) << 7);
436 default: /* octets, msb first */
439 for (k = 0; k < 8; ++k) {
440 LP_SAMPLE (VBI3_PAYLOAD_BIT);
441 c = c * 2 + (raw0 >= tr);
449 if (unlikely (NULL != points)) {
450 *n_points = points - points_start;
457 null_function (vbi3_bit_slicer * bs,
459 vbi3_bit_slicer_point * points, unsigned int *n_points, const uint8_t * raw)
461 /* buffer = buffer; /\* unused *\/ */
462 /* points = points; */
463 /* n_points = n_points; */
466 warning (&bs->log, "vbi3_bit_slicer_set_params() not called.");
472 * @param bs Pointer to vbi3_bit_slicer object allocated with
473 * vbi3_bit_slicer_new().
474 * @param buffer Output data.
475 * @param buffer_size Size of the output buffer. The buffer must be
476 + large enough to store the number of bits given as @a payload_bits to
477 * vbi3_bit_slicer_new().
478 * @param points Information about the bits sampled by the bit slicer
480 * @param n_points The number of sampling points stored in the
481 * @a points array will be stored here.
482 * @param max_points Size of the @a points array. The array must be
483 * large enough to store one sampling point for all @a crc_bits,
484 * @a frc_bits and @a payload_bits given to vbi3_bit_slicer_new().
485 * @param raw Input data. At least the number of pixels or samples
486 * given as @a samples_per_line to vbi3_bit_slicer_new().
488 * Like vbi3_bit_slicer_slice() but additionally provides information
489 * about where and how bits were sampled. This is mainly interesting
493 * @c FALSE if the @a buffer or @a points array is too small, if the
494 * pixel format is not supported or if the raw data does not contain
495 * the expected information, i. e. the CRI/FRC has not been found. In
496 * these cases the @a buffer remains unmodified but the @a points
497 * array may contain data.
500 * Currently this function is only implemented for
501 * raw data in planar YUV formats and @c VBI3_PIXFMT_Y8.
504 vbi3_bit_slicer_slice_with_points
505 (vbi3_bit_slicer * bs,
507 unsigned int buffer_size,
508 vbi3_bit_slicer_point * points,
509 unsigned int *n_points, unsigned int max_points, const uint8_t * raw) {
510 static const vbi_pixfmt pixfmt = VBI_PIXFMT_Y8;
511 static const unsigned int bpp = 1;
512 static const unsigned int oversampling = 4; /* see above */
513 static const unsigned int thresh_frac = DEF_THR_FRAC;
514 static const vbi_bool collect_points = TRUE;
515 vbi3_bit_slicer_point *points_start;
518 assert (NULL != buffer);
519 assert (NULL != points);
520 assert (NULL != n_points);
521 assert (NULL != raw);
523 points_start = points;
526 if (bs->payload > buffer_size * 8) {
528 "buffer_size %u < %u bits of payload.", buffer_size * 8, bs->payload);
532 if (bs->total_bits > max_points) {
534 "max_points %u < %u CRI, FRC and payload bits.",
535 max_points, bs->total_bits);
539 if (low_pass_bit_slicer_Y8 == bs->func) {
540 return bs->func (bs, buffer, points, n_points, raw);
541 } else if (bit_slicer_Y8 != bs->func) {
543 "Function not implemented for pixfmt %u.", bs->sample_format);
544 return bs->func (bs, buffer,
554 * @param bs Pointer to vbi3_bit_slicer object allocated with
555 * vbi3_bit_slicer_new(). You must also call
556 * vbi3_bit_slicer_set_params() before calling this function.
557 * @param buffer Output data.
558 * @param buffer_size Size of the output buffer. The buffer must be
559 + large enough to store the number of bits given as @a payload to
560 * vbi3_bit_slicer_new().
561 * @param raw Input data. At least the number of pixels or samples
562 * given as @a samples_per_line to vbi3_bit_slicer_new().
564 * Decodes one scan line of raw vbi data. Note the bit slicer tries
565 * to adapt to the average signal amplitude, you should avoid
566 * using the same vbi3_bit_slicer object for data from different
570 * @c FALSE if the @a buffer is too small or if the raw data does not
571 * contain the expected information, i. e. the CRI/FRC has not been
572 * found. This may also result from a too weak or noisy signal. Error
573 * correction must be implemented at a higher layer. When the function
574 * fails, the @a buffer remains unmodified.
577 vbi3_bit_slicer_slice (vbi3_bit_slicer * bs,
578 uint8_t * buffer, unsigned int buffer_size, const uint8_t * raw)
581 assert (NULL != buffer);
582 assert (NULL != raw);
584 if (bs->payload > buffer_size * 8) {
586 "buffer_size %u < %u bits of payload.", buffer_size * 8, bs->payload);
590 return bs->func (bs, buffer,
597 * @param bs Pointer to vbi3_bit_slicer object allocated with
598 * vbi3_bit_slicer_new().
599 * @param sample_format Format of the raw data, see vbi3_pixfmt.
600 * Note the bit slicer looks only at the green component of RGB
602 * @param sampling_rate Raw vbi sampling rate in Hz, that is the number
603 * of samples or pixels sampled per second by the hardware.
604 * @param sample_offset The bit slicer shall skip this number of samples at
605 * the start of the line.
606 * @param samples_per_line Number of samples or pixels in one raw vbi
607 * line later passed to vbi3_bit_slicer_slice(). This limits the number of
608 * bytes read from the raw data buffer. Do not to confuse the value
609 * with bytes per line.
610 * @param cri The Clock Run In is a NRZ modulated sequence of '1'
611 * and '0' bits prepending most data transmissions to synchronize data
612 * acquisition circuits. The bit slicer compares the bits in this
613 * word, lsb last transmitted, against the transmitted CRI. Decoding
614 * of FRC and payload starts with the next bit after a match, thus
615 * @a cri must contain a unique bit sequence. For example 0xAB to
616 * match '101010101011xxx'.
617 * @param cri_mask Of the CRI bits in @a cri, only these bits are
618 * significant for a match. For instance it is wise not to rely on
619 * the very first CRI bits transmitted.
620 * @param cri_bits Number of CRI bits, must not exceed 32.
621 * @param cri_rate CRI bit rate in Hz, the number of CRI bits
622 * transmitted per second.
623 * @param cri_end Number of samples between the start of the line and
624 * the latest possible end of the CRI. This is useful when
625 * the transmission is much shorter than samples_per_line, otherwise
626 * just pass @c ~0 and a limit will be calculated.
627 * @param frc The FRaming Code usually following the CRI is a bit
628 * sequence identifying the data service. There is no mask parameter,
629 * all bits must match. We assume FRC has the same @a modulation as
630 * the payload and is transmitted at @a payload_rate.
631 * @param frc_bits Number of FRC bits, must not exceed 32.
632 * @param payload_bits Number of payload bits. Only this data
633 * will be stored in the vbi3_bit_slicer_slice() output. If this number
634 * is no multiple of eight, the most significant bits of the
635 * last byte are undefined.
636 * @param payload_rate Payload bit rate in Hz, the number of payload
637 * bits transmitted per second.
638 * @param modulation Modulation of the payload, see vbi3_modulation.
640 * Initializes a vbi3_bit_slicer object for use with
641 * vbi3_bit_slicer_slice(). This is a low level function, see also
642 * vbi3_raw_decoder_new().
645 * @c FALSE when the parameters are invalid (e. g.
646 * @a samples_per_line too small to contain CRI, FRC and payload).
649 vbi3_bit_slicer_set_params (vbi3_bit_slicer * bs,
650 vbi_pixfmt sample_format,
651 unsigned int sampling_rate,
652 unsigned int sample_offset,
653 unsigned int samples_per_line,
655 unsigned int cri_mask,
656 unsigned int cri_bits,
657 unsigned int cri_rate,
658 unsigned int cri_end,
660 unsigned int frc_bits,
661 unsigned int payload_bits,
662 unsigned int payload_rate, vbi3_modulation modulation)
666 unsigned int min_samples_per_bit;
667 unsigned int oversampling;
668 unsigned int data_bits;
669 unsigned int data_samples;
670 unsigned int cri_samples;
674 assert (cri_bits <= 32);
675 assert (frc_bits <= 32);
676 assert (payload_bits <= 32767);
677 assert (samples_per_line <= 32767);
679 if (cri_rate > sampling_rate) {
681 "cri_rate %u > sampling_rate %u.", cri_rate, sampling_rate);
685 if (payload_rate > sampling_rate) {
687 "payload_rate %u > sampling_rate %u.", payload_rate, sampling_rate);
691 min_samples_per_bit = sampling_rate / MAX (cri_rate, payload_rate);
693 bs->sample_format = sample_format;
695 c_mask = (cri_bits == 32) ? ~0U : (1U << cri_bits) - 1;
696 f_mask = (frc_bits == 32) ? ~0U : (1U << frc_bits) - 1;
701 /* 0-1 threshold, start value. */
702 bs->thresh = 105 << DEF_THR_FRAC;
703 bs->thresh_frac = DEF_THR_FRAC;
705 switch (sample_format) {
706 case VBI_PIXFMT_YUV420:
707 bs->bytes_per_sample = 1;
708 bs->func = bit_slicer_Y8;
709 if (min_samples_per_bit > (3U << (LP_AVG - 1))) {
710 bs->func = low_pass_bit_slicer_Y8;
712 bs->thresh <<= LP_AVG - 2;
713 bs->thresh_frac += LP_AVG - 2;
718 case VBI_PIXFMT_YUYV:
719 case VBI_PIXFMT_YVYU:
720 bs->bytes_per_sample = 2;
721 bs->func = bit_slicer_YUYV;
722 if (min_samples_per_bit > (3U << (LP_AVG - 1))) {
723 bs->func = low_pass_bit_slicer_Y8;
725 bs->thresh <<= LP_AVG - 2;
726 bs->thresh_frac += LP_AVG - 2;
730 case VBI_PIXFMT_UYVY:
731 case VBI_PIXFMT_VYUY:
733 bs->bytes_per_sample = 2;
734 bs->func = bit_slicer_YUYV;
735 if (min_samples_per_bit > (3U << (LP_AVG - 1))) {
736 bs->func = low_pass_bit_slicer_Y8;
738 bs->thresh <<= LP_AVG - 2;
739 bs->thresh_frac += LP_AVG - 2;
743 case VBI_PIXFMT_RGBA24_LE:
744 case VBI_PIXFMT_BGRA24_LE:
746 bs->bytes_per_sample = 4;
747 bs->func = bit_slicer_RGBA24_LE;
748 if (min_samples_per_bit > (3U << (LP_AVG - 1))) {
749 bs->func = low_pass_bit_slicer_Y8;
751 bs->thresh <<= LP_AVG - 2;
752 bs->thresh_frac += LP_AVG - 2;
756 case VBI_PIXFMT_RGBA24_BE:
757 case VBI_PIXFMT_BGRA24_BE:
759 bs->bytes_per_sample = 4;
760 bs->func = bit_slicer_RGBA24_LE;
761 if (min_samples_per_bit > (3U << (LP_AVG - 1))) {
762 bs->func = low_pass_bit_slicer_Y8;
764 bs->thresh <<= LP_AVG - 2;
765 bs->thresh_frac += LP_AVG - 2;
769 case VBI_PIXFMT_RGB24_LE:
770 case VBI_PIXFMT_BGR24_LE:
772 bs->bytes_per_sample = 3;
773 bs->func = bit_slicer_RGB24_LE;
774 if (min_samples_per_bit > (3U << (LP_AVG - 1))) {
775 bs->func = low_pass_bit_slicer_Y8;
777 bs->thresh <<= LP_AVG - 2;
778 bs->thresh_frac += LP_AVG - 2;
782 case VBI_PIXFMT_RGB16_LE:
783 case VBI_PIXFMT_BGR16_LE:
784 bs->func = bit_slicer_RGB16_LE;
785 bs->green_mask = 0x07E0;
786 bs->thresh = 105 << (5 - 2 + 12);
787 bs->thresh_frac = 12;
788 bs->bytes_per_sample = 2;
791 case VBI_PIXFMT_RGB16_BE:
792 case VBI_PIXFMT_BGR16_BE:
793 bs->func = bit_slicer_RGB16_BE;
794 bs->green_mask = 0x07E0;
795 bs->thresh = 105 << (5 - 2 + 12);
796 bs->thresh_frac = 12;
797 bs->bytes_per_sample = 2;
800 case VBI_PIXFMT_RGBA15_LE:
801 case VBI_PIXFMT_BGRA15_LE:
802 bs->func = bit_slicer_RGB16_LE;
803 bs->green_mask = 0x03E0;
804 bs->thresh = 105 << (5 - 3 + 11);
805 bs->thresh_frac = 11;
806 bs->bytes_per_sample = 2;
809 case VBI_PIXFMT_RGBA15_BE:
810 case VBI_PIXFMT_BGRA15_BE:
811 bs->func = bit_slicer_RGB16_BE;
812 bs->green_mask = 0x03E0;
813 bs->thresh = 105 << (5 - 3 + 11);
814 bs->thresh_frac = 11;
815 bs->bytes_per_sample = 2;
818 case VBI_PIXFMT_ARGB15_LE:
819 case VBI_PIXFMT_ABGR15_LE:
820 bs->func = bit_slicer_RGB16_LE;
821 bs->green_mask = 0x07C0;
822 bs->thresh = 105 << (6 - 3 + 12);
823 bs->thresh_frac = 12;
824 bs->bytes_per_sample = 2;
827 case VBI_PIXFMT_ARGB15_BE:
828 case VBI_PIXFMT_ABGR15_BE:
829 bs->func = bit_slicer_RGB16_BE;
830 bs->green_mask = 0x07C0;
831 bs->thresh = 105 << (6 - 3 + 12);
832 bs->thresh_frac = 12;
833 bs->bytes_per_sample = 2;
839 "Unknown sample_format 0x%x.", (unsigned int) sample_format);
843 bs->skip = sample_offset * bs->bytes_per_sample + skip;
845 bs->cri_mask = cri_mask & c_mask;
846 bs->cri = cri & bs->cri_mask;
848 /* We stop searching for CRI when CRI, FRC and payload
849 cannot possibly fit anymore. Additionally this eliminates
850 a data end check in the payload loop. */
851 cri_samples = (sampling_rate * (int64_t) cri_bits) / cri_rate;
853 data_bits = payload_bits + frc_bits;
854 data_samples = (sampling_rate * (int64_t) data_bits) / payload_rate;
856 bs->total_bits = cri_bits + data_bits;
858 if ((sample_offset > samples_per_line)
859 || ((cri_samples + data_samples)
860 > (samples_per_line - sample_offset))) {
862 "%u samples_per_line too small for "
863 "sample_offset %u + %u cri_bits (%u samples) "
864 "+ %u frc_bits and %u payload_bits "
866 samples_per_line, sample_offset,
867 cri_bits, cri_samples, frc_bits, payload_bits, data_samples);
871 cri_end = MIN (cri_end, samples_per_line - data_samples);
873 bs->cri_samples = cri_end - sample_offset;
874 bs->cri_rate = cri_rate;
876 bs->oversampling_rate = sampling_rate * oversampling;
878 bs->frc = frc & f_mask;
879 bs->frc_bits = frc_bits;
881 /* Payload bit distance in 1/256 raw samples. */
882 bs->step = (sampling_rate * (int64_t) 256) / payload_rate;
884 if (payload_bits & 7) {
885 /* Use bit routines. */
886 bs->payload = payload_bits;
889 /* Use faster octet routines. */
890 bs->payload = payload_bits >> 3;
894 switch (modulation) {
895 case VBI3_MODULATION_NRZ_MSB:
900 case VBI3_MODULATION_NRZ_LSB:
901 bs->phase_shift = (int)
902 (sampling_rate * 256.0 / cri_rate * .5 + bs->step * .5 + 128);
905 case VBI3_MODULATION_BIPHASE_MSB:
910 case VBI3_MODULATION_BIPHASE_LSB:
911 /* Phase shift between the NRZ modulated CRI and the
912 biphase modulated rest. */
913 bs->phase_shift = (int)
914 (sampling_rate * 256.0 / cri_rate * .5 + bs->step * .25 + 128);
921 bs->func = null_function;
927 vbi3_bit_slicer_set_log_fn (vbi3_bit_slicer * bs,
928 vbi_log_mask mask, vbi_log_fn * log_fn, void *user_data)
937 bs->log.user_data = user_data;
944 _vbi3_bit_slicer_destroy (vbi3_bit_slicer * bs)
956 _vbi3_bit_slicer_init (vbi3_bit_slicer * bs)
962 bs->func = null_function;
968 * @param bs Pointer to a vbi3_bit_slicer object allocated with
969 * vbi3_bit_slicer_new(), can be NULL.
971 * Deletes a vbi3_bit_slicer object.
974 vbi3_bit_slicer_delete (vbi3_bit_slicer * bs)
979 _vbi3_bit_slicer_destroy (bs);
985 * Allocates a new vbi3_bit_slicer object.
988 * @c NULL when out of memory.
991 vbi3_bit_slicer_new (void)
995 bs = vbi_malloc (sizeof (*bs));
1000 _vbi3_bit_slicer_init (bs);