]>
Commit | Line | Data |
---|---|---|
1c1e45d1 HV |
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. | |
1c1e45d1 HV |
17 | */ |
18 | ||
19 | #include "cx18-driver.h" | |
20 | #include "cx18-vbi.h" | |
21 | #include "cx18-ioctl.h" | |
22 | #include "cx18-queue.h" | |
1c1e45d1 | 23 | |
302df970 AW |
24 | /* |
25 | * Raster Reference/Protection (RP) bytes, used in Start/End Active | |
26 | * Video codes emitted from the digitzer in VIP 1.x mode, that flag the start | |
25985edc | 27 | * of VBI sample or VBI ancillary data regions in the digitial ratser line. |
302df970 AW |
28 | * |
29 | * Task FieldEven VerticalBlank HorizontalBlank 0 0 0 0 | |
30 | */ | |
31 | static const u8 raw_vbi_sav_rp[2] = { 0x20, 0x60 }; /* __V_, _FV_ */ | |
32 | static const u8 sliced_vbi_eav_rp[2] = { 0xb0, 0xf0 }; /* T_VH, TFVH */ | |
33 | ||
1c1e45d1 HV |
34 | static void copy_vbi_data(struct cx18 *cx, int lines, u32 pts_stamp) |
35 | { | |
36 | int line = 0; | |
37 | int i; | |
38 | u32 linemask[2] = { 0, 0 }; | |
39 | unsigned short size; | |
40 | static const u8 mpeg_hdr_data[] = { | |
302df970 AW |
41 | /* MPEG-2 Program Pack */ |
42 | 0x00, 0x00, 0x01, 0xba, /* Prog Pack start code */ | |
43 | 0x44, 0x00, 0x0c, 0x66, 0x24, 0x01, /* SCR, SCR Ext, markers */ | |
44 | 0x01, 0xd1, 0xd3, /* Mux Rate, markers */ | |
45 | 0xfa, 0xff, 0xff, /* Res, Suff cnt, Stuff */ | |
46 | /* MPEG-2 Private Stream 1 PES Packet */ | |
47 | 0x00, 0x00, 0x01, 0xbd, /* Priv Stream 1 start */ | |
48 | 0x00, 0x1a, /* length */ | |
49 | 0x84, 0x80, 0x07, /* flags, hdr data len */ | |
50 | 0x21, 0x00, 0x5d, 0x63, 0xa7, /* PTS, markers */ | |
51 | 0xff, 0xff /* stuffing */ | |
1c1e45d1 HV |
52 | }; |
53 | const int sd = sizeof(mpeg_hdr_data); /* start of vbi data */ | |
54 | int idx = cx->vbi.frame % CX18_VBI_FRAMES; | |
55 | u8 *dst = &cx->vbi.sliced_mpeg_data[idx][0]; | |
56 | ||
57 | for (i = 0; i < lines; i++) { | |
58 | struct v4l2_sliced_vbi_data *sdata = cx->vbi.sliced_data + i; | |
59 | int f, l; | |
60 | ||
61 | if (sdata->id == 0) | |
62 | continue; | |
63 | ||
64 | l = sdata->line - 6; | |
65 | f = sdata->field; | |
66 | if (f) | |
67 | l += 18; | |
68 | if (l < 32) | |
69 | linemask[0] |= (1 << l); | |
70 | else | |
71 | linemask[1] |= (1 << (l - 32)); | |
aed6abd6 | 72 | dst[sd + 12 + line * 43] = cx18_service2vbi(sdata->id); |
1c1e45d1 HV |
73 | memcpy(dst + sd + 12 + line * 43 + 1, sdata->data, 42); |
74 | line++; | |
75 | } | |
76 | memcpy(dst, mpeg_hdr_data, sizeof(mpeg_hdr_data)); | |
77 | if (line == 36) { | |
78 | /* All lines are used, so there is no space for the linemask | |
79 | (the max size of the VBI data is 36 * 43 + 4 bytes). | |
80 | So in this case we use the magic number 'ITV0'. */ | |
81 | memcpy(dst + sd, "ITV0", 4); | |
bb71b14d | 82 | memmove(dst + sd + 4, dst + sd + 12, line * 43); |
1c1e45d1 HV |
83 | size = 4 + ((43 * line + 3) & ~3); |
84 | } else { | |
302df970 | 85 | memcpy(dst + sd, "itv0", 4); |
6e1a6372 AW |
86 | cpu_to_le32s(&linemask[0]); |
87 | cpu_to_le32s(&linemask[1]); | |
1c1e45d1 HV |
88 | memcpy(dst + sd + 4, &linemask[0], 8); |
89 | size = 12 + ((43 * line + 3) & ~3); | |
90 | } | |
91 | dst[4+16] = (size + 10) >> 8; | |
92 | dst[5+16] = (size + 10) & 0xff; | |
93 | dst[9+16] = 0x21 | ((pts_stamp >> 29) & 0x6); | |
94 | dst[10+16] = (pts_stamp >> 22) & 0xff; | |
95 | dst[11+16] = 1 | ((pts_stamp >> 14) & 0xff); | |
96 | dst[12+16] = (pts_stamp >> 7) & 0xff; | |
97 | dst[13+16] = 1 | ((pts_stamp & 0x7f) << 1); | |
98 | cx->vbi.sliced_mpeg_size[idx] = sd + size; | |
99 | } | |
100 | ||
101 | /* Compress raw VBI format, removes leading SAV codes and surplus space | |
466df464 | 102 | after the frame. Returns new compressed size. */ |
52fcb3ec | 103 | /* FIXME - this function ignores the input size. */ |
4ab9b256 | 104 | static u32 compress_raw_buf(struct cx18 *cx, u8 *buf, u32 size, u32 hdr_size) |
1c1e45d1 | 105 | { |
318de791 | 106 | u32 line_size = VBI_ACTIVE_SAMPLES; |
466df464 | 107 | u32 lines = cx->vbi.count * 2; |
1c1e45d1 HV |
108 | u8 *q = buf; |
109 | u8 *p; | |
110 | int i; | |
111 | ||
4ab9b256 AW |
112 | /* Skip the header */ |
113 | buf += hdr_size; | |
114 | ||
1c1e45d1 HV |
115 | for (i = 0; i < lines; i++) { |
116 | p = buf + i * line_size; | |
117 | ||
118 | /* Look for SAV code */ | |
119 | if (p[0] != 0xff || p[1] || p[2] || | |
4ab9b256 AW |
120 | (p[3] != raw_vbi_sav_rp[0] && |
121 | p[3] != raw_vbi_sav_rp[1])) | |
1c1e45d1 | 122 | break; |
4ab9b256 AW |
123 | if (i == lines - 1) { |
124 | /* last line is hdr_size bytes short - extrapolate it */ | |
125 | memcpy(q, p + 4, line_size - 4 - hdr_size); | |
126 | q += line_size - 4 - hdr_size; | |
127 | p += line_size - hdr_size - 1; | |
128 | memset(q, (int) *p, hdr_size); | |
129 | } else { | |
130 | memcpy(q, p + 4, line_size - 4); | |
131 | q += line_size - 4; | |
132 | } | |
1c1e45d1 HV |
133 | } |
134 | return lines * (line_size - 4); | |
135 | } | |
136 | ||
4ab9b256 AW |
137 | static u32 compress_sliced_buf(struct cx18 *cx, u8 *buf, u32 size, |
138 | const u32 hdr_size) | |
1c1e45d1 | 139 | { |
1c1e45d1 HV |
140 | struct v4l2_decode_vbi_line vbi; |
141 | int i; | |
4ab9b256 | 142 | u32 line = 0; |
318de791 MCC |
143 | u32 line_size = cx->is_60hz ? VBI_HBLANK_SAMPLES_60HZ |
144 | : VBI_HBLANK_SAMPLES_50HZ; | |
1c1e45d1 HV |
145 | |
146 | /* find the first valid line */ | |
4ab9b256 AW |
147 | for (i = hdr_size, buf += hdr_size; i < size; i++, buf++) { |
148 | if (buf[0] == 0xff && !buf[1] && !buf[2] && | |
149 | (buf[3] == sliced_vbi_eav_rp[0] || | |
150 | buf[3] == sliced_vbi_eav_rp[1])) | |
1c1e45d1 HV |
151 | break; |
152 | } | |
153 | ||
4ab9b256 AW |
154 | /* |
155 | * The last line is short by hdr_size bytes, but for the remaining | |
156 | * checks against size, we pretend that it is not, by counting the | |
157 | * header bytes we knowingly skipped | |
158 | */ | |
159 | size -= (i - hdr_size); | |
1c1e45d1 HV |
160 | if (size < line_size) |
161 | return line; | |
4ab9b256 | 162 | |
1c1e45d1 HV |
163 | for (i = 0; i < size / line_size; i++) { |
164 | u8 *p = buf + i * line_size; | |
165 | ||
302df970 | 166 | /* Look for EAV code */ |
4ab9b256 AW |
167 | if (p[0] != 0xff || p[1] || p[2] || |
168 | (p[3] != sliced_vbi_eav_rp[0] && | |
169 | p[3] != sliced_vbi_eav_rp[1])) | |
1c1e45d1 HV |
170 | continue; |
171 | vbi.p = p + 4; | |
32cd527f | 172 | v4l2_subdev_call(cx->sd_av, vbi, decode_vbi_line, &vbi); |
1c1e45d1 HV |
173 | if (vbi.type) { |
174 | cx->vbi.sliced_data[line].id = vbi.type; | |
175 | cx->vbi.sliced_data[line].field = vbi.is_second_field; | |
176 | cx->vbi.sliced_data[line].line = vbi.line; | |
177 | memcpy(cx->vbi.sliced_data[line].data, vbi.p, 42); | |
178 | line++; | |
179 | } | |
180 | } | |
181 | return line; | |
182 | } | |
183 | ||
52fcb3ec | 184 | static void _cx18_process_vbi_data(struct cx18 *cx, struct cx18_buffer *buf) |
1c1e45d1 | 185 | { |
4ab9b256 AW |
186 | /* |
187 | * The CX23418 provides a 12 byte header in its raw VBI buffers to us: | |
188 | * 0x3fffffff [4 bytes of something] [4 byte presentation time stamp] | |
189 | */ | |
190 | struct vbi_data_hdr { | |
191 | __be32 magic; | |
192 | __be32 unknown; | |
193 | __be32 pts; | |
194 | } *hdr = (struct vbi_data_hdr *) buf->buf; | |
195 | ||
1c1e45d1 HV |
196 | u8 *p = (u8 *) buf->buf; |
197 | u32 size = buf->bytesused; | |
776fa869 | 198 | u32 pts; |
1c1e45d1 HV |
199 | int lines; |
200 | ||
01cbc214 | 201 | /* |
583803d1 AW |
202 | * The CX23418 sends us data that is 32 bit little-endian swapped, |
203 | * but we want the raw VBI bytes in the order they were in the raster | |
4ab9b256 | 204 | * line. This has a side effect of making the header big endian |
01cbc214 | 205 | */ |
776fa869 AW |
206 | cx18_buf_swap(buf); |
207 | ||
1c1e45d1 | 208 | /* Raw VBI data */ |
dd073434 | 209 | if (cx18_raw_vbi(cx)) { |
1c1e45d1 | 210 | |
4ab9b256 AW |
211 | size = buf->bytesused = |
212 | compress_raw_buf(cx, p, size, sizeof(struct vbi_data_hdr)); | |
1c1e45d1 | 213 | |
466df464 AW |
214 | /* |
215 | * Hack needed for compatibility with old VBI software. | |
216 | * Write the frame # at the last 4 bytes of the frame | |
217 | */ | |
218 | p += size - 4; | |
219 | memcpy(p, &cx->vbi.frame, 4); | |
220 | cx->vbi.frame++; | |
1c1e45d1 HV |
221 | return; |
222 | } | |
223 | ||
224 | /* Sliced VBI data with data insertion */ | |
776fa869 | 225 | |
4ab9b256 AW |
226 | pts = (be32_to_cpu(hdr->magic) == 0x3fffffff) ? be32_to_cpu(hdr->pts) |
227 | : 0; | |
1c1e45d1 | 228 | |
4ab9b256 | 229 | lines = compress_sliced_buf(cx, p, size, sizeof(struct vbi_data_hdr)); |
812b1f9d | 230 | |
1c1e45d1 HV |
231 | /* always return at least one empty line */ |
232 | if (lines == 0) { | |
233 | cx->vbi.sliced_data[0].id = 0; | |
234 | cx->vbi.sliced_data[0].line = 0; | |
235 | cx->vbi.sliced_data[0].field = 0; | |
236 | lines = 1; | |
237 | } | |
238 | buf->bytesused = size = lines * sizeof(cx->vbi.sliced_data[0]); | |
239 | memcpy(p, &cx->vbi.sliced_data[0], size); | |
240 | ||
241 | if (cx->vbi.insert_mpeg) | |
776fa869 | 242 | copy_vbi_data(cx, lines, pts); |
1c1e45d1 HV |
243 | cx->vbi.frame++; |
244 | } | |
52fcb3ec AW |
245 | |
246 | void cx18_process_vbi_data(struct cx18 *cx, struct cx18_mdl *mdl, | |
247 | int streamtype) | |
248 | { | |
249 | struct cx18_buffer *buf; | |
250 | u32 orig_used; | |
251 | ||
252 | if (streamtype != CX18_ENC_STREAM_TYPE_VBI) | |
253 | return; | |
254 | ||
255 | /* | |
256 | * Big assumption here: | |
257 | * Every buffer hooked to the MDL's buf_list is a complete VBI frame | |
258 | * that ends at the end of the buffer. | |
259 | * | |
260 | * To assume anything else would make the code in this file | |
261 | * more complex, or require extra memcpy()'s to make the | |
262 | * buffers satisfy the above assumption. It's just simpler to set | |
263 | * up the encoder buffer transfers to make the assumption true. | |
264 | */ | |
265 | list_for_each_entry(buf, &mdl->buf_list, list) { | |
266 | orig_used = buf->bytesused; | |
267 | if (orig_used == 0) | |
268 | break; | |
269 | _cx18_process_vbi_data(cx, buf); | |
270 | mdl->bytesused -= (orig_used - buf->bytesused); | |
271 | } | |
272 | } |