Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1 | /* |
| 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" |
| 28 | #include "cx18-av-core.h" |
| 29 | |
Andy Walls | 302df97 | 2009-01-31 00:33:02 -0300 | [diff] [blame] | 30 | /* |
| 31 | * Raster Reference/Protection (RP) bytes, used in Start/End Active |
| 32 | * Video codes emitted from the digitzer in VIP 1.x mode, that flag the start |
| 33 | * of VBI sample or VBI ancilliary data regions in the digitial ratser line. |
| 34 | * |
| 35 | * Task FieldEven VerticalBlank HorizontalBlank 0 0 0 0 |
| 36 | */ |
| 37 | static const u8 raw_vbi_sav_rp[2] = { 0x20, 0x60 }; /* __V_, _FV_ */ |
| 38 | static const u8 sliced_vbi_eav_rp[2] = { 0xb0, 0xf0 }; /* T_VH, TFVH */ |
| 39 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 40 | static void copy_vbi_data(struct cx18 *cx, int lines, u32 pts_stamp) |
| 41 | { |
| 42 | int line = 0; |
| 43 | int i; |
| 44 | u32 linemask[2] = { 0, 0 }; |
| 45 | unsigned short size; |
| 46 | static const u8 mpeg_hdr_data[] = { |
Andy Walls | 302df97 | 2009-01-31 00:33:02 -0300 | [diff] [blame] | 47 | /* MPEG-2 Program Pack */ |
| 48 | 0x00, 0x00, 0x01, 0xba, /* Prog Pack start code */ |
| 49 | 0x44, 0x00, 0x0c, 0x66, 0x24, 0x01, /* SCR, SCR Ext, markers */ |
| 50 | 0x01, 0xd1, 0xd3, /* Mux Rate, markers */ |
| 51 | 0xfa, 0xff, 0xff, /* Res, Suff cnt, Stuff */ |
| 52 | /* MPEG-2 Private Stream 1 PES Packet */ |
| 53 | 0x00, 0x00, 0x01, 0xbd, /* Priv Stream 1 start */ |
| 54 | 0x00, 0x1a, /* length */ |
| 55 | 0x84, 0x80, 0x07, /* flags, hdr data len */ |
| 56 | 0x21, 0x00, 0x5d, 0x63, 0xa7, /* PTS, markers */ |
| 57 | 0xff, 0xff /* stuffing */ |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 58 | }; |
| 59 | const int sd = sizeof(mpeg_hdr_data); /* start of vbi data */ |
| 60 | int idx = cx->vbi.frame % CX18_VBI_FRAMES; |
| 61 | u8 *dst = &cx->vbi.sliced_mpeg_data[idx][0]; |
| 62 | |
| 63 | for (i = 0; i < lines; i++) { |
| 64 | struct v4l2_sliced_vbi_data *sdata = cx->vbi.sliced_data + i; |
| 65 | int f, l; |
| 66 | |
| 67 | if (sdata->id == 0) |
| 68 | continue; |
| 69 | |
| 70 | l = sdata->line - 6; |
| 71 | f = sdata->field; |
| 72 | if (f) |
| 73 | l += 18; |
| 74 | if (l < 32) |
| 75 | linemask[0] |= (1 << l); |
| 76 | else |
| 77 | linemask[1] |= (1 << (l - 32)); |
Mauro Carvalho Chehab | aed6abd | 2008-04-29 21:38:51 -0300 | [diff] [blame] | 78 | dst[sd + 12 + line * 43] = cx18_service2vbi(sdata->id); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 79 | memcpy(dst + sd + 12 + line * 43 + 1, sdata->data, 42); |
| 80 | line++; |
| 81 | } |
| 82 | memcpy(dst, mpeg_hdr_data, sizeof(mpeg_hdr_data)); |
| 83 | if (line == 36) { |
| 84 | /* All lines are used, so there is no space for the linemask |
| 85 | (the max size of the VBI data is 36 * 43 + 4 bytes). |
| 86 | So in this case we use the magic number 'ITV0'. */ |
| 87 | memcpy(dst + sd, "ITV0", 4); |
| 88 | memcpy(dst + sd + 4, dst + sd + 12, line * 43); |
| 89 | size = 4 + ((43 * line + 3) & ~3); |
| 90 | } else { |
Andy Walls | 302df97 | 2009-01-31 00:33:02 -0300 | [diff] [blame] | 91 | memcpy(dst + sd, "itv0", 4); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 92 | memcpy(dst + sd + 4, &linemask[0], 8); |
| 93 | size = 12 + ((43 * line + 3) & ~3); |
| 94 | } |
| 95 | dst[4+16] = (size + 10) >> 8; |
| 96 | dst[5+16] = (size + 10) & 0xff; |
| 97 | dst[9+16] = 0x21 | ((pts_stamp >> 29) & 0x6); |
| 98 | dst[10+16] = (pts_stamp >> 22) & 0xff; |
| 99 | dst[11+16] = 1 | ((pts_stamp >> 14) & 0xff); |
| 100 | dst[12+16] = (pts_stamp >> 7) & 0xff; |
| 101 | dst[13+16] = 1 | ((pts_stamp & 0x7f) << 1); |
| 102 | cx->vbi.sliced_mpeg_size[idx] = sd + size; |
| 103 | } |
| 104 | |
| 105 | /* Compress raw VBI format, removes leading SAV codes and surplus space |
Andy Walls | 466df46 | 2009-02-07 15:47:28 -0300 | [diff] [blame] | 106 | after the frame. Returns new compressed size. */ |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 107 | static u32 compress_raw_buf(struct cx18 *cx, u8 *buf, u32 size) |
| 108 | { |
Andy Walls | 302df97 | 2009-01-31 00:33:02 -0300 | [diff] [blame] | 109 | u32 line_size = vbi_active_samples; |
Andy Walls | 466df46 | 2009-02-07 15:47:28 -0300 | [diff] [blame] | 110 | u32 lines = cx->vbi.count * 2; |
Andy Walls | 302df97 | 2009-01-31 00:33:02 -0300 | [diff] [blame] | 111 | u8 sav1 = raw_vbi_sav_rp[0]; |
| 112 | u8 sav2 = raw_vbi_sav_rp[1]; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 113 | u8 *q = buf; |
| 114 | u8 *p; |
| 115 | int i; |
| 116 | |
| 117 | for (i = 0; i < lines; i++) { |
| 118 | p = buf + i * line_size; |
| 119 | |
| 120 | /* Look for SAV code */ |
| 121 | if (p[0] != 0xff || p[1] || p[2] || |
| 122 | (p[3] != sav1 && p[3] != sav2)) |
| 123 | break; |
| 124 | memcpy(q, p + 4, line_size - 4); |
| 125 | q += line_size - 4; |
| 126 | } |
| 127 | return lines * (line_size - 4); |
| 128 | } |
| 129 | |
| 130 | |
| 131 | /* Compressed VBI format, all found sliced blocks put next to one another |
| 132 | Returns new compressed size */ |
| 133 | static u32 compress_sliced_buf(struct cx18 *cx, u32 line, u8 *buf, |
Andy Walls | 302df97 | 2009-01-31 00:33:02 -0300 | [diff] [blame] | 134 | u32 size, u8 eav) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 135 | { |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 136 | struct v4l2_decode_vbi_line vbi; |
| 137 | int i; |
Andy Walls | 302df97 | 2009-01-31 00:33:02 -0300 | [diff] [blame] | 138 | u32 line_size = cx->is_60hz ? vbi_hblank_samples_60Hz |
| 139 | : vbi_hblank_samples_50Hz; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 140 | |
| 141 | /* find the first valid line */ |
| 142 | for (i = 0; i < size; i++, buf++) { |
Andy Walls | 302df97 | 2009-01-31 00:33:02 -0300 | [diff] [blame] | 143 | if (buf[0] == 0xff && !buf[1] && !buf[2] && buf[3] == eav) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 144 | break; |
| 145 | } |
| 146 | |
| 147 | size -= i; |
| 148 | if (size < line_size) |
| 149 | return line; |
| 150 | for (i = 0; i < size / line_size; i++) { |
| 151 | u8 *p = buf + i * line_size; |
| 152 | |
Andy Walls | 302df97 | 2009-01-31 00:33:02 -0300 | [diff] [blame] | 153 | /* Look for EAV code */ |
| 154 | if (p[0] != 0xff || p[1] || p[2] || p[3] != eav) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 155 | continue; |
| 156 | vbi.p = p + 4; |
| 157 | cx18_av_cmd(cx, VIDIOC_INT_DECODE_VBI_LINE, &vbi); |
| 158 | if (vbi.type) { |
| 159 | cx->vbi.sliced_data[line].id = vbi.type; |
| 160 | cx->vbi.sliced_data[line].field = vbi.is_second_field; |
| 161 | cx->vbi.sliced_data[line].line = vbi.line; |
| 162 | memcpy(cx->vbi.sliced_data[line].data, vbi.p, 42); |
| 163 | line++; |
| 164 | } |
| 165 | } |
| 166 | return line; |
| 167 | } |
| 168 | |
| 169 | void cx18_process_vbi_data(struct cx18 *cx, struct cx18_buffer *buf, |
Andy Walls | 776fa86 | 2009-02-01 21:42:12 -0300 | [diff] [blame] | 170 | int streamtype) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 171 | { |
| 172 | u8 *p = (u8 *) buf->buf; |
Andy Walls | 776fa86 | 2009-02-01 21:42:12 -0300 | [diff] [blame] | 173 | u32 *q = (u32 *) buf->buf; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 174 | u32 size = buf->bytesused; |
Andy Walls | 776fa86 | 2009-02-01 21:42:12 -0300 | [diff] [blame] | 175 | u32 pts; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 176 | int lines; |
| 177 | |
| 178 | if (streamtype != CX18_ENC_STREAM_TYPE_VBI) |
| 179 | return; |
| 180 | |
Andy Walls | 01cbc21 | 2009-02-07 01:31:22 -0300 | [diff] [blame] | 181 | /* |
| 182 | * The CX23418 sends us data that is 32 bit LE swapped, but we want |
| 183 | * the raw VBI bytes in the order they were in the raster line |
| 184 | */ |
Andy Walls | 776fa86 | 2009-02-01 21:42:12 -0300 | [diff] [blame] | 185 | cx18_buf_swap(buf); |
| 186 | |
Andy Walls | 302df97 | 2009-01-31 00:33:02 -0300 | [diff] [blame] | 187 | /* |
Andy Walls | 776fa86 | 2009-02-01 21:42:12 -0300 | [diff] [blame] | 188 | * The CX23418 provides a 12 byte header in it's raw VBI buffers to us: |
| 189 | * 0x3fffffff [4 bytes of something] [4 byte presentation time stamp?] |
Andy Walls | 302df97 | 2009-01-31 00:33:02 -0300 | [diff] [blame] | 190 | */ |
| 191 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 192 | /* Raw VBI data */ |
Andy Walls | dd07343 | 2008-12-12 16:24:04 -0300 | [diff] [blame] | 193 | if (cx18_raw_vbi(cx)) { |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 194 | u8 type; |
| 195 | |
Andy Walls | 01cbc21 | 2009-02-07 01:31:22 -0300 | [diff] [blame] | 196 | /* |
Andy Walls | 466df46 | 2009-02-07 15:47:28 -0300 | [diff] [blame] | 197 | * We've set up to get a frame's worth of VBI data at a time. |
| 198 | * Skip 12 bytes of header prefixing the first field. |
Andy Walls | 01cbc21 | 2009-02-07 01:31:22 -0300 | [diff] [blame] | 199 | */ |
Andy Walls | af009cf | 2008-12-12 20:00:29 -0300 | [diff] [blame] | 200 | size -= 12; |
| 201 | memcpy(p, &buf->buf[12], size); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 202 | type = p[3]; |
| 203 | |
Andy Walls | 466df46 | 2009-02-07 15:47:28 -0300 | [diff] [blame] | 204 | /* Extrapolate the last 12 bytes of the frame's last line */ |
Andy Walls | 01cbc21 | 2009-02-07 01:31:22 -0300 | [diff] [blame] | 205 | memset(&p[size], (int) p[size - 1], 12); |
Andy Walls | 466df46 | 2009-02-07 15:47:28 -0300 | [diff] [blame] | 206 | size += 12; |
Andy Walls | 01cbc21 | 2009-02-07 01:31:22 -0300 | [diff] [blame] | 207 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 208 | size = buf->bytesused = compress_raw_buf(cx, p, size); |
| 209 | |
Andy Walls | 466df46 | 2009-02-07 15:47:28 -0300 | [diff] [blame] | 210 | /* |
| 211 | * Hack needed for compatibility with old VBI software. |
| 212 | * Write the frame # at the last 4 bytes of the frame |
| 213 | */ |
| 214 | p += size - 4; |
| 215 | memcpy(p, &cx->vbi.frame, 4); |
| 216 | cx->vbi.frame++; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 217 | return; |
| 218 | } |
| 219 | |
| 220 | /* Sliced VBI data with data insertion */ |
Andy Walls | 776fa86 | 2009-02-01 21:42:12 -0300 | [diff] [blame] | 221 | |
Andy Walls | 01cbc21 | 2009-02-07 01:31:22 -0300 | [diff] [blame] | 222 | pts = (be32_to_cpu(q[0] == 0x3fffffff)) ? be32_to_cpu(q[2]) : 0; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 223 | |
Andy Walls | 812b1f9 | 2009-02-08 22:40:04 -0300 | [diff] [blame] | 224 | /* |
| 225 | * For calls to compress_sliced_buf(), ensure there are an integral |
| 226 | * number of lines by shifting the real data up over the 12 bytes header |
| 227 | * that got stuffed in. |
| 228 | * FIXME - there's a smarter way to do this with pointers, but for some |
| 229 | * reason I can't get it to work correctly right now. |
| 230 | */ |
| 231 | memcpy(p, &buf->buf[12], size-12); |
| 232 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 233 | /* first field */ |
Andy Walls | 302df97 | 2009-01-31 00:33:02 -0300 | [diff] [blame] | 234 | lines = compress_sliced_buf(cx, 0, p, size / 2, sliced_vbi_eav_rp[0]); |
Andy Walls | 812b1f9 | 2009-02-08 22:40:04 -0300 | [diff] [blame] | 235 | /* |
| 236 | * second field |
| 237 | * In case the second half does not always begin at the exact address, |
| 238 | * start a bit earlier (hence 32). |
| 239 | */ |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 240 | lines = compress_sliced_buf(cx, lines, p + size / 2 - 32, |
Andy Walls | 302df97 | 2009-01-31 00:33:02 -0300 | [diff] [blame] | 241 | size / 2 + 32, sliced_vbi_eav_rp[1]); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 242 | /* always return at least one empty line */ |
| 243 | if (lines == 0) { |
| 244 | cx->vbi.sliced_data[0].id = 0; |
| 245 | cx->vbi.sliced_data[0].line = 0; |
| 246 | cx->vbi.sliced_data[0].field = 0; |
| 247 | lines = 1; |
| 248 | } |
| 249 | buf->bytesused = size = lines * sizeof(cx->vbi.sliced_data[0]); |
| 250 | memcpy(p, &cx->vbi.sliced_data[0], size); |
| 251 | |
| 252 | if (cx->vbi.insert_mpeg) |
Andy Walls | 776fa86 | 2009-02-01 21:42:12 -0300 | [diff] [blame] | 253 | copy_vbi_data(cx, lines, pts); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 254 | cx->vbi.frame++; |
| 255 | } |