cx18-vbi.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * cx18 Vertical Blank Interval support functions
  3. *
  4. * Derived from ivtv-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 modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (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., 59 Temple Place, Suite 330, Boston, MA
  21. * 02111-1307 USA
  22. */
  23. #include "cx18-driver.h"
  24. #include "cx18-vbi.h"
  25. #include "cx18-ioctl.h"
  26. #include "cx18-queue.h"
  27. #include "cx18-av-core.h"
  28. /*
  29. * Raster Reference/Protection (RP) bytes, used in Start/End Active
  30. * Video codes emitted from the digitzer in VIP 1.x mode, that flag the start
  31. * of VBI sample or VBI ancilliary data regions in the digitial ratser line.
  32. *
  33. * Task FieldEven VerticalBlank HorizontalBlank 0 0 0 0
  34. */
  35. static const u8 raw_vbi_sav_rp[2] = { 0x20, 0x60 }; /* __V_, _FV_ */
  36. static const u8 sliced_vbi_eav_rp[2] = { 0xb0, 0xf0 }; /* T_VH, TFVH */
  37. static void copy_vbi_data(struct cx18 *cx, int lines, u32 pts_stamp)
  38. {
  39. int line = 0;
  40. int i;
  41. u32 linemask[2] = { 0, 0 };
  42. unsigned short size;
  43. static const u8 mpeg_hdr_data[] = {
  44. /* MPEG-2 Program Pack */
  45. 0x00, 0x00, 0x01, 0xba, /* Prog Pack start code */
  46. 0x44, 0x00, 0x0c, 0x66, 0x24, 0x01, /* SCR, SCR Ext, markers */
  47. 0x01, 0xd1, 0xd3, /* Mux Rate, markers */
  48. 0xfa, 0xff, 0xff, /* Res, Suff cnt, Stuff */
  49. /* MPEG-2 Private Stream 1 PES Packet */
  50. 0x00, 0x00, 0x01, 0xbd, /* Priv Stream 1 start */
  51. 0x00, 0x1a, /* length */
  52. 0x84, 0x80, 0x07, /* flags, hdr data len */
  53. 0x21, 0x00, 0x5d, 0x63, 0xa7, /* PTS, markers */
  54. 0xff, 0xff /* stuffing */
  55. };
  56. const int sd = sizeof(mpeg_hdr_data); /* start of vbi data */
  57. int idx = cx->vbi.frame % CX18_VBI_FRAMES;
  58. u8 *dst = &cx->vbi.sliced_mpeg_data[idx][0];
  59. for (i = 0; i < lines; i++) {
  60. struct v4l2_sliced_vbi_data *sdata = cx->vbi.sliced_data + i;
  61. int f, l;
  62. if (sdata->id == 0)
  63. continue;
  64. l = sdata->line - 6;
  65. f = sdata->field;
  66. if (f)
  67. l += 18;
  68. if (l < 32)
  69. linemask[0] |= (1 << l);
  70. else
  71. linemask[1] |= (1 << (l - 32));
  72. dst[sd + 12 + line * 43] = cx18_service2vbi(sdata->id);
  73. memcpy(dst + sd + 12 + line * 43 + 1, sdata->data, 42);
  74. line++;
  75. }
  76. memcpy(dst, mpeg_hdr_data, sizeof(mpeg_hdr_data));
  77. if (line == 36) {
  78. /* All lines are used, so there is no space for the linemask
  79. (the max size of the VBI data is 36 * 43 + 4 bytes).
  80. So in this case we use the magic number 'ITV0'. */
  81. memcpy(dst + sd, "ITV0", 4);
  82. memcpy(dst + sd + 4, dst + sd + 12, line * 43);
  83. size = 4 + ((43 * line + 3) & ~3);
  84. } else {
  85. memcpy(dst + sd, "itv0", 4);
  86. memcpy(dst + sd + 4, &linemask[0], 8);
  87. size = 12 + ((43 * line + 3) & ~3);
  88. }
  89. dst[4+16] = (size + 10) >> 8;
  90. dst[5+16] = (size + 10) & 0xff;
  91. dst[9+16] = 0x21 | ((pts_stamp >> 29) & 0x6);
  92. dst[10+16] = (pts_stamp >> 22) & 0xff;
  93. dst[11+16] = 1 | ((pts_stamp >> 14) & 0xff);
  94. dst[12+16] = (pts_stamp >> 7) & 0xff;
  95. dst[13+16] = 1 | ((pts_stamp & 0x7f) << 1);
  96. cx->vbi.sliced_mpeg_size[idx] = sd + size;
  97. }
  98. /* Compress raw VBI format, removes leading SAV codes and surplus space
  99. after the frame. Returns new compressed size. */
  100. static u32 compress_raw_buf(struct cx18 *cx, u8 *buf, u32 size)
  101. {
  102. u32 line_size = vbi_active_samples;
  103. u32 lines = cx->vbi.count * 2;
  104. u8 sav1 = raw_vbi_sav_rp[0];
  105. u8 sav2 = raw_vbi_sav_rp[1];
  106. u8 *q = buf;
  107. u8 *p;
  108. int i;
  109. for (i = 0; i < lines; i++) {
  110. p = buf + i * line_size;
  111. /* Look for SAV code */
  112. if (p[0] != 0xff || p[1] || p[2] ||
  113. (p[3] != sav1 && p[3] != sav2))
  114. break;
  115. memcpy(q, p + 4, line_size - 4);
  116. q += line_size - 4;
  117. }
  118. return lines * (line_size - 4);
  119. }
  120. /* Compressed VBI format, all found sliced blocks put next to one another
  121. Returns new compressed size */
  122. static u32 compress_sliced_buf(struct cx18 *cx, u32 line, u8 *buf,
  123. u32 size, u8 eav)
  124. {
  125. struct v4l2_decode_vbi_line vbi;
  126. int i;
  127. u32 line_size = cx->is_60hz ? vbi_hblank_samples_60Hz
  128. : vbi_hblank_samples_50Hz;
  129. /* find the first valid line */
  130. for (i = 0; i < size; i++, buf++) {
  131. if (buf[0] == 0xff && !buf[1] && !buf[2] && buf[3] == eav)
  132. break;
  133. }
  134. size -= i;
  135. if (size < line_size)
  136. return line;
  137. for (i = 0; i < size / line_size; i++) {
  138. u8 *p = buf + i * line_size;
  139. /* Look for EAV code */
  140. if (p[0] != 0xff || p[1] || p[2] || p[3] != eav)
  141. continue;
  142. vbi.p = p + 4;
  143. cx18_av_cmd(cx, VIDIOC_INT_DECODE_VBI_LINE, &vbi);
  144. if (vbi.type) {
  145. cx->vbi.sliced_data[line].id = vbi.type;
  146. cx->vbi.sliced_data[line].field = vbi.is_second_field;
  147. cx->vbi.sliced_data[line].line = vbi.line;
  148. memcpy(cx->vbi.sliced_data[line].data, vbi.p, 42);
  149. line++;
  150. }
  151. }
  152. return line;
  153. }
  154. void cx18_process_vbi_data(struct cx18 *cx, struct cx18_buffer *buf,
  155. int streamtype)
  156. {
  157. u8 *p = (u8 *) buf->buf;
  158. u32 *q = (u32 *) buf->buf;
  159. u32 size = buf->bytesused;
  160. u32 pts;
  161. int lines;
  162. if (streamtype != CX18_ENC_STREAM_TYPE_VBI)
  163. return;
  164. /*
  165. * The CX23418 sends us data that is 32 bit LE swapped, but we want
  166. * the raw VBI bytes in the order they were in the raster line
  167. */
  168. cx18_buf_swap(buf);
  169. /*
  170. * The CX23418 provides a 12 byte header in it's raw VBI buffers to us:
  171. * 0x3fffffff [4 bytes of something] [4 byte presentation time stamp?]
  172. */
  173. /* Raw VBI data */
  174. if (cx18_raw_vbi(cx)) {
  175. u8 type;
  176. /*
  177. * We've set up to get a frame's worth of VBI data at a time.
  178. * Skip 12 bytes of header prefixing the first field.
  179. */
  180. size -= 12;
  181. memcpy(p, &buf->buf[12], size);
  182. type = p[3];
  183. /* Extrapolate the last 12 bytes of the frame's last line */
  184. memset(&p[size], (int) p[size - 1], 12);
  185. size += 12;
  186. size = buf->bytesused = compress_raw_buf(cx, p, size);
  187. /*
  188. * Hack needed for compatibility with old VBI software.
  189. * Write the frame # at the last 4 bytes of the frame
  190. */
  191. p += size - 4;
  192. memcpy(p, &cx->vbi.frame, 4);
  193. cx->vbi.frame++;
  194. return;
  195. }
  196. /* Sliced VBI data with data insertion */
  197. pts = (be32_to_cpu(q[0] == 0x3fffffff)) ? be32_to_cpu(q[2]) : 0;
  198. /*
  199. * For calls to compress_sliced_buf(), ensure there are an integral
  200. * number of lines by shifting the real data up over the 12 bytes header
  201. * that got stuffed in.
  202. * FIXME - there's a smarter way to do this with pointers, but for some
  203. * reason I can't get it to work correctly right now.
  204. */
  205. memcpy(p, &buf->buf[12], size-12);
  206. /* first field */
  207. lines = compress_sliced_buf(cx, 0, p, size / 2, sliced_vbi_eav_rp[0]);
  208. /*
  209. * second field
  210. * In case the second half does not always begin at the exact address,
  211. * start a bit earlier (hence 32).
  212. */
  213. lines = compress_sliced_buf(cx, lines, p + size / 2 - 32,
  214. size / 2 + 32, sliced_vbi_eav_rp[1]);
  215. /* always return at least one empty line */
  216. if (lines == 0) {
  217. cx->vbi.sliced_data[0].id = 0;
  218. cx->vbi.sliced_data[0].line = 0;
  219. cx->vbi.sliced_data[0].field = 0;
  220. lines = 1;
  221. }
  222. buf->bytesused = size = lines * sizeof(cx->vbi.sliced_data[0]);
  223. memcpy(p, &cx->vbi.sliced_data[0], size);
  224. if (cx->vbi.insert_mpeg)
  225. copy_vbi_data(cx, lines, pts);
  226. cx->vbi.frame++;
  227. }