ivtv-vbi.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. /*
  2. Vertical Blank Interval support functions
  3. Copyright (C) 2004-2007 Hans Verkuil <hverkuil@xs4all.nl>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. */
  16. #include "ivtv-driver.h"
  17. #include "ivtv-i2c.h"
  18. #include "ivtv-ioctl.h"
  19. #include "ivtv-queue.h"
  20. #include "ivtv-cards.h"
  21. #include "ivtv-vbi.h"
  22. static void ivtv_set_vps(struct ivtv *itv, int enabled)
  23. {
  24. struct v4l2_sliced_vbi_data data;
  25. if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
  26. return;
  27. data.id = V4L2_SLICED_VPS;
  28. data.field = 0;
  29. data.line = enabled ? 16 : 0;
  30. data.data[2] = itv->vbi.vps_payload.data[0];
  31. data.data[8] = itv->vbi.vps_payload.data[1];
  32. data.data[9] = itv->vbi.vps_payload.data[2];
  33. data.data[10] = itv->vbi.vps_payload.data[3];
  34. data.data[11] = itv->vbi.vps_payload.data[4];
  35. ivtv_call_hw(itv, IVTV_HW_SAA7127, vbi, s_vbi_data, &data);
  36. }
  37. static void ivtv_set_cc(struct ivtv *itv, int mode, const struct vbi_cc *cc)
  38. {
  39. struct v4l2_sliced_vbi_data data;
  40. if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
  41. return;
  42. data.id = V4L2_SLICED_CAPTION_525;
  43. data.field = 0;
  44. data.line = (mode & 1) ? 21 : 0;
  45. data.data[0] = cc->odd[0];
  46. data.data[1] = cc->odd[1];
  47. ivtv_call_hw(itv, IVTV_HW_SAA7127, vbi, s_vbi_data, &data);
  48. data.field = 1;
  49. data.line = (mode & 2) ? 21 : 0;
  50. data.data[0] = cc->even[0];
  51. data.data[1] = cc->even[1];
  52. ivtv_call_hw(itv, IVTV_HW_SAA7127, vbi, s_vbi_data, &data);
  53. }
  54. static void ivtv_set_wss(struct ivtv *itv, int enabled, int mode)
  55. {
  56. struct v4l2_sliced_vbi_data data;
  57. if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
  58. return;
  59. /* When using a 50 Hz system, always turn on the
  60. wide screen signal with 4x3 ratio as the default.
  61. Turning this signal on and off can confuse certain
  62. TVs. As far as I can tell there is no reason not to
  63. transmit this signal. */
  64. if ((itv->std_out & V4L2_STD_625_50) && !enabled) {
  65. enabled = 1;
  66. mode = 0x08; /* 4x3 full format */
  67. }
  68. data.id = V4L2_SLICED_WSS_625;
  69. data.field = 0;
  70. data.line = enabled ? 23 : 0;
  71. data.data[0] = mode & 0xff;
  72. data.data[1] = (mode >> 8) & 0xff;
  73. ivtv_call_hw(itv, IVTV_HW_SAA7127, vbi, s_vbi_data, &data);
  74. }
  75. static int odd_parity(u8 c)
  76. {
  77. c ^= (c >> 4);
  78. c ^= (c >> 2);
  79. c ^= (c >> 1);
  80. return c & 1;
  81. }
  82. static void ivtv_write_vbi_line(struct ivtv *itv,
  83. const struct v4l2_sliced_vbi_data *d,
  84. struct vbi_cc *cc, int *found_cc)
  85. {
  86. struct vbi_info *vi = &itv->vbi;
  87. if (d->id == V4L2_SLICED_CAPTION_525 && d->line == 21) {
  88. if (d->field) {
  89. cc->even[0] = d->data[0];
  90. cc->even[1] = d->data[1];
  91. } else {
  92. cc->odd[0] = d->data[0];
  93. cc->odd[1] = d->data[1];
  94. }
  95. *found_cc = 1;
  96. } else if (d->id == V4L2_SLICED_VPS && d->line == 16 && d->field == 0) {
  97. struct vbi_vps vps;
  98. vps.data[0] = d->data[2];
  99. vps.data[1] = d->data[8];
  100. vps.data[2] = d->data[9];
  101. vps.data[3] = d->data[10];
  102. vps.data[4] = d->data[11];
  103. if (memcmp(&vps, &vi->vps_payload, sizeof(vps))) {
  104. vi->vps_payload = vps;
  105. set_bit(IVTV_F_I_UPDATE_VPS, &itv->i_flags);
  106. }
  107. } else if (d->id == V4L2_SLICED_WSS_625 &&
  108. d->line == 23 && d->field == 0) {
  109. int wss = d->data[0] | d->data[1] << 8;
  110. if (vi->wss_payload != wss) {
  111. vi->wss_payload = wss;
  112. set_bit(IVTV_F_I_UPDATE_WSS, &itv->i_flags);
  113. }
  114. }
  115. }
  116. static void ivtv_write_vbi_cc_lines(struct ivtv *itv, const struct vbi_cc *cc)
  117. {
  118. struct vbi_info *vi = &itv->vbi;
  119. if (vi->cc_payload_idx < ARRAY_SIZE(vi->cc_payload)) {
  120. memcpy(&vi->cc_payload[vi->cc_payload_idx], cc,
  121. sizeof(struct vbi_cc));
  122. vi->cc_payload_idx++;
  123. set_bit(IVTV_F_I_UPDATE_CC, &itv->i_flags);
  124. }
  125. }
  126. static void ivtv_write_vbi(struct ivtv *itv,
  127. const struct v4l2_sliced_vbi_data *sliced,
  128. size_t cnt)
  129. {
  130. struct vbi_cc cc = { .odd = { 0x80, 0x80 }, .even = { 0x80, 0x80 } };
  131. int found_cc = 0;
  132. size_t i;
  133. for (i = 0; i < cnt; i++)
  134. ivtv_write_vbi_line(itv, sliced + i, &cc, &found_cc);
  135. if (found_cc)
  136. ivtv_write_vbi_cc_lines(itv, &cc);
  137. }
  138. ssize_t
  139. ivtv_write_vbi_from_user(struct ivtv *itv,
  140. const struct v4l2_sliced_vbi_data __user *sliced,
  141. size_t cnt)
  142. {
  143. struct vbi_cc cc = { .odd = { 0x80, 0x80 }, .even = { 0x80, 0x80 } };
  144. int found_cc = 0;
  145. size_t i;
  146. struct v4l2_sliced_vbi_data d;
  147. ssize_t ret = cnt * sizeof(struct v4l2_sliced_vbi_data);
  148. for (i = 0; i < cnt; i++) {
  149. if (copy_from_user(&d, sliced + i,
  150. sizeof(struct v4l2_sliced_vbi_data))) {
  151. ret = -EFAULT;
  152. break;
  153. }
  154. ivtv_write_vbi_line(itv, &d, &cc, &found_cc);
  155. }
  156. if (found_cc)
  157. ivtv_write_vbi_cc_lines(itv, &cc);
  158. return ret;
  159. }
  160. static void copy_vbi_data(struct ivtv *itv, int lines, u32 pts_stamp)
  161. {
  162. int line = 0;
  163. int i;
  164. u32 linemask[2] = { 0, 0 };
  165. unsigned short size;
  166. static const u8 mpeg_hdr_data[] = {
  167. 0x00, 0x00, 0x01, 0xba, 0x44, 0x00, 0x0c, 0x66,
  168. 0x24, 0x01, 0x01, 0xd1, 0xd3, 0xfa, 0xff, 0xff,
  169. 0x00, 0x00, 0x01, 0xbd, 0x00, 0x1a, 0x84, 0x80,
  170. 0x07, 0x21, 0x00, 0x5d, 0x63, 0xa7, 0xff, 0xff
  171. };
  172. const int sd = sizeof(mpeg_hdr_data); /* start of vbi data */
  173. int idx = itv->vbi.frame % IVTV_VBI_FRAMES;
  174. u8 *dst = &itv->vbi.sliced_mpeg_data[idx][0];
  175. for (i = 0; i < lines; i++) {
  176. int f, l;
  177. if (itv->vbi.sliced_data[i].id == 0)
  178. continue;
  179. l = itv->vbi.sliced_data[i].line - 6;
  180. f = itv->vbi.sliced_data[i].field;
  181. if (f)
  182. l += 18;
  183. if (l < 32)
  184. linemask[0] |= (1 << l);
  185. else
  186. linemask[1] |= (1 << (l - 32));
  187. dst[sd + 12 + line * 43] =
  188. ivtv_service2vbi(itv->vbi.sliced_data[i].id);
  189. memcpy(dst + sd + 12 + line * 43 + 1, itv->vbi.sliced_data[i].data, 42);
  190. line++;
  191. }
  192. memcpy(dst, mpeg_hdr_data, sizeof(mpeg_hdr_data));
  193. if (line == 36) {
  194. /* All lines are used, so there is no space for the linemask
  195. (the max size of the VBI data is 36 * 43 + 4 bytes).
  196. So in this case we use the magic number 'ITV0'. */
  197. memcpy(dst + sd, "ITV0", 4);
  198. memmove(dst + sd + 4, dst + sd + 12, line * 43);
  199. size = 4 + ((43 * line + 3) & ~3);
  200. } else {
  201. memcpy(dst + sd, "itv0", 4);
  202. cpu_to_le32s(&linemask[0]);
  203. cpu_to_le32s(&linemask[1]);
  204. memcpy(dst + sd + 4, &linemask[0], 8);
  205. size = 12 + ((43 * line + 3) & ~3);
  206. }
  207. dst[4+16] = (size + 10) >> 8;
  208. dst[5+16] = (size + 10) & 0xff;
  209. dst[9+16] = 0x21 | ((pts_stamp >> 29) & 0x6);
  210. dst[10+16] = (pts_stamp >> 22) & 0xff;
  211. dst[11+16] = 1 | ((pts_stamp >> 14) & 0xff);
  212. dst[12+16] = (pts_stamp >> 7) & 0xff;
  213. dst[13+16] = 1 | ((pts_stamp & 0x7f) << 1);
  214. itv->vbi.sliced_mpeg_size[idx] = sd + size;
  215. }
  216. static int ivtv_convert_ivtv_vbi(struct ivtv *itv, u8 *p)
  217. {
  218. u32 linemask[2];
  219. int i, l, id2;
  220. int line = 0;
  221. if (!memcmp(p, "itv0", 4)) {
  222. memcpy(linemask, p + 4, 8);
  223. p += 12;
  224. } else if (!memcmp(p, "ITV0", 4)) {
  225. linemask[0] = 0xffffffff;
  226. linemask[1] = 0xf;
  227. p += 4;
  228. } else {
  229. /* unknown VBI data, convert to empty VBI frame */
  230. linemask[0] = linemask[1] = 0;
  231. }
  232. for (i = 0; i < 36; i++) {
  233. int err = 0;
  234. if (i < 32 && !(linemask[0] & (1 << i)))
  235. continue;
  236. if (i >= 32 && !(linemask[1] & (1 << (i - 32))))
  237. continue;
  238. id2 = *p & 0xf;
  239. switch (id2) {
  240. case IVTV_SLICED_TYPE_TELETEXT_B:
  241. id2 = V4L2_SLICED_TELETEXT_B;
  242. break;
  243. case IVTV_SLICED_TYPE_CAPTION_525:
  244. id2 = V4L2_SLICED_CAPTION_525;
  245. err = !odd_parity(p[1]) || !odd_parity(p[2]);
  246. break;
  247. case IVTV_SLICED_TYPE_VPS:
  248. id2 = V4L2_SLICED_VPS;
  249. break;
  250. case IVTV_SLICED_TYPE_WSS_625:
  251. id2 = V4L2_SLICED_WSS_625;
  252. break;
  253. default:
  254. id2 = 0;
  255. break;
  256. }
  257. if (err == 0) {
  258. l = (i < 18) ? i + 6 : i - 18 + 6;
  259. itv->vbi.sliced_dec_data[line].line = l;
  260. itv->vbi.sliced_dec_data[line].field = i >= 18;
  261. itv->vbi.sliced_dec_data[line].id = id2;
  262. memcpy(itv->vbi.sliced_dec_data[line].data, p + 1, 42);
  263. line++;
  264. }
  265. p += 43;
  266. }
  267. while (line < 36) {
  268. itv->vbi.sliced_dec_data[line].id = 0;
  269. itv->vbi.sliced_dec_data[line].line = 0;
  270. itv->vbi.sliced_dec_data[line].field = 0;
  271. line++;
  272. }
  273. return line * sizeof(itv->vbi.sliced_dec_data[0]);
  274. }
  275. /* Compress raw VBI format, removes leading SAV codes and surplus space after the
  276. field.
  277. Returns new compressed size. */
  278. static u32 compress_raw_buf(struct ivtv *itv, u8 *buf, u32 size)
  279. {
  280. u32 line_size = itv->vbi.raw_decoder_line_size;
  281. u32 lines = itv->vbi.count;
  282. u8 sav1 = itv->vbi.raw_decoder_sav_odd_field;
  283. u8 sav2 = itv->vbi.raw_decoder_sav_even_field;
  284. u8 *q = buf;
  285. u8 *p;
  286. int i;
  287. for (i = 0; i < lines; i++) {
  288. p = buf + i * line_size;
  289. /* Look for SAV code */
  290. if (p[0] != 0xff || p[1] || p[2] || (p[3] != sav1 && p[3] != sav2)) {
  291. break;
  292. }
  293. memcpy(q, p + 4, line_size - 4);
  294. q += line_size - 4;
  295. }
  296. return lines * (line_size - 4);
  297. }
  298. /* Compressed VBI format, all found sliced blocks put next to one another
  299. Returns new compressed size */
  300. static u32 compress_sliced_buf(struct ivtv *itv, u32 line, u8 *buf, u32 size, u8 sav)
  301. {
  302. u32 line_size = itv->vbi.sliced_decoder_line_size;
  303. struct v4l2_decode_vbi_line vbi;
  304. int i;
  305. unsigned lines = 0;
  306. /* find the first valid line */
  307. for (i = 0; i < size; i++, buf++) {
  308. if (buf[0] == 0xff && !buf[1] && !buf[2] && buf[3] == sav)
  309. break;
  310. }
  311. size -= i;
  312. if (size < line_size) {
  313. return line;
  314. }
  315. for (i = 0; i < size / line_size; i++) {
  316. u8 *p = buf + i * line_size;
  317. /* Look for SAV code */
  318. if (p[0] != 0xff || p[1] || p[2] || p[3] != sav) {
  319. continue;
  320. }
  321. vbi.p = p + 4;
  322. v4l2_subdev_call(itv->sd_video, vbi, decode_vbi_line, &vbi);
  323. if (vbi.type && !(lines & (1 << vbi.line))) {
  324. lines |= 1 << vbi.line;
  325. itv->vbi.sliced_data[line].id = vbi.type;
  326. itv->vbi.sliced_data[line].field = vbi.is_second_field;
  327. itv->vbi.sliced_data[line].line = vbi.line;
  328. memcpy(itv->vbi.sliced_data[line].data, vbi.p, 42);
  329. line++;
  330. }
  331. }
  332. return line;
  333. }
  334. void ivtv_process_vbi_data(struct ivtv *itv, struct ivtv_buffer *buf,
  335. u64 pts_stamp, int streamtype)
  336. {
  337. u8 *p = (u8 *) buf->buf;
  338. u32 size = buf->bytesused;
  339. int y;
  340. /* Raw VBI data */
  341. if (streamtype == IVTV_ENC_STREAM_TYPE_VBI && ivtv_raw_vbi(itv)) {
  342. u8 type;
  343. ivtv_buf_swap(buf);
  344. type = p[3];
  345. size = buf->bytesused = compress_raw_buf(itv, p, size);
  346. /* second field of the frame? */
  347. if (type == itv->vbi.raw_decoder_sav_even_field) {
  348. /* Dirty hack needed for backwards
  349. compatibility of old VBI software. */
  350. p += size - 4;
  351. memcpy(p, &itv->vbi.frame, 4);
  352. itv->vbi.frame++;
  353. }
  354. return;
  355. }
  356. /* Sliced VBI data with data insertion */
  357. if (streamtype == IVTV_ENC_STREAM_TYPE_VBI) {
  358. int lines;
  359. ivtv_buf_swap(buf);
  360. /* first field */
  361. lines = compress_sliced_buf(itv, 0, p, size / 2,
  362. itv->vbi.sliced_decoder_sav_odd_field);
  363. /* second field */
  364. /* experimentation shows that the second half does not always begin
  365. at the exact address. So start a bit earlier (hence 32). */
  366. lines = compress_sliced_buf(itv, lines, p + size / 2 - 32, size / 2 + 32,
  367. itv->vbi.sliced_decoder_sav_even_field);
  368. /* always return at least one empty line */
  369. if (lines == 0) {
  370. itv->vbi.sliced_data[0].id = 0;
  371. itv->vbi.sliced_data[0].line = 0;
  372. itv->vbi.sliced_data[0].field = 0;
  373. lines = 1;
  374. }
  375. buf->bytesused = size = lines * sizeof(itv->vbi.sliced_data[0]);
  376. memcpy(p, &itv->vbi.sliced_data[0], size);
  377. if (itv->vbi.insert_mpeg) {
  378. copy_vbi_data(itv, lines, pts_stamp);
  379. }
  380. itv->vbi.frame++;
  381. return;
  382. }
  383. /* Sliced VBI re-inserted from an MPEG stream */
  384. if (streamtype == IVTV_DEC_STREAM_TYPE_VBI) {
  385. /* If the size is not 4-byte aligned, then the starting address
  386. for the swapping is also shifted. After swapping the data the
  387. real start address of the VBI data is exactly 4 bytes after the
  388. original start. It's a bit fiddly but it works like a charm.
  389. Non-4-byte alignment happens when an lseek is done on the input
  390. mpeg file to a non-4-byte aligned position. So on arrival here
  391. the VBI data is also non-4-byte aligned. */
  392. int offset = size & 3;
  393. int cnt;
  394. if (offset) {
  395. p += 4 - offset;
  396. }
  397. /* Swap Buffer */
  398. for (y = 0; y < size; y += 4) {
  399. swab32s((u32 *)(p + y));
  400. }
  401. cnt = ivtv_convert_ivtv_vbi(itv, p + offset);
  402. memcpy(buf->buf, itv->vbi.sliced_dec_data, cnt);
  403. buf->bytesused = cnt;
  404. ivtv_write_vbi(itv, itv->vbi.sliced_dec_data,
  405. cnt / sizeof(itv->vbi.sliced_dec_data[0]));
  406. return;
  407. }
  408. }
  409. void ivtv_disable_cc(struct ivtv *itv)
  410. {
  411. struct vbi_cc cc = { .odd = { 0x80, 0x80 }, .even = { 0x80, 0x80 } };
  412. clear_bit(IVTV_F_I_UPDATE_CC, &itv->i_flags);
  413. ivtv_set_cc(itv, 0, &cc);
  414. itv->vbi.cc_payload_idx = 0;
  415. }
  416. void ivtv_vbi_work_handler(struct ivtv *itv)
  417. {
  418. struct vbi_info *vi = &itv->vbi;
  419. struct v4l2_sliced_vbi_data data;
  420. struct vbi_cc cc = { .odd = { 0x80, 0x80 }, .even = { 0x80, 0x80 } };
  421. /* Lock */
  422. if (itv->output_mode == OUT_PASSTHROUGH) {
  423. if (itv->is_50hz) {
  424. data.id = V4L2_SLICED_WSS_625;
  425. data.field = 0;
  426. if (v4l2_subdev_call(itv->sd_video, vbi, g_vbi_data, &data) == 0) {
  427. ivtv_set_wss(itv, 1, data.data[0] & 0xf);
  428. vi->wss_missing_cnt = 0;
  429. } else if (vi->wss_missing_cnt == 4) {
  430. ivtv_set_wss(itv, 1, 0x8); /* 4x3 full format */
  431. } else {
  432. vi->wss_missing_cnt++;
  433. }
  434. }
  435. else {
  436. int mode = 0;
  437. data.id = V4L2_SLICED_CAPTION_525;
  438. data.field = 0;
  439. if (v4l2_subdev_call(itv->sd_video, vbi, g_vbi_data, &data) == 0) {
  440. mode |= 1;
  441. cc.odd[0] = data.data[0];
  442. cc.odd[1] = data.data[1];
  443. }
  444. data.field = 1;
  445. if (v4l2_subdev_call(itv->sd_video, vbi, g_vbi_data, &data) == 0) {
  446. mode |= 2;
  447. cc.even[0] = data.data[0];
  448. cc.even[1] = data.data[1];
  449. }
  450. if (mode) {
  451. vi->cc_missing_cnt = 0;
  452. ivtv_set_cc(itv, mode, &cc);
  453. } else if (vi->cc_missing_cnt == 4) {
  454. ivtv_set_cc(itv, 0, &cc);
  455. } else {
  456. vi->cc_missing_cnt++;
  457. }
  458. }
  459. return;
  460. }
  461. if (test_and_clear_bit(IVTV_F_I_UPDATE_WSS, &itv->i_flags)) {
  462. ivtv_set_wss(itv, 1, vi->wss_payload & 0xf);
  463. }
  464. if (test_bit(IVTV_F_I_UPDATE_CC, &itv->i_flags)) {
  465. if (vi->cc_payload_idx == 0) {
  466. clear_bit(IVTV_F_I_UPDATE_CC, &itv->i_flags);
  467. ivtv_set_cc(itv, 3, &cc);
  468. }
  469. while (vi->cc_payload_idx) {
  470. cc = vi->cc_payload[0];
  471. memmove(vi->cc_payload, vi->cc_payload + 1,
  472. sizeof(vi->cc_payload) - sizeof(vi->cc_payload[0]));
  473. vi->cc_payload_idx--;
  474. if (vi->cc_payload_idx && cc.odd[0] == 0x80 && cc.odd[1] == 0x80)
  475. continue;
  476. ivtv_set_cc(itv, 3, &cc);
  477. break;
  478. }
  479. }
  480. if (test_and_clear_bit(IVTV_F_I_UPDATE_VPS, &itv->i_flags)) {
  481. ivtv_set_vps(itv, 1);
  482. }
  483. }