cx18-vbi.c 7.8 KB

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