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" |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 28 | |
Andy Walls | 302df97 | 2009-01-31 00:33:02 -0300 | [diff] [blame] | 29 | /* |
| 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 |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 32 | * of VBI sample or VBI ancillary data regions in the digitial ratser line. |
Andy Walls | 302df97 | 2009-01-31 00:33:02 -0300 | [diff] [blame] | 33 | * |
| 34 | * Task FieldEven VerticalBlank HorizontalBlank 0 0 0 0 |
| 35 | */ |
| 36 | static const u8 raw_vbi_sav_rp[2] = { 0x20, 0x60 }; /* __V_, _FV_ */ |
| 37 | static const u8 sliced_vbi_eav_rp[2] = { 0xb0, 0xf0 }; /* T_VH, TFVH */ |
| 38 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 39 | static 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 Walls | 302df97 | 2009-01-31 00:33:02 -0300 | [diff] [blame] | 46 | /* 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 Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 57 | }; |
| 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 Chehab | aed6abd | 2008-04-29 21:38:51 -0300 | [diff] [blame] | 77 | dst[sd + 12 + line * 43] = cx18_service2vbi(sdata->id); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 78 | 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); |
Nickolai Zeldovich | bb71b14 | 2013-01-07 22:28:05 -0300 | [diff] [blame] | 87 | memmove(dst + sd + 4, dst + sd + 12, line * 43); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 88 | size = 4 + ((43 * line + 3) & ~3); |
| 89 | } else { |
Andy Walls | 302df97 | 2009-01-31 00:33:02 -0300 | [diff] [blame] | 90 | memcpy(dst + sd, "itv0", 4); |
Andy Walls | 6e1a6372 | 2009-03-14 20:03:26 -0300 | [diff] [blame] | 91 | cpu_to_le32s(&linemask[0]); |
| 92 | cpu_to_le32s(&linemask[1]); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 93 | 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 Walls | 466df46 | 2009-02-07 15:47:28 -0300 | [diff] [blame] | 107 | after the frame. Returns new compressed size. */ |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 108 | /* FIXME - this function ignores the input size. */ |
Andy Walls | 4ab9b25 | 2009-03-14 23:20:49 -0300 | [diff] [blame] | 109 | static u32 compress_raw_buf(struct cx18 *cx, u8 *buf, u32 size, u32 hdr_size) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 110 | { |
Andy Walls | 302df97 | 2009-01-31 00:33:02 -0300 | [diff] [blame] | 111 | u32 line_size = vbi_active_samples; |
Andy Walls | 466df46 | 2009-02-07 15:47:28 -0300 | [diff] [blame] | 112 | u32 lines = cx->vbi.count * 2; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 113 | u8 *q = buf; |
| 114 | u8 *p; |
| 115 | int i; |
| 116 | |
Andy Walls | 4ab9b25 | 2009-03-14 23:20:49 -0300 | [diff] [blame] | 117 | /* Skip the header */ |
| 118 | buf += hdr_size; |
| 119 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 120 | for (i = 0; i < lines; i++) { |
| 121 | p = buf + i * line_size; |
| 122 | |
| 123 | /* Look for SAV code */ |
| 124 | if (p[0] != 0xff || p[1] || p[2] || |
Andy Walls | 4ab9b25 | 2009-03-14 23:20:49 -0300 | [diff] [blame] | 125 | (p[3] != raw_vbi_sav_rp[0] && |
| 126 | p[3] != raw_vbi_sav_rp[1])) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 127 | break; |
Andy Walls | 4ab9b25 | 2009-03-14 23:20:49 -0300 | [diff] [blame] | 128 | if (i == lines - 1) { |
| 129 | /* last line is hdr_size bytes short - extrapolate it */ |
| 130 | memcpy(q, p + 4, line_size - 4 - hdr_size); |
| 131 | q += line_size - 4 - hdr_size; |
| 132 | p += line_size - hdr_size - 1; |
| 133 | memset(q, (int) *p, hdr_size); |
| 134 | } else { |
| 135 | memcpy(q, p + 4, line_size - 4); |
| 136 | q += line_size - 4; |
| 137 | } |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 138 | } |
| 139 | return lines * (line_size - 4); |
| 140 | } |
| 141 | |
Andy Walls | 4ab9b25 | 2009-03-14 23:20:49 -0300 | [diff] [blame] | 142 | static u32 compress_sliced_buf(struct cx18 *cx, u8 *buf, u32 size, |
| 143 | const u32 hdr_size) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 144 | { |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 145 | struct v4l2_decode_vbi_line vbi; |
| 146 | int i; |
Andy Walls | 4ab9b25 | 2009-03-14 23:20:49 -0300 | [diff] [blame] | 147 | u32 line = 0; |
Andy Walls | 302df97 | 2009-01-31 00:33:02 -0300 | [diff] [blame] | 148 | u32 line_size = cx->is_60hz ? vbi_hblank_samples_60Hz |
| 149 | : vbi_hblank_samples_50Hz; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 150 | |
| 151 | /* find the first valid line */ |
Andy Walls | 4ab9b25 | 2009-03-14 23:20:49 -0300 | [diff] [blame] | 152 | for (i = hdr_size, buf += hdr_size; i < size; i++, buf++) { |
| 153 | if (buf[0] == 0xff && !buf[1] && !buf[2] && |
| 154 | (buf[3] == sliced_vbi_eav_rp[0] || |
| 155 | buf[3] == sliced_vbi_eav_rp[1])) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 156 | break; |
| 157 | } |
| 158 | |
Andy Walls | 4ab9b25 | 2009-03-14 23:20:49 -0300 | [diff] [blame] | 159 | /* |
| 160 | * The last line is short by hdr_size bytes, but for the remaining |
| 161 | * checks against size, we pretend that it is not, by counting the |
| 162 | * header bytes we knowingly skipped |
| 163 | */ |
| 164 | size -= (i - hdr_size); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 165 | if (size < line_size) |
| 166 | return line; |
Andy Walls | 4ab9b25 | 2009-03-14 23:20:49 -0300 | [diff] [blame] | 167 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 168 | for (i = 0; i < size / line_size; i++) { |
| 169 | u8 *p = buf + i * line_size; |
| 170 | |
Andy Walls | 302df97 | 2009-01-31 00:33:02 -0300 | [diff] [blame] | 171 | /* Look for EAV code */ |
Andy Walls | 4ab9b25 | 2009-03-14 23:20:49 -0300 | [diff] [blame] | 172 | if (p[0] != 0xff || p[1] || p[2] || |
| 173 | (p[3] != sliced_vbi_eav_rp[0] && |
| 174 | p[3] != sliced_vbi_eav_rp[1])) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 175 | continue; |
| 176 | vbi.p = p + 4; |
Hans Verkuil | 32cd527 | 2010-03-14 09:57:30 -0300 | [diff] [blame] | 177 | v4l2_subdev_call(cx->sd_av, vbi, decode_vbi_line, &vbi); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 178 | if (vbi.type) { |
| 179 | cx->vbi.sliced_data[line].id = vbi.type; |
| 180 | cx->vbi.sliced_data[line].field = vbi.is_second_field; |
| 181 | cx->vbi.sliced_data[line].line = vbi.line; |
| 182 | memcpy(cx->vbi.sliced_data[line].data, vbi.p, 42); |
| 183 | line++; |
| 184 | } |
| 185 | } |
| 186 | return line; |
| 187 | } |
| 188 | |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 189 | static void _cx18_process_vbi_data(struct cx18 *cx, struct cx18_buffer *buf) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 190 | { |
Andy Walls | 4ab9b25 | 2009-03-14 23:20:49 -0300 | [diff] [blame] | 191 | /* |
| 192 | * The CX23418 provides a 12 byte header in its raw VBI buffers to us: |
| 193 | * 0x3fffffff [4 bytes of something] [4 byte presentation time stamp] |
| 194 | */ |
| 195 | struct vbi_data_hdr { |
| 196 | __be32 magic; |
| 197 | __be32 unknown; |
| 198 | __be32 pts; |
| 199 | } *hdr = (struct vbi_data_hdr *) buf->buf; |
| 200 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 201 | u8 *p = (u8 *) buf->buf; |
| 202 | u32 size = buf->bytesused; |
Andy Walls | 776fa86 | 2009-02-01 21:42:12 -0300 | [diff] [blame] | 203 | u32 pts; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 204 | int lines; |
| 205 | |
Andy Walls | 01cbc21 | 2009-02-07 01:31:22 -0300 | [diff] [blame] | 206 | /* |
Andy Walls | 583803d | 2009-02-28 18:48:27 -0300 | [diff] [blame] | 207 | * The CX23418 sends us data that is 32 bit little-endian swapped, |
| 208 | * but we want the raw VBI bytes in the order they were in the raster |
Andy Walls | 4ab9b25 | 2009-03-14 23:20:49 -0300 | [diff] [blame] | 209 | * line. This has a side effect of making the header big endian |
Andy Walls | 01cbc21 | 2009-02-07 01:31:22 -0300 | [diff] [blame] | 210 | */ |
Andy Walls | 776fa86 | 2009-02-01 21:42:12 -0300 | [diff] [blame] | 211 | cx18_buf_swap(buf); |
| 212 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 213 | /* Raw VBI data */ |
Andy Walls | dd07343 | 2008-12-12 16:24:04 -0300 | [diff] [blame] | 214 | if (cx18_raw_vbi(cx)) { |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 215 | |
Andy Walls | 4ab9b25 | 2009-03-14 23:20:49 -0300 | [diff] [blame] | 216 | size = buf->bytesused = |
| 217 | compress_raw_buf(cx, p, size, sizeof(struct vbi_data_hdr)); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 218 | |
Andy Walls | 466df46 | 2009-02-07 15:47:28 -0300 | [diff] [blame] | 219 | /* |
| 220 | * Hack needed for compatibility with old VBI software. |
| 221 | * Write the frame # at the last 4 bytes of the frame |
| 222 | */ |
| 223 | p += size - 4; |
| 224 | memcpy(p, &cx->vbi.frame, 4); |
| 225 | cx->vbi.frame++; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 226 | return; |
| 227 | } |
| 228 | |
| 229 | /* Sliced VBI data with data insertion */ |
Andy Walls | 776fa86 | 2009-02-01 21:42:12 -0300 | [diff] [blame] | 230 | |
Andy Walls | 4ab9b25 | 2009-03-14 23:20:49 -0300 | [diff] [blame] | 231 | pts = (be32_to_cpu(hdr->magic) == 0x3fffffff) ? be32_to_cpu(hdr->pts) |
| 232 | : 0; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 233 | |
Andy Walls | 4ab9b25 | 2009-03-14 23:20:49 -0300 | [diff] [blame] | 234 | lines = compress_sliced_buf(cx, p, size, sizeof(struct vbi_data_hdr)); |
Andy Walls | 812b1f9 | 2009-02-08 22:40:04 -0300 | [diff] [blame] | 235 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 236 | /* always return at least one empty line */ |
| 237 | if (lines == 0) { |
| 238 | cx->vbi.sliced_data[0].id = 0; |
| 239 | cx->vbi.sliced_data[0].line = 0; |
| 240 | cx->vbi.sliced_data[0].field = 0; |
| 241 | lines = 1; |
| 242 | } |
| 243 | buf->bytesused = size = lines * sizeof(cx->vbi.sliced_data[0]); |
| 244 | memcpy(p, &cx->vbi.sliced_data[0], size); |
| 245 | |
| 246 | if (cx->vbi.insert_mpeg) |
Andy Walls | 776fa86 | 2009-02-01 21:42:12 -0300 | [diff] [blame] | 247 | copy_vbi_data(cx, lines, pts); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 248 | cx->vbi.frame++; |
| 249 | } |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 250 | |
| 251 | void cx18_process_vbi_data(struct cx18 *cx, struct cx18_mdl *mdl, |
| 252 | int streamtype) |
| 253 | { |
| 254 | struct cx18_buffer *buf; |
| 255 | u32 orig_used; |
| 256 | |
| 257 | if (streamtype != CX18_ENC_STREAM_TYPE_VBI) |
| 258 | return; |
| 259 | |
| 260 | /* |
| 261 | * Big assumption here: |
| 262 | * Every buffer hooked to the MDL's buf_list is a complete VBI frame |
| 263 | * that ends at the end of the buffer. |
| 264 | * |
| 265 | * To assume anything else would make the code in this file |
| 266 | * more complex, or require extra memcpy()'s to make the |
| 267 | * buffers satisfy the above assumption. It's just simpler to set |
| 268 | * up the encoder buffer transfers to make the assumption true. |
| 269 | */ |
| 270 | list_for_each_entry(buf, &mdl->buf_list, list) { |
| 271 | orig_used = buf->bytesused; |
| 272 | if (orig_used == 0) |
| 273 | break; |
| 274 | _cx18_process_vbi_data(cx, buf); |
| 275 | mdl->bytesused -= (orig_used - buf->bytesused); |
| 276 | } |
| 277 | } |