cx18-vbi.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. static void copy_vbi_data(struct cx18 *cx, int lines, u32 pts_stamp)
  29. {
  30. int line = 0;
  31. int i;
  32. u32 linemask[2] = { 0, 0 };
  33. unsigned short size;
  34. static const u8 mpeg_hdr_data[] = {
  35. 0x00, 0x00, 0x01, 0xba, 0x44, 0x00, 0x0c, 0x66,
  36. 0x24, 0x01, 0x01, 0xd1, 0xd3, 0xfa, 0xff, 0xff,
  37. 0x00, 0x00, 0x01, 0xbd, 0x00, 0x1a, 0x84, 0x80,
  38. 0x07, 0x21, 0x00, 0x5d, 0x63, 0xa7, 0xff, 0xff
  39. };
  40. const int sd = sizeof(mpeg_hdr_data); /* start of vbi data */
  41. int idx = cx->vbi.frame % CX18_VBI_FRAMES;
  42. u8 *dst = &cx->vbi.sliced_mpeg_data[idx][0];
  43. for (i = 0; i < lines; i++) {
  44. struct v4l2_sliced_vbi_data *sdata = cx->vbi.sliced_data + i;
  45. int f, l;
  46. if (sdata->id == 0)
  47. continue;
  48. l = sdata->line - 6;
  49. f = sdata->field;
  50. if (f)
  51. l += 18;
  52. if (l < 32)
  53. linemask[0] |= (1 << l);
  54. else
  55. linemask[1] |= (1 << (l - 32));
  56. dst[sd + 12 + line * 43] = service2vbi(sdata->id);
  57. memcpy(dst + sd + 12 + line * 43 + 1, sdata->data, 42);
  58. line++;
  59. }
  60. memcpy(dst, mpeg_hdr_data, sizeof(mpeg_hdr_data));
  61. if (line == 36) {
  62. /* All lines are used, so there is no space for the linemask
  63. (the max size of the VBI data is 36 * 43 + 4 bytes).
  64. So in this case we use the magic number 'ITV0'. */
  65. memcpy(dst + sd, "ITV0", 4);
  66. memcpy(dst + sd + 4, dst + sd + 12, line * 43);
  67. size = 4 + ((43 * line + 3) & ~3);
  68. } else {
  69. memcpy(dst + sd, "cx0", 4);
  70. memcpy(dst + sd + 4, &linemask[0], 8);
  71. size = 12 + ((43 * line + 3) & ~3);
  72. }
  73. dst[4+16] = (size + 10) >> 8;
  74. dst[5+16] = (size + 10) & 0xff;
  75. dst[9+16] = 0x21 | ((pts_stamp >> 29) & 0x6);
  76. dst[10+16] = (pts_stamp >> 22) & 0xff;
  77. dst[11+16] = 1 | ((pts_stamp >> 14) & 0xff);
  78. dst[12+16] = (pts_stamp >> 7) & 0xff;
  79. dst[13+16] = 1 | ((pts_stamp & 0x7f) << 1);
  80. cx->vbi.sliced_mpeg_size[idx] = sd + size;
  81. }
  82. /* Compress raw VBI format, removes leading SAV codes and surplus space
  83. after the field.
  84. Returns new compressed size. */
  85. static u32 compress_raw_buf(struct cx18 *cx, u8 *buf, u32 size)
  86. {
  87. u32 line_size = cx->vbi.raw_decoder_line_size;
  88. u32 lines = cx->vbi.count;
  89. u8 sav1 = cx->vbi.raw_decoder_sav_odd_field;
  90. u8 sav2 = cx->vbi.raw_decoder_sav_even_field;
  91. u8 *q = buf;
  92. u8 *p;
  93. int i;
  94. for (i = 0; i < lines; i++) {
  95. p = buf + i * line_size;
  96. /* Look for SAV code */
  97. if (p[0] != 0xff || p[1] || p[2] ||
  98. (p[3] != sav1 && p[3] != sav2))
  99. break;
  100. memcpy(q, p + 4, line_size - 4);
  101. q += line_size - 4;
  102. }
  103. return lines * (line_size - 4);
  104. }
  105. /* Compressed VBI format, all found sliced blocks put next to one another
  106. Returns new compressed size */
  107. static u32 compress_sliced_buf(struct cx18 *cx, u32 line, u8 *buf,
  108. u32 size, u8 sav)
  109. {
  110. u32 line_size = cx->vbi.sliced_decoder_line_size;
  111. struct v4l2_decode_vbi_line vbi;
  112. int i;
  113. /* find the first valid line */
  114. for (i = 0; i < size; i++, buf++) {
  115. if (buf[0] == 0xff && !buf[1] && !buf[2] && buf[3] == sav)
  116. break;
  117. }
  118. size -= i;
  119. if (size < line_size)
  120. return line;
  121. for (i = 0; i < size / line_size; i++) {
  122. u8 *p = buf + i * line_size;
  123. /* Look for SAV code */
  124. if (p[0] != 0xff || p[1] || p[2] || p[3] != sav)
  125. continue;
  126. vbi.p = p + 4;
  127. cx18_av_cmd(cx, VIDIOC_INT_DECODE_VBI_LINE, &vbi);
  128. if (vbi.type) {
  129. cx->vbi.sliced_data[line].id = vbi.type;
  130. cx->vbi.sliced_data[line].field = vbi.is_second_field;
  131. cx->vbi.sliced_data[line].line = vbi.line;
  132. memcpy(cx->vbi.sliced_data[line].data, vbi.p, 42);
  133. line++;
  134. }
  135. }
  136. return line;
  137. }
  138. void cx18_process_vbi_data(struct cx18 *cx, struct cx18_buffer *buf,
  139. u64 pts_stamp, int streamtype)
  140. {
  141. u8 *p = (u8 *) buf->buf;
  142. u32 size = buf->bytesused;
  143. int lines;
  144. if (streamtype != CX18_ENC_STREAM_TYPE_VBI)
  145. return;
  146. /* Raw VBI data */
  147. if (cx->vbi.sliced_in->service_set == 0) {
  148. u8 type;
  149. cx18_buf_swap(buf);
  150. type = p[3];
  151. size = buf->bytesused = compress_raw_buf(cx, p, size);
  152. /* second field of the frame? */
  153. if (type == cx->vbi.raw_decoder_sav_even_field) {
  154. /* Dirty hack needed for backwards
  155. compatibility of old VBI software. */
  156. p += size - 4;
  157. memcpy(p, &cx->vbi.frame, 4);
  158. cx->vbi.frame++;
  159. }
  160. return;
  161. }
  162. /* Sliced VBI data with data insertion */
  163. cx18_buf_swap(buf);
  164. /* first field */
  165. lines = compress_sliced_buf(cx, 0, p, size / 2,
  166. cx->vbi.sliced_decoder_sav_odd_field);
  167. /* second field */
  168. /* experimentation shows that the second half does not always
  169. begin at the exact address. So start a bit earlier
  170. (hence 32). */
  171. lines = compress_sliced_buf(cx, lines, p + size / 2 - 32,
  172. size / 2 + 32, cx->vbi.sliced_decoder_sav_even_field);
  173. /* always return at least one empty line */
  174. if (lines == 0) {
  175. cx->vbi.sliced_data[0].id = 0;
  176. cx->vbi.sliced_data[0].line = 0;
  177. cx->vbi.sliced_data[0].field = 0;
  178. lines = 1;
  179. }
  180. buf->bytesused = size = lines * sizeof(cx->vbi.sliced_data[0]);
  181. memcpy(p, &cx->vbi.sliced_data[0], size);
  182. if (cx->vbi.insert_mpeg)
  183. copy_vbi_data(cx, lines, pts_stamp);
  184. cx->vbi.frame++;
  185. }