Path: blob/master/drivers/media/video/cx18/cx18-vbi.c
17780 views
/*1* cx18 Vertical Blank Interval support functions2*3* Derived from ivtv-vbi.c4*5* Copyright (C) 2007 Hans Verkuil <[email protected]>6*7* This program is free software; you can redistribute it and/or modify8* it under the terms of the GNU General Public License as published by9* the Free Software Foundation; either version 2 of the License, or10* (at your option) any later version.11*12* This program is distributed in the hope that it will be useful,13* but WITHOUT ANY WARRANTY; without even the implied warranty of14* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the15* GNU General Public License for more details.16*17* You should have received a copy of the GNU General Public License18* along with this program; if not, write to the Free Software19* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA20* 02111-1307 USA21*/2223#include "cx18-driver.h"24#include "cx18-vbi.h"25#include "cx18-ioctl.h"26#include "cx18-queue.h"2728/*29* Raster Reference/Protection (RP) bytes, used in Start/End Active30* Video codes emitted from the digitzer in VIP 1.x mode, that flag the start31* of VBI sample or VBI ancillary data regions in the digitial ratser line.32*33* Task FieldEven VerticalBlank HorizontalBlank 0 0 0 034*/35static const u8 raw_vbi_sav_rp[2] = { 0x20, 0x60 }; /* __V_, _FV_ */36static const u8 sliced_vbi_eav_rp[2] = { 0xb0, 0xf0 }; /* T_VH, TFVH */3738static void copy_vbi_data(struct cx18 *cx, int lines, u32 pts_stamp)39{40int line = 0;41int i;42u32 linemask[2] = { 0, 0 };43unsigned short size;44static const u8 mpeg_hdr_data[] = {45/* MPEG-2 Program Pack */460x00, 0x00, 0x01, 0xba, /* Prog Pack start code */470x44, 0x00, 0x0c, 0x66, 0x24, 0x01, /* SCR, SCR Ext, markers */480x01, 0xd1, 0xd3, /* Mux Rate, markers */490xfa, 0xff, 0xff, /* Res, Suff cnt, Stuff */50/* MPEG-2 Private Stream 1 PES Packet */510x00, 0x00, 0x01, 0xbd, /* Priv Stream 1 start */520x00, 0x1a, /* length */530x84, 0x80, 0x07, /* flags, hdr data len */540x21, 0x00, 0x5d, 0x63, 0xa7, /* PTS, markers */550xff, 0xff /* stuffing */56};57const int sd = sizeof(mpeg_hdr_data); /* start of vbi data */58int idx = cx->vbi.frame % CX18_VBI_FRAMES;59u8 *dst = &cx->vbi.sliced_mpeg_data[idx][0];6061for (i = 0; i < lines; i++) {62struct v4l2_sliced_vbi_data *sdata = cx->vbi.sliced_data + i;63int f, l;6465if (sdata->id == 0)66continue;6768l = sdata->line - 6;69f = sdata->field;70if (f)71l += 18;72if (l < 32)73linemask[0] |= (1 << l);74else75linemask[1] |= (1 << (l - 32));76dst[sd + 12 + line * 43] = cx18_service2vbi(sdata->id);77memcpy(dst + sd + 12 + line * 43 + 1, sdata->data, 42);78line++;79}80memcpy(dst, mpeg_hdr_data, sizeof(mpeg_hdr_data));81if (line == 36) {82/* All lines are used, so there is no space for the linemask83(the max size of the VBI data is 36 * 43 + 4 bytes).84So in this case we use the magic number 'ITV0'. */85memcpy(dst + sd, "ITV0", 4);86memcpy(dst + sd + 4, dst + sd + 12, line * 43);87size = 4 + ((43 * line + 3) & ~3);88} else {89memcpy(dst + sd, "itv0", 4);90cpu_to_le32s(&linemask[0]);91cpu_to_le32s(&linemask[1]);92memcpy(dst + sd + 4, &linemask[0], 8);93size = 12 + ((43 * line + 3) & ~3);94}95dst[4+16] = (size + 10) >> 8;96dst[5+16] = (size + 10) & 0xff;97dst[9+16] = 0x21 | ((pts_stamp >> 29) & 0x6);98dst[10+16] = (pts_stamp >> 22) & 0xff;99dst[11+16] = 1 | ((pts_stamp >> 14) & 0xff);100dst[12+16] = (pts_stamp >> 7) & 0xff;101dst[13+16] = 1 | ((pts_stamp & 0x7f) << 1);102cx->vbi.sliced_mpeg_size[idx] = sd + size;103}104105/* Compress raw VBI format, removes leading SAV codes and surplus space106after the frame. Returns new compressed size. */107/* FIXME - this function ignores the input size. */108static u32 compress_raw_buf(struct cx18 *cx, u8 *buf, u32 size, u32 hdr_size)109{110u32 line_size = vbi_active_samples;111u32 lines = cx->vbi.count * 2;112u8 *q = buf;113u8 *p;114int i;115116/* Skip the header */117buf += hdr_size;118119for (i = 0; i < lines; i++) {120p = buf + i * line_size;121122/* Look for SAV code */123if (p[0] != 0xff || p[1] || p[2] ||124(p[3] != raw_vbi_sav_rp[0] &&125p[3] != raw_vbi_sav_rp[1]))126break;127if (i == lines - 1) {128/* last line is hdr_size bytes short - extrapolate it */129memcpy(q, p + 4, line_size - 4 - hdr_size);130q += line_size - 4 - hdr_size;131p += line_size - hdr_size - 1;132memset(q, (int) *p, hdr_size);133} else {134memcpy(q, p + 4, line_size - 4);135q += line_size - 4;136}137}138return lines * (line_size - 4);139}140141static u32 compress_sliced_buf(struct cx18 *cx, u8 *buf, u32 size,142const u32 hdr_size)143{144struct v4l2_decode_vbi_line vbi;145int i;146u32 line = 0;147u32 line_size = cx->is_60hz ? vbi_hblank_samples_60Hz148: vbi_hblank_samples_50Hz;149150/* find the first valid line */151for (i = hdr_size, buf += hdr_size; i < size; i++, buf++) {152if (buf[0] == 0xff && !buf[1] && !buf[2] &&153(buf[3] == sliced_vbi_eav_rp[0] ||154buf[3] == sliced_vbi_eav_rp[1]))155break;156}157158/*159* The last line is short by hdr_size bytes, but for the remaining160* checks against size, we pretend that it is not, by counting the161* header bytes we knowingly skipped162*/163size -= (i - hdr_size);164if (size < line_size)165return line;166167for (i = 0; i < size / line_size; i++) {168u8 *p = buf + i * line_size;169170/* Look for EAV code */171if (p[0] != 0xff || p[1] || p[2] ||172(p[3] != sliced_vbi_eav_rp[0] &&173p[3] != sliced_vbi_eav_rp[1]))174continue;175vbi.p = p + 4;176v4l2_subdev_call(cx->sd_av, vbi, decode_vbi_line, &vbi);177if (vbi.type) {178cx->vbi.sliced_data[line].id = vbi.type;179cx->vbi.sliced_data[line].field = vbi.is_second_field;180cx->vbi.sliced_data[line].line = vbi.line;181memcpy(cx->vbi.sliced_data[line].data, vbi.p, 42);182line++;183}184}185return line;186}187188static void _cx18_process_vbi_data(struct cx18 *cx, struct cx18_buffer *buf)189{190/*191* The CX23418 provides a 12 byte header in its raw VBI buffers to us:192* 0x3fffffff [4 bytes of something] [4 byte presentation time stamp]193*/194struct vbi_data_hdr {195__be32 magic;196__be32 unknown;197__be32 pts;198} *hdr = (struct vbi_data_hdr *) buf->buf;199200u8 *p = (u8 *) buf->buf;201u32 size = buf->bytesused;202u32 pts;203int lines;204205/*206* The CX23418 sends us data that is 32 bit little-endian swapped,207* but we want the raw VBI bytes in the order they were in the raster208* line. This has a side effect of making the header big endian209*/210cx18_buf_swap(buf);211212/* Raw VBI data */213if (cx18_raw_vbi(cx)) {214215size = buf->bytesused =216compress_raw_buf(cx, p, size, sizeof(struct vbi_data_hdr));217218/*219* Hack needed for compatibility with old VBI software.220* Write the frame # at the last 4 bytes of the frame221*/222p += size - 4;223memcpy(p, &cx->vbi.frame, 4);224cx->vbi.frame++;225return;226}227228/* Sliced VBI data with data insertion */229230pts = (be32_to_cpu(hdr->magic) == 0x3fffffff) ? be32_to_cpu(hdr->pts)231: 0;232233lines = compress_sliced_buf(cx, p, size, sizeof(struct vbi_data_hdr));234235/* always return at least one empty line */236if (lines == 0) {237cx->vbi.sliced_data[0].id = 0;238cx->vbi.sliced_data[0].line = 0;239cx->vbi.sliced_data[0].field = 0;240lines = 1;241}242buf->bytesused = size = lines * sizeof(cx->vbi.sliced_data[0]);243memcpy(p, &cx->vbi.sliced_data[0], size);244245if (cx->vbi.insert_mpeg)246copy_vbi_data(cx, lines, pts);247cx->vbi.frame++;248}249250void cx18_process_vbi_data(struct cx18 *cx, struct cx18_mdl *mdl,251int streamtype)252{253struct cx18_buffer *buf;254u32 orig_used;255256if (streamtype != CX18_ENC_STREAM_TYPE_VBI)257return;258259/*260* Big assumption here:261* Every buffer hooked to the MDL's buf_list is a complete VBI frame262* that ends at the end of the buffer.263*264* To assume anything else would make the code in this file265* more complex, or require extra memcpy()'s to make the266* buffers satisfy the above assumption. It's just simpler to set267* up the encoder buffer transfers to make the assumption true.268*/269list_for_each_entry(buf, &mdl->buf_list, list) {270orig_used = buf->bytesused;271if (orig_used == 0)272break;273_cx18_process_vbi_data(cx, buf);274mdl->bytesused -= (orig_used - buf->bytesused);275}276}277278279