1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 *  cx18 Vertical Blank Interval support functions
4 *
5 *  Derived from ivtv-vbi.c
6 *
7 *  Copyright (C) 2007  Hans Verkuil <hverkuil@xs4all.nl>
8 */
9
10#include "cx18-driver.h"
11#include "cx18-vbi.h"
12#include "cx18-ioctl.h"
13#include "cx18-queue.h"
14
15/*
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.
19 *
20 * Task FieldEven VerticalBlank HorizontalBlank 0 0 0 0
21 */
22static const u8 raw_vbi_sav_rp[2] = { 0x20, 0x60 };    /* __V_, _FV_ */
23static const u8 sliced_vbi_eav_rp[2] = { 0xb0, 0xf0 }; /* T_VH, TFVH */
24
25static void copy_vbi_data(struct cx18 *cx, int lines, u32 pts_stamp)
26{
27	int line = 0;
28	int i;
29	u32 linemask[2] = { 0, 0 };
30	unsigned short size;
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 */
43	};
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];
47
48	for (i = 0; i < lines; i++) {
49		struct v4l2_sliced_vbi_data *sdata = cx->vbi.sliced_data + i;
50		int f, l;
51
52		if (sdata->id == 0)
53			continue;
54
55		l = sdata->line - 6;
56		f = sdata->field;
57		if (f)
58			l += 18;
59		if (l < 32)
60			linemask[0] |= (1 << l);
61		else
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);
65		line++;
66	}
67	memcpy(dst, mpeg_hdr_data, sizeof(mpeg_hdr_data));
68	if (line == 36) {
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);
75	} else {
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);
81	}
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;
90}
91
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. */
95static u32 compress_raw_buf(struct cx18 *cx, u8 *buf, u32 size, u32 hdr_size)
96{
97	u32 line_size = VBI_ACTIVE_SAMPLES;
98	u32 lines = cx->vbi.count * 2;
99	u8 *q = buf;
100	u8 *p;
101	int i;
102
103	/* Skip the header */
104	buf += hdr_size;
105
106	for (i = 0; i < lines; i++) {
107		p = buf + i * line_size;
108
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]))
113			break;
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);
120		} else {
121			memcpy(q, p + 4, line_size - 4);
122			q += line_size - 4;
123		}
124	}
125	return lines * (line_size - 4);
126}
127
128static u32 compress_sliced_buf(struct cx18 *cx, u8 *buf, u32 size,
129			       const u32 hdr_size)
130{
131	struct v4l2_decode_vbi_line vbi;
132	int i;
133	u32 line = 0;
134	u32 line_size = cx->is_60hz ? VBI_HBLANK_SAMPLES_60HZ
135				    : VBI_HBLANK_SAMPLES_50HZ;
136
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]))
142			break;
143	}
144
145	/*
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
149	 */
150	size -= (i - hdr_size);
151	if (size < line_size)
152		return line;
153
154	for (i = 0; i < size / line_size; i++) {
155		u8 *p = buf + i * line_size;
156
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]))
161			continue;
162		vbi.p = p + 4;
163		v4l2_subdev_call(cx->sd_av, vbi, decode_vbi_line, &vbi);
164		if (vbi.type) {
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);
169			line++;
170		}
171	}
172	return line;
173}
174
175static void _cx18_process_vbi_data(struct cx18 *cx, struct cx18_buffer *buf)
176{
177	/*
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]
180	 */
181	struct vbi_data_hdr {
182		__be32 magic;
183		__be32 unknown;
184		__be32 pts;
185	} *hdr = (struct vbi_data_hdr *) buf->buf;
186
187	u8 *p = (u8 *) buf->buf;
188	u32 size = buf->bytesused;
189	u32 pts;
190	int lines;
191
192	/*
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
196	 */
197	cx18_buf_swap(buf);
198
199	/* Raw VBI data */
200	if (cx18_raw_vbi(cx)) {
201
202		size = buf->bytesused =
203		     compress_raw_buf(cx, p, size, sizeof(struct vbi_data_hdr));
204
205		/*
206		 * Hack needed for compatibility with old VBI software.
207		 * Write the frame # at the last 4 bytes of the frame
208		 */
209		p += size - 4;
210		memcpy(p, &cx->vbi.frame, 4);
211		cx->vbi.frame++;
212		return;
213	}
214
215	/* Sliced VBI data with data insertion */
216
217	pts = (be32_to_cpu(hdr->magic) == 0x3fffffff) ? be32_to_cpu(hdr->pts)
218						      : 0;
219
220	lines = compress_sliced_buf(cx, p, size, sizeof(struct vbi_data_hdr));
221
222	/* always return at least one empty line */
223	if (lines == 0) {
224		cx->vbi.sliced_data[0].id = 0;
225		cx->vbi.sliced_data[0].line = 0;
226		cx->vbi.sliced_data[0].field = 0;
227		lines = 1;
228	}
229	buf->bytesused = size = lines * sizeof(cx->vbi.sliced_data[0]);
230	memcpy(p, &cx->vbi.sliced_data[0], size);
231
232	if (cx->vbi.insert_mpeg)
233		copy_vbi_data(cx, lines, pts);
234	cx->vbi.frame++;
235}
236
237void cx18_process_vbi_data(struct cx18 *cx, struct cx18_mdl *mdl,
238			   int streamtype)
239{
240	struct cx18_buffer *buf;
241	u32 orig_used;
242
243	if (streamtype != CX18_ENC_STREAM_TYPE_VBI)
244		return;
245
246	/*
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.
250	 *
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.
255	 */
256	list_for_each_entry(buf, &mdl->buf_list, list) {
257		orig_used = buf->bytesused;
258		if (orig_used == 0)
259			break;
260		_cx18_process_vbi_data(cx, buf);
261		mdl->bytesused -= (orig_used - buf->bytesused);
262	}
263}
264