vivid-vbi-gen.c 8.6 KB

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