ivtv-vbi.c 15 KB

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