2 * libzvbi -- VBI device simulation
4 * Copyright (C) 2004, 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: io-sim.c,v 1.18 2009-12-14 23:43:40 mschimek Exp $ */
29 #define _USE_MATH_DEFINES /* Needed for M_PI and M_LN2 */
31 #include <math.h> /* sin(), log() */
33 #include <ctype.h> /* isspace() */
34 #include <limits.h> /* INT_MAX */
38 #include "sampling_par.h"
39 #include "raw_decoder.h"
42 # define sp_sample_format sampling_format
43 # define SAMPLES_PER_LINE(sp) \
44 ((sp)->bytes_per_line / VBI_PIXFMT_BPP ((sp)->sampling_format))
45 # define SYSTEM_525(sp) \
46 (525 == (sp)->scanning)
51 * @addtogroup Rawenc Raw VBI encoder
53 * @brief Converting sliced VBI data to raw VBI images.
55 * These are functions converting sliced VBI data to raw VBI images as
56 * transmitted in the vertical blanking interval of analog video standards.
57 * They are mainly intended for tests of the libzvbi bit slicer and
61 # define VBI_PIXFMT_RGB24_LE VBI_PIXFMT_RGB24
62 # define VBI_PIXFMT_BGR24_LE VBI_PIXFMT_BGR24
63 # define VBI_PIXFMT_RGBA24_LE VBI_PIXFMT_RGBA32_LE
64 # define VBI_PIXFMT_BGRA24_LE VBI_PIXFMT_BGRA32_LE
65 # define VBI_PIXFMT_RGBA24_BE VBI_PIXFMT_RGBA32_BE
66 # define VBI_PIXFMT_BGRA24_BE VBI_PIXFMT_BGRA32_BE
67 # define vbi_pixfmt_bytes_per_pixel VBI_PIXFMT_BPP
69 #define PI 3.1415926535897932384626433832795029
71 #define PULSE(zero_level) \
74 raw[i] = SATURATE (zero_level, 0, 255); \
75 } else if (3 == seq) { \
76 raw[i] = SATURATE (zero_level + (int) signal_amp, \
78 } else if ((seq ^ bit) & 1) { /* down */ \
79 double r = sin (q * tr - (PI / 2.0)); \
80 r = r * r * signal_amp; \
81 raw[i] = SATURATE (zero_level + (int) r, 0, 255); \
83 double r = sin (q * tr); \
84 r = r * r * signal_amp; \
85 raw[i] = SATURATE (zero_level + (int) r, 0, 255); \
89 #define PULSE_SEQ(zero_level) \
97 bit = tr * bit_rate; \
100 seq = (buf[byte] >> 7) + buf[byte + 1] * 2; \
101 seq = (seq >> bit) & 3; \
102 PULSE (zero_level); \
106 vbi_sincos (double x, double *sinx, double *cosx)
112 #define vbi_log2(x) (log (x) / M_LN2)
115 signal_teletext (uint8_t * raw,
116 const vbi_sampling_par * sp,
120 unsigned int frc, unsigned int payload, const vbi_sliced * sliced)
122 double bit_period = 1.0 / bit_rate;
123 /* Teletext System B: Sixth CRI pulse at 12 us
124 (+.5 b/c we start with a 0 bit). */
125 double t1 = 12e-6 - 13 * bit_period;
126 double t2 = t1 + (payload * 8 + 24 + 1) * bit_period;
127 double q = (PI / 2.0) * bit_rate;
128 double sample_period = 1.0 / sp->sampling_rate;
129 unsigned int samples_per_line;
135 buf[1] = 0x55; /* clock run-in */
139 memcpy (buf + 4, sliced->data, payload);
141 buf[payload + 4] = 0x00;
143 t = sp->offset / (double) sp->sampling_rate;
145 samples_per_line = SAMPLES_PER_LINE (sp);
147 for (i = 0; i < samples_per_line; ++i) {
148 if (t >= t1 && t < t2)
149 PULSE_SEQ (black_level);
156 signal_vps (uint8_t * raw,
157 const vbi_sampling_par * sp,
158 int black_level, int white_level, const vbi_sliced * sliced)
160 static const uint8_t biphase[] = {
161 0xAA, 0x6A, 0x9A, 0x5A,
162 0xA6, 0x66, 0x96, 0x56,
163 0xA9, 0x69, 0x99, 0x59,
164 0xA5, 0x65, 0x95, 0x55
166 double bit_rate = 15625 * 160 * 2;
167 double t1 = 12.5e-6 - .5 / bit_rate;
168 double t4 = t1 + ((4 + 13 * 2) * 8) / bit_rate;
169 double q = (PI / 2.0) * bit_rate;
170 double sample_period = 1.0 / sp->sampling_rate;
171 unsigned int samples_per_line;
172 double signal_amp = (0.5 / 0.7) * (white_level - black_level);
179 buf[1] = 0x55; /* 0101 0101 */
180 buf[2] = 0x55; /* 0101 0101 */
181 buf[3] = 0x51; /* 0101 0001 */
182 buf[4] = 0x99; /* 1001 1001 */
184 for (i = 0; i < 13; ++i) {
185 unsigned int b = sliced->data[i];
187 buf[5 + i * 2] = biphase[b >> 4];
188 buf[6 + i * 2] = biphase[b & 15];
191 buf[6 + 12 * 2] &= 0x7F;
193 t = sp->offset / (double) sp->sampling_rate;
195 samples_per_line = SAMPLES_PER_LINE (sp);
197 for (i = 0; i < samples_per_line; ++i) {
198 if (t >= t1 && t < t4)
199 PULSE_SEQ (black_level);
206 wss_biphase (uint8_t buf[32], const vbi_sliced * sliced)
212 /* 29 bit run-in and 24 bit start code, lsb first. */
215 buf[1] = 0x1F; /* 0001 1111 */
216 buf[2] = 0xC7; /* 1100 0111 */
217 buf[3] = 0x71; /* 0111 0001 */
218 buf[4] = 0x1C; /* 000 | 1 1100 */
219 buf[5] = 0x8F; /* 1000 1111 */
220 buf[6] = 0x07; /* 0000 0111 */
221 buf[7] = 0x1F; /* 1 1111 */
224 data = sliced->data[0] + sliced->data[1] * 256;
226 for (i = 0; i < 14; ++i) {
227 static const unsigned int biphase[] = { 0x38, 0x07 };
236 seq = biphase[data & 1] << shift;
242 buf[byte + 1] = seq >> 8;
247 signal_wss_625 (uint8_t * raw,
248 const vbi_sampling_par * sp,
249 int black_level, int white_level, const vbi_sliced * sliced)
251 double bit_rate = 15625 * 320;
252 double t1 = 11.0e-6 - .5 / bit_rate;
253 double t4 = t1 + (29 + 24 + 14 * 6 + 1) / bit_rate;
254 double q = (PI / 2.0) * bit_rate;
255 double sample_period = 1.0 / sp->sampling_rate;
256 double signal_amp = (0.5 / 0.7) * (white_level - black_level);
257 unsigned int samples_per_line;
264 wss_biphase (buf, sliced);
266 t = sp->offset / (double) sp->sampling_rate;
268 samples_per_line = SAMPLES_PER_LINE (sp);
270 for (i = 0; i < samples_per_line; ++i) {
271 if (t >= t1 && t < t4)
272 PULSE_SEQ (black_level);
279 signal_closed_caption (uint8_t * raw,
280 const vbi_sampling_par * sp,
283 unsigned int flags, double bit_rate, const vbi_sliced * sliced)
285 double D = 1.0 / bit_rate;
286 double t0 = 10.5e-6; /* CRI start half amplitude (EIA 608-B) */
287 double t1 = t0 - .25 * D; /* CRI start, blanking level */
288 double t2 = t1 + 7 * D; /* CRI 7 cycles */
289 /* First start bit, left edge half amplitude, minus rise time. */
290 double t3 = t0 + 6.5 * D - 120e-9;
291 double q1 = PI * bit_rate * 2;
292 /* Max. rise/fall time 240 ns (EIA 608-B). */
293 double q2 = PI / 120e-9;
296 double sample_period = 1.0 / sp->sampling_rate;
297 unsigned int samples_per_line;
302 /* Twice 7 data + odd parity, start bit 0 -> 1 */
304 data = (sliced->data[1] << 12) + (sliced->data[0] << 4) + 8;
306 t = sp->offset / (double) sp->sampling_rate;
308 samples_per_line = SAMPLES_PER_LINE (sp);
310 if (flags & _VBI_RAW_SHIFT_CC_CRI) {
311 /* Wrong signal shape found by Rich Kadel,
312 zapping-misc@lists.sourceforge.net 2006-07-16. */
318 if (flags & _VBI_RAW_LOW_AMP_CC) {
319 /* Low amplitude signal found by Rich Kadel,
320 zapping-misc@lists.sourceforge.net 2007-08-15. */
321 white_level = white_level * 6 / 10;
324 signal_mean = (white_level - blank_level) * .25; /* 25 IRE */
325 signal_high = blank_level + (white_level - blank_level) * .5;
327 for (i = 0; i < samples_per_line; ++i) {
328 if (t >= t1 && t < t2) {
329 raw[i] = SATURATE (blank_level + (1.0 - cos (q1 * (t - t1)))
330 * signal_mean, 0, 255);
338 seq = (data >> bit) & 3;
341 if ((1 == seq || 2 == seq)
342 && fabs (d) < .120e-6) {
346 level = blank_level + (1.0 + cos (q2 * d))
349 level = blank_level + (1.0 - cos (q2 * d))
351 raw[i] = SATURATE (level, 0, 255);
352 } else if (data & (2 << bit)) {
353 raw[i] = SATURATE (signal_high, 0, 255);
355 raw[i] = SATURATE (blank_level, 0, 255);
364 clear_image (uint8_t * p,
366 unsigned int width, unsigned int height, unsigned int bytes_per_line)
368 if (width == bytes_per_line) {
369 memset (p, value, height * bytes_per_line);
371 while (height-- > 0) {
372 memset (p, value, width);
379 * @param raw Noise will be added to this raw VBI image.
380 * @param sp Describes the raw VBI data in the buffer. @a sp->sampling_format
381 * must be @c VBI_PIXFMT_Y8 (@c VBI_PIXFMT_YUV420 in libzvbi 0.2.x).
382 * Note for compatibility in libzvbi 0.2.x vbi_sampling_par is a
383 * synonym of vbi_raw_decoder, but the (private) decoder fields in
384 * this structure are ignored.
385 * @param min_freq Minimum frequency of the noise in Hz.
386 * @param max_freq Maximum frequency of the noise in Hz. @a min_freq and
387 * @a max_freq define the cut off frequency at the half power points
389 * @param amplitude Maximum amplitude of the noise, should lie in range
391 * @param seed Seed for the pseudo random number generator built into
392 * this function. Given the same @a seed value the function will add
393 * the same noise, which can be useful for tests.
395 * This function adds white noise to a raw VBI image.
397 * To produce realistic noise @a min_freq = 0, @a max_freq = 5e6 and
398 * @a amplitude = 20 to 50 seems appropriate.
401 * FALSE if the @a sp sampling parameters are invalid.
406 vbi_raw_add_noise (uint8_t * raw,
407 const vbi_sampling_par * sp,
408 unsigned int min_freq,
409 unsigned int max_freq, unsigned int amplitude, unsigned int seed)
411 double f0, w0, sn, cs, bw, alpha, a0;
412 float a1, a2, b0, b1, z0, z1, z2;
413 unsigned int n_lines;
414 unsigned long samples_per_line;
415 unsigned long padding;
418 assert (NULL != raw);
421 if (unlikely (!_vbi_sampling_par_valid_log (sp, /* log */ NULL)))
424 switch (sp->sp_sample_format) {
425 case VBI_PIXFMT_YUV420:
432 if (unlikely (sp->sampling_rate <= 0))
435 /* Biquad bandpass filter.
436 http://www.musicdsp.org/files/Audio-EQ-Cookbook.txt */
438 f0 = ((double) min_freq + max_freq) * 0.5;
443 w0 = 2 * M_PI * f0 / sp->sampling_rate;
444 vbi_sincos (w0, &sn, &cs);
445 bw = fabs (vbi_log2 (MAX (min_freq, max_freq) / f0));
446 alpha = sn * sinh (log (2) / 2 * bw * w0 / sn);
449 a2 = (alpha - 1) / a0;
456 n_lines = sp->count[0] + sp->count[1];
458 if (unlikely (0 == amplitude || 0 == n_lines || 0 == sp->bytes_per_line))
461 samples_per_line = sp->bytes_per_line;
470 uint8_t *raw_end = raw + samples_per_line;
475 /* We use our own simple PRNG to produce
476 predictable results for tests. */
477 seed32 = seed32 * 1103515245u + 12345;
478 noise = ((seed32 / 65536) % (amplitude * 2 + 1))
481 z0 = noise + a1 * z1 + a2 * z2;
482 noise = (int) (b0 * (z0 - z2) + b1 * z1);
486 *raw++ = SATURATE (*raw + noise, 0, 255);
487 } while (raw < raw_end);
490 } while (--n_lines > 0);
496 signal_u8 (uint8_t * raw,
497 const vbi_sampling_par * sp,
502 const vbi_sliced * sliced, unsigned int n_sliced_lines, const char *caller)
504 unsigned int n_scan_lines;
505 unsigned int samples_per_line;
507 n_scan_lines = sp->count[0] + sp->count[1];
508 samples_per_line = SAMPLES_PER_LINE (sp);
511 SATURATE (blank_level, 0, 255),
512 samples_per_line, n_scan_lines, sp->bytes_per_line);
514 for (; n_sliced_lines-- > 0; ++sliced) {
518 if (0 == sliced->line) {
520 } else if (0 != sp->start[1]
521 && sliced->line >= (unsigned int) sp->start[1]) {
522 row = sliced->line - sp->start[1];
523 if (row >= (unsigned int) sp->count[1])
526 if (sp->interlaced) {
527 row = row * 2 + !(flags & _VBI_RAW_SWAP_FIELDS);
528 } else if (0 == (flags & _VBI_RAW_SWAP_FIELDS)) {
531 } else if (0 != sp->start[0]
532 && sliced->line >= (unsigned int) sp->start[0]) {
533 row = sliced->line - sp->start[0];
534 if (row >= (unsigned int) sp->count[0])
537 if (sp->interlaced) {
538 row *= 2 + ! !(flags & _VBI_RAW_SWAP_FIELDS);
539 } else if (flags & _VBI_RAW_SWAP_FIELDS) {
544 warning (caller, "Sliced line %u out of bounds.", sliced->line);
548 raw1 = raw + row * sp->bytes_per_line;
550 switch (sliced->id) {
551 case VBI_SLICED_TELETEXT_A: /* ok? */
552 signal_teletext (raw1, sp, black_level,
553 /* amplitude */ .7 * (white_level
555 /* bit_rate */ 25 * 625 * 397,
561 case VBI_SLICED_TELETEXT_B_L10_625:
562 case VBI_SLICED_TELETEXT_B_L25_625:
563 case VBI_SLICED_TELETEXT_B:
564 signal_teletext (raw1, sp, black_level,
565 .66 * (white_level - black_level),
566 25 * 625 * 444, 0x27, 42, sliced);
569 case VBI_SLICED_TELETEXT_C_625:
570 signal_teletext (raw1, sp, black_level,
571 .7 * (white_level - black_level), 25 * 625 * 367, 0xE7, 33, sliced);
574 case VBI_SLICED_TELETEXT_D_625:
575 signal_teletext (raw1, sp, black_level,
576 .7 * (white_level - black_level), 5642787, 0xA7, 34, sliced);
579 case VBI_SLICED_CAPTION_625_F1:
580 case VBI_SLICED_CAPTION_625_F2:
581 case VBI_SLICED_CAPTION_625:
582 signal_closed_caption (raw1, sp,
583 blank_level, white_level, flags, 25 * 625 * 32, sliced);
587 case VBI_SLICED_VPS_F2:
588 signal_vps (raw1, sp, black_level, white_level, sliced);
591 case VBI_SLICED_WSS_625:
592 signal_wss_625 (raw1, sp, black_level, white_level, sliced);
595 case VBI_SLICED_TELETEXT_B_525:
596 signal_teletext (raw1, sp, black_level,
597 /* amplitude */ .7 * (white_level
599 /* bit_rate */ 5727272,
605 case VBI_SLICED_TELETEXT_C_525:
606 signal_teletext (raw1, sp, black_level,
607 .7 * (white_level - black_level), 5727272, 0xE7, 33, sliced);
610 case VBI_SLICED_TELETEXT_D_525:
611 signal_teletext (raw1, sp, black_level,
612 .7 * (white_level - black_level), 5727272, 0xA7, 34, sliced);
615 case VBI_SLICED_CAPTION_525_F1:
616 case VBI_SLICED_CAPTION_525_F2:
617 case VBI_SLICED_CAPTION_525:
618 signal_closed_caption (raw1, sp,
619 blank_level, white_level, flags, 30000 * 525 * 32 / 1001, sliced);
624 "Service 0x%08x (%s) not supported.",
625 sliced->id, vbi_sliced_name (sliced->id));
634 _vbi_raw_vbi_image (uint8_t * raw,
635 unsigned long raw_size,
636 const vbi_sampling_par * sp,
639 unsigned int flags, const vbi_sliced * sliced, unsigned int n_sliced_lines)
641 unsigned int n_scan_lines;
642 unsigned int black_level;
644 if (unlikely (!_vbi_sampling_par_valid_log (sp, NULL)))
647 n_scan_lines = sp->count[0] + sp->count[1];
648 if (unlikely (n_scan_lines * sp->bytes_per_line > raw_size)) {
649 warning (__FUNCTION__,
650 "(%u + %u lines) * %lu bytes_per_line "
652 sp->count[0], sp->count[1],
653 (unsigned long) sp->bytes_per_line, raw_size);
657 if (unlikely (0 != white_level && blank_level > white_level)) {
658 warning (__FUNCTION__,
659 "Invalid blanking %d or peak white level %d.",
660 blank_level, white_level);
663 if (SYSTEM_525 (sp)) {
664 /* Observed value. */
665 const unsigned int peak = 200; /* 255 */
667 if (0 == white_level) {
668 blank_level = (int) (40.0 * peak / 140);
669 black_level = (int) (47.5 * peak / 140);
672 black_level = (int) (blank_level + 7.5 * (white_level - blank_level));
675 const unsigned int peak = 200; /* 255 */
677 if (0 == white_level) {
678 blank_level = (int) (43.0 * peak / 140);
682 black_level = blank_level;
685 return signal_u8 (raw, sp,
686 blank_level, black_level, white_level,
687 flags, sliced, n_sliced_lines, __FUNCTION__);
690 #define RGBA_TO_RGB16(value) \
691 (+(((value) & 0xF8) >> (3 - 0)) \
692 +(((value) & 0xFC00) >> (10 - 5)) \
693 +(((value) & 0xF80000) >> (19 - 11)))
695 #define RGBA_TO_RGBA15(value) \
696 (+(((value) & 0xF8) >> (3 - 0)) \
697 +(((value) & 0xF800) >> (11 - 5)) \
698 +(((value) & 0xF80000) >> (19 - 10)) \
699 +(((value) & 0x80000000) >> (31 - 15)))
701 #define RGBA_TO_ARGB15(value) \
702 (+(((value) & 0xF8) >> (3 - 1)) \
703 +(((value) & 0xF800) >> (11 - 6)) \
704 +(((value) & 0xF80000) >> (19 - 11)) \
705 +(((value) & 0x80000000) >> (31 - 0)))
707 #define RGBA_TO_RGBA12(value) \
708 (+(((value) & 0xF0) >> (4 - 0)) \
709 +(((value) & 0xF000) >> (12 - 4)) \
710 +(((value) & 0xF00000) >> (20 - 8)) \
711 +(((value) & 0xF0000000) >> (28 - 12)))
713 #define RGBA_TO_ARGB12(value) \
714 (+(((value) & 0xF0) << -(4 - 12)) \
715 +(((value) & 0xF000) >> (12 - 8)) \
716 +(((value) & 0xF00000) >> (20 - 4)) \
717 +(((value) & 0xF0000000) >> (28 - 0)))
719 #define RGBA_TO_RGB8(value) \
720 (+(((value) & 0xE0) >> (5 - 0)) \
721 +(((value) & 0xE000) >> (13 - 3)) \
722 +(((value) & 0xC00000) >> (22 - 6)))
724 #define RGBA_TO_BGR8(value) \
725 (+(((value) & 0xE0) >> (5 - 5)) \
726 +(((value) & 0xE000) >> (13 - 2)) \
727 +(((value) & 0xC00000) >> (22 - 0)))
729 #define RGBA_TO_RGBA7(value) \
730 (+(((value) & 0xC0) >> (6 - 0)) \
731 +(((value) & 0xE000) >> (13 - 2)) \
732 +(((value) & 0xC00000) >> (22 - 5)) \
733 +(((value) & 0x80000000) >> (31 - 7)))
735 #define RGBA_TO_ARGB7(value) \
736 (+(((value) & 0xC0) >> (6 - 6)) \
737 +(((value) & 0xE000) >> (13 - 3)) \
738 +(((value) & 0xC00000) >> (22 - 1)) \
739 +(((value) & 0x80000000) >> (31 - 0)))
741 #define MST1(d, val, mask) (d) = ((d) & ~(mask)) | ((val) & (mask))
742 #define MST2(d, val, mask) (d) = ((d) & (mask)) | (val)
744 #define SCAN_LINE_TO_N(conv, n) \
746 for (i = 0; i < samples_per_line; ++i) { \
747 uint8_t *dd = d + i * (n); \
748 unsigned int value = s[i] * 0x01010101; \
749 unsigned int mask = ~pixel_mask; \
751 value = conv (value) & pixel_mask; \
752 MST2 (dd[0], value >> 0, mask >> 0); \
754 MST2 (dd[1], value >> 8, mask >> 8); \
756 MST2 (dd[2], value >> 16, mask >> 16); \
758 MST2 (dd[3], value >> 24, mask >> 24); \
762 #define SCAN_LINE_TO_RGB2(conv, endian) \
764 for (i = 0; i < samples_per_line; ++i) { \
765 uint8_t *dd = d + i * 2; \
766 unsigned int value = s[i] * 0x01010101; \
769 value = conv (value) & pixel_mask; \
770 mask = ~pixel_mask; \
771 MST2 (dd[0 + endian], value >> 0, mask >> 0); \
772 MST2 (dd[1 - endian], value >> 8, mask >> 8); \
777 _vbi_raw_video_image (uint8_t * raw,
778 unsigned long raw_size,
779 const vbi_sampling_par * sp,
783 unsigned int pixel_mask,
784 unsigned int flags, const vbi_sliced * sliced, unsigned int n_sliced_lines)
786 unsigned int n_scan_lines;
787 unsigned int samples_per_line;
788 vbi_sampling_par sp8;
794 if (unlikely (!_vbi_sampling_par_valid_log (sp, NULL)))
797 n_scan_lines = sp->count[0] + sp->count[1];
798 if (unlikely (n_scan_lines * sp->bytes_per_line > raw_size)) {
799 warning (__FUNCTION__,
800 "%u + %u lines * %lu bytes_per_line > %lu raw_size.",
801 sp->count[0], sp->count[1],
802 (unsigned long) sp->bytes_per_line, raw_size);
806 if (unlikely (0 != white_level
807 && (blank_level > black_level || black_level > white_level))) {
808 warning (__FUNCTION__,
809 "Invalid blanking %d, black %d or peak "
810 "white level %d.", blank_level, black_level, white_level);
813 switch (sp->sp_sample_format) {
814 case VBI_PIXFMT_YVYU:
815 case VBI_PIXFMT_VYUY: /* 0xAAUUVVYY */
816 pixel_mask = (+((pixel_mask & 0xFF00) << 8)
817 + ((pixel_mask & 0xFF0000) >> 8)
818 + ((pixel_mask & 0xFF0000FF)));
821 case VBI_PIXFMT_RGBA24_BE: /* 0xRRGGBBAA */
822 pixel_mask = SWAB32 (pixel_mask);
825 case VBI_PIXFMT_BGR24_LE: /* 0x00RRGGBB */
826 case VBI_PIXFMT_BGRA15_LE:
827 case VBI_PIXFMT_BGRA15_BE:
828 case VBI_PIXFMT_ABGR15_LE:
829 case VBI_PIXFMT_ABGR15_BE:
830 pixel_mask = (+((pixel_mask & 0xFF) << 16)
831 + ((pixel_mask & 0xFF0000) >> 16)
832 + ((pixel_mask & 0xFF00FF00)));
835 case VBI_PIXFMT_BGRA24_BE: /* 0xBBGGRRAA */
836 pixel_mask = (+((pixel_mask & 0xFFFFFF) << 8)
837 + ((pixel_mask & 0xFF000000) >> 24));
844 switch (sp->sp_sample_format) {
845 case VBI_PIXFMT_RGB16_LE:
846 case VBI_PIXFMT_RGB16_BE:
847 case VBI_PIXFMT_BGR16_LE:
848 case VBI_PIXFMT_BGR16_BE:
849 pixel_mask = RGBA_TO_RGB16 (pixel_mask);
852 case VBI_PIXFMT_RGBA15_LE:
853 case VBI_PIXFMT_RGBA15_BE:
854 case VBI_PIXFMT_BGRA15_LE:
855 case VBI_PIXFMT_BGRA15_BE:
856 pixel_mask = RGBA_TO_RGBA15 (pixel_mask);
859 case VBI_PIXFMT_ARGB15_LE:
860 case VBI_PIXFMT_ARGB15_BE:
861 case VBI_PIXFMT_ABGR15_LE:
862 case VBI_PIXFMT_ABGR15_BE:
863 pixel_mask = RGBA_TO_ARGB15 (pixel_mask);
870 if (0 == pixel_mask) {
875 /* ITU-R BT.601 sampling assumed. */
877 if (SYSTEM_525 (sp)) {
878 if (0 == white_level) {
879 /* Cutting off the bottom of the signal
880 confuses the vbi_bit_slicer (can't adjust
881 the threshold fast enough), probably other
883 blank_level = 5; /* 16 - 40 * 220 / 100; */
885 white_level = 16 + 219;
888 if (0 == white_level) {
889 /* Observed values: 30-30-280 (WSS PAL) -? */
890 blank_level = 5; /* 16 - 43 * 220 / 100; */
892 white_level = 16 + 219;
898 samples_per_line = SAMPLES_PER_LINE (sp);
900 sp8.sampling_format = VBI_PIXFMT_YUV420;
902 sp8.bytes_per_line = samples_per_line * 1 /* bpp */ ;
904 size = n_scan_lines * samples_per_line;
905 buf = vbi_malloc (size);
907 error (NULL, "Out of memory.");
912 if (!signal_u8 (buf, &sp8,
913 blank_level, black_level, white_level,
914 flags, sliced, n_sliced_lines, __FUNCTION__)) {
922 while (n_scan_lines-- > 0) {
925 switch (sp->sp_sample_format) {
926 case VBI_PIXFMT_PAL8:
927 case VBI_PIXFMT_YUV420:
928 for (i = 0; i < samples_per_line; ++i)
929 MST1 (d[i], s[i], pixel_mask);
932 case VBI_PIXFMT_RGBA24_LE:
933 case VBI_PIXFMT_RGBA24_BE:
934 case VBI_PIXFMT_BGRA24_LE:
935 case VBI_PIXFMT_BGRA24_BE:
936 SCAN_LINE_TO_N (+, 4);
939 case VBI_PIXFMT_RGB24_LE:
940 case VBI_PIXFMT_BGR24_LE:
941 SCAN_LINE_TO_N (+, 3);
944 case VBI_PIXFMT_YUYV:
945 case VBI_PIXFMT_YVYU:
946 for (i = 0; i < samples_per_line; i += 2) {
947 uint8_t *dd = d + i * 2;
948 unsigned int uv = (s[i] + s[i + 1] + 1) >> 1;
950 MST1 (dd[0], s[i], pixel_mask);
951 MST1 (dd[1], uv, pixel_mask >> 8);
952 MST1 (dd[2], s[i + 1], pixel_mask);
953 MST1 (dd[3], uv, pixel_mask >> 16);
957 case VBI_PIXFMT_UYVY:
958 case VBI_PIXFMT_VYUY:
959 for (i = 0; i < samples_per_line; i += 2) {
960 uint8_t *dd = d + i * 2;
961 unsigned int uv = (s[i] + s[i + 1] + 1) >> 1;
963 MST1 (dd[0], uv, pixel_mask >> 8);
964 MST1 (dd[1], s[i], pixel_mask);
965 MST1 (dd[2], uv, pixel_mask >> 16);
966 MST1 (dd[3], s[i + 1], pixel_mask);
970 case VBI_PIXFMT_RGB16_LE:
971 case VBI_PIXFMT_BGR16_LE:
972 SCAN_LINE_TO_RGB2 (RGBA_TO_RGB16, 0);
975 case VBI_PIXFMT_RGB16_BE:
976 case VBI_PIXFMT_BGR16_BE:
977 SCAN_LINE_TO_RGB2 (RGBA_TO_RGB16, 1);
980 case VBI_PIXFMT_RGBA15_LE:
981 case VBI_PIXFMT_BGRA15_LE:
982 SCAN_LINE_TO_RGB2 (RGBA_TO_RGBA15, 0);
985 case VBI_PIXFMT_RGBA15_BE:
986 case VBI_PIXFMT_BGRA15_BE:
987 SCAN_LINE_TO_RGB2 (RGBA_TO_RGBA15, 1);
990 case VBI_PIXFMT_ARGB15_LE:
991 case VBI_PIXFMT_ABGR15_LE:
992 SCAN_LINE_TO_RGB2 (RGBA_TO_ARGB15, 0);
995 case VBI_PIXFMT_ARGB15_BE:
996 case VBI_PIXFMT_ABGR15_BE:
997 SCAN_LINE_TO_RGB2 (RGBA_TO_ARGB15, 1);
1002 s += sp8.bytes_per_line;
1003 d += sp->bytes_per_line;
1012 * @example examples/rawout.c
1013 * Raw VBI output example.
1017 * @param raw A raw VBI image will be stored here.
1018 * @param raw_size Size of the @a raw buffer in bytes. The buffer
1019 * must be large enough for @a sp->count[0] + count[1] lines
1020 * of @a sp->bytes_per_line each, with @a sp->samples_per_line
1021 * (in libzvbi 0.2.x @a sp->bytes_per_line) bytes actually written.
1022 * @param sp Describes the raw VBI data to generate. @a sp->sampling_format
1023 * must be @c VBI_PIXFMT_Y8 (@c VBI_PIXFMT_YUV420 with libzvbi 0.2.x).
1024 * @a sp->synchronous is ignored. Note for compatibility in libzvbi
1025 * 0.2.x vbi_sampling_par is a synonym of vbi_raw_decoder, but the
1026 * (private) decoder fields in this structure are ignored.
1027 * @param blank_level The level of the horizontal blanking in the raw
1028 * VBI image. Must be <= @a white_level.
1029 * @param white_level The peak white level in the raw VBI image. Set to
1030 * zero to get the default blanking and white level.
1031 * @param swap_fields If @c TRUE the second field will be stored first
1032 * in the @c raw buffer. Note you can also get an interlaced image
1033 * by setting @a sp->interlaced to @c TRUE. @a sp->synchronous is
1035 * @param sliced Pointer to an array of vbi_sliced containing the
1036 * VBI data to be encoded.
1037 * @param n_sliced_lines Number of elements in the @a sliced array.
1039 * This function basically reverses the operation of the vbi_raw_decoder,
1040 * taking sliced VBI data and generating a raw VBI image similar to those
1041 * you would get from raw VBI sampling hardware. The following data services
1042 * are currently supported: All Teletext services, VPS, WSS 625, Closed
1043 * Caption 525 and 625.
1045 * The function encodes sliced data as is, e.g. without adding or
1046 * checking parity bits, without checking if the line number is correct
1047 * for the respective data service, or if the signal will fit completely
1048 * in the given space (@a sp->offset and @a sp->samples_per_line at
1049 * @a sp->sampling_rate).
1051 * Apart of the payload the generated video signal is invariable and
1052 * attempts to be faithful to related standards. You can only change the
1053 * characteristics of the assumed capture device. Sync pulses and color
1054 * bursts and not generated if the sampling parameters extend to this area.
1057 * This function is mainly intended for testing purposes. It is optimized
1058 * for accuracy, not for speed.
1061 * @c FALSE if the @a raw_size is too small, if the @a sp sampling
1062 * parameters are invalid, if the signal levels are invalid,
1063 * if the @a sliced array contains unsupported services or line numbers
1064 * outside the @a sp sampling parameters.
1069 vbi_raw_vbi_image (uint8_t * raw,
1070 unsigned long raw_size,
1071 const vbi_sampling_par * sp,
1074 vbi_bool swap_fields,
1075 const vbi_sliced * sliced, unsigned int n_sliced_lines)
1077 return _vbi_raw_vbi_image (raw, raw_size, sp,
1078 blank_level, white_level,
1079 swap_fields ? _VBI_RAW_SWAP_FIELDS : 0, sliced, n_sliced_lines);
1083 * @param raw A raw VBI image will be stored here.
1084 * @param raw_size Size of the @a raw buffer in bytes. The buffer
1085 * must be large enough for @a sp->count[0] + count[1] lines
1086 * of @a sp->bytes_per_line each, with @a sp->samples_per_line
1087 * times bytes per pixel (in libzvbi 0.2.x @a sp->bytes_per_line)
1089 * @param sp Describes the raw VBI data to generate. Note for
1090 * compatibility in libzvbi 0.2.x vbi_sampling_par is a synonym of
1091 * vbi_raw_decoder, but the (private) decoder fields in this
1092 * structure are ignored.
1093 * @param blank_level The level of the horizontal blanking in the raw
1094 * VBI image. Must be <= @a black_level.
1095 * @param black_level The black level in the raw VBI image. Must be
1096 * <= @a white_level.
1097 * @param white_level The peak white level in the raw VBI image. Set to
1098 * zero to get the default blanking, black and white level.
1099 * @param pixel_mask This mask selects which color or alpha channel
1100 * shall contain VBI data. Depending on @a sp->sampling_format it is
1101 * interpreted as 0xAABBGGRR or 0xAAVVUUYY. A value of 0x000000FF
1102 * for example writes data in "red bits", not changing other
1103 * bits in the @a raw buffer. When the @a sp->sampling_format is a
1104 * planar YUV the function writes the Y plane only.
1105 * @param swap_fields If @c TRUE the second field will be stored first
1106 * in the @c raw buffer. Note you can also get an interlaced image
1107 * by setting @a sp->interlaced to @c TRUE. @a sp->synchronous is
1109 * @param sliced Pointer to an array of vbi_sliced containing the
1110 * VBI data to be encoded.
1111 * @param n_sliced_lines Number of elements in the @a sliced array.
1113 * Generates a raw VBI image similar to those you get from video
1114 * capture hardware. Otherwise identical to vbi_raw_vbi_image().
1117 * @c FALSE if the @a raw_size is too small, if the @a sp sampling
1118 * parameters are invalid, if the signal levels are invalid,
1119 * if the @a sliced array contains unsupported services or line numbers
1120 * outside the @a sp sampling parameters.
1125 vbi_raw_video_image (uint8_t * raw,
1126 unsigned long raw_size,
1127 const vbi_sampling_par * sp,
1131 unsigned int pixel_mask,
1132 vbi_bool swap_fields,
1133 const vbi_sliced * sliced, unsigned int n_sliced_lines)
1135 return _vbi_raw_video_image (raw, raw_size, sp,
1136 blank_level, black_level,
1137 white_level, pixel_mask,
1138 swap_fields ? _VBI_RAW_SWAP_FIELDS : 0, sliced, n_sliced_lines);