vivid-vbi-gen.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * vivid-vbi-gen.c - vbi generator support functions.
  4. *
  5. * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
  6. */
  7. #include <linux/errno.h>
  8. #include <linux/kernel.h>
  9. #include <linux/ktime.h>
  10. #include <linux/string.h>
  11. #include <linux/videodev2.h>
  12. #include "vivid-vbi-gen.h"
  13. static void wss_insert(u8 *wss, u32 val, unsigned size)
  14. {
  15. while (size--)
  16. *wss++ = (val & (1 << size)) ? 0xc0 : 0x10;
  17. }
  18. static void vivid_vbi_gen_wss_raw(const struct v4l2_sliced_vbi_data *data,
  19. u8 *buf, unsigned sampling_rate)
  20. {
  21. const unsigned rate = 5000000; /* WSS has a 5 MHz transmission rate */
  22. u8 wss[29 + 24 + 24 + 24 + 18 + 18] = { 0 };
  23. const unsigned zero = 0x07;
  24. const unsigned one = 0x38;
  25. unsigned bit = 0;
  26. u16 wss_data;
  27. int i;
  28. wss_insert(wss + bit, 0x1f1c71c7, 29); bit += 29;
  29. wss_insert(wss + bit, 0x1e3c1f, 24); bit += 24;
  30. wss_data = (data->data[1] << 8) | data->data[0];
  31. for (i = 0; i <= 13; i++, bit += 6)
  32. wss_insert(wss + bit, (wss_data & (1 << i)) ? one : zero, 6);
  33. for (i = 0, bit = 0; bit < sizeof(wss); bit++) {
  34. unsigned n = ((bit + 1) * sampling_rate) / rate;
  35. while (i < n)
  36. buf[i++] = wss[bit];
  37. }
  38. }
  39. static void vivid_vbi_gen_teletext_raw(const struct v4l2_sliced_vbi_data *data,
  40. u8 *buf, unsigned sampling_rate)
  41. {
  42. const unsigned rate = 6937500 / 10; /* Teletext has a 6.9375 MHz transmission rate */
  43. u8 teletext[45] = { 0x55, 0x55, 0x27 };
  44. unsigned bit = 0;
  45. int i;
  46. memcpy(teletext + 3, data->data, sizeof(teletext) - 3);
  47. /* prevents 32 bit overflow */
  48. sampling_rate /= 10;
  49. for (i = 0, bit = 0; bit < sizeof(teletext) * 8; bit++) {
  50. unsigned n = ((bit + 1) * sampling_rate) / rate;
  51. u8 val = (teletext[bit / 8] & (1 << (bit & 7))) ? 0xc0 : 0x10;
  52. while (i < n)
  53. buf[i++] = val;
  54. }
  55. }
  56. static void cc_insert(u8 *cc, u8 ch)
  57. {
  58. unsigned tot = 0;
  59. unsigned i;
  60. for (i = 0; i < 7; i++) {
  61. cc[2 * i] = cc[2 * i + 1] = (ch & (1 << i)) ? 1 : 0;
  62. tot += cc[2 * i];
  63. }
  64. cc[14] = cc[15] = !(tot & 1);
  65. }
  66. #define CC_PREAMBLE_BITS (14 + 4 + 2)
  67. static void vivid_vbi_gen_cc_raw(const struct v4l2_sliced_vbi_data *data,
  68. u8 *buf, unsigned sampling_rate)
  69. {
  70. const unsigned rate = 1000000; /* CC has a 1 MHz transmission rate */
  71. u8 cc[CC_PREAMBLE_BITS + 2 * 16] = {
  72. /* Clock run-in: 7 cycles */
  73. 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  74. /* 2 cycles of 0 */
  75. 0, 0, 0, 0,
  76. /* Start bit of 1 (each bit is two cycles) */
  77. 1, 1
  78. };
  79. unsigned bit, i;
  80. cc_insert(cc + CC_PREAMBLE_BITS, data->data[0]);
  81. cc_insert(cc + CC_PREAMBLE_BITS + 16, data->data[1]);
  82. for (i = 0, bit = 0; bit < sizeof(cc); bit++) {
  83. unsigned n = ((bit + 1) * sampling_rate) / rate;
  84. while (i < n)
  85. buf[i++] = cc[bit] ? 0xc0 : 0x10;
  86. }
  87. }
  88. void vivid_vbi_gen_raw(const struct vivid_vbi_gen_data *vbi,
  89. const struct v4l2_vbi_format *vbi_fmt, u8 *buf)
  90. {
  91. unsigned idx;
  92. for (idx = 0; idx < 25; idx++) {
  93. const struct v4l2_sliced_vbi_data *data = vbi->data + idx;
  94. unsigned start_2nd_field;
  95. unsigned line = data->line;
  96. u8 *linebuf = buf;
  97. start_2nd_field = (data->id & V4L2_SLICED_VBI_525) ? 263 : 313;
  98. if (data->field)
  99. line += start_2nd_field;
  100. line -= vbi_fmt->start[data->field];
  101. if (vbi_fmt->flags & V4L2_VBI_INTERLACED)
  102. linebuf += (line * 2 + data->field) *
  103. vbi_fmt->samples_per_line;
  104. else
  105. linebuf += (line + data->field * vbi_fmt->count[0]) *
  106. vbi_fmt->samples_per_line;
  107. if (data->id == V4L2_SLICED_CAPTION_525)
  108. vivid_vbi_gen_cc_raw(data, linebuf, vbi_fmt->sampling_rate);
  109. else if (data->id == V4L2_SLICED_WSS_625)
  110. vivid_vbi_gen_wss_raw(data, linebuf, vbi_fmt->sampling_rate);
  111. else if (data->id == V4L2_SLICED_TELETEXT_B)
  112. vivid_vbi_gen_teletext_raw(data, linebuf, vbi_fmt->sampling_rate);
  113. }
  114. }
  115. static const u8 vivid_cc_sequence1[30] = {
  116. 0x14, 0x20, /* Resume Caption Loading */
  117. 'H', 'e',
  118. 'l', 'l',
  119. 'o', ' ',
  120. 'w', 'o',
  121. 'r', 'l',
  122. 'd', '!',
  123. 0x14, 0x2f, /* End of Caption */
  124. };
  125. static const u8 vivid_cc_sequence2[30] = {
  126. 0x14, 0x20, /* Resume Caption Loading */
  127. 'C', 'l',
  128. 'o', 's',
  129. 'e', 'd',
  130. ' ', 'c',
  131. 'a', 'p',
  132. 't', 'i',
  133. 'o', 'n',
  134. 's', ' ',
  135. 't', 'e',
  136. 's', 't',
  137. 0x14, 0x2f, /* End of Caption */
  138. };
  139. static u8 calc_parity(u8 val)
  140. {
  141. unsigned i;
  142. unsigned tot = 0;
  143. for (i = 0; i < 7; i++)
  144. tot += (val & (1 << i)) ? 1 : 0;
  145. return val | ((tot & 1) ? 0 : 0x80);
  146. }
  147. static void vivid_vbi_gen_set_time_of_day(u8 *packet)
  148. {
  149. struct tm tm;
  150. u8 checksum, i;
  151. time64_to_tm(ktime_get_real_seconds(), 0, &tm);
  152. packet[0] = calc_parity(0x07);
  153. packet[1] = calc_parity(0x01);
  154. packet[2] = calc_parity(0x40 | tm.tm_min);
  155. packet[3] = calc_parity(0x40 | tm.tm_hour);
  156. packet[4] = calc_parity(0x40 | tm.tm_mday);
  157. if (tm.tm_mday == 1 && tm.tm_mon == 2 &&
  158. sys_tz.tz_minuteswest > tm.tm_min + tm.tm_hour * 60)
  159. packet[4] = calc_parity(0x60 | tm.tm_mday);
  160. packet[5] = calc_parity(0x40 | (1 + tm.tm_mon));
  161. packet[6] = calc_parity(0x40 | (1 + tm.tm_wday));
  162. packet[7] = calc_parity(0x40 | ((tm.tm_year - 90) & 0x3f));
  163. packet[8] = calc_parity(0x0f);
  164. for (checksum = i = 0; i <= 8; i++)
  165. checksum += packet[i] & 0x7f;
  166. packet[9] = calc_parity(0x100 - checksum);
  167. checksum = 0;
  168. packet[10] = calc_parity(0x07);
  169. packet[11] = calc_parity(0x04);
  170. if (sys_tz.tz_minuteswest >= 0)
  171. packet[12] = calc_parity(0x40 | ((sys_tz.tz_minuteswest / 60) & 0x1f));
  172. else
  173. packet[12] = calc_parity(0x40 | ((24 + sys_tz.tz_minuteswest / 60) & 0x1f));
  174. packet[13] = calc_parity(0);
  175. packet[14] = calc_parity(0x0f);
  176. for (checksum = 0, i = 10; i <= 14; i++)
  177. checksum += packet[i] & 0x7f;
  178. packet[15] = calc_parity(0x100 - checksum);
  179. }
  180. static const u8 hamming[16] = {
  181. 0x15, 0x02, 0x49, 0x5e, 0x64, 0x73, 0x38, 0x2f,
  182. 0xd0, 0xc7, 0x8c, 0x9b, 0xa1, 0xb6, 0xfd, 0xea
  183. };
  184. static void vivid_vbi_gen_teletext(u8 *packet, unsigned line, unsigned frame)
  185. {
  186. unsigned offset = 2;
  187. unsigned i;
  188. packet[0] = hamming[1 + ((line & 1) << 3)];
  189. packet[1] = hamming[line >> 1];
  190. memset(packet + 2, 0x20, 40);
  191. if (line == 0) {
  192. /* subcode */
  193. packet[2] = hamming[frame % 10];
  194. packet[3] = hamming[frame / 10];
  195. packet[4] = hamming[0];
  196. packet[5] = hamming[0];
  197. packet[6] = hamming[0];
  198. packet[7] = hamming[0];
  199. packet[8] = hamming[0];
  200. packet[9] = hamming[1];
  201. offset = 10;
  202. }
  203. packet += offset;
  204. memcpy(packet, "Page: 100 Row: 10", 17);
  205. packet[7] = '0' + frame / 10;
  206. packet[8] = '0' + frame % 10;
  207. packet[15] = '0' + line / 10;
  208. packet[16] = '0' + line % 10;
  209. for (i = 0; i < 42 - offset; i++)
  210. packet[i] = calc_parity(packet[i]);
  211. }
  212. void vivid_vbi_gen_sliced(struct vivid_vbi_gen_data *vbi,
  213. bool is_60hz, unsigned seqnr)
  214. {
  215. struct v4l2_sliced_vbi_data *data0 = vbi->data;
  216. struct v4l2_sliced_vbi_data *data1 = vbi->data + 1;
  217. unsigned frame = seqnr % 60;
  218. memset(vbi->data, 0, sizeof(vbi->data));
  219. if (!is_60hz) {
  220. unsigned i;
  221. for (i = 0; i <= 11; i++) {
  222. data0->id = V4L2_SLICED_TELETEXT_B;
  223. data0->line = 7 + i;
  224. vivid_vbi_gen_teletext(data0->data, i, frame);
  225. data0++;
  226. }
  227. data0->id = V4L2_SLICED_WSS_625;
  228. data0->line = 23;
  229. /* 4x3 video aspect ratio */
  230. data0->data[0] = 0x08;
  231. data0++;
  232. for (i = 0; i <= 11; i++) {
  233. data0->id = V4L2_SLICED_TELETEXT_B;
  234. data0->field = 1;
  235. data0->line = 7 + i;
  236. vivid_vbi_gen_teletext(data0->data, 12 + i, frame);
  237. data0++;
  238. }
  239. return;
  240. }
  241. data0->id = V4L2_SLICED_CAPTION_525;
  242. data0->line = 21;
  243. data1->id = V4L2_SLICED_CAPTION_525;
  244. data1->field = 1;
  245. data1->line = 21;
  246. if (frame < 15) {
  247. data0->data[0] = calc_parity(vivid_cc_sequence1[2 * frame]);
  248. data0->data[1] = calc_parity(vivid_cc_sequence1[2 * frame + 1]);
  249. } else if (frame >= 30 && frame < 45) {
  250. frame -= 30;
  251. data0->data[0] = calc_parity(vivid_cc_sequence2[2 * frame]);
  252. data0->data[1] = calc_parity(vivid_cc_sequence2[2 * frame + 1]);
  253. } else {
  254. data0->data[0] = calc_parity(0);
  255. data0->data[1] = calc_parity(0);
  256. }
  257. frame = seqnr % (30 * 60);
  258. switch (frame) {
  259. case 0:
  260. vivid_vbi_gen_set_time_of_day(vbi->time_of_day_packet);
  261. /* fall through */
  262. case 1 ... 7:
  263. data1->data[0] = vbi->time_of_day_packet[frame * 2];
  264. data1->data[1] = vbi->time_of_day_packet[frame * 2 + 1];
  265. break;
  266. default:
  267. data1->data[0] = calc_parity(0);
  268. data1->data[1] = calc_parity(0);
  269. break;
  270. }
  271. }