2 Vertical Blank Interval support functions
3 Copyright (C) 2004-2007 Hans Verkuil <hverkuil@xs4all.nl>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include "ivtv-driver.h"
22 #include "ivtv-ioctl.h"
23 #include "ivtv-queue.h"
26 static void ivtv_set_vps(struct ivtv *itv, int enabled, u8 vps1, u8 vps2, u8 vps3,
29 struct v4l2_sliced_vbi_data data;
31 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
33 data.id = V4L2_SLICED_VPS;
35 data.line = enabled ? 16 : 0;
41 ivtv_saa7127(itv, VIDIOC_INT_S_VBI_DATA, &data);
44 static void ivtv_set_cc(struct ivtv *itv, int mode, u8 cc1, u8 cc2, u8 cc3, u8 cc4)
46 struct v4l2_sliced_vbi_data data;
48 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
50 data.id = V4L2_SLICED_CAPTION_525;
52 data.line = (mode & 1) ? 21 : 0;
55 ivtv_saa7127(itv, VIDIOC_INT_S_VBI_DATA, &data);
57 data.line = (mode & 2) ? 21 : 0;
60 ivtv_saa7127(itv, VIDIOC_INT_S_VBI_DATA, &data);
63 static void ivtv_set_wss(struct ivtv *itv, int enabled, int mode)
65 struct v4l2_sliced_vbi_data data;
67 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
69 /* When using a 50 Hz system, always turn on the
70 wide screen signal with 4x3 ratio as the default.
71 Turning this signal on and off can confuse certain
72 TVs. As far as I can tell there is no reason not to
73 transmit this signal. */
74 if ((itv->std & V4L2_STD_625_50) && !enabled) {
76 mode = 0x08; /* 4x3 full format */
78 data.id = V4L2_SLICED_WSS_625;
80 data.line = enabled ? 23 : 0;
81 data.data[0] = mode & 0xff;
82 data.data[1] = (mode >> 8) & 0xff;
83 ivtv_saa7127(itv, VIDIOC_INT_S_VBI_DATA, &data);
86 static int odd_parity(u8 c)
95 static void passthrough_vbi_data(struct ivtv *itv, int cnt)
98 u8 cc[4] = { 0x80, 0x80, 0x80, 0x80 };
103 int cc_pos = itv->vbi.cc_pos;
106 for (i = 0; i < cnt; i++) {
107 struct v4l2_sliced_vbi_data *d = itv->vbi.sliced_dec_data + i;
109 if (d->id == V4L2_SLICED_CAPTION_525 && d->line == 21) {
119 else if (d->id == V4L2_SLICED_VPS && d->line == 16 && d->field == 0) {
120 memcpy(vps, d->data, sizeof(vps));
123 else if (d->id == V4L2_SLICED_WSS_625 && d->line == 23 && d->field == 0) {
124 wss = d->data[0] | d->data[1] << 8;
129 if (itv->vbi.wss_found != found_wss || itv->vbi.wss != wss) {
131 itv->vbi.wss_found = found_wss;
132 set_bit(IVTV_F_I_UPDATE_WSS, &itv->i_flags);
135 if (found_vps || itv->vbi.vps_found) {
136 itv->vbi.vps[0] = vps[2];
137 itv->vbi.vps[1] = vps[8];
138 itv->vbi.vps[2] = vps[9];
139 itv->vbi.vps[3] = vps[10];
140 itv->vbi.vps[4] = vps[11];
141 itv->vbi.vps_found = found_vps;
142 set_bit(IVTV_F_I_UPDATE_VPS, &itv->i_flags);
145 if (found_cc && cc_pos < sizeof(itv->vbi.cc_data_even)) {
146 itv->vbi.cc_data_odd[cc_pos] = cc[0];
147 itv->vbi.cc_data_odd[cc_pos + 1] = cc[1];
148 itv->vbi.cc_data_even[cc_pos] = cc[2];
149 itv->vbi.cc_data_even[cc_pos + 1] = cc[3];
150 itv->vbi.cc_pos = cc_pos + 2;
151 set_bit(IVTV_F_I_UPDATE_CC, &itv->i_flags);
155 static void copy_vbi_data(struct ivtv *itv, int lines, u32 pts_stamp)
159 u32 linemask[2] = { 0, 0 };
161 static const u8 mpeg_hdr_data[] = {
162 0x00, 0x00, 0x01, 0xba, 0x44, 0x00, 0x0c, 0x66,
163 0x24, 0x01, 0x01, 0xd1, 0xd3, 0xfa, 0xff, 0xff,
164 0x00, 0x00, 0x01, 0xbd, 0x00, 0x1a, 0x84, 0x80,
165 0x07, 0x21, 0x00, 0x5d, 0x63, 0xa7, 0xff, 0xff
167 const int sd = sizeof(mpeg_hdr_data); /* start of vbi data */
168 int idx = itv->vbi.frame % IVTV_VBI_FRAMES;
169 u8 *dst = &itv->vbi.sliced_mpeg_data[idx][0];
171 for (i = 0; i < lines; i++) {
174 if (itv->vbi.sliced_data[i].id == 0)
177 l = itv->vbi.sliced_data[i].line - 6;
178 f = itv->vbi.sliced_data[i].field;
182 linemask[0] |= (1 << l);
184 linemask[1] |= (1 << (l - 32));
185 dst[sd + 12 + line * 43] = service2vbi(itv->vbi.sliced_data[i].id);
186 memcpy(dst + sd + 12 + line * 43 + 1, itv->vbi.sliced_data[i].data, 42);
189 memcpy(dst, mpeg_hdr_data, sizeof(mpeg_hdr_data));
191 /* All lines are used, so there is no space for the linemask
192 (the max size of the VBI data is 36 * 43 + 4 bytes).
193 So in this case we use the magic number 'ITV0'. */
194 memcpy(dst + sd, "ITV0", 4);
195 memcpy(dst + sd + 4, dst + sd + 12, line * 43);
196 size = 4 + ((43 * line + 3) & ~3);
198 memcpy(dst + sd, "itv0", 4);
199 memcpy(dst + sd + 4, &linemask[0], 8);
200 size = 12 + ((43 * line + 3) & ~3);
202 dst[4+16] = (size + 10) >> 8;
203 dst[5+16] = (size + 10) & 0xff;
204 dst[9+16] = 0x21 | ((pts_stamp >> 29) & 0x6);
205 dst[10+16] = (pts_stamp >> 22) & 0xff;
206 dst[11+16] = 1 | ((pts_stamp >> 14) & 0xff);
207 dst[12+16] = (pts_stamp >> 7) & 0xff;
208 dst[13+16] = 1 | ((pts_stamp & 0x7f) << 1);
209 itv->vbi.sliced_mpeg_size[idx] = sd + size;
212 static int ivtv_convert_ivtv_vbi(struct ivtv *itv, u8 *p)
218 if (!memcmp(p, "itv0", 4)) {
219 memcpy(linemask, p + 4, 8);
221 } else if (!memcmp(p, "ITV0", 4)) {
222 linemask[0] = 0xffffffff;
226 /* unknown VBI data, convert to empty VBI frame */
227 linemask[0] = linemask[1] = 0;
229 for (i = 0; i < 36; i++) {
232 if (i < 32 && !(linemask[0] & (1 << i)))
234 if (i >= 32 && !(linemask[1] & (1 << (i - 32))))
238 case IVTV_SLICED_TYPE_TELETEXT_B:
239 id2 = V4L2_SLICED_TELETEXT_B;
241 case IVTV_SLICED_TYPE_CAPTION_525:
242 id2 = V4L2_SLICED_CAPTION_525;
243 err = !odd_parity(p[1]) || !odd_parity(p[2]);
245 case IVTV_SLICED_TYPE_VPS:
246 id2 = V4L2_SLICED_VPS;
248 case IVTV_SLICED_TYPE_WSS_625:
249 id2 = V4L2_SLICED_WSS_625;
256 l = (i < 18) ? i + 6 : i - 18 + 6;
257 itv->vbi.sliced_dec_data[line].line = l;
258 itv->vbi.sliced_dec_data[line].field = i >= 18;
259 itv->vbi.sliced_dec_data[line].id = id2;
260 memcpy(itv->vbi.sliced_dec_data[line].data, p + 1, 42);
266 itv->vbi.sliced_dec_data[line].id = 0;
267 itv->vbi.sliced_dec_data[line].line = 0;
268 itv->vbi.sliced_dec_data[line].field = 0;
271 return line * sizeof(itv->vbi.sliced_dec_data[0]);
274 ssize_t ivtv_write_vbi(struct ivtv *itv, const char __user *ubuf, size_t count)
276 /* Should be a __user pointer, but sparse doesn't parse this bit correctly. */
277 const struct v4l2_sliced_vbi_data *p = (const struct v4l2_sliced_vbi_data *)ubuf;
278 u8 cc[4] = { 0x80, 0x80, 0x80, 0x80 };
280 int cc_pos = itv->vbi.cc_pos;
282 while (count >= sizeof(struct v4l2_sliced_vbi_data)) {
284 case V4L2_SLICED_CAPTION_525:
297 case V4L2_SLICED_VPS:
298 if (p->line == 16 && p->field == 0) {
299 itv->vbi.vps[0] = p->data[2];
300 itv->vbi.vps[1] = p->data[8];
301 itv->vbi.vps[2] = p->data[9];
302 itv->vbi.vps[3] = p->data[10];
303 itv->vbi.vps[4] = p->data[11];
304 itv->vbi.vps_found = 1;
305 set_bit(IVTV_F_I_UPDATE_VPS, &itv->i_flags);
309 case V4L2_SLICED_WSS_625:
310 if (p->line == 23 && p->field == 0) {
311 /* No lock needed for WSS */
312 itv->vbi.wss = p->data[0] | (p->data[1] << 8);
313 itv->vbi.wss_found = 1;
314 set_bit(IVTV_F_I_UPDATE_WSS, &itv->i_flags);
325 if (found_cc && cc_pos < sizeof(itv->vbi.cc_data_even)) {
326 itv->vbi.cc_data_odd[cc_pos] = cc[0];
327 itv->vbi.cc_data_odd[cc_pos + 1] = cc[1];
328 itv->vbi.cc_data_even[cc_pos] = cc[2];
329 itv->vbi.cc_data_even[cc_pos + 1] = cc[3];
330 itv->vbi.cc_pos = cc_pos + 2;
331 set_bit(IVTV_F_I_UPDATE_CC, &itv->i_flags);
334 return (const char __user *)p - ubuf;
337 /* Compress raw VBI format, removes leading SAV codes and surplus space after the
339 Returns new compressed size. */
340 static u32 compress_raw_buf(struct ivtv *itv, u8 *buf, u32 size)
342 u32 line_size = itv->vbi.raw_decoder_line_size;
343 u32 lines = itv->vbi.count;
344 u8 sav1 = itv->vbi.raw_decoder_sav_odd_field;
345 u8 sav2 = itv->vbi.raw_decoder_sav_even_field;
350 for (i = 0; i < lines; i++) {
351 p = buf + i * line_size;
353 /* Look for SAV code */
354 if (p[0] != 0xff || p[1] || p[2] || (p[3] != sav1 && p[3] != sav2)) {
357 memcpy(q, p + 4, line_size - 4);
360 return lines * (line_size - 4);
364 /* Compressed VBI format, all found sliced blocks put next to one another
365 Returns new compressed size */
366 static u32 compress_sliced_buf(struct ivtv *itv, u32 line, u8 *buf, u32 size, u8 sav)
368 u32 line_size = itv->vbi.sliced_decoder_line_size;
369 struct v4l2_decode_vbi_line vbi;
372 /* find the first valid line */
373 for (i = 0; i < size; i++, buf++) {
374 if (buf[0] == 0xff && !buf[1] && !buf[2] && buf[3] == sav)
379 if (size < line_size) {
382 for (i = 0; i < size / line_size; i++) {
383 u8 *p = buf + i * line_size;
385 /* Look for SAV code */
386 if (p[0] != 0xff || p[1] || p[2] || p[3] != sav) {
390 itv->video_dec_func(itv, VIDIOC_INT_DECODE_VBI_LINE, &vbi);
392 itv->vbi.sliced_data[line].id = vbi.type;
393 itv->vbi.sliced_data[line].field = vbi.is_second_field;
394 itv->vbi.sliced_data[line].line = vbi.line;
395 memcpy(itv->vbi.sliced_data[line].data, vbi.p, 42);
402 void ivtv_process_vbi_data(struct ivtv *itv, struct ivtv_buffer *buf,
403 u64 pts_stamp, int streamtype)
405 u8 *p = (u8 *) buf->buf;
406 u32 size = buf->bytesused;
410 if (streamtype == IVTV_ENC_STREAM_TYPE_VBI && itv->vbi.sliced_in->service_set == 0) {
417 size = buf->bytesused = compress_raw_buf(itv, p, size);
419 /* second field of the frame? */
420 if (type == itv->vbi.raw_decoder_sav_even_field) {
421 /* Dirty hack needed for backwards
422 compatibility of old VBI software. */
424 memcpy(p, &itv->vbi.frame, 4);
430 /* Sliced VBI data with data insertion */
431 if (streamtype == IVTV_ENC_STREAM_TYPE_VBI) {
437 lines = compress_sliced_buf(itv, 0, p, size / 2,
438 itv->vbi.sliced_decoder_sav_odd_field);
440 /* experimentation shows that the second half does not always begin
441 at the exact address. So start a bit earlier (hence 32). */
442 lines = compress_sliced_buf(itv, lines, p + size / 2 - 32, size / 2 + 32,
443 itv->vbi.sliced_decoder_sav_even_field);
444 /* always return at least one empty line */
446 itv->vbi.sliced_data[0].id = 0;
447 itv->vbi.sliced_data[0].line = 0;
448 itv->vbi.sliced_data[0].field = 0;
451 buf->bytesused = size = lines * sizeof(itv->vbi.sliced_data[0]);
452 memcpy(p, &itv->vbi.sliced_data[0], size);
454 if (itv->vbi.insert_mpeg) {
455 copy_vbi_data(itv, lines, pts_stamp);
461 /* Sliced VBI re-inserted from an MPEG stream */
462 if (streamtype == IVTV_DEC_STREAM_TYPE_VBI) {
463 /* If the size is not 4-byte aligned, then the starting address
464 for the swapping is also shifted. After swapping the data the
465 real start address of the VBI data is exactly 4 bytes after the
466 original start. It's a bit fiddly but it works like a charm.
467 Non-4-byte alignment happens when an lseek is done on the input
468 mpeg file to a non-4-byte aligned position. So on arrival here
469 the VBI data is also non-4-byte aligned. */
470 int offset = size & 3;
477 for (y = 0; y < size; y += 4) {
478 swab32s((u32 *)(p + y));
481 cnt = ivtv_convert_ivtv_vbi(itv, p + offset);
482 memcpy(buf->buf, itv->vbi.sliced_dec_data, cnt);
483 buf->bytesused = cnt;
485 passthrough_vbi_data(itv, cnt / sizeof(itv->vbi.sliced_dec_data[0]));
490 void ivtv_disable_vbi(struct ivtv *itv)
492 clear_bit(IVTV_F_I_UPDATE_WSS, &itv->i_flags);
493 clear_bit(IVTV_F_I_UPDATE_VPS, &itv->i_flags);
494 clear_bit(IVTV_F_I_UPDATE_CC, &itv->i_flags);
495 ivtv_set_wss(itv, 0, 0);
496 ivtv_set_cc(itv, 0, 0, 0, 0, 0);
497 ivtv_set_vps(itv, 0, 0, 0, 0, 0, 0);
498 itv->vbi.vps_found = itv->vbi.wss_found = 0;
504 void ivtv_vbi_work_handler(struct ivtv *itv)
506 struct v4l2_sliced_vbi_data data;
509 if (itv->output_mode == OUT_PASSTHROUGH) {
510 /* Note: currently only the saa7115 is used in a PVR350,
511 so these commands are for now saa7115 specific. */
513 data.id = V4L2_SLICED_WSS_625;
516 if (itv->video_dec_func(itv, VIDIOC_INT_G_VBI_DATA, &data) == 0) {
517 ivtv_set_wss(itv, 1, data.data[0] & 0xf);
518 itv->vbi.wss_no_update = 0;
519 } else if (itv->vbi.wss_no_update == 4) {
520 ivtv_set_wss(itv, 1, 0x8); /* 4x3 full format */
522 itv->vbi.wss_no_update++;
526 u8 c1 = 0, c2 = 0, c3 = 0, c4 = 0;
529 data.id = V4L2_SLICED_CAPTION_525;
531 if (itv->video_dec_func(itv, VIDIOC_INT_G_VBI_DATA, &data) == 0) {
537 if (itv->video_dec_func(itv, VIDIOC_INT_G_VBI_DATA, &data) == 0) {
543 itv->vbi.cc_no_update = 0;
544 ivtv_set_cc(itv, mode, c1, c2, c3, c4);
545 } else if (itv->vbi.cc_no_update == 4) {
546 ivtv_set_cc(itv, 0, 0, 0, 0, 0);
548 itv->vbi.cc_no_update++;
554 if (test_and_clear_bit(IVTV_F_I_UPDATE_WSS, &itv->i_flags)) {
556 ivtv_set_wss(itv, itv->vbi.wss_found, itv->vbi.wss & 0xf);
559 if (test_and_clear_bit(IVTV_F_I_UPDATE_CC, &itv->i_flags)) {
560 if (itv->vbi.cc_pos == 0) {
561 ivtv_set_cc(itv, 3, 0x80, 0x80, 0x80, 0x80);
563 while (itv->vbi.cc_pos) {
564 u8 cc_odd0 = itv->vbi.cc_data_odd[0];
565 u8 cc_odd1 = itv->vbi.cc_data_odd[1];
566 u8 cc_even0 = itv->vbi.cc_data_even[0];
567 u8 cc_even1 = itv->vbi.cc_data_even[1];
569 memcpy(itv->vbi.cc_data_odd, itv->vbi.cc_data_odd + 2, sizeof(itv->vbi.cc_data_odd) - 2);
570 memcpy(itv->vbi.cc_data_even, itv->vbi.cc_data_even + 2, sizeof(itv->vbi.cc_data_even) - 2);
571 itv->vbi.cc_pos -= 2;
572 if (itv->vbi.cc_pos && cc_odd0 == 0x80 && cc_odd1 == 0x80)
575 /* Send to Saa7127 */
576 ivtv_set_cc(itv, 3, cc_odd0, cc_odd1, cc_even0, cc_even1);
577 if (itv->vbi.cc_pos == 0)
578 set_bit(IVTV_F_I_UPDATE_CC, &itv->i_flags);
583 if (test_and_clear_bit(IVTV_F_I_UPDATE_VPS, &itv->i_flags)) {
585 ivtv_set_vps(itv, itv->vbi.vps_found,
586 itv->vbi.vps[0], itv->vbi.vps[1],
587 itv->vbi.vps[2], itv->vbi.vps[3], itv->vbi.vps[4]);