blob: 99f9b2c86b0c798fe0cc13d1d323192b69882af0 [file] [log] [blame]
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001/*
2 * cx18 Vertical Blank Interval support functions
3 *
4 * Derived from ivtv-vbi.c
5 *
6 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 * 02111-1307 USA
22 */
23
24#include "cx18-driver.h"
25#include "cx18-vbi.h"
26#include "cx18-ioctl.h"
27#include "cx18-queue.h"
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030028
Andy Walls302df972009-01-31 00:33:02 -030029/*
30 * Raster Reference/Protection (RP) bytes, used in Start/End Active
31 * Video codes emitted from the digitzer in VIP 1.x mode, that flag the start
32 * of VBI sample or VBI ancilliary data regions in the digitial ratser line.
33 *
34 * Task FieldEven VerticalBlank HorizontalBlank 0 0 0 0
35 */
36static const u8 raw_vbi_sav_rp[2] = { 0x20, 0x60 }; /* __V_, _FV_ */
37static const u8 sliced_vbi_eav_rp[2] = { 0xb0, 0xf0 }; /* T_VH, TFVH */
38
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030039static void copy_vbi_data(struct cx18 *cx, int lines, u32 pts_stamp)
40{
41 int line = 0;
42 int i;
43 u32 linemask[2] = { 0, 0 };
44 unsigned short size;
45 static const u8 mpeg_hdr_data[] = {
Andy Walls302df972009-01-31 00:33:02 -030046 /* MPEG-2 Program Pack */
47 0x00, 0x00, 0x01, 0xba, /* Prog Pack start code */
48 0x44, 0x00, 0x0c, 0x66, 0x24, 0x01, /* SCR, SCR Ext, markers */
49 0x01, 0xd1, 0xd3, /* Mux Rate, markers */
50 0xfa, 0xff, 0xff, /* Res, Suff cnt, Stuff */
51 /* MPEG-2 Private Stream 1 PES Packet */
52 0x00, 0x00, 0x01, 0xbd, /* Priv Stream 1 start */
53 0x00, 0x1a, /* length */
54 0x84, 0x80, 0x07, /* flags, hdr data len */
55 0x21, 0x00, 0x5d, 0x63, 0xa7, /* PTS, markers */
56 0xff, 0xff /* stuffing */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030057 };
58 const int sd = sizeof(mpeg_hdr_data); /* start of vbi data */
59 int idx = cx->vbi.frame % CX18_VBI_FRAMES;
60 u8 *dst = &cx->vbi.sliced_mpeg_data[idx][0];
61
62 for (i = 0; i < lines; i++) {
63 struct v4l2_sliced_vbi_data *sdata = cx->vbi.sliced_data + i;
64 int f, l;
65
66 if (sdata->id == 0)
67 continue;
68
69 l = sdata->line - 6;
70 f = sdata->field;
71 if (f)
72 l += 18;
73 if (l < 32)
74 linemask[0] |= (1 << l);
75 else
76 linemask[1] |= (1 << (l - 32));
Mauro Carvalho Chehabaed6abd2008-04-29 21:38:51 -030077 dst[sd + 12 + line * 43] = cx18_service2vbi(sdata->id);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030078 memcpy(dst + sd + 12 + line * 43 + 1, sdata->data, 42);
79 line++;
80 }
81 memcpy(dst, mpeg_hdr_data, sizeof(mpeg_hdr_data));
82 if (line == 36) {
83 /* All lines are used, so there is no space for the linemask
84 (the max size of the VBI data is 36 * 43 + 4 bytes).
85 So in this case we use the magic number 'ITV0'. */
86 memcpy(dst + sd, "ITV0", 4);
87 memcpy(dst + sd + 4, dst + sd + 12, line * 43);
88 size = 4 + ((43 * line + 3) & ~3);
89 } else {
Andy Walls302df972009-01-31 00:33:02 -030090 memcpy(dst + sd, "itv0", 4);
Andy Walls6e1a63722009-03-14 20:03:26 -030091 cpu_to_le32s(&linemask[0]);
92 cpu_to_le32s(&linemask[1]);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030093 memcpy(dst + sd + 4, &linemask[0], 8);
94 size = 12 + ((43 * line + 3) & ~3);
95 }
96 dst[4+16] = (size + 10) >> 8;
97 dst[5+16] = (size + 10) & 0xff;
98 dst[9+16] = 0x21 | ((pts_stamp >> 29) & 0x6);
99 dst[10+16] = (pts_stamp >> 22) & 0xff;
100 dst[11+16] = 1 | ((pts_stamp >> 14) & 0xff);
101 dst[12+16] = (pts_stamp >> 7) & 0xff;
102 dst[13+16] = 1 | ((pts_stamp & 0x7f) << 1);
103 cx->vbi.sliced_mpeg_size[idx] = sd + size;
104}
105
106/* Compress raw VBI format, removes leading SAV codes and surplus space
Andy Walls466df462009-02-07 15:47:28 -0300107 after the frame. Returns new compressed size. */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300108static u32 compress_raw_buf(struct cx18 *cx, u8 *buf, u32 size)
109{
Andy Walls302df972009-01-31 00:33:02 -0300110 u32 line_size = vbi_active_samples;
Andy Walls466df462009-02-07 15:47:28 -0300111 u32 lines = cx->vbi.count * 2;
Andy Walls302df972009-01-31 00:33:02 -0300112 u8 sav1 = raw_vbi_sav_rp[0];
113 u8 sav2 = raw_vbi_sav_rp[1];
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300114 u8 *q = buf;
115 u8 *p;
116 int i;
117
118 for (i = 0; i < lines; i++) {
119 p = buf + i * line_size;
120
121 /* Look for SAV code */
122 if (p[0] != 0xff || p[1] || p[2] ||
123 (p[3] != sav1 && p[3] != sav2))
124 break;
125 memcpy(q, p + 4, line_size - 4);
126 q += line_size - 4;
127 }
128 return lines * (line_size - 4);
129}
130
131
132/* Compressed VBI format, all found sliced blocks put next to one another
133 Returns new compressed size */
134static u32 compress_sliced_buf(struct cx18 *cx, u32 line, u8 *buf,
Andy Walls302df972009-01-31 00:33:02 -0300135 u32 size, u8 eav)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300136{
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300137 struct v4l2_decode_vbi_line vbi;
138 int i;
Andy Walls302df972009-01-31 00:33:02 -0300139 u32 line_size = cx->is_60hz ? vbi_hblank_samples_60Hz
140 : vbi_hblank_samples_50Hz;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300141
142 /* find the first valid line */
143 for (i = 0; i < size; i++, buf++) {
Andy Walls302df972009-01-31 00:33:02 -0300144 if (buf[0] == 0xff && !buf[1] && !buf[2] && buf[3] == eav)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300145 break;
146 }
147
148 size -= i;
149 if (size < line_size)
150 return line;
151 for (i = 0; i < size / line_size; i++) {
152 u8 *p = buf + i * line_size;
153
Andy Walls302df972009-01-31 00:33:02 -0300154 /* Look for EAV code */
155 if (p[0] != 0xff || p[1] || p[2] || p[3] != eav)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300156 continue;
157 vbi.p = p + 4;
Andy Wallsfa3e7032009-02-16 02:23:25 -0300158 v4l2_subdev_call(cx->sd_av, video, decode_vbi_line, &vbi);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300159 if (vbi.type) {
160 cx->vbi.sliced_data[line].id = vbi.type;
161 cx->vbi.sliced_data[line].field = vbi.is_second_field;
162 cx->vbi.sliced_data[line].line = vbi.line;
163 memcpy(cx->vbi.sliced_data[line].data, vbi.p, 42);
164 line++;
165 }
166 }
167 return line;
168}
169
170void cx18_process_vbi_data(struct cx18 *cx, struct cx18_buffer *buf,
Andy Walls776fa862009-02-01 21:42:12 -0300171 int streamtype)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300172{
173 u8 *p = (u8 *) buf->buf;
Andy Walls583803d2009-02-28 18:48:27 -0300174 __be32 *q = (__be32 *) buf->buf;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300175 u32 size = buf->bytesused;
Andy Walls776fa862009-02-01 21:42:12 -0300176 u32 pts;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300177 int lines;
178
179 if (streamtype != CX18_ENC_STREAM_TYPE_VBI)
180 return;
181
Andy Walls01cbc212009-02-07 01:31:22 -0300182 /*
Andy Walls583803d2009-02-28 18:48:27 -0300183 * The CX23418 sends us data that is 32 bit little-endian swapped,
184 * but we want the raw VBI bytes in the order they were in the raster
185 * line. This has a side effect of making the 12 byte header big endian
Andy Walls01cbc212009-02-07 01:31:22 -0300186 */
Andy Walls776fa862009-02-01 21:42:12 -0300187 cx18_buf_swap(buf);
188
Andy Walls302df972009-01-31 00:33:02 -0300189 /*
Andy Walls776fa862009-02-01 21:42:12 -0300190 * The CX23418 provides a 12 byte header in it's raw VBI buffers to us:
191 * 0x3fffffff [4 bytes of something] [4 byte presentation time stamp?]
Andy Walls302df972009-01-31 00:33:02 -0300192 */
193
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300194 /* Raw VBI data */
Andy Wallsdd073432008-12-12 16:24:04 -0300195 if (cx18_raw_vbi(cx)) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300196 u8 type;
197
Andy Walls01cbc212009-02-07 01:31:22 -0300198 /*
Andy Walls466df462009-02-07 15:47:28 -0300199 * We've set up to get a frame's worth of VBI data at a time.
200 * Skip 12 bytes of header prefixing the first field.
Andy Walls01cbc212009-02-07 01:31:22 -0300201 */
Andy Wallsaf009cf2008-12-12 20:00:29 -0300202 size -= 12;
203 memcpy(p, &buf->buf[12], size);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300204 type = p[3];
205
Andy Walls466df462009-02-07 15:47:28 -0300206 /* Extrapolate the last 12 bytes of the frame's last line */
Andy Walls01cbc212009-02-07 01:31:22 -0300207 memset(&p[size], (int) p[size - 1], 12);
Andy Walls466df462009-02-07 15:47:28 -0300208 size += 12;
Andy Walls01cbc212009-02-07 01:31:22 -0300209
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300210 size = buf->bytesused = compress_raw_buf(cx, p, size);
211
Andy Walls466df462009-02-07 15:47:28 -0300212 /*
213 * Hack needed for compatibility with old VBI software.
214 * Write the frame # at the last 4 bytes of the frame
215 */
216 p += size - 4;
217 memcpy(p, &cx->vbi.frame, 4);
218 cx->vbi.frame++;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300219 return;
220 }
221
222 /* Sliced VBI data with data insertion */
Andy Walls776fa862009-02-01 21:42:12 -0300223
Andy Walls583803d2009-02-28 18:48:27 -0300224 pts = (be32_to_cpu(q[0]) == 0x3fffffff) ? be32_to_cpu(q[2]) : 0;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300225
Andy Walls812b1f92009-02-08 22:40:04 -0300226 /*
227 * For calls to compress_sliced_buf(), ensure there are an integral
228 * number of lines by shifting the real data up over the 12 bytes header
229 * that got stuffed in.
230 * FIXME - there's a smarter way to do this with pointers, but for some
231 * reason I can't get it to work correctly right now.
232 */
233 memcpy(p, &buf->buf[12], size-12);
234
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300235 /* first field */
Andy Walls302df972009-01-31 00:33:02 -0300236 lines = compress_sliced_buf(cx, 0, p, size / 2, sliced_vbi_eav_rp[0]);
Andy Walls812b1f92009-02-08 22:40:04 -0300237 /*
238 * second field
239 * In case the second half does not always begin at the exact address,
240 * start a bit earlier (hence 32).
241 */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300242 lines = compress_sliced_buf(cx, lines, p + size / 2 - 32,
Andy Walls302df972009-01-31 00:33:02 -0300243 size / 2 + 32, sliced_vbi_eav_rp[1]);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300244 /* always return at least one empty line */
245 if (lines == 0) {
246 cx->vbi.sliced_data[0].id = 0;
247 cx->vbi.sliced_data[0].line = 0;
248 cx->vbi.sliced_data[0].field = 0;
249 lines = 1;
250 }
251 buf->bytesused = size = lines * sizeof(cx->vbi.sliced_data[0]);
252 memcpy(p, &cx->vbi.sliced_data[0], size);
253
254 if (cx->vbi.insert_mpeg)
Andy Walls776fa862009-02-01 21:42:12 -0300255 copy_vbi_data(cx, lines, pts);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300256 cx->vbi.frame++;
257}