2 * libzvbi -- Old raw VBI decoder
4 * Copyright (C) 2000, 2001, 2002 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: decoder.c,v 1.25 2008-02-19 00:35:15 mschimek Exp $ */
24 /* Note this code is only retained for compatibility with older versions
25 of libzvbi. vbi_raw_decoder is now just a wrapper for the new raw
26 decoder (raw_decoder.c) and bit slicer (bit_slicer.c). We'll drop
27 the old API in libzvbi 0.3. Other modules (e.g. io-v4l2k.c) should
28 already use the new raw VBI decoder directly. */
38 #include "raw_decoder.h"
41 * @addtogroup Rawdec Raw VBI decoder
43 * @brief Converting raw VBI samples to bits and bytes.
45 * The libzvbi already offers hardware interfaces to obtain sliced
46 * VBI data for further processing. However if you want to write your own
47 * interface or decode data services not covered by libzvbi you can use
48 * these lower level functions.
51 #if 0 /* LEGACY BIT SLICER */
56 #define OVERSAMPLING 4 /* 1, 2, 4, 8 */
60 * Note this is just a template. The code is inlined,
61 * with bpp and endian being const.
63 * This function translates from the image format to
64 * plain bytes, with linear interpolation of samples.
65 * Could be further improved with a lowpass filter.
67 static inline unsigned int
68 sample (uint8_t * raw, int offs, int bpp, int endian)
70 unsigned char frac = offs;
74 case 14: /* 1:5:5:5 LE/BE */
75 raw += (offs >> 8) * 2;
76 raw0 = (raw[0 + endian] + raw[1 - endian] * 256) & 0x07C0;
77 raw1 = (raw[2 + endian] + raw[3 - endian] * 256) & 0x07C0;
78 return (raw1 - raw0) * frac + (raw0 << 8);
80 case 15: /* 5:5:5:1 LE/BE */
81 raw += (offs >> 8) * 2;
82 raw0 = (raw[0 + endian] + raw[1 - endian] * 256) & 0x03E0;
83 raw1 = (raw[2 + endian] + raw[3 - endian] * 256) & 0x03E0;
84 return (raw1 - raw0) * frac + (raw0 << 8);
86 case 16: /* 5:6:5 LE/BE */
87 raw += (offs >> 8) * 2;
88 raw0 = (raw[0 + endian] + raw[1 - endian] * 256) & 0x07E0;
89 raw1 = (raw[2 + endian] + raw[3 - endian] * 256) & 0x07E0;
90 return (raw1 - raw0) * frac + (raw0 << 8);
92 default: /* 8 (intermediate bytes skipped by caller) */
93 raw += (offs >> 8) * bpp;
94 return (raw[bpp] - raw[0]) * frac + (raw[0] << 8);
99 * Note this is just a template. The code is inlined,
100 * with bpp being const.
102 static inline vbi_bool
103 bit_slicer_tmpl (vbi_bit_slicer * d, uint8_t * raw,
104 uint8_t * buf, int bpp, int endian)
106 unsigned int i, j, k;
107 unsigned int cl = 0, thresh0 = d->thresh, tr;
108 unsigned int c = 0, t;
109 unsigned char b, b1 = 0;
110 int raw0, raw1, mask;
121 for (i = d->cri_bytes; i > 0; raw += (bpp >= 14 && bpp <= 16) ? 2 : bpp, i--) {
122 if (bpp >= 14 && bpp <= 16) {
123 raw0 = (raw[0 + endian] + raw[1 - endian] * 256) & mask;
124 raw1 = (raw[2 + endian] + raw[3 - endian] * 256) & mask;
125 tr = d->thresh >> THRESH_FRAC;
126 d->thresh += ((raw0 - tr) * (int) ABS (raw1 - raw0)) >>
127 ((bpp == 15) ? 2 : 3);
128 t = raw0 * OVERSAMPLING;
130 tr = d->thresh >> THRESH_FRAC;
131 d->thresh += ((int) raw[0] - tr) * (int) ABS (raw[bpp] - raw[0]);
132 t = raw[0] * OVERSAMPLING;
135 for (j = OVERSAMPLING; j > 0; j--) {
136 b = ((t + (OVERSAMPLING / 2)) / OVERSAMPLING >= tr);
139 cl = d->oversampling_rate >> 1;
143 if (cl >= (unsigned int) d->oversampling_rate) {
144 cl -= d->oversampling_rate;
148 if ((c & d->cri_mask) == d->cri) {
153 for (j = d->frc_bits; j > 0; j--) {
154 c = c * 2 + (sample (raw, i, bpp, endian) >= tr);
161 /* CRI/FRC found, now get the
166 for (j = 0; j < (unsigned int) d->payload; j++) {
168 c += (sample (raw, i, bpp, endian) >= tr) << 7;
175 *buf = c >> ((8 - d->payload) & 7);
179 for (j = 0; j < (unsigned int) d->payload; j++) {
180 c = c * 2 + (sample (raw, i, bpp, endian) >= tr);
187 *buf = c & ((1 << (d->payload & 7)) - 1);
191 for (j = d->payload; j > 0; j--) {
192 for (k = 0; k < 8; k++) {
194 c += (sample (raw, i, bpp, endian) >= tr) << 7;
204 for (j = d->payload; j > 0; j--) {
205 for (k = 0; k < 8; k++) {
206 c = c * 2 + (sample (raw, i, bpp, endian) >= tr);
223 if (OVERSAMPLING > 1) {
224 if (bpp >= 14 && bpp <= 16) {
241 bit_slicer_1 (vbi_bit_slicer * d, uint8_t * raw, uint8_t * buf)
243 return bit_slicer_tmpl (d, raw, buf, 1, 0);
247 bit_slicer_2 (vbi_bit_slicer * d, uint8_t * raw, uint8_t * buf)
249 return bit_slicer_tmpl (d, raw, buf, 2, 0);
253 bit_slicer_3 (vbi_bit_slicer * d, uint8_t * raw, uint8_t * buf)
255 return bit_slicer_tmpl (d, raw, buf, 3, 0);
259 bit_slicer_4 (vbi_bit_slicer * d, uint8_t * raw, uint8_t * buf)
261 return bit_slicer_tmpl (d, raw, buf, 4, 0);
265 bit_slicer_1555_le (vbi_bit_slicer * d, uint8_t * raw, uint8_t * buf)
267 return bit_slicer_tmpl (d, raw, buf, 14, 0);
271 bit_slicer_5551_le (vbi_bit_slicer * d, uint8_t * raw, uint8_t * buf)
273 return bit_slicer_tmpl (d, raw, buf, 15, 0);
277 bit_slicer_565_le (vbi_bit_slicer * d, uint8_t * raw, uint8_t * buf)
279 return bit_slicer_tmpl (d, raw, buf, 16, 0);
283 bit_slicer_1555_be (vbi_bit_slicer * d, uint8_t * raw, uint8_t * buf)
285 return bit_slicer_tmpl (d, raw, buf, 14, 1);
289 bit_slicer_5551_be (vbi_bit_slicer * d, uint8_t * raw, uint8_t * buf)
291 return bit_slicer_tmpl (d, raw, buf, 15, 1);
295 bit_slicer_565_be (vbi_bit_slicer * d, uint8_t * raw, uint8_t * buf)
297 return bit_slicer_tmpl (d, raw, buf, 16, 1);
301 * @param slicer Pointer to vbi_bit_slicer object to be initialized.
302 * @param raw_samples Number of samples or pixels in one raw vbi line
303 * later passed to vbi_bit_slice(). This limits the number of
304 * bytes read from the sample buffer.
305 * @param sampling_rate Raw vbi sampling rate in Hz, that is the number of
306 * samples or pixels sampled per second by the hardware.
307 * @param cri_rate The Clock Run In is a NRZ modulated
308 * sequence of '0' and '1' bits prepending most data transmissions to
309 * synchronize data acquisition circuits. This parameter gives the CRI bit
310 * rate in Hz, that is the number of CRI bits transmitted per second.
311 * @param bit_rate The transmission bit rate of all data bits following the CRI
313 * @param cri_frc The FRaming Code usually following the CRI is a bit sequence
314 * identifying the data service, and per libzvbi definition modulated
315 * and transmitted at the same bit rate as the payload (however nothing
316 * stops you from counting all nominal CRI and FRC bits as CRI).
317 * The bit slicer compares the bits in this word, lsb last transmitted,
318 * against the transmitted CRI and FRC. Decoding of payload starts
319 * with the next bit after a match.
320 * @param cri_mask Of the CRI bits in @c cri_frc, only these bits are
321 * actually significant for a match. For instance it is wise
322 * not to rely on the very first CRI bits transmitted. Note this
323 * mask is not shifted left by @a frc_bits.
325 * @param frc_bits Number of CRI and FRC bits in @a cri_frc, respectively.
326 * Their sum is limited to 32.
327 * @param payload Number of payload <em>bits</em>. Only this data
328 * will be stored in the vbi_bit_slice() output. If this number
329 * is no multiple of eight, the most significant bits of the
330 * last byte are undefined.
331 * @param modulation Modulation of the vbi data, see vbi_modulation.
332 * @param fmt Format of the raw data, see vbi_pixfmt.
334 * Initializes vbi_bit_slicer object. Usually you will not use this
335 * function but vbi_raw_decode(), the vbi image decoder which handles
339 vbi_bit_slicer_init (vbi_bit_slicer * slicer,
340 int raw_samples, int sampling_rate,
341 int cri_rate, int bit_rate,
342 unsigned int cri_frc, unsigned int cri_mask,
343 int cri_bits, int frc_bits, int payload,
344 vbi_modulation modulation, vbi_pixfmt fmt)
346 unsigned int c_mask = (unsigned int) (-(cri_bits > 0)) >> (32 - cri_bits);
347 unsigned int f_mask = (unsigned int) (-(frc_bits > 0)) >> (32 - frc_bits);
350 slicer->func = bit_slicer_1;
353 case VBI_PIXFMT_RGB24:
354 case VBI_PIXFMT_BGR24:
355 slicer->func = bit_slicer_3;
359 case VBI_PIXFMT_RGBA32_LE:
360 case VBI_PIXFMT_BGRA32_LE:
361 slicer->func = bit_slicer_4;
365 case VBI_PIXFMT_RGBA32_BE:
366 case VBI_PIXFMT_BGRA32_BE:
367 slicer->func = bit_slicer_4;
371 case VBI_PIXFMT_RGB16_LE:
372 case VBI_PIXFMT_BGR16_LE:
373 slicer->func = bit_slicer_565_le;
374 gsh = 3; /* (green << 3) & 0x07E0 */
378 case VBI_PIXFMT_RGBA15_LE:
379 case VBI_PIXFMT_BGRA15_LE:
380 slicer->func = bit_slicer_5551_le;
381 gsh = 2; /* (green << 2) & 0x03E0 */
385 case VBI_PIXFMT_ARGB15_LE:
386 case VBI_PIXFMT_ABGR15_LE:
387 slicer->func = bit_slicer_1555_le;
388 gsh = 3; /* (green << 2) & 0x07C0 */
392 case VBI_PIXFMT_RGB16_BE:
393 case VBI_PIXFMT_BGR16_BE:
394 slicer->func = bit_slicer_565_be;
395 gsh = 3; /* (green << 3) & 0x07E0 */
399 case VBI_PIXFMT_RGBA15_BE:
400 case VBI_PIXFMT_BGRA15_BE:
401 slicer->func = bit_slicer_5551_be;
402 gsh = 2; /* (green << 2) & 0x03E0 */
406 case VBI_PIXFMT_ARGB15_BE:
407 case VBI_PIXFMT_ABGR15_BE:
408 slicer->func = bit_slicer_1555_be;
409 gsh = 3; /* (green << 2) & 0x07C0 */
413 case VBI_PIXFMT_YUV420:
414 slicer->func = bit_slicer_1;
418 case VBI_PIXFMT_YUYV:
419 case VBI_PIXFMT_YVYU:
420 slicer->func = bit_slicer_2;
424 case VBI_PIXFMT_UYVY:
425 case VBI_PIXFMT_VYUY:
426 slicer->func = bit_slicer_2;
431 fprintf (stderr, "vbi_bit_slicer_init: unknown pixfmt %d\n", fmt);
435 slicer->cri_mask = cri_mask & c_mask;
436 slicer->cri = (cri_frc >> frc_bits) & slicer->cri_mask;
437 /* We stop searching for CRI/FRC when the payload
438 cannot possibly fit anymore. */
439 slicer->cri_bytes = raw_samples
440 - ((long long) sampling_rate * (payload + frc_bits)) / bit_rate;
441 slicer->cri_rate = cri_rate;
442 /* Raw vbi data is oversampled to account for low sampling rates. */
443 slicer->oversampling_rate = sampling_rate * OVERSAMPLING;
445 slicer->thresh = 105 << (THRESH_FRAC + gsh);
446 slicer->frc = cri_frc & f_mask;
447 slicer->frc_bits = frc_bits;
448 /* Payload bit distance in 1/256 raw samples. */
449 slicer->step = (int) (sampling_rate * 256.0 / bit_rate);
452 slicer->payload = payload;
455 slicer->payload = payload >> 3;
459 switch (modulation) {
460 case VBI_MODULATION_NRZ_MSB:
462 case VBI_MODULATION_NRZ_LSB:
463 slicer->phase_shift = (int)
464 (sampling_rate * 256.0 / cri_rate * .5
465 + sampling_rate * 256.0 / bit_rate * .5 + 128);
468 case VBI_MODULATION_BIPHASE_MSB:
470 case VBI_MODULATION_BIPHASE_LSB:
471 /* Phase shift between the NRZ modulated CRI and the rest */
472 slicer->phase_shift = (int)
473 (sampling_rate * 256.0 / cri_rate * .5
474 + sampling_rate * 256.0 / bit_rate * .25 + 128);
482 * @example examples/wss.c
483 * WSS capture example.
487 * @param rd Initialized vbi_raw_decoder structure.
488 * @param raw A raw vbi image as defined in the vbi_raw_decoder structure
489 * (rd->sampling_format, rd->bytes_per_line, rd->count[0] + rd->count[1]
491 * @param out Buffer to store the decoded vbi_sliced data. Since every
492 * vbi scan line may contain data, this must be an array of vbi_sliced
493 * with the same number of entries as scan lines in the raw image
494 * (rd->count[0] + rd->count[1]).
496 * Decode a raw vbi image, consisting of several scan lines of raw vbi data,
497 * into sliced vbi data. The output is sorted by line number.
499 * Note this function attempts to learn which lines carry which data
500 * service, or none, to speed up decoding. You should avoid using the same
501 * vbi_raw_decoder structure for different sources.
504 * The number of lines decoded, i. e. the number of vbi_sliced records
508 vbi_raw_decode (vbi_raw_decoder * rd, uint8_t * raw, vbi_sliced * out)
510 vbi3_raw_decoder *rd3;
511 unsigned int n_lines;
514 assert (NULL != raw);
515 assert (NULL != out);
517 rd3 = (vbi3_raw_decoder *) rd->pattern;
518 n_lines = rd->count[0] + rd->count[1];
520 g_mutex_lock (&rd->mutex);
523 n_lines = vbi3_raw_decoder_decode (rd3, out, n_lines, raw);
526 g_mutex_unlock (&rd->mutex);
532 * @param rd Initialized vbi_raw_decoder structure.
533 * @param start Array of start line indices for both fields
534 * @param count Array of line counts for both fields
536 * Grows or shrinks the internal state arrays for VBI geometry changes
539 vbi_raw_decoder_resize (vbi_raw_decoder * rd, int *start, unsigned int *count)
541 #if 0 /* Set but unused */
542 vbi_service_set service_set;
544 vbi3_raw_decoder *rd3;
547 assert (NULL != start);
548 assert (NULL != count);
550 rd3 = (vbi3_raw_decoder *) rd->pattern;
552 g_mutex_lock (&rd->mutex);
555 if ((rd->start[0] == start[0])
556 && (rd->start[1] == start[1])
557 && (rd->count[0] == (int) count[0])
558 && (rd->count[1] == (int) count[1])) {
559 g_mutex_unlock (&rd->mutex);
563 rd->start[0] = start[0];
564 rd->start[1] = start[1];
565 rd->count[0] = count[0];
566 rd->count[1] = count[1];
568 #if 0 /* Set but unused */
569 service_set = vbi3_raw_decoder_set_sampling_par
570 (rd3, (vbi_sampling_par *) rd, /* strict */ 0);
572 vbi3_raw_decoder_set_sampling_par
573 (rd3, (vbi_sampling_par *) rd, /* strict */ 0);
577 g_mutex_unlock (&rd->mutex);
581 * @param rd Initialized vbi_raw_decoder structure.
582 * @param services Set of @ref VBI_SLICED_ symbols.
584 * Removes one or more data services to be decoded from the
585 * vbi_raw_decoder structure. This function can be called at any
586 * time and does not touch sampling parameters.
589 * Set of @ref VBI_SLICED_ symbols describing the remaining data
590 * services that will be decoded.
593 vbi_raw_decoder_remove_services (vbi_raw_decoder * rd, unsigned int services)
595 vbi_service_set service_set;
596 vbi3_raw_decoder *rd3;
600 rd3 = (vbi3_raw_decoder *) rd->pattern;
601 service_set = services;
603 g_mutex_lock (&rd->mutex);
606 service_set = vbi3_raw_decoder_remove_services (rd3, service_set);
609 g_mutex_unlock (&rd->mutex);
615 * @param rd Initialized vbi_raw_decoder structure.
616 * @param services Set of @ref VBI_SLICED_ symbols.
617 * @param strict See description of vbi_raw_decoder_add_services()
619 * Check which of the given services can be decoded with current capture
620 * parameters at a given strictness level.
623 * Subset of services actually decodable.
626 vbi_raw_decoder_check_services (vbi_raw_decoder * rd,
627 unsigned int services, int strict)
629 vbi_service_set service_set;
633 service_set = services;
635 g_mutex_lock (&rd->mutex);
638 service_set = vbi_sampling_par_check_services
639 ((vbi_sampling_par *) rd, service_set, strict);
642 g_mutex_unlock (&rd->mutex);
644 return (unsigned int) service_set;
648 * @param rd Initialized vbi_raw_decoder structure.
649 * @param services Set of @ref VBI_SLICED_ symbols.
650 * @param strict A value of 0, 1 or 2 requests loose, reliable or strict
651 * matching of sampling parameters. For example if the data service
652 * requires knowledge of line numbers while they are not known, @c 0
653 * will accept the service (which may work if the scan lines are
654 * populated in a non-confusing way) but @c 1 or @c 2 will not. If the
655 * data service <i>may</i> use more lines than are sampled, @c 1 will
656 * accept but @c 2 will not. If unsure, set to @c 1.
658 * After you initialized the sampling parameters in @a rd (according to
659 * the abilities of your raw vbi source), this function adds one or more
660 * data services to be decoded. The libzvbi raw vbi decoder can decode up
661 * to eight data services in parallel. You can call this function while
662 * already decoding, it does not change sampling parameters and you must
663 * not change them either after calling this.
666 * Set of @ref VBI_SLICED_ symbols describing the data services that actually
667 * will be decoded. This excludes those services not decodable given
668 * the sampling parameters in @a rd.
671 vbi_raw_decoder_add_services (vbi_raw_decoder * rd,
672 unsigned int services, int strict)
674 vbi_service_set service_set;
675 vbi3_raw_decoder *rd3;
679 rd3 = (vbi3_raw_decoder *) rd->pattern;
680 service_set = services;
682 g_mutex_lock (&rd->mutex);
685 vbi3_raw_decoder_set_sampling_par (rd3, (vbi_sampling_par *) rd, strict);
687 service_set = vbi3_raw_decoder_add_services (rd3, service_set, strict);
690 g_mutex_unlock (&rd->mutex);
696 * @param rd Initialized vbi_raw_decoder structure.
697 * @param services Set of VBI_SLICED_ symbols. Here (and only here) you
698 * can add @c VBI_SLICED_VBI_625 or @c VBI_SLICED_VBI_525 to include all
699 * vbi scan lines in the calculated sampling parameters.
700 * @param scanning When 525 accept only NTSC services, when 625
701 * only PAL/SECAM services. When scanning is 0, determine the scanning
702 * from the requested services, an ambiguous set will pick
703 * a 525 or 625 line system at random.
704 * @param max_rate If given, the highest data bit rate in Hz of all
705 * services requested is stored here. (The sampling rate
706 * should be at least twice as high; rd->sampling_rate will
707 * be set to a more reasonable value of 27 MHz derived
708 * from ITU-R Rec. 601.)
710 * Calculate the sampling parameters in @a rd required to receive and
711 * decode the requested data @a services. rd->sampling_format will be
712 * @c VBI_PIXFMT_YUV420, rd->bytes_per_line set accordingly to a
713 * reasonable minimum. This function can be used to initialize hardware
714 * prior to calling vbi_raw_decoder_add_service().
717 * Set of @ref VBI_SLICED_ symbols describing the data services covered
718 * by the calculated sampling parameters. This excludes services the libzvbi
719 * raw decoder cannot decode.
722 vbi_raw_decoder_parameters (vbi_raw_decoder * rd,
723 unsigned int services, int scanning, int *max_rate)
725 vbi_videostd_set videostd_set;
726 vbi_service_set service_set;
730 videostd_set = VBI_VIDEOSTD_SET_525_60;
734 videostd_set = VBI_VIDEOSTD_SET_625_50;
742 service_set = services;
744 g_mutex_lock (&rd->mutex);
747 service_set = vbi_sampling_par_from_services
748 ((vbi_sampling_par *) rd,
749 (unsigned int *) max_rate, videostd_set, service_set);
752 g_mutex_unlock (&rd->mutex);
754 return (unsigned int) service_set;
758 * @param rd Initialized vbi_raw_decoder structure.
760 * Reset a vbi_raw_decoder structure. This removes
761 * all previously added services to be decoded (if any)
762 * but does not touch the sampling parameters. You are
763 * free to change the sampling parameters after calling this.
766 vbi_raw_decoder_reset (vbi_raw_decoder * rd)
768 vbi3_raw_decoder *rd3;
771 return; /* compatibility */
775 rd3 = (vbi3_raw_decoder *) rd->pattern;
777 g_mutex_lock (&rd->mutex);
780 vbi3_raw_decoder_reset (rd3);
783 g_mutex_unlock (&rd->mutex);
787 * @param rd Pointer to initialized vbi_raw_decoder
788 * structure, can be @c NULL.
790 * Free all resources associated with @a rd.
793 vbi_raw_decoder_destroy (vbi_raw_decoder * rd)
795 vbi3_raw_decoder *rd3;
799 rd3 = (vbi3_raw_decoder *) rd->pattern;
801 vbi3_raw_decoder_delete (rd3);
803 g_mutex_clear (&rd->mutex);
809 * @param rd Pointer to a vbi_raw_decoder structure.
811 * Initializes a vbi_raw_decoder structure.
814 vbi_raw_decoder_init (vbi_raw_decoder * rd)
816 vbi3_raw_decoder *rd3;
822 g_mutex_init (&rd->mutex);
824 rd3 = vbi3_raw_decoder_new ( /* sampling_par */ NULL);
825 assert (NULL != rd3);
827 rd->pattern = (int8_t *) rd3;
830 GST_DEBUG_CATEGORY (libzvbi_debug);
832 vbi_initialize_gst_debug (void)
834 GST_DEBUG_CATEGORY_INIT (libzvbi_debug, "libzvbi", 0, "libzvbi");