ivtv-vbi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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, convert to empty VBI frame */
  149. linemask[0] = linemask[1] = 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. while (count >= sizeof(struct v4l2_sliced_vbi_data)) {
  202. switch (p->id) {
  203. case V4L2_SLICED_CAPTION_525:
  204. if (p->line == 21) {
  205. found_cc = 1;
  206. if (p->field) {
  207. cc[2] = p->data[0];
  208. cc[3] = p->data[1];
  209. } else {
  210. cc[0] = p->data[0];
  211. cc[1] = p->data[1];
  212. }
  213. }
  214. break;
  215. case V4L2_SLICED_VPS:
  216. if (p->line == 16 && p->field == 0) {
  217. itv->vbi.vps[0] = p->data[2];
  218. itv->vbi.vps[1] = p->data[8];
  219. itv->vbi.vps[2] = p->data[9];
  220. itv->vbi.vps[3] = p->data[10];
  221. itv->vbi.vps[4] = p->data[11];
  222. itv->vbi.vps_found = 1;
  223. set_bit(IVTV_F_I_UPDATE_VPS, &itv->i_flags);
  224. }
  225. break;
  226. case V4L2_SLICED_WSS_625:
  227. if (p->line == 23 && p->field == 0) {
  228. /* No lock needed for WSS */
  229. itv->vbi.wss = p->data[0] | (p->data[1] << 8);
  230. itv->vbi.wss_found = 1;
  231. set_bit(IVTV_F_I_UPDATE_WSS, &itv->i_flags);
  232. }
  233. break;
  234. default:
  235. break;
  236. }
  237. count -= sizeof(*p);
  238. p++;
  239. }
  240. if (found_cc && cc_pos < sizeof(itv->vbi.cc_data_even)) {
  241. itv->vbi.cc_data_odd[cc_pos] = cc[0];
  242. itv->vbi.cc_data_odd[cc_pos + 1] = cc[1];
  243. itv->vbi.cc_data_even[cc_pos] = cc[2];
  244. itv->vbi.cc_data_even[cc_pos + 1] = cc[3];
  245. itv->vbi.cc_pos = cc_pos + 2;
  246. set_bit(IVTV_F_I_UPDATE_CC, &itv->i_flags);
  247. }
  248. return (const char __user *)p - ubuf;
  249. }
  250. /* Compress raw VBI format, removes leading SAV codes and surplus space after the
  251. field.
  252. Returns new compressed size. */
  253. static u32 compress_raw_buf(struct ivtv *itv, u8 *buf, u32 size)
  254. {
  255. u32 line_size = itv->vbi.raw_decoder_line_size;
  256. u32 lines = itv->vbi.count;
  257. u8 sav1 = itv->vbi.raw_decoder_sav_odd_field;
  258. u8 sav2 = itv->vbi.raw_decoder_sav_even_field;
  259. u8 *q = buf;
  260. u8 *p;
  261. int i;
  262. for (i = 0; i < lines; i++) {
  263. p = buf + i * line_size;
  264. /* Look for SAV code */
  265. if (p[0] != 0xff || p[1] || p[2] || (p[3] != sav1 && p[3] != sav2)) {
  266. break;
  267. }
  268. memcpy(q, p + 4, line_size - 4);
  269. q += line_size - 4;
  270. }
  271. return lines * (line_size - 4);
  272. }
  273. /* Compressed VBI format, all found sliced blocks put next to one another
  274. Returns new compressed size */
  275. static u32 compress_sliced_buf(struct ivtv *itv, u32 line, u8 *buf, u32 size, u8 sav)
  276. {
  277. u32 line_size = itv->vbi.sliced_decoder_line_size;
  278. struct v4l2_decode_vbi_line vbi;
  279. int i;
  280. /* find the first valid line */
  281. for (i = 0; i < size; i++, buf++) {
  282. if (buf[0] == 0xff && !buf[1] && !buf[2] && buf[3] == sav)
  283. break;
  284. }
  285. size -= i;
  286. if (size < line_size) {
  287. return line;
  288. }
  289. for (i = 0; i < size / line_size; i++) {
  290. u8 *p = buf + i * line_size;
  291. /* Look for SAV code */
  292. if (p[0] != 0xff || p[1] || p[2] || p[3] != sav) {
  293. continue;
  294. }
  295. vbi.p = p + 4;
  296. itv->video_dec_func(itv, VIDIOC_INT_DECODE_VBI_LINE, &vbi);
  297. if (vbi.type) {
  298. itv->vbi.sliced_data[line].id = vbi.type;
  299. itv->vbi.sliced_data[line].field = vbi.is_second_field;
  300. itv->vbi.sliced_data[line].line = vbi.line;
  301. memcpy(itv->vbi.sliced_data[line].data, vbi.p, 42);
  302. line++;
  303. }
  304. }
  305. return line;
  306. }
  307. void ivtv_process_vbi_data(struct ivtv *itv, struct ivtv_buffer *buf,
  308. u64 pts_stamp, int streamtype)
  309. {
  310. u8 *p = (u8 *) buf->buf;
  311. u32 size = buf->bytesused;
  312. int y;
  313. /* Raw VBI data */
  314. if (streamtype == IVTV_ENC_STREAM_TYPE_VBI && itv->vbi.sliced_in->service_set == 0) {
  315. u8 type;
  316. ivtv_buf_swap(buf);
  317. type = p[3];
  318. size = buf->bytesused = compress_raw_buf(itv, p, size);
  319. /* second field of the frame? */
  320. if (type == itv->vbi.raw_decoder_sav_even_field) {
  321. /* Dirty hack needed for backwards
  322. compatibility of old VBI software. */
  323. p += size - 4;
  324. memcpy(p, &itv->vbi.frame, 4);
  325. itv->vbi.frame++;
  326. }
  327. return;
  328. }
  329. /* Sliced VBI data with data insertion */
  330. if (streamtype == IVTV_ENC_STREAM_TYPE_VBI) {
  331. int lines;
  332. ivtv_buf_swap(buf);
  333. /* first field */
  334. lines = compress_sliced_buf(itv, 0, p, size / 2,
  335. itv->vbi.sliced_decoder_sav_odd_field);
  336. /* second field */
  337. /* experimentation shows that the second half does not always begin
  338. at the exact address. So start a bit earlier (hence 32). */
  339. lines = compress_sliced_buf(itv, lines, p + size / 2 - 32, size / 2 + 32,
  340. itv->vbi.sliced_decoder_sav_even_field);
  341. /* always return at least one empty line */
  342. if (lines == 0) {
  343. itv->vbi.sliced_data[0].id = 0;
  344. itv->vbi.sliced_data[0].line = 0;
  345. itv->vbi.sliced_data[0].field = 0;
  346. lines = 1;
  347. }
  348. buf->bytesused = size = lines * sizeof(itv->vbi.sliced_data[0]);
  349. memcpy(p, &itv->vbi.sliced_data[0], size);
  350. if (itv->vbi.insert_mpeg) {
  351. copy_vbi_data(itv, lines, pts_stamp);
  352. }
  353. itv->vbi.frame++;
  354. return;
  355. }
  356. /* Sliced VBI re-inserted from an MPEG stream */
  357. if (streamtype == IVTV_DEC_STREAM_TYPE_VBI) {
  358. /* If the size is not 4-byte aligned, then the starting address
  359. for the swapping is also shifted. After swapping the data the
  360. real start address of the VBI data is exactly 4 bytes after the
  361. original start. It's a bit fiddly but it works like a charm.
  362. Non-4-byte alignment happens when an lseek is done on the input
  363. mpeg file to a non-4-byte aligned position. So on arrival here
  364. the VBI data is also non-4-byte aligned. */
  365. int offset = size & 3;
  366. int cnt;
  367. if (offset) {
  368. p += 4 - offset;
  369. }
  370. /* Swap Buffer */
  371. for (y = 0; y < size; y += 4) {
  372. swab32s((u32 *)(p + y));
  373. }
  374. cnt = ivtv_convert_ivtv_vbi(itv, p + offset);
  375. memcpy(buf->buf, itv->vbi.sliced_dec_data, cnt);
  376. buf->bytesused = cnt;
  377. passthrough_vbi_data(itv, cnt / sizeof(itv->vbi.sliced_dec_data[0]));
  378. return;
  379. }
  380. }
  381. void ivtv_disable_vbi(struct ivtv *itv)
  382. {
  383. clear_bit(IVTV_F_I_UPDATE_WSS, &itv->i_flags);
  384. clear_bit(IVTV_F_I_UPDATE_VPS, &itv->i_flags);
  385. clear_bit(IVTV_F_I_UPDATE_CC, &itv->i_flags);
  386. ivtv_set_wss(itv, 0, 0);
  387. ivtv_set_cc(itv, 0, 0, 0, 0, 0);
  388. ivtv_set_vps(itv, 0, 0, 0, 0, 0, 0);
  389. itv->vbi.vps_found = itv->vbi.wss_found = 0;
  390. itv->vbi.wss = 0;
  391. itv->vbi.cc_pos = 0;
  392. }
  393. void ivtv_vbi_work_handler(struct ivtv *itv)
  394. {
  395. struct v4l2_sliced_vbi_data data;
  396. /* Lock */
  397. if (itv->output_mode == OUT_PASSTHROUGH) {
  398. /* Note: currently only the saa7115 is used in a PVR350,
  399. so these commands are for now saa7115 specific. */
  400. if (itv->is_50hz) {
  401. data.id = V4L2_SLICED_WSS_625;
  402. data.field = 0;
  403. if (itv->video_dec_func(itv, VIDIOC_INT_G_VBI_DATA, &data) == 0) {
  404. ivtv_set_wss(itv, 1, data.data[0] & 0xf);
  405. itv->vbi.wss_no_update = 0;
  406. } else if (itv->vbi.wss_no_update == 4) {
  407. ivtv_set_wss(itv, 1, 0x8); /* 4x3 full format */
  408. } else {
  409. itv->vbi.wss_no_update++;
  410. }
  411. }
  412. else {
  413. u8 c1 = 0, c2 = 0, c3 = 0, c4 = 0;
  414. int mode = 0;
  415. data.id = V4L2_SLICED_CAPTION_525;
  416. data.field = 0;
  417. if (itv->video_dec_func(itv, VIDIOC_INT_G_VBI_DATA, &data) == 0) {
  418. mode |= 1;
  419. c1 = data.data[0];
  420. c2 = data.data[1];
  421. }
  422. data.field = 1;
  423. if (itv->video_dec_func(itv, VIDIOC_INT_G_VBI_DATA, &data) == 0) {
  424. mode |= 2;
  425. c3 = data.data[0];
  426. c4 = data.data[1];
  427. }
  428. if (mode) {
  429. itv->vbi.cc_no_update = 0;
  430. ivtv_set_cc(itv, mode, c1, c2, c3, c4);
  431. } else if (itv->vbi.cc_no_update == 4) {
  432. ivtv_set_cc(itv, 0, 0, 0, 0, 0);
  433. } else {
  434. itv->vbi.cc_no_update++;
  435. }
  436. }
  437. return;
  438. }
  439. if (test_and_clear_bit(IVTV_F_I_UPDATE_WSS, &itv->i_flags)) {
  440. /* Lock */
  441. ivtv_set_wss(itv, itv->vbi.wss_found, itv->vbi.wss & 0xf);
  442. }
  443. if (test_and_clear_bit(IVTV_F_I_UPDATE_CC, &itv->i_flags)) {
  444. if (itv->vbi.cc_pos == 0) {
  445. ivtv_set_cc(itv, 3, 0x80, 0x80, 0x80, 0x80);
  446. }
  447. while (itv->vbi.cc_pos) {
  448. u8 cc_odd0 = itv->vbi.cc_data_odd[0];
  449. u8 cc_odd1 = itv->vbi.cc_data_odd[1];
  450. u8 cc_even0 = itv->vbi.cc_data_even[0];
  451. u8 cc_even1 = itv->vbi.cc_data_even[1];
  452. memcpy(itv->vbi.cc_data_odd, itv->vbi.cc_data_odd + 2, sizeof(itv->vbi.cc_data_odd) - 2);
  453. memcpy(itv->vbi.cc_data_even, itv->vbi.cc_data_even + 2, sizeof(itv->vbi.cc_data_even) - 2);
  454. itv->vbi.cc_pos -= 2;
  455. if (itv->vbi.cc_pos && cc_odd0 == 0x80 && cc_odd1 == 0x80)
  456. continue;
  457. /* Send to Saa7127 */
  458. ivtv_set_cc(itv, 3, cc_odd0, cc_odd1, cc_even0, cc_even1);
  459. if (itv->vbi.cc_pos == 0)
  460. set_bit(IVTV_F_I_UPDATE_CC, &itv->i_flags);
  461. break;
  462. }
  463. }
  464. if (test_and_clear_bit(IVTV_F_I_UPDATE_VPS, &itv->i_flags)) {
  465. /* Lock */
  466. ivtv_set_vps(itv, itv->vbi.vps_found,
  467. itv->vbi.vps[0], itv->vbi.vps[1],
  468. itv->vbi.vps[2], itv->vbi.vps[3], itv->vbi.vps[4]);
  469. }
  470. }