cx18-vbi.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. /*
  28. * Raster Reference/Protection (RP) bytes, used in Start/End Active
  29. * Video codes emitted from the digitzer in VIP 1.x mode, that flag the start
  30. * of VBI sample or VBI ancillary data regions in the digitial ratser line.
  31. *
  32. * Task FieldEven VerticalBlank HorizontalBlank 0 0 0 0
  33. */
  34. static const u8 raw_vbi_sav_rp[2] = { 0x20, 0x60 }; /* __V_, _FV_ */
  35. static const u8 sliced_vbi_eav_rp[2] = { 0xb0, 0xf0 }; /* T_VH, TFVH */
  36. static void copy_vbi_data(struct cx18 *cx, int lines, u32 pts_stamp)
  37. {
  38. int line = 0;
  39. int i;
  40. u32 linemask[2] = { 0, 0 };
  41. unsigned short size;
  42. static const u8 mpeg_hdr_data[] = {
  43. /* MPEG-2 Program Pack */
  44. 0x00, 0x00, 0x01, 0xba, /* Prog Pack start code */
  45. 0x44, 0x00, 0x0c, 0x66, 0x24, 0x01, /* SCR, SCR Ext, markers */
  46. 0x01, 0xd1, 0xd3, /* Mux Rate, markers */
  47. 0xfa, 0xff, 0xff, /* Res, Suff cnt, Stuff */
  48. /* MPEG-2 Private Stream 1 PES Packet */
  49. 0x00, 0x00, 0x01, 0xbd, /* Priv Stream 1 start */
  50. 0x00, 0x1a, /* length */
  51. 0x84, 0x80, 0x07, /* flags, hdr data len */
  52. 0x21, 0x00, 0x5d, 0x63, 0xa7, /* PTS, markers */
  53. 0xff, 0xff /* stuffing */
  54. };
  55. const int sd = sizeof(mpeg_hdr_data); /* start of vbi data */
  56. int idx = cx->vbi.frame % CX18_VBI_FRAMES;
  57. u8 *dst = &cx->vbi.sliced_mpeg_data[idx][0];
  58. for (i = 0; i < lines; i++) {
  59. struct v4l2_sliced_vbi_data *sdata = cx->vbi.sliced_data + i;
  60. int f, l;
  61. if (sdata->id == 0)
  62. continue;
  63. l = sdata->line - 6;
  64. f = sdata->field;
  65. if (f)
  66. l += 18;
  67. if (l < 32)
  68. linemask[0] |= (1 << l);
  69. else
  70. linemask[1] |= (1 << (l - 32));
  71. dst[sd + 12 + line * 43] = cx18_service2vbi(sdata->id);
  72. memcpy(dst + sd + 12 + line * 43 + 1, sdata->data, 42);
  73. line++;
  74. }
  75. memcpy(dst, mpeg_hdr_data, sizeof(mpeg_hdr_data));
  76. if (line == 36) {
  77. /* All lines are used, so there is no space for the linemask
  78. (the max size of the VBI data is 36 * 43 + 4 bytes).
  79. So in this case we use the magic number 'ITV0'. */
  80. memcpy(dst + sd, "ITV0", 4);
  81. memmove(dst + sd + 4, dst + sd + 12, line * 43);
  82. size = 4 + ((43 * line + 3) & ~3);
  83. } else {
  84. memcpy(dst + sd, "itv0", 4);
  85. cpu_to_le32s(&linemask[0]);
  86. cpu_to_le32s(&linemask[1]);
  87. memcpy(dst + sd + 4, &linemask[0], 8);
  88. size = 12 + ((43 * line + 3) & ~3);
  89. }
  90. dst[4+16] = (size + 10) >> 8;
  91. dst[5+16] = (size + 10) & 0xff;
  92. dst[9+16] = 0x21 | ((pts_stamp >> 29) & 0x6);
  93. dst[10+16] = (pts_stamp >> 22) & 0xff;
  94. dst[11+16] = 1 | ((pts_stamp >> 14) & 0xff);
  95. dst[12+16] = (pts_stamp >> 7) & 0xff;
  96. dst[13+16] = 1 | ((pts_stamp & 0x7f) << 1);
  97. cx->vbi.sliced_mpeg_size[idx] = sd + size;
  98. }
  99. /* Compress raw VBI format, removes leading SAV codes and surplus space
  100. after the frame. Returns new compressed size. */
  101. /* FIXME - this function ignores the input size. */
  102. static u32 compress_raw_buf(struct cx18 *cx, u8 *buf, u32 size, u32 hdr_size)
  103. {
  104. u32 line_size = vbi_active_samples;
  105. u32 lines = cx->vbi.count * 2;
  106. u8 *q = buf;
  107. u8 *p;
  108. int i;
  109. /* Skip the header */
  110. buf += hdr_size;
  111. for (i = 0; i < lines; i++) {
  112. p = buf + i * line_size;
  113. /* Look for SAV code */
  114. if (p[0] != 0xff || p[1] || p[2] ||
  115. (p[3] != raw_vbi_sav_rp[0] &&
  116. p[3] != raw_vbi_sav_rp[1]))
  117. break;
  118. if (i == lines - 1) {
  119. /* last line is hdr_size bytes short - extrapolate it */
  120. memcpy(q, p + 4, line_size - 4 - hdr_size);
  121. q += line_size - 4 - hdr_size;
  122. p += line_size - hdr_size - 1;
  123. memset(q, (int) *p, hdr_size);
  124. } else {
  125. memcpy(q, p + 4, line_size - 4);
  126. q += line_size - 4;
  127. }
  128. }
  129. return lines * (line_size - 4);
  130. }
  131. static u32 compress_sliced_buf(struct cx18 *cx, u8 *buf, u32 size,
  132. const u32 hdr_size)
  133. {
  134. struct v4l2_decode_vbi_line vbi;
  135. int i;
  136. u32 line = 0;
  137. u32 line_size = cx->is_60hz ? vbi_hblank_samples_60Hz
  138. : vbi_hblank_samples_50Hz;
  139. /* find the first valid line */
  140. for (i = hdr_size, buf += hdr_size; i < size; i++, buf++) {
  141. if (buf[0] == 0xff && !buf[1] && !buf[2] &&
  142. (buf[3] == sliced_vbi_eav_rp[0] ||
  143. buf[3] == sliced_vbi_eav_rp[1]))
  144. break;
  145. }
  146. /*
  147. * The last line is short by hdr_size bytes, but for the remaining
  148. * checks against size, we pretend that it is not, by counting the
  149. * header bytes we knowingly skipped
  150. */
  151. size -= (i - hdr_size);
  152. if (size < line_size)
  153. return line;
  154. for (i = 0; i < size / line_size; i++) {
  155. u8 *p = buf + i * line_size;
  156. /* Look for EAV code */
  157. if (p[0] != 0xff || p[1] || p[2] ||
  158. (p[3] != sliced_vbi_eav_rp[0] &&
  159. p[3] != sliced_vbi_eav_rp[1]))
  160. continue;
  161. vbi.p = p + 4;
  162. v4l2_subdev_call(cx->sd_av, vbi, decode_vbi_line, &vbi);
  163. if (vbi.type) {
  164. cx->vbi.sliced_data[line].id = vbi.type;
  165. cx->vbi.sliced_data[line].field = vbi.is_second_field;
  166. cx->vbi.sliced_data[line].line = vbi.line;
  167. memcpy(cx->vbi.sliced_data[line].data, vbi.p, 42);
  168. line++;
  169. }
  170. }
  171. return line;
  172. }
  173. static void _cx18_process_vbi_data(struct cx18 *cx, struct cx18_buffer *buf)
  174. {
  175. /*
  176. * The CX23418 provides a 12 byte header in its raw VBI buffers to us:
  177. * 0x3fffffff [4 bytes of something] [4 byte presentation time stamp]
  178. */
  179. struct vbi_data_hdr {
  180. __be32 magic;
  181. __be32 unknown;
  182. __be32 pts;
  183. } *hdr = (struct vbi_data_hdr *) buf->buf;
  184. u8 *p = (u8 *) buf->buf;
  185. u32 size = buf->bytesused;
  186. u32 pts;
  187. int lines;
  188. /*
  189. * The CX23418 sends us data that is 32 bit little-endian swapped,
  190. * but we want the raw VBI bytes in the order they were in the raster
  191. * line. This has a side effect of making the header big endian
  192. */
  193. cx18_buf_swap(buf);
  194. /* Raw VBI data */
  195. if (cx18_raw_vbi(cx)) {
  196. size = buf->bytesused =
  197. compress_raw_buf(cx, p, size, sizeof(struct vbi_data_hdr));
  198. /*
  199. * Hack needed for compatibility with old VBI software.
  200. * Write the frame # at the last 4 bytes of the frame
  201. */
  202. p += size - 4;
  203. memcpy(p, &cx->vbi.frame, 4);
  204. cx->vbi.frame++;
  205. return;
  206. }
  207. /* Sliced VBI data with data insertion */
  208. pts = (be32_to_cpu(hdr->magic) == 0x3fffffff) ? be32_to_cpu(hdr->pts)
  209. : 0;
  210. lines = compress_sliced_buf(cx, p, size, sizeof(struct vbi_data_hdr));
  211. /* always return at least one empty line */
  212. if (lines == 0) {
  213. cx->vbi.sliced_data[0].id = 0;
  214. cx->vbi.sliced_data[0].line = 0;
  215. cx->vbi.sliced_data[0].field = 0;
  216. lines = 1;
  217. }
  218. buf->bytesused = size = lines * sizeof(cx->vbi.sliced_data[0]);
  219. memcpy(p, &cx->vbi.sliced_data[0], size);
  220. if (cx->vbi.insert_mpeg)
  221. copy_vbi_data(cx, lines, pts);
  222. cx->vbi.frame++;
  223. }
  224. void cx18_process_vbi_data(struct cx18 *cx, struct cx18_mdl *mdl,
  225. int streamtype)
  226. {
  227. struct cx18_buffer *buf;
  228. u32 orig_used;
  229. if (streamtype != CX18_ENC_STREAM_TYPE_VBI)
  230. return;
  231. /*
  232. * Big assumption here:
  233. * Every buffer hooked to the MDL's buf_list is a complete VBI frame
  234. * that ends at the end of the buffer.
  235. *
  236. * To assume anything else would make the code in this file
  237. * more complex, or require extra memcpy()'s to make the
  238. * buffers satisfy the above assumption. It's just simpler to set
  239. * up the encoder buffer transfers to make the assumption true.
  240. */
  241. list_for_each_entry(buf, &mdl->buf_list, list) {
  242. orig_used = buf->bytesused;
  243. if (orig_used == 0)
  244. break;
  245. _cx18_process_vbi_data(cx, buf);
  246. mdl->bytesused -= (orig_used - buf->bytesused);
  247. }
  248. }