ivtv-vbi.c 14 KB

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