Path: blob/master/drivers/media/video/ivtv/ivtv-vbi.c
17887 views
/*1Vertical Blank Interval support functions2Copyright (C) 2004-2007 Hans Verkuil <[email protected]>34This program is free software; you can redistribute it and/or modify5it under the terms of the GNU General Public License as published by6the Free Software Foundation; either version 2 of the License, or7(at your option) any later version.89This program is distributed in the hope that it will be useful,10but WITHOUT ANY WARRANTY; without even the implied warranty of11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12GNU General Public License for more details.1314You should have received a copy of the GNU General Public License15along with this program; if not, write to the Free Software16Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA17*/1819#include "ivtv-driver.h"20#include "ivtv-i2c.h"21#include "ivtv-ioctl.h"22#include "ivtv-queue.h"23#include "ivtv-cards.h"24#include "ivtv-vbi.h"2526static void ivtv_set_vps(struct ivtv *itv, int enabled)27{28struct v4l2_sliced_vbi_data data;2930if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))31return;32data.id = V4L2_SLICED_VPS;33data.field = 0;34data.line = enabled ? 16 : 0;35data.data[2] = itv->vbi.vps_payload.data[0];36data.data[8] = itv->vbi.vps_payload.data[1];37data.data[9] = itv->vbi.vps_payload.data[2];38data.data[10] = itv->vbi.vps_payload.data[3];39data.data[11] = itv->vbi.vps_payload.data[4];40ivtv_call_hw(itv, IVTV_HW_SAA7127, vbi, s_vbi_data, &data);41}4243static void ivtv_set_cc(struct ivtv *itv, int mode, const struct vbi_cc *cc)44{45struct v4l2_sliced_vbi_data data;4647if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))48return;49data.id = V4L2_SLICED_CAPTION_525;50data.field = 0;51data.line = (mode & 1) ? 21 : 0;52data.data[0] = cc->odd[0];53data.data[1] = cc->odd[1];54ivtv_call_hw(itv, IVTV_HW_SAA7127, vbi, s_vbi_data, &data);55data.field = 1;56data.line = (mode & 2) ? 21 : 0;57data.data[0] = cc->even[0];58data.data[1] = cc->even[1];59ivtv_call_hw(itv, IVTV_HW_SAA7127, vbi, s_vbi_data, &data);60}6162static void ivtv_set_wss(struct ivtv *itv, int enabled, int mode)63{64struct v4l2_sliced_vbi_data data;6566if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))67return;68/* When using a 50 Hz system, always turn on the69wide screen signal with 4x3 ratio as the default.70Turning this signal on and off can confuse certain71TVs. As far as I can tell there is no reason not to72transmit this signal. */73if ((itv->std_out & V4L2_STD_625_50) && !enabled) {74enabled = 1;75mode = 0x08; /* 4x3 full format */76}77data.id = V4L2_SLICED_WSS_625;78data.field = 0;79data.line = enabled ? 23 : 0;80data.data[0] = mode & 0xff;81data.data[1] = (mode >> 8) & 0xff;82ivtv_call_hw(itv, IVTV_HW_SAA7127, vbi, s_vbi_data, &data);83}8485static int odd_parity(u8 c)86{87c ^= (c >> 4);88c ^= (c >> 2);89c ^= (c >> 1);9091return c & 1;92}9394static void ivtv_write_vbi_line(struct ivtv *itv,95const struct v4l2_sliced_vbi_data *d,96struct vbi_cc *cc, int *found_cc)97{98struct vbi_info *vi = &itv->vbi;99100if (d->id == V4L2_SLICED_CAPTION_525 && d->line == 21) {101if (d->field) {102cc->even[0] = d->data[0];103cc->even[1] = d->data[1];104} else {105cc->odd[0] = d->data[0];106cc->odd[1] = d->data[1];107}108*found_cc = 1;109} else if (d->id == V4L2_SLICED_VPS && d->line == 16 && d->field == 0) {110struct vbi_vps vps;111112vps.data[0] = d->data[2];113vps.data[1] = d->data[8];114vps.data[2] = d->data[9];115vps.data[3] = d->data[10];116vps.data[4] = d->data[11];117if (memcmp(&vps, &vi->vps_payload, sizeof(vps))) {118vi->vps_payload = vps;119set_bit(IVTV_F_I_UPDATE_VPS, &itv->i_flags);120}121} else if (d->id == V4L2_SLICED_WSS_625 &&122d->line == 23 && d->field == 0) {123int wss = d->data[0] | d->data[1] << 8;124125if (vi->wss_payload != wss) {126vi->wss_payload = wss;127set_bit(IVTV_F_I_UPDATE_WSS, &itv->i_flags);128}129}130}131132static void ivtv_write_vbi_cc_lines(struct ivtv *itv, const struct vbi_cc *cc)133{134struct vbi_info *vi = &itv->vbi;135136if (vi->cc_payload_idx < ARRAY_SIZE(vi->cc_payload)) {137memcpy(&vi->cc_payload[vi->cc_payload_idx], cc,138sizeof(struct vbi_cc));139vi->cc_payload_idx++;140set_bit(IVTV_F_I_UPDATE_CC, &itv->i_flags);141}142}143144static void ivtv_write_vbi(struct ivtv *itv,145const struct v4l2_sliced_vbi_data *sliced,146size_t cnt)147{148struct vbi_cc cc = { .odd = { 0x80, 0x80 }, .even = { 0x80, 0x80 } };149int found_cc = 0;150size_t i;151152for (i = 0; i < cnt; i++)153ivtv_write_vbi_line(itv, sliced + i, &cc, &found_cc);154155if (found_cc)156ivtv_write_vbi_cc_lines(itv, &cc);157}158159ssize_t160ivtv_write_vbi_from_user(struct ivtv *itv,161const struct v4l2_sliced_vbi_data __user *sliced,162size_t cnt)163{164struct vbi_cc cc = { .odd = { 0x80, 0x80 }, .even = { 0x80, 0x80 } };165int found_cc = 0;166size_t i;167struct v4l2_sliced_vbi_data d;168ssize_t ret = cnt * sizeof(struct v4l2_sliced_vbi_data);169170for (i = 0; i < cnt; i++) {171if (copy_from_user(&d, sliced + i,172sizeof(struct v4l2_sliced_vbi_data))) {173ret = -EFAULT;174break;175}176ivtv_write_vbi_line(itv, &d, &cc, &found_cc);177}178179if (found_cc)180ivtv_write_vbi_cc_lines(itv, &cc);181182return ret;183}184185static void copy_vbi_data(struct ivtv *itv, int lines, u32 pts_stamp)186{187int line = 0;188int i;189u32 linemask[2] = { 0, 0 };190unsigned short size;191static const u8 mpeg_hdr_data[] = {1920x00, 0x00, 0x01, 0xba, 0x44, 0x00, 0x0c, 0x66,1930x24, 0x01, 0x01, 0xd1, 0xd3, 0xfa, 0xff, 0xff,1940x00, 0x00, 0x01, 0xbd, 0x00, 0x1a, 0x84, 0x80,1950x07, 0x21, 0x00, 0x5d, 0x63, 0xa7, 0xff, 0xff196};197const int sd = sizeof(mpeg_hdr_data); /* start of vbi data */198int idx = itv->vbi.frame % IVTV_VBI_FRAMES;199u8 *dst = &itv->vbi.sliced_mpeg_data[idx][0];200201for (i = 0; i < lines; i++) {202int f, l;203204if (itv->vbi.sliced_data[i].id == 0)205continue;206207l = itv->vbi.sliced_data[i].line - 6;208f = itv->vbi.sliced_data[i].field;209if (f)210l += 18;211if (l < 32)212linemask[0] |= (1 << l);213else214linemask[1] |= (1 << (l - 32));215dst[sd + 12 + line * 43] =216ivtv_service2vbi(itv->vbi.sliced_data[i].id);217memcpy(dst + sd + 12 + line * 43 + 1, itv->vbi.sliced_data[i].data, 42);218line++;219}220memcpy(dst, mpeg_hdr_data, sizeof(mpeg_hdr_data));221if (line == 36) {222/* All lines are used, so there is no space for the linemask223(the max size of the VBI data is 36 * 43 + 4 bytes).224So in this case we use the magic number 'ITV0'. */225memcpy(dst + sd, "ITV0", 4);226memcpy(dst + sd + 4, dst + sd + 12, line * 43);227size = 4 + ((43 * line + 3) & ~3);228} else {229memcpy(dst + sd, "itv0", 4);230cpu_to_le32s(&linemask[0]);231cpu_to_le32s(&linemask[1]);232memcpy(dst + sd + 4, &linemask[0], 8);233size = 12 + ((43 * line + 3) & ~3);234}235dst[4+16] = (size + 10) >> 8;236dst[5+16] = (size + 10) & 0xff;237dst[9+16] = 0x21 | ((pts_stamp >> 29) & 0x6);238dst[10+16] = (pts_stamp >> 22) & 0xff;239dst[11+16] = 1 | ((pts_stamp >> 14) & 0xff);240dst[12+16] = (pts_stamp >> 7) & 0xff;241dst[13+16] = 1 | ((pts_stamp & 0x7f) << 1);242itv->vbi.sliced_mpeg_size[idx] = sd + size;243}244245static int ivtv_convert_ivtv_vbi(struct ivtv *itv, u8 *p)246{247u32 linemask[2];248int i, l, id2;249int line = 0;250251if (!memcmp(p, "itv0", 4)) {252memcpy(linemask, p + 4, 8);253p += 12;254} else if (!memcmp(p, "ITV0", 4)) {255linemask[0] = 0xffffffff;256linemask[1] = 0xf;257p += 4;258} else {259/* unknown VBI data, convert to empty VBI frame */260linemask[0] = linemask[1] = 0;261}262for (i = 0; i < 36; i++) {263int err = 0;264265if (i < 32 && !(linemask[0] & (1 << i)))266continue;267if (i >= 32 && !(linemask[1] & (1 << (i - 32))))268continue;269id2 = *p & 0xf;270switch (id2) {271case IVTV_SLICED_TYPE_TELETEXT_B:272id2 = V4L2_SLICED_TELETEXT_B;273break;274case IVTV_SLICED_TYPE_CAPTION_525:275id2 = V4L2_SLICED_CAPTION_525;276err = !odd_parity(p[1]) || !odd_parity(p[2]);277break;278case IVTV_SLICED_TYPE_VPS:279id2 = V4L2_SLICED_VPS;280break;281case IVTV_SLICED_TYPE_WSS_625:282id2 = V4L2_SLICED_WSS_625;283break;284default:285id2 = 0;286break;287}288if (err == 0) {289l = (i < 18) ? i + 6 : i - 18 + 6;290itv->vbi.sliced_dec_data[line].line = l;291itv->vbi.sliced_dec_data[line].field = i >= 18;292itv->vbi.sliced_dec_data[line].id = id2;293memcpy(itv->vbi.sliced_dec_data[line].data, p + 1, 42);294line++;295}296p += 43;297}298while (line < 36) {299itv->vbi.sliced_dec_data[line].id = 0;300itv->vbi.sliced_dec_data[line].line = 0;301itv->vbi.sliced_dec_data[line].field = 0;302line++;303}304return line * sizeof(itv->vbi.sliced_dec_data[0]);305}306307/* Compress raw VBI format, removes leading SAV codes and surplus space after the308field.309Returns new compressed size. */310static u32 compress_raw_buf(struct ivtv *itv, u8 *buf, u32 size)311{312u32 line_size = itv->vbi.raw_decoder_line_size;313u32 lines = itv->vbi.count;314u8 sav1 = itv->vbi.raw_decoder_sav_odd_field;315u8 sav2 = itv->vbi.raw_decoder_sav_even_field;316u8 *q = buf;317u8 *p;318int i;319320for (i = 0; i < lines; i++) {321p = buf + i * line_size;322323/* Look for SAV code */324if (p[0] != 0xff || p[1] || p[2] || (p[3] != sav1 && p[3] != sav2)) {325break;326}327memcpy(q, p + 4, line_size - 4);328q += line_size - 4;329}330return lines * (line_size - 4);331}332333334/* Compressed VBI format, all found sliced blocks put next to one another335Returns new compressed size */336static u32 compress_sliced_buf(struct ivtv *itv, u32 line, u8 *buf, u32 size, u8 sav)337{338u32 line_size = itv->vbi.sliced_decoder_line_size;339struct v4l2_decode_vbi_line vbi;340int i;341unsigned lines = 0;342343/* find the first valid line */344for (i = 0; i < size; i++, buf++) {345if (buf[0] == 0xff && !buf[1] && !buf[2] && buf[3] == sav)346break;347}348349size -= i;350if (size < line_size) {351return line;352}353for (i = 0; i < size / line_size; i++) {354u8 *p = buf + i * line_size;355356/* Look for SAV code */357if (p[0] != 0xff || p[1] || p[2] || p[3] != sav) {358continue;359}360vbi.p = p + 4;361v4l2_subdev_call(itv->sd_video, vbi, decode_vbi_line, &vbi);362if (vbi.type && !(lines & (1 << vbi.line))) {363lines |= 1 << vbi.line;364itv->vbi.sliced_data[line].id = vbi.type;365itv->vbi.sliced_data[line].field = vbi.is_second_field;366itv->vbi.sliced_data[line].line = vbi.line;367memcpy(itv->vbi.sliced_data[line].data, vbi.p, 42);368line++;369}370}371return line;372}373374void ivtv_process_vbi_data(struct ivtv *itv, struct ivtv_buffer *buf,375u64 pts_stamp, int streamtype)376{377u8 *p = (u8 *) buf->buf;378u32 size = buf->bytesused;379int y;380381/* Raw VBI data */382if (streamtype == IVTV_ENC_STREAM_TYPE_VBI && ivtv_raw_vbi(itv)) {383u8 type;384385ivtv_buf_swap(buf);386387type = p[3];388389size = buf->bytesused = compress_raw_buf(itv, p, size);390391/* second field of the frame? */392if (type == itv->vbi.raw_decoder_sav_even_field) {393/* Dirty hack needed for backwards394compatibility of old VBI software. */395p += size - 4;396memcpy(p, &itv->vbi.frame, 4);397itv->vbi.frame++;398}399return;400}401402/* Sliced VBI data with data insertion */403if (streamtype == IVTV_ENC_STREAM_TYPE_VBI) {404int lines;405406ivtv_buf_swap(buf);407408/* first field */409lines = compress_sliced_buf(itv, 0, p, size / 2,410itv->vbi.sliced_decoder_sav_odd_field);411/* second field */412/* experimentation shows that the second half does not always begin413at the exact address. So start a bit earlier (hence 32). */414lines = compress_sliced_buf(itv, lines, p + size / 2 - 32, size / 2 + 32,415itv->vbi.sliced_decoder_sav_even_field);416/* always return at least one empty line */417if (lines == 0) {418itv->vbi.sliced_data[0].id = 0;419itv->vbi.sliced_data[0].line = 0;420itv->vbi.sliced_data[0].field = 0;421lines = 1;422}423buf->bytesused = size = lines * sizeof(itv->vbi.sliced_data[0]);424memcpy(p, &itv->vbi.sliced_data[0], size);425426if (itv->vbi.insert_mpeg) {427copy_vbi_data(itv, lines, pts_stamp);428}429itv->vbi.frame++;430return;431}432433/* Sliced VBI re-inserted from an MPEG stream */434if (streamtype == IVTV_DEC_STREAM_TYPE_VBI) {435/* If the size is not 4-byte aligned, then the starting address436for the swapping is also shifted. After swapping the data the437real start address of the VBI data is exactly 4 bytes after the438original start. It's a bit fiddly but it works like a charm.439Non-4-byte alignment happens when an lseek is done on the input440mpeg file to a non-4-byte aligned position. So on arrival here441the VBI data is also non-4-byte aligned. */442int offset = size & 3;443int cnt;444445if (offset) {446p += 4 - offset;447}448/* Swap Buffer */449for (y = 0; y < size; y += 4) {450swab32s((u32 *)(p + y));451}452453cnt = ivtv_convert_ivtv_vbi(itv, p + offset);454memcpy(buf->buf, itv->vbi.sliced_dec_data, cnt);455buf->bytesused = cnt;456457ivtv_write_vbi(itv, itv->vbi.sliced_dec_data,458cnt / sizeof(itv->vbi.sliced_dec_data[0]));459return;460}461}462463void ivtv_disable_cc(struct ivtv *itv)464{465struct vbi_cc cc = { .odd = { 0x80, 0x80 }, .even = { 0x80, 0x80 } };466467clear_bit(IVTV_F_I_UPDATE_CC, &itv->i_flags);468ivtv_set_cc(itv, 0, &cc);469itv->vbi.cc_payload_idx = 0;470}471472473void ivtv_vbi_work_handler(struct ivtv *itv)474{475struct vbi_info *vi = &itv->vbi;476struct v4l2_sliced_vbi_data data;477struct vbi_cc cc = { .odd = { 0x80, 0x80 }, .even = { 0x80, 0x80 } };478479/* Lock */480if (itv->output_mode == OUT_PASSTHROUGH) {481if (itv->is_50hz) {482data.id = V4L2_SLICED_WSS_625;483data.field = 0;484485if (v4l2_subdev_call(itv->sd_video, vbi, g_vbi_data, &data) == 0) {486ivtv_set_wss(itv, 1, data.data[0] & 0xf);487vi->wss_missing_cnt = 0;488} else if (vi->wss_missing_cnt == 4) {489ivtv_set_wss(itv, 1, 0x8); /* 4x3 full format */490} else {491vi->wss_missing_cnt++;492}493}494else {495int mode = 0;496497data.id = V4L2_SLICED_CAPTION_525;498data.field = 0;499if (v4l2_subdev_call(itv->sd_video, vbi, g_vbi_data, &data) == 0) {500mode |= 1;501cc.odd[0] = data.data[0];502cc.odd[1] = data.data[1];503}504data.field = 1;505if (v4l2_subdev_call(itv->sd_video, vbi, g_vbi_data, &data) == 0) {506mode |= 2;507cc.even[0] = data.data[0];508cc.even[1] = data.data[1];509}510if (mode) {511vi->cc_missing_cnt = 0;512ivtv_set_cc(itv, mode, &cc);513} else if (vi->cc_missing_cnt == 4) {514ivtv_set_cc(itv, 0, &cc);515} else {516vi->cc_missing_cnt++;517}518}519return;520}521522if (test_and_clear_bit(IVTV_F_I_UPDATE_WSS, &itv->i_flags)) {523ivtv_set_wss(itv, 1, vi->wss_payload & 0xf);524}525526if (test_bit(IVTV_F_I_UPDATE_CC, &itv->i_flags)) {527if (vi->cc_payload_idx == 0) {528clear_bit(IVTV_F_I_UPDATE_CC, &itv->i_flags);529ivtv_set_cc(itv, 3, &cc);530}531while (vi->cc_payload_idx) {532cc = vi->cc_payload[0];533534memcpy(vi->cc_payload, vi->cc_payload + 1,535sizeof(vi->cc_payload) - sizeof(vi->cc_payload[0]));536vi->cc_payload_idx--;537if (vi->cc_payload_idx && cc.odd[0] == 0x80 && cc.odd[1] == 0x80)538continue;539540ivtv_set_cc(itv, 3, &cc);541break;542}543}544545if (test_and_clear_bit(IVTV_F_I_UPDATE_VPS, &itv->i_flags)) {546ivtv_set_vps(itv, 1);547}548}549550551