1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * cx18 Vertical Blank Interval support functions
5 * Derived from ivtv-vbi.c
7 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
10 #include "cx18-driver.h"
12 #include "cx18-ioctl.h"
13 #include "cx18-queue.h"
16 * Raster Reference/Protection (RP) bytes, used in Start/End Active
17 * Video codes emitted from the digitzer in VIP 1.x mode, that flag the start
18 * of VBI sample or VBI ancillary data regions in the digital ratser line.
20 * Task FieldEven VerticalBlank HorizontalBlank 0 0 0 0
22 static const u8 raw_vbi_sav_rp
[2] = { 0x20, 0x60 }; /* __V_, _FV_ */
23 static const u8 sliced_vbi_eav_rp
[2] = { 0xb0, 0xf0 }; /* T_VH, TFVH */
25 static void copy_vbi_data(struct cx18
*cx
, int lines
, u32 pts_stamp
)
29 u32 linemask
[2] = { 0, 0 };
31 static const u8 mpeg_hdr_data
[] = {
32 /* MPEG-2 Program Pack */
33 0x00, 0x00, 0x01, 0xba, /* Prog Pack start code */
34 0x44, 0x00, 0x0c, 0x66, 0x24, 0x01, /* SCR, SCR Ext, markers */
35 0x01, 0xd1, 0xd3, /* Mux Rate, markers */
36 0xfa, 0xff, 0xff, /* Res, Suff cnt, Stuff */
37 /* MPEG-2 Private Stream 1 PES Packet */
38 0x00, 0x00, 0x01, 0xbd, /* Priv Stream 1 start */
39 0x00, 0x1a, /* length */
40 0x84, 0x80, 0x07, /* flags, hdr data len */
41 0x21, 0x00, 0x5d, 0x63, 0xa7, /* PTS, markers */
42 0xff, 0xff /* stuffing */
44 const int sd
= sizeof(mpeg_hdr_data
); /* start of vbi data */
45 int idx
= cx
->vbi
.frame
% CX18_VBI_FRAMES
;
46 u8
*dst
= &cx
->vbi
.sliced_mpeg_data
[idx
][0];
48 for (i
= 0; i
< lines
; i
++) {
49 struct v4l2_sliced_vbi_data
*sdata
= cx
->vbi
.sliced_data
+ i
;
60 linemask
[0] |= (1 << l
);
62 linemask
[1] |= (1 << (l
- 32));
63 dst
[sd
+ 12 + line
* 43] = cx18_service2vbi(sdata
->id
);
64 memcpy(dst
+ sd
+ 12 + line
* 43 + 1, sdata
->data
, 42);
67 memcpy(dst
, mpeg_hdr_data
, sizeof(mpeg_hdr_data
));
69 /* All lines are used, so there is no space for the linemask
70 (the max size of the VBI data is 36 * 43 + 4 bytes).
71 So in this case we use the magic number 'ITV0'. */
72 memcpy(dst
+ sd
, "ITV0", 4);
73 memmove(dst
+ sd
+ 4, dst
+ sd
+ 12, line
* 43);
74 size
= 4 + ((43 * line
+ 3) & ~3);
76 memcpy(dst
+ sd
, "itv0", 4);
77 cpu_to_le32s(&linemask
[0]);
78 cpu_to_le32s(&linemask
[1]);
79 memcpy(dst
+ sd
+ 4, &linemask
[0], 8);
80 size
= 12 + ((43 * line
+ 3) & ~3);
82 dst
[4+16] = (size
+ 10) >> 8;
83 dst
[5+16] = (size
+ 10) & 0xff;
84 dst
[9+16] = 0x21 | ((pts_stamp
>> 29) & 0x6);
85 dst
[10+16] = (pts_stamp
>> 22) & 0xff;
86 dst
[11+16] = 1 | ((pts_stamp
>> 14) & 0xff);
87 dst
[12+16] = (pts_stamp
>> 7) & 0xff;
88 dst
[13+16] = 1 | ((pts_stamp
& 0x7f) << 1);
89 cx
->vbi
.sliced_mpeg_size
[idx
] = sd
+ size
;
92 /* Compress raw VBI format, removes leading SAV codes and surplus space
93 after the frame. Returns new compressed size. */
94 /* FIXME - this function ignores the input size. */
95 static u32
compress_raw_buf(struct cx18
*cx
, u8
*buf
, u32 size
, u32 hdr_size
)
97 u32 line_size
= VBI_ACTIVE_SAMPLES
;
98 u32 lines
= cx
->vbi
.count
* 2;
103 /* Skip the header */
106 for (i
= 0; i
< lines
; i
++) {
107 p
= buf
+ i
* line_size
;
109 /* Look for SAV code */
110 if (p
[0] != 0xff || p
[1] || p
[2] ||
111 (p
[3] != raw_vbi_sav_rp
[0] &&
112 p
[3] != raw_vbi_sav_rp
[1]))
114 if (i
== lines
- 1) {
115 /* last line is hdr_size bytes short - extrapolate it */
116 memcpy(q
, p
+ 4, line_size
- 4 - hdr_size
);
117 q
+= line_size
- 4 - hdr_size
;
118 p
+= line_size
- hdr_size
- 1;
119 memset(q
, (int) *p
, hdr_size
);
121 memcpy(q
, p
+ 4, line_size
- 4);
125 return lines
* (line_size
- 4);
128 static u32
compress_sliced_buf(struct cx18
*cx
, u8
*buf
, u32 size
,
131 struct v4l2_decode_vbi_line vbi
;
134 u32 line_size
= cx
->is_60hz
? VBI_HBLANK_SAMPLES_60HZ
135 : VBI_HBLANK_SAMPLES_50HZ
;
137 /* find the first valid line */
138 for (i
= hdr_size
, buf
+= hdr_size
; i
< size
; i
++, buf
++) {
139 if (buf
[0] == 0xff && !buf
[1] && !buf
[2] &&
140 (buf
[3] == sliced_vbi_eav_rp
[0] ||
141 buf
[3] == sliced_vbi_eav_rp
[1]))
146 * The last line is short by hdr_size bytes, but for the remaining
147 * checks against size, we pretend that it is not, by counting the
148 * header bytes we knowingly skipped
150 size
-= (i
- hdr_size
);
151 if (size
< line_size
)
154 for (i
= 0; i
< size
/ line_size
; i
++) {
155 u8
*p
= buf
+ i
* line_size
;
157 /* Look for EAV code */
158 if (p
[0] != 0xff || p
[1] || p
[2] ||
159 (p
[3] != sliced_vbi_eav_rp
[0] &&
160 p
[3] != sliced_vbi_eav_rp
[1]))
163 v4l2_subdev_call(cx
->sd_av
, vbi
, decode_vbi_line
, &vbi
);
165 cx
->vbi
.sliced_data
[line
].id
= vbi
.type
;
166 cx
->vbi
.sliced_data
[line
].field
= vbi
.is_second_field
;
167 cx
->vbi
.sliced_data
[line
].line
= vbi
.line
;
168 memcpy(cx
->vbi
.sliced_data
[line
].data
, vbi
.p
, 42);
175 static void _cx18_process_vbi_data(struct cx18
*cx
, struct cx18_buffer
*buf
)
178 * The CX23418 provides a 12 byte header in its raw VBI buffers to us:
179 * 0x3fffffff [4 bytes of something] [4 byte presentation time stamp]
181 struct vbi_data_hdr
{
185 } *hdr
= (struct vbi_data_hdr
*) buf
->buf
;
187 u8
*p
= (u8
*) buf
->buf
;
188 u32 size
= buf
->bytesused
;
193 * The CX23418 sends us data that is 32 bit little-endian swapped,
194 * but we want the raw VBI bytes in the order they were in the raster
195 * line. This has a side effect of making the header big endian
200 if (cx18_raw_vbi(cx
)) {
202 size
= buf
->bytesused
=
203 compress_raw_buf(cx
, p
, size
, sizeof(struct vbi_data_hdr
));
206 * Hack needed for compatibility with old VBI software.
207 * Write the frame # at the last 4 bytes of the frame
210 memcpy(p
, &cx
->vbi
.frame
, 4);
215 /* Sliced VBI data with data insertion */
217 pts
= (be32_to_cpu(hdr
->magic
) == 0x3fffffff) ? be32_to_cpu(hdr
->pts
)
220 lines
= compress_sliced_buf(cx
, p
, size
, sizeof(struct vbi_data_hdr
));
222 /* always return at least one empty line */
224 cx
->vbi
.sliced_data
[0].id
= 0;
225 cx
->vbi
.sliced_data
[0].line
= 0;
226 cx
->vbi
.sliced_data
[0].field
= 0;
229 buf
->bytesused
= size
= lines
* sizeof(cx
->vbi
.sliced_data
[0]);
230 memcpy(p
, &cx
->vbi
.sliced_data
[0], size
);
232 if (cx
->vbi
.insert_mpeg
)
233 copy_vbi_data(cx
, lines
, pts
);
237 void cx18_process_vbi_data(struct cx18
*cx
, struct cx18_mdl
*mdl
,
240 struct cx18_buffer
*buf
;
243 if (streamtype
!= CX18_ENC_STREAM_TYPE_VBI
)
247 * Big assumption here:
248 * Every buffer hooked to the MDL's buf_list is a complete VBI frame
249 * that ends at the end of the buffer.
251 * To assume anything else would make the code in this file
252 * more complex, or require extra memcpy()'s to make the
253 * buffers satisfy the above assumption. It's just simpler to set
254 * up the encoder buffer transfers to make the assumption true.
256 list_for_each_entry(buf
, &mdl
->buf_list
, list
) {
257 orig_used
= buf
->bytesused
;
260 _cx18_process_vbi_data(cx
, buf
);
261 mdl
->bytesused
-= (orig_used
- buf
->bytesused
);