ivtv-vbi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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, video, 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, video, 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, video, 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 & 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, video, 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. void ivtv_write_vbi(struct ivtv *itv, const struct v4l2_sliced_vbi_data *sliced, size_t cnt)
  83. {
  84. struct vbi_info *vi = &itv->vbi;
  85. struct vbi_cc cc = { .odd = { 0x80, 0x80 }, .even = { 0x80, 0x80 } };
  86. int found_cc = 0;
  87. size_t i;
  88. for (i = 0; i < cnt; i++) {
  89. const struct v4l2_sliced_vbi_data *d = sliced + i;
  90. if (d->id == V4L2_SLICED_CAPTION_525 && d->line == 21) {
  91. if (d->field) {
  92. cc.even[0] = d->data[0];
  93. cc.even[1] = d->data[1];
  94. } else {
  95. cc.odd[0] = d->data[0];
  96. cc.odd[1] = d->data[1];
  97. }
  98. found_cc = 1;
  99. }
  100. else if (d->id == V4L2_SLICED_VPS && d->line == 16 && d->field == 0) {
  101. struct vbi_vps vps;
  102. vps.data[0] = d->data[2];
  103. vps.data[1] = d->data[8];
  104. vps.data[2] = d->data[9];
  105. vps.data[3] = d->data[10];
  106. vps.data[4] = d->data[11];
  107. if (memcmp(&vps, &vi->vps_payload, sizeof(vps))) {
  108. vi->vps_payload = vps;
  109. set_bit(IVTV_F_I_UPDATE_VPS, &itv->i_flags);
  110. }
  111. }
  112. else if (d->id == V4L2_SLICED_WSS_625 && d->line == 23 && d->field == 0) {
  113. int wss = d->data[0] | d->data[1] << 8;
  114. if (vi->wss_payload != wss) {
  115. vi->wss_payload = wss;
  116. set_bit(IVTV_F_I_UPDATE_WSS, &itv->i_flags);
  117. }
  118. }
  119. }
  120. if (found_cc && vi->cc_payload_idx < sizeof(vi->cc_payload)) {
  121. vi->cc_payload[vi->cc_payload_idx++] = cc;
  122. set_bit(IVTV_F_I_UPDATE_CC, &itv->i_flags);
  123. }
  124. }
  125. static void copy_vbi_data(struct ivtv *itv, int lines, u32 pts_stamp)
  126. {
  127. int line = 0;
  128. int i;
  129. u32 linemask[2] = { 0, 0 };
  130. unsigned short size;
  131. static const u8 mpeg_hdr_data[] = {
  132. 0x00, 0x00, 0x01, 0xba, 0x44, 0x00, 0x0c, 0x66,
  133. 0x24, 0x01, 0x01, 0xd1, 0xd3, 0xfa, 0xff, 0xff,
  134. 0x00, 0x00, 0x01, 0xbd, 0x00, 0x1a, 0x84, 0x80,
  135. 0x07, 0x21, 0x00, 0x5d, 0x63, 0xa7, 0xff, 0xff
  136. };
  137. const int sd = sizeof(mpeg_hdr_data); /* start of vbi data */
  138. int idx = itv->vbi.frame % IVTV_VBI_FRAMES;
  139. u8 *dst = &itv->vbi.sliced_mpeg_data[idx][0];
  140. for (i = 0; i < lines; i++) {
  141. int f, l;
  142. if (itv->vbi.sliced_data[i].id == 0)
  143. continue;
  144. l = itv->vbi.sliced_data[i].line - 6;
  145. f = itv->vbi.sliced_data[i].field;
  146. if (f)
  147. l += 18;
  148. if (l < 32)
  149. linemask[0] |= (1 << l);
  150. else
  151. linemask[1] |= (1 << (l - 32));
  152. dst[sd + 12 + line * 43] =
  153. ivtv_service2vbi(itv->vbi.sliced_data[i].id);
  154. memcpy(dst + sd + 12 + line * 43 + 1, itv->vbi.sliced_data[i].data, 42);
  155. line++;
  156. }
  157. memcpy(dst, mpeg_hdr_data, sizeof(mpeg_hdr_data));
  158. if (line == 36) {
  159. /* All lines are used, so there is no space for the linemask
  160. (the max size of the VBI data is 36 * 43 + 4 bytes).
  161. So in this case we use the magic number 'ITV0'. */
  162. memcpy(dst + sd, "ITV0", 4);
  163. memcpy(dst + sd + 4, dst + sd + 12, line * 43);
  164. size = 4 + ((43 * line + 3) & ~3);
  165. } else {
  166. memcpy(dst + sd, "itv0", 4);
  167. cpu_to_le32s(&linemask[0]);
  168. cpu_to_le32s(&linemask[1]);
  169. memcpy(dst + sd + 4, &linemask[0], 8);
  170. size = 12 + ((43 * line + 3) & ~3);
  171. }
  172. dst[4+16] = (size + 10) >> 8;
  173. dst[5+16] = (size + 10) & 0xff;
  174. dst[9+16] = 0x21 | ((pts_stamp >> 29) & 0x6);
  175. dst[10+16] = (pts_stamp >> 22) & 0xff;
  176. dst[11+16] = 1 | ((pts_stamp >> 14) & 0xff);
  177. dst[12+16] = (pts_stamp >> 7) & 0xff;
  178. dst[13+16] = 1 | ((pts_stamp & 0x7f) << 1);
  179. itv->vbi.sliced_mpeg_size[idx] = sd + size;
  180. }
  181. static int ivtv_convert_ivtv_vbi(struct ivtv *itv, u8 *p)
  182. {
  183. u32 linemask[2];
  184. int i, l, id2;
  185. int line = 0;
  186. if (!memcmp(p, "itv0", 4)) {
  187. memcpy(linemask, p + 4, 8);
  188. p += 12;
  189. } else if (!memcmp(p, "ITV0", 4)) {
  190. linemask[0] = 0xffffffff;
  191. linemask[1] = 0xf;
  192. p += 4;
  193. } else {
  194. /* unknown VBI data, convert to empty VBI frame */
  195. linemask[0] = linemask[1] = 0;
  196. }
  197. for (i = 0; i < 36; i++) {
  198. int err = 0;
  199. if (i < 32 && !(linemask[0] & (1 << i)))
  200. continue;
  201. if (i >= 32 && !(linemask[1] & (1 << (i - 32))))
  202. continue;
  203. id2 = *p & 0xf;
  204. switch (id2) {
  205. case IVTV_SLICED_TYPE_TELETEXT_B:
  206. id2 = V4L2_SLICED_TELETEXT_B;
  207. break;
  208. case IVTV_SLICED_TYPE_CAPTION_525:
  209. id2 = V4L2_SLICED_CAPTION_525;
  210. err = !odd_parity(p[1]) || !odd_parity(p[2]);
  211. break;
  212. case IVTV_SLICED_TYPE_VPS:
  213. id2 = V4L2_SLICED_VPS;
  214. break;
  215. case IVTV_SLICED_TYPE_WSS_625:
  216. id2 = V4L2_SLICED_WSS_625;
  217. break;
  218. default:
  219. id2 = 0;
  220. break;
  221. }
  222. if (err == 0) {
  223. l = (i < 18) ? i + 6 : i - 18 + 6;
  224. itv->vbi.sliced_dec_data[line].line = l;
  225. itv->vbi.sliced_dec_data[line].field = i >= 18;
  226. itv->vbi.sliced_dec_data[line].id = id2;
  227. memcpy(itv->vbi.sliced_dec_data[line].data, p + 1, 42);
  228. line++;
  229. }
  230. p += 43;
  231. }
  232. while (line < 36) {
  233. itv->vbi.sliced_dec_data[line].id = 0;
  234. itv->vbi.sliced_dec_data[line].line = 0;
  235. itv->vbi.sliced_dec_data[line].field = 0;
  236. line++;
  237. }
  238. return line * sizeof(itv->vbi.sliced_dec_data[0]);
  239. }
  240. /* Compress raw VBI format, removes leading SAV codes and surplus space after the
  241. field.
  242. Returns new compressed size. */
  243. static u32 compress_raw_buf(struct ivtv *itv, u8 *buf, u32 size)
  244. {
  245. u32 line_size = itv->vbi.raw_decoder_line_size;
  246. u32 lines = itv->vbi.count;
  247. u8 sav1 = itv->vbi.raw_decoder_sav_odd_field;
  248. u8 sav2 = itv->vbi.raw_decoder_sav_even_field;
  249. u8 *q = buf;
  250. u8 *p;
  251. int i;
  252. for (i = 0; i < lines; i++) {
  253. p = buf + i * line_size;
  254. /* Look for SAV code */
  255. if (p[0] != 0xff || p[1] || p[2] || (p[3] != sav1 && p[3] != sav2)) {
  256. break;
  257. }
  258. memcpy(q, p + 4, line_size - 4);
  259. q += line_size - 4;
  260. }
  261. return lines * (line_size - 4);
  262. }
  263. /* Compressed VBI format, all found sliced blocks put next to one another
  264. Returns new compressed size */
  265. static u32 compress_sliced_buf(struct ivtv *itv, u32 line, u8 *buf, u32 size, u8 sav)
  266. {
  267. u32 line_size = itv->vbi.sliced_decoder_line_size;
  268. struct v4l2_decode_vbi_line vbi;
  269. int i;
  270. unsigned lines = 0;
  271. /* find the first valid line */
  272. for (i = 0; i < size; i++, buf++) {
  273. if (buf[0] == 0xff && !buf[1] && !buf[2] && buf[3] == sav)
  274. break;
  275. }
  276. size -= i;
  277. if (size < line_size) {
  278. return line;
  279. }
  280. for (i = 0; i < size / line_size; i++) {
  281. u8 *p = buf + i * line_size;
  282. /* Look for SAV code */
  283. if (p[0] != 0xff || p[1] || p[2] || p[3] != sav) {
  284. continue;
  285. }
  286. vbi.p = p + 4;
  287. v4l2_subdev_call(itv->sd_video, video, decode_vbi_line, &vbi);
  288. if (vbi.type && !(lines & (1 << vbi.line))) {
  289. lines |= 1 << vbi.line;
  290. itv->vbi.sliced_data[line].id = vbi.type;
  291. itv->vbi.sliced_data[line].field = vbi.is_second_field;
  292. itv->vbi.sliced_data[line].line = vbi.line;
  293. memcpy(itv->vbi.sliced_data[line].data, vbi.p, 42);
  294. line++;
  295. }
  296. }
  297. return line;
  298. }
  299. void ivtv_process_vbi_data(struct ivtv *itv, struct ivtv_buffer *buf,
  300. u64 pts_stamp, int streamtype)
  301. {
  302. u8 *p = (u8 *) buf->buf;
  303. u32 size = buf->bytesused;
  304. int y;
  305. /* Raw VBI data */
  306. if (streamtype == IVTV_ENC_STREAM_TYPE_VBI && ivtv_raw_vbi(itv)) {
  307. u8 type;
  308. ivtv_buf_swap(buf);
  309. type = p[3];
  310. size = buf->bytesused = compress_raw_buf(itv, p, size);
  311. /* second field of the frame? */
  312. if (type == itv->vbi.raw_decoder_sav_even_field) {
  313. /* Dirty hack needed for backwards
  314. compatibility of old VBI software. */
  315. p += size - 4;
  316. memcpy(p, &itv->vbi.frame, 4);
  317. itv->vbi.frame++;
  318. }
  319. return;
  320. }
  321. /* Sliced VBI data with data insertion */
  322. if (streamtype == IVTV_ENC_STREAM_TYPE_VBI) {
  323. int lines;
  324. ivtv_buf_swap(buf);
  325. /* first field */
  326. lines = compress_sliced_buf(itv, 0, p, size / 2,
  327. itv->vbi.sliced_decoder_sav_odd_field);
  328. /* second field */
  329. /* experimentation shows that the second half does not always begin
  330. at the exact address. So start a bit earlier (hence 32). */
  331. lines = compress_sliced_buf(itv, lines, p + size / 2 - 32, size / 2 + 32,
  332. itv->vbi.sliced_decoder_sav_even_field);
  333. /* always return at least one empty line */
  334. if (lines == 0) {
  335. itv->vbi.sliced_data[0].id = 0;
  336. itv->vbi.sliced_data[0].line = 0;
  337. itv->vbi.sliced_data[0].field = 0;
  338. lines = 1;
  339. }
  340. buf->bytesused = size = lines * sizeof(itv->vbi.sliced_data[0]);
  341. memcpy(p, &itv->vbi.sliced_data[0], size);
  342. if (itv->vbi.insert_mpeg) {
  343. copy_vbi_data(itv, lines, pts_stamp);
  344. }
  345. itv->vbi.frame++;
  346. return;
  347. }
  348. /* Sliced VBI re-inserted from an MPEG stream */
  349. if (streamtype == IVTV_DEC_STREAM_TYPE_VBI) {
  350. /* If the size is not 4-byte aligned, then the starting address
  351. for the swapping is also shifted. After swapping the data the
  352. real start address of the VBI data is exactly 4 bytes after the
  353. original start. It's a bit fiddly but it works like a charm.
  354. Non-4-byte alignment happens when an lseek is done on the input
  355. mpeg file to a non-4-byte aligned position. So on arrival here
  356. the VBI data is also non-4-byte aligned. */
  357. int offset = size & 3;
  358. int cnt;
  359. if (offset) {
  360. p += 4 - offset;
  361. }
  362. /* Swap Buffer */
  363. for (y = 0; y < size; y += 4) {
  364. swab32s((u32 *)(p + y));
  365. }
  366. cnt = ivtv_convert_ivtv_vbi(itv, p + offset);
  367. memcpy(buf->buf, itv->vbi.sliced_dec_data, cnt);
  368. buf->bytesused = cnt;
  369. ivtv_write_vbi(itv, itv->vbi.sliced_dec_data,
  370. cnt / sizeof(itv->vbi.sliced_dec_data[0]));
  371. return;
  372. }
  373. }
  374. void ivtv_disable_cc(struct ivtv *itv)
  375. {
  376. struct vbi_cc cc = { .odd = { 0x80, 0x80 }, .even = { 0x80, 0x80 } };
  377. clear_bit(IVTV_F_I_UPDATE_CC, &itv->i_flags);
  378. ivtv_set_cc(itv, 0, &cc);
  379. itv->vbi.cc_payload_idx = 0;
  380. }
  381. void ivtv_vbi_work_handler(struct ivtv *itv)
  382. {
  383. struct vbi_info *vi = &itv->vbi;
  384. struct v4l2_sliced_vbi_data data;
  385. struct vbi_cc cc = { .odd = { 0x80, 0x80 }, .even = { 0x80, 0x80 } };
  386. /* Lock */
  387. if (itv->output_mode == OUT_PASSTHROUGH) {
  388. if (itv->is_50hz) {
  389. data.id = V4L2_SLICED_WSS_625;
  390. data.field = 0;
  391. if (v4l2_subdev_call(itv->sd_video, video, g_vbi_data, &data) == 0) {
  392. ivtv_set_wss(itv, 1, data.data[0] & 0xf);
  393. vi->wss_missing_cnt = 0;
  394. } else if (vi->wss_missing_cnt == 4) {
  395. ivtv_set_wss(itv, 1, 0x8); /* 4x3 full format */
  396. } else {
  397. vi->wss_missing_cnt++;
  398. }
  399. }
  400. else {
  401. int mode = 0;
  402. data.id = V4L2_SLICED_CAPTION_525;
  403. data.field = 0;
  404. if (v4l2_subdev_call(itv->sd_video, video, g_vbi_data, &data) == 0) {
  405. mode |= 1;
  406. cc.odd[0] = data.data[0];
  407. cc.odd[1] = data.data[1];
  408. }
  409. data.field = 1;
  410. if (v4l2_subdev_call(itv->sd_video, video, g_vbi_data, &data) == 0) {
  411. mode |= 2;
  412. cc.even[0] = data.data[0];
  413. cc.even[1] = data.data[1];
  414. }
  415. if (mode) {
  416. vi->cc_missing_cnt = 0;
  417. ivtv_set_cc(itv, mode, &cc);
  418. } else if (vi->cc_missing_cnt == 4) {
  419. ivtv_set_cc(itv, 0, &cc);
  420. } else {
  421. vi->cc_missing_cnt++;
  422. }
  423. }
  424. return;
  425. }
  426. if (test_and_clear_bit(IVTV_F_I_UPDATE_WSS, &itv->i_flags)) {
  427. ivtv_set_wss(itv, 1, vi->wss_payload & 0xf);
  428. }
  429. if (test_bit(IVTV_F_I_UPDATE_CC, &itv->i_flags)) {
  430. if (vi->cc_payload_idx == 0) {
  431. clear_bit(IVTV_F_I_UPDATE_CC, &itv->i_flags);
  432. ivtv_set_cc(itv, 3, &cc);
  433. }
  434. while (vi->cc_payload_idx) {
  435. cc = vi->cc_payload[0];
  436. memcpy(vi->cc_payload, vi->cc_payload + 1,
  437. sizeof(vi->cc_payload) - sizeof(vi->cc_payload[0]));
  438. vi->cc_payload_idx--;
  439. if (vi->cc_payload_idx && cc.odd[0] == 0x80 && cc.odd[1] == 0x80)
  440. continue;
  441. ivtv_set_cc(itv, 3, &cc);
  442. break;
  443. }
  444. }
  445. if (test_and_clear_bit(IVTV_F_I_UPDATE_VPS, &itv->i_flags)) {
  446. ivtv_set_vps(itv, 1);
  447. }
  448. }