cx18-av-vbi.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * cx18 ADEC VBI functions
  3. *
  4. * Derived from cx25840-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
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (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., 51 Franklin Street, Fifth Floor, Boston, MA
  21. * 02110-1301, USA.
  22. */
  23. #include "cx18-driver.h"
  24. static int odd_parity(u8 c)
  25. {
  26. c ^= (c >> 4);
  27. c ^= (c >> 2);
  28. c ^= (c >> 1);
  29. return c & 1;
  30. }
  31. static int decode_vps(u8 *dst, u8 *p)
  32. {
  33. static const u8 biphase_tbl[] = {
  34. 0xf0, 0x78, 0x70, 0xf0, 0xb4, 0x3c, 0x34, 0xb4,
  35. 0xb0, 0x38, 0x30, 0xb0, 0xf0, 0x78, 0x70, 0xf0,
  36. 0xd2, 0x5a, 0x52, 0xd2, 0x96, 0x1e, 0x16, 0x96,
  37. 0x92, 0x1a, 0x12, 0x92, 0xd2, 0x5a, 0x52, 0xd2,
  38. 0xd0, 0x58, 0x50, 0xd0, 0x94, 0x1c, 0x14, 0x94,
  39. 0x90, 0x18, 0x10, 0x90, 0xd0, 0x58, 0x50, 0xd0,
  40. 0xf0, 0x78, 0x70, 0xf0, 0xb4, 0x3c, 0x34, 0xb4,
  41. 0xb0, 0x38, 0x30, 0xb0, 0xf0, 0x78, 0x70, 0xf0,
  42. 0xe1, 0x69, 0x61, 0xe1, 0xa5, 0x2d, 0x25, 0xa5,
  43. 0xa1, 0x29, 0x21, 0xa1, 0xe1, 0x69, 0x61, 0xe1,
  44. 0xc3, 0x4b, 0x43, 0xc3, 0x87, 0x0f, 0x07, 0x87,
  45. 0x83, 0x0b, 0x03, 0x83, 0xc3, 0x4b, 0x43, 0xc3,
  46. 0xc1, 0x49, 0x41, 0xc1, 0x85, 0x0d, 0x05, 0x85,
  47. 0x81, 0x09, 0x01, 0x81, 0xc1, 0x49, 0x41, 0xc1,
  48. 0xe1, 0x69, 0x61, 0xe1, 0xa5, 0x2d, 0x25, 0xa5,
  49. 0xa1, 0x29, 0x21, 0xa1, 0xe1, 0x69, 0x61, 0xe1,
  50. 0xe0, 0x68, 0x60, 0xe0, 0xa4, 0x2c, 0x24, 0xa4,
  51. 0xa0, 0x28, 0x20, 0xa0, 0xe0, 0x68, 0x60, 0xe0,
  52. 0xc2, 0x4a, 0x42, 0xc2, 0x86, 0x0e, 0x06, 0x86,
  53. 0x82, 0x0a, 0x02, 0x82, 0xc2, 0x4a, 0x42, 0xc2,
  54. 0xc0, 0x48, 0x40, 0xc0, 0x84, 0x0c, 0x04, 0x84,
  55. 0x80, 0x08, 0x00, 0x80, 0xc0, 0x48, 0x40, 0xc0,
  56. 0xe0, 0x68, 0x60, 0xe0, 0xa4, 0x2c, 0x24, 0xa4,
  57. 0xa0, 0x28, 0x20, 0xa0, 0xe0, 0x68, 0x60, 0xe0,
  58. 0xf0, 0x78, 0x70, 0xf0, 0xb4, 0x3c, 0x34, 0xb4,
  59. 0xb0, 0x38, 0x30, 0xb0, 0xf0, 0x78, 0x70, 0xf0,
  60. 0xd2, 0x5a, 0x52, 0xd2, 0x96, 0x1e, 0x16, 0x96,
  61. 0x92, 0x1a, 0x12, 0x92, 0xd2, 0x5a, 0x52, 0xd2,
  62. 0xd0, 0x58, 0x50, 0xd0, 0x94, 0x1c, 0x14, 0x94,
  63. 0x90, 0x18, 0x10, 0x90, 0xd0, 0x58, 0x50, 0xd0,
  64. 0xf0, 0x78, 0x70, 0xf0, 0xb4, 0x3c, 0x34, 0xb4,
  65. 0xb0, 0x38, 0x30, 0xb0, 0xf0, 0x78, 0x70, 0xf0,
  66. };
  67. u8 c, err = 0;
  68. int i;
  69. for (i = 0; i < 2 * 13; i += 2) {
  70. err |= biphase_tbl[p[i]] | biphase_tbl[p[i + 1]];
  71. c = (biphase_tbl[p[i + 1]] & 0xf) |
  72. ((biphase_tbl[p[i]] & 0xf) << 4);
  73. dst[i / 2] = c;
  74. }
  75. return err & 0xf0;
  76. }
  77. int cx18_av_vbi(struct cx18 *cx, unsigned int cmd, void *arg)
  78. {
  79. struct cx18_av_state *state = &cx->av_state;
  80. struct v4l2_format *fmt;
  81. struct v4l2_sliced_vbi_format *svbi;
  82. switch (cmd) {
  83. case VIDIOC_G_FMT:
  84. {
  85. static u16 lcr2vbi[] = {
  86. 0, V4L2_SLICED_TELETEXT_B, 0, /* 1 */
  87. 0, V4L2_SLICED_WSS_625, 0, /* 4 */
  88. V4L2_SLICED_CAPTION_525, /* 6 */
  89. 0, 0, V4L2_SLICED_VPS, 0, 0, /* 9 */
  90. 0, 0, 0, 0
  91. };
  92. int is_pal = !(state->std & V4L2_STD_525_60);
  93. int i;
  94. fmt = arg;
  95. if (fmt->type != V4L2_BUF_TYPE_SLICED_VBI_CAPTURE)
  96. return -EINVAL;
  97. svbi = &fmt->fmt.sliced;
  98. memset(svbi, 0, sizeof(*svbi));
  99. /* we're done if raw VBI is active */
  100. if ((cx18_av_read(cx, 0x404) & 0x10) == 0)
  101. break;
  102. if (is_pal) {
  103. for (i = 7; i <= 23; i++) {
  104. u8 v = cx18_av_read(cx, 0x424 + i - 7);
  105. svbi->service_lines[0][i] = lcr2vbi[v >> 4];
  106. svbi->service_lines[1][i] = lcr2vbi[v & 0xf];
  107. svbi->service_set |= svbi->service_lines[0][i] |
  108. svbi->service_lines[1][i];
  109. }
  110. } else {
  111. for (i = 10; i <= 21; i++) {
  112. u8 v = cx18_av_read(cx, 0x424 + i - 10);
  113. svbi->service_lines[0][i] = lcr2vbi[v >> 4];
  114. svbi->service_lines[1][i] = lcr2vbi[v & 0xf];
  115. svbi->service_set |= svbi->service_lines[0][i] |
  116. svbi->service_lines[1][i];
  117. }
  118. }
  119. break;
  120. }
  121. case VIDIOC_S_FMT:
  122. {
  123. int is_pal = !(state->std & V4L2_STD_525_60);
  124. int vbi_offset = is_pal ? 1 : 0;
  125. int i, x;
  126. u8 lcr[24];
  127. fmt = arg;
  128. if (fmt->type != V4L2_BUF_TYPE_SLICED_VBI_CAPTURE)
  129. return -EINVAL;
  130. svbi = &fmt->fmt.sliced;
  131. if (svbi->service_set == 0) {
  132. /* raw VBI */
  133. memset(svbi, 0, sizeof(*svbi));
  134. /* Setup standard */
  135. cx18_av_std_setup(cx);
  136. /* VBI Offset */
  137. cx18_av_write(cx, 0x47f, vbi_offset);
  138. cx18_av_write(cx, 0x404, 0x2e);
  139. break;
  140. }
  141. for (x = 0; x <= 23; x++)
  142. lcr[x] = 0x00;
  143. /* Setup standard */
  144. cx18_av_std_setup(cx);
  145. /* Sliced VBI */
  146. cx18_av_write(cx, 0x404, 0x32); /* Ancillary data */
  147. cx18_av_write(cx, 0x406, 0x13);
  148. cx18_av_write(cx, 0x47f, vbi_offset);
  149. if (is_pal) {
  150. for (i = 0; i <= 6; i++)
  151. svbi->service_lines[0][i] =
  152. svbi->service_lines[1][i] = 0;
  153. } else {
  154. for (i = 0; i <= 9; i++)
  155. svbi->service_lines[0][i] =
  156. svbi->service_lines[1][i] = 0;
  157. for (i = 22; i <= 23; i++)
  158. svbi->service_lines[0][i] =
  159. svbi->service_lines[1][i] = 0;
  160. }
  161. for (i = 7; i <= 23; i++) {
  162. for (x = 0; x <= 1; x++) {
  163. switch (svbi->service_lines[1-x][i]) {
  164. case V4L2_SLICED_TELETEXT_B:
  165. lcr[i] |= 1 << (4 * x);
  166. break;
  167. case V4L2_SLICED_WSS_625:
  168. lcr[i] |= 4 << (4 * x);
  169. break;
  170. case V4L2_SLICED_CAPTION_525:
  171. lcr[i] |= 6 << (4 * x);
  172. break;
  173. case V4L2_SLICED_VPS:
  174. lcr[i] |= 9 << (4 * x);
  175. break;
  176. }
  177. }
  178. }
  179. if (is_pal) {
  180. for (x = 1, i = 0x424; i <= 0x434; i++, x++)
  181. cx18_av_write(cx, i, lcr[6 + x]);
  182. } else {
  183. for (x = 1, i = 0x424; i <= 0x430; i++, x++)
  184. cx18_av_write(cx, i, lcr[9 + x]);
  185. for (i = 0x431; i <= 0x434; i++)
  186. cx18_av_write(cx, i, 0);
  187. }
  188. cx18_av_write(cx, 0x43c, 0x16);
  189. cx18_av_write(cx, 0x474, is_pal ? 0x2a : 0x22);
  190. break;
  191. }
  192. case VIDIOC_INT_DECODE_VBI_LINE:
  193. {
  194. struct v4l2_decode_vbi_line *vbi = arg;
  195. u8 *p = vbi->p;
  196. int id1, id2, l, err = 0;
  197. if (p[0] || p[1] != 0xff || p[2] != 0xff ||
  198. (p[3] != 0x55 && p[3] != 0x91)) {
  199. vbi->line = vbi->type = 0;
  200. break;
  201. }
  202. p += 4;
  203. id1 = p[-1];
  204. id2 = p[0] & 0xf;
  205. l = p[2] & 0x3f;
  206. l += state->vbi_line_offset;
  207. p += 4;
  208. switch (id2) {
  209. case 1:
  210. id2 = V4L2_SLICED_TELETEXT_B;
  211. break;
  212. case 4:
  213. id2 = V4L2_SLICED_WSS_625;
  214. break;
  215. case 6:
  216. id2 = V4L2_SLICED_CAPTION_525;
  217. err = !odd_parity(p[0]) || !odd_parity(p[1]);
  218. break;
  219. case 9:
  220. id2 = V4L2_SLICED_VPS;
  221. if (decode_vps(p, p) != 0)
  222. err = 1;
  223. break;
  224. default:
  225. id2 = 0;
  226. err = 1;
  227. break;
  228. }
  229. vbi->type = err ? 0 : id2;
  230. vbi->line = err ? 0 : l;
  231. vbi->is_second_field = err ? 0 : (id1 == 0x55);
  232. vbi->p = p;
  233. break;
  234. }
  235. }
  236. return 0;
  237. }